Skip to content

Commit a832b0f

Browse files
committed
#825 Save displayStyle to localStorage
1 parent a4a94ee commit a832b0f

File tree

10 files changed

+41
-40
lines changed

10 files changed

+41
-40
lines changed

browser/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This changelog covers all five packages, as they are (for now) updated as a whol
1313
- [#850](https://github.com/atomicdata-dev/atomic-server/issues/850) Add drag & drop sorting to ResourceArray inputs.
1414
- [#757](https://github.com/atomicdata-dev/atomic-server/issues/757) Add drag & drop sorting to sidebar.
1515
- [#873](https://github.com/atomicdata-dev/atomic-server/issues/873) Add option to allow multiple resources in relation columns (Tables).
16+
- [#825](https://github.com/atomicdata-dev/atomic-server/issues/825) Folder display styles are now saved locally instead of on the resource. The display style property will now act as the default view style.
1617

1718
### @tomic/lib
1819

@@ -44,6 +45,7 @@ This changelog covers all five packages, as they are (for now) updated as a whol
4445
- Added `useCollectionPage` hook.
4546
- Fix bug where `useCollection` would fetch the collection twice on mount.
4647
- `useServerURL` no longer stores the server url in localstorage.
48+
- BREAKING CHANGE: Removed the `useLocalStorage` hook.
4749

4850
### @tomic/cli
4951

browser/data-browser/src/components/SideBar/usePanelList.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { useLocalStorage } from '@tomic/react';
2-
31
export enum Panel {
42
Ontologies = 'ontologies',
53
}
64

75
import { useCallback, useMemo } from 'react';
6+
import { useLocalStorage } from '../../hooks/useLocalStorage';
87

98
export const usePanelList = (): {
109
enabledPanels: Set<Panel>;

browser/data-browser/src/helpers/AppSettings.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ import {
66
useMemo,
77
} from 'react';
88
import { DarkModeOption, useDarkMode } from './useDarkMode';
9-
import {
10-
useLocalStorage,
11-
useCurrentAgent,
12-
useServerURL,
13-
Agent,
14-
} from '@tomic/react';
9+
import { useCurrentAgent, useServerURL, Agent } from '@tomic/react';
1510
import toast from 'react-hot-toast';
1611
import { SIDEBAR_TOGGLE_WIDTH } from '../components/SideBar';
1712
import { handleError } from './loggingHandlers';
1813
import { serverURLStorage } from './serverURLStorage';
14+
import { useLocalStorage } from '../hooks/useLocalStorage';
1915

2016
interface ProviderProps {
2117
children: ReactNode;

browser/data-browser/src/helpers/useDarkMode.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Dispatch, useEffect, useState } from 'react';
2-
import { useLocalStorage } from '@tomic/react';
2+
import { useLocalStorage } from '../hooks/useLocalStorage';
33

44
export enum DarkModeOption {
55
/** Always use dark mode */

browser/data-browser/src/hooks/useDriveHistory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { useLocalStorage } from '@tomic/react';
21
import { useCallback, useMemo } from 'react';
32
import { useSavedDrives } from './useSavedDrives';
3+
import { useLocalStorage } from './useLocalStorage';
44

55
const MAX_DRIVE_HISTORY = 5;
66

browser/react/src/useLocalStorage.ts renamed to browser/data-browser/src/hooks/useLocalStorage.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ const listeners = new Map<
66
>();
77

88
export type SetLocalStorageValue<T> = (value: T | ((val: T) => T)) => void;
9-
/**
10-
* Hook for storing information to LocalStorage. Note that if you use this same
11-
* hook in multiple component instances, these will *not* share state! If you
12-
* want that behavior, you should use this hook inside a Context object.
13-
*/
9+
1410
export function useLocalStorage<T>(
1511
key: string,
1612
initialValue: T,

browser/data-browser/src/views/CollectionPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
useNumber,
77
useResource,
88
useString,
9-
useLocalStorage,
109
useStore,
1110
properties,
1211
} from '@tomic/react';
@@ -35,6 +34,7 @@ import { ResourcePageProps } from './ResourcePage';
3534
import { useEffectOnce } from '../hooks/useEffectOnce';
3635
import { Row } from '../components/Row';
3736
import { EditableTitle } from '../components/EditableTitle';
37+
import { useLocalStorage } from '../hooks/useLocalStorage';
3838

3939
const displayStyles = [
4040
{

browser/data-browser/src/views/FolderPage/index.tsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import {
22
classes,
3-
properties,
4-
useArray,
53
useCanWrite,
64
useResources,
7-
useString,
5+
type DataBrowser,
86
} from '@tomic/react';
97
import { useMemo } from 'react';
108
import { styled } from 'styled-components';
@@ -15,39 +13,49 @@ import { ResourcePageProps } from '../ResourcePage';
1513
import { DisplayStyleButton } from './DisplayStyleButton';
1614
import { GridView } from './GridView';
1715
import { ListView } from './ListView';
16+
import { useLocalStorage } from '../../hooks/useLocalStorage';
1817

19-
const displayStyleOpts = {
20-
commit: true,
21-
};
18+
type PreferredFolderStyles = Record<string, string>;
2219

2320
const viewMap = new Map([
2421
[classes.displayStyles.list, ListView],
2522
[classes.displayStyles.grid, GridView],
2623
]);
2724

28-
const subResourceOpts = {
29-
commit: true,
25+
const displayStyleStorageKey = 'folderDisplayPrefs';
26+
27+
const useDisplayStyle = (
28+
subject: string,
29+
): [
30+
preferredStyle: string | undefined,
31+
setPreferredStyle: (style: string) => void,
32+
] => {
33+
const [preferredStyles, setPreferredStyles] =
34+
useLocalStorage<PreferredFolderStyles>(displayStyleStorageKey, {});
35+
36+
const setPreferredStyle = (style: string) => {
37+
setPreferredStyles({ ...preferredStyles, [subject]: style });
38+
};
39+
40+
return [preferredStyles[subject], setPreferredStyle];
3041
};
3142

32-
export function FolderPage({ resource }: ResourcePageProps) {
33-
const [subResourceSubjects] = useArray(
34-
resource,
35-
properties.subResources,
36-
subResourceOpts,
37-
);
38-
const [displayStyle, setDisplayStyle] = useString(
39-
resource,
40-
properties.displayStyle,
41-
displayStyleOpts,
43+
export function FolderPage({
44+
resource,
45+
}: ResourcePageProps<DataBrowser.Folder>) {
46+
const [preferedDisplayStyle, setPreferedDisplayStyle] = useDisplayStyle(
47+
resource.subject,
4248
);
4349

50+
const displayStyle = preferedDisplayStyle ?? resource.props.displayStyle;
51+
4452
const View = useMemo(
4553
() => viewMap.get(displayStyle!) ?? ListView,
4654
[displayStyle],
4755
);
4856

49-
const subResources = useResources(subResourceSubjects);
50-
const navigateToNewRoute = useNewRoute(resource.getSubject());
57+
const subResources = useResources(resource.props.subResources ?? []);
58+
const navigateToNewRoute = useNewRoute(resource.subject);
5159
const [canEdit] = useCanWrite(resource);
5260

5361
return (
@@ -56,7 +64,7 @@ export function FolderPage({ resource }: ResourcePageProps) {
5664
<TitleBarInner>
5765
<EditableTitle resource={resource} />
5866
<DisplayStyleButton
59-
onClick={setDisplayStyle}
67+
onClick={setPreferedDisplayStyle}
6068
displayStyle={displayStyle}
6169
/>
6270
</TitleBarInner>

browser/data-browser/src/views/ResourcePage.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
properties,
66
Resource,
77
urls,
8+
type OptionalClass,
89
} from '@tomic/react';
910

1011
import { ContainerNarrow } from '../components/Containers';
@@ -31,8 +32,8 @@ import { Main } from '../components/Main';
3132
import { OntologyPage } from './OntologyPage';
3233

3334
/** These properties are passed to every View at Page level */
34-
export type ResourcePageProps = {
35-
resource: Resource;
35+
export type ResourcePageProps<Subject extends OptionalClass = never> = {
36+
resource: Resource<Subject>;
3637
};
3738

3839
type Props = {

browser/react/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export * from './useServerURL.js';
2727
export * from './useCurrentAgent.js';
2828
export * from './useChildren.js';
2929
export * from './useDebounce.js';
30-
export * from './useLocalStorage.js';
3130
export * from './useMarkdown.js';
3231
export * from './useServerSearch.js';
3332
export * from './useCollection.js';

0 commit comments

Comments
 (0)