Skip to content

Commit

Permalink
Merge pull request #127 from NFDI4Chem/customize-nmrium-default-empty…
Browse files Browse the repository at this point in the history
…-message

feat: customize the NMRium default empty message
  • Loading branch information
CS76 authored Aug 23, 2023
2 parents f317a4d + acd1c87 commit 62aba3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/NMRiumWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default function NMRiumWrapper() {
const { allowedOrigins, isFetchAllowedOriginsPending } = useWhiteList();
const nmriumRef = useRef<NMRiumRef>(null);
const [data, setDate] = useState<NMRiumData>();
const { workspace, preferences } = usePreferences();
const { workspace, preferences, defaultEmptyMessage } = usePreferences();
const dataChangeHandler = useCallback<OnNMRiumChange>((state, source) => {
events.trigger('data-change', { state, source });
}, []);
Expand Down Expand Up @@ -101,6 +101,8 @@ export default function NMRiumWrapper() {
};
});

console.log(defaultEmptyMessage);

Check warning on line 104 in src/NMRiumWrapper.tsx

View workflow job for this annotation

GitHub Actions / lint

Unexpected console statement

return (
<div style={styles.container}>
{isLoading ||
Expand All @@ -115,6 +117,7 @@ export default function NMRiumWrapper() {
onChange={dataChangeHandler}
preferences={preferences}
workspace={workspace}
emptyText={defaultEmptyMessage}
/>
</div>
);
Expand Down
25 changes: 20 additions & 5 deletions src/hooks/usePreferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,41 @@ import { WorkspacePreferences } from 'nmr-load-save';
import { NMRiumWorkspace } from 'nmrium';
import { useLayoutEffect, useState } from 'react';

interface Preferences {
preferences: WorkspacePreferences | undefined;
workspace: NMRiumWorkspace | undefined;
defaultEmptyMessage: string | undefined;
}

const DEFAULT_PREFERENCES = {
preferences: undefined,
workspace: undefined,
defaultEmptyMessage: undefined,
};

export function usePreferences() {
const [configuration, setConfiguration] = useState<{
preferences: WorkspacePreferences | undefined;
workspace: NMRiumWorkspace | undefined;
}>({ preferences: undefined, workspace: undefined });
const [configuration, setConfiguration] =
useState<Preferences>(DEFAULT_PREFERENCES);

useLayoutEffect(() => {
const { href } = window.location;
const parameters = new URL(href).searchParams;

let preferences: WorkspacePreferences | undefined;
let workspace: NMRiumWorkspace | undefined;
let defaultEmptyMessage: string | undefined;

if (parameters.has('workspace')) {
workspace = parameters.get('workspace') as NMRiumWorkspace;
}
if (parameters.has('preferences')) {
preferences = JSON.parse(parameters.get('preferences') || '');
}
setConfiguration({ preferences, workspace });

if (parameters.has('defaultEmptyMessage')) {
defaultEmptyMessage = parameters.get('defaultEmptyMessage') as string;
}
setConfiguration({ preferences, workspace, defaultEmptyMessage });
}, []);

return configuration;
Expand Down

0 comments on commit 62aba3d

Please sign in to comment.