Skip to content

Commit

Permalink
feat: store panel open state in storage
Browse files Browse the repository at this point in the history
  • Loading branch information
tlow92 committed Nov 2, 2024
1 parent bc326fc commit deb7f21
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 7 deletions.
8 changes: 6 additions & 2 deletions packages/react-native-ui/src/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BottomBarToggleIcon } from './icon/BottomBarToggleIcon';
import { DarkLogo } from './icon/DarkLogo';
import { Logo } from './icon/Logo';
import { MenuIcon } from './icon/MenuIcon';
import { useStoreState } from './hooks/useStoreState';

export const Layout = ({
storyHash,
Expand All @@ -34,9 +35,12 @@ export const Layout = ({
const insets = useSafeAreaInsets();
const { isDesktop } = useLayout();

const [desktopSidebarOpen, setDesktopSidebarOpen] = useState(true);
const [desktopSidebarOpen, setDesktopSidebarOpen] = useStoreState('desktopSidebarState', true);

const [desktopAddonsPanelOpen, setDesktopAddonsPanelOpen] = useState(true);
const [desktopAddonsPanelOpen, setDesktopAddonsPanelOpen] = useStoreState(
'desktopPanelState',
true
);

if (isDesktop) {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native-ui/src/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Explorer } from './Explorer';
import { Search } from './Search';
import { SearchResults } from './SearchResults';
import type { CombinedDataset, Selection } from './types';
import { useLastViewed } from './useLastViewed';
import { useLastViewed } from './hooks/useLastViewed';
import { DEFAULT_REF_ID } from './constants';
import { View } from 'react-native';

Expand Down
4 changes: 2 additions & 2 deletions packages/react-native-ui/src/Tree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { CollapseAllIcon } from './icon/CollapseAllIcon';
import { CollapseIcon } from './icon/CollapseIcon';
import { ExpandAllIcon } from './icon/ExpandAllIcon';
import { Item } from './types';
import type { ExpandAction, ExpandedState } from './useExpanded';
import { useExpanded } from './useExpanded';
import type { ExpandAction, ExpandedState } from './hooks/useExpanded';
import { useExpanded } from './hooks/useExpanded';
import { getGroupStatus, statusMapping } from './util/status';
import { createId, getAncestorIds, getDescendantIds, isStoryHoistable } from './util/tree';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { StoriesHash } from '@storybook/core/manager-api';
import type { Dispatch, Reducer } from 'react';
import { useCallback, useEffect, useReducer } from 'react';
import { getAncestorIds } from './util/tree';
import { getAncestorIds } from '../util/tree';

export type ExpandedState = Record<string, boolean>;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import debounce from 'lodash/debounce.js';
import { useCallback, useEffect, useMemo, useRef } from 'react';
import store from 'store2';

import type { Selection, StoryRef } from './types';
import type { Selection, StoryRef } from '../types';

const save = debounce((value) => store.set('lastViewedStoryIds', value), 1000);

Expand Down
12 changes: 12 additions & 0 deletions packages/react-native-ui/src/hooks/useStoreState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useEffect, useState } from 'react';
import store from 'store2';

export const useStoreState = <T>(key: string, defaultValue: T): ReturnType<typeof useState<T>> => {
const [val, setVal] = useState<T>(store.get(key) ?? defaultValue);

useEffect(() => {
store.set(key, val);
}, [key, val]);

return [val, setVal];
};

0 comments on commit deb7f21

Please sign in to comment.