Skip to content

Commit

Permalink
feat: store web panel open state in storage (#641)
Browse files Browse the repository at this point in the history
* feat: store panel open state in storage

* fix: use storage provided by user

* fix: respect default value

---------

Co-authored-by: Daniel Williams <[email protected]>
  • Loading branch information
tlow92 and dannyhw authored Nov 23, 2024
1 parent 32574a4 commit 55ef108
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 13 deletions.
13 changes: 10 additions & 3 deletions packages/react-native-ui/src/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { addons } from '@storybook/core/manager-api';
import { type API_IndexHash, type Args, type StoryContext } from '@storybook/core/types';
import type { ReactRenderer } from '@storybook/react';
import { styled, useTheme } from '@storybook/react-native-theming';
import { ReactNode, useRef, useState } from 'react';
import { ReactNode, useRef } from 'react';
import { ScrollView, Text, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { IconButton } from './IconButton';
Expand All @@ -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 { useStoreBooleanState } from './hooks/useStoreState';

export const Layout = ({
storyHash,
Expand All @@ -32,9 +33,15 @@ export const Layout = ({
const insets = useSafeAreaInsets();
const { isDesktop } = useLayout();

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

const [desktopAddonsPanelOpen, setDesktopAddonsPanelOpen] = useState(true);
const [desktopAddonsPanelOpen, setDesktopAddonsPanelOpen] = useStoreBooleanState(
'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
21 changes: 21 additions & 0 deletions packages/react-native-ui/src/StorageProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { FC, PropsWithChildren } from 'react';
import { createContext, useContext } from 'react';

interface Storage {
getItem: (key: string) => Promise<string | null>;
setItem: (key: string, value: string) => Promise<void>;
}

const StorageContext = createContext<Storage>({
getItem: async () => null,
setItem: async () => {},
});

export const StorageProvider: FC<PropsWithChildren<{ storage: Storage }>> = ({
storage,
children,
}) => {
return <StorageContext.Provider value={storage}>{children}</StorageContext.Provider>;
};

export const useStorage = () => useContext(StorageContext);
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
27 changes: 27 additions & 0 deletions packages/react-native-ui/src/hooks/useStoreState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { useEffect, useState } from 'react';
import { useStorage } from '../StorageProvider';

export const useStoreBooleanState = (
key: string,
defaultValue: boolean
): ReturnType<typeof useState<boolean>> => {
const storage = useStorage();

const [val, setVal] = useState<boolean>(defaultValue);

useEffect(() => {
storage.getItem(key).then((newVal) => {
if (newVal === null || newVal === undefined) {
setVal(defaultValue);
} else {
setVal(newVal === 'true');
}
});
}, [key, storage, defaultValue]);

useEffect(() => {
storage.setItem(key, val.toString());
}, [key, storage, val]);

return [val, setVal];
};
1 change: 1 addition & 0 deletions packages/react-native-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './Sidebar';
export * from './types';
export * from './Layout';
export * from './util/StoryHash';
export * from './StorageProvider';
13 changes: 8 additions & 5 deletions packages/react-native/src/View.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Theme, ThemeProvider, darkTheme, theme } from '@storybook/react-native-
import {
Layout,
LayoutProvider,
StorageProvider,
transformStoryIndexToStoriesHash,
} from '@storybook/react-native-ui';
import type { API_IndexHash, PreparedStory, StoryId, StoryIndex } from '@storybook/core/types';
Expand Down Expand Up @@ -287,11 +288,13 @@ export class View {
<GestureHandlerRootView style={{ flex: 1 }}>
<BottomSheetModalProvider>
{/* @ts-ignore something weird with story type */}
<LayoutProvider>
<Layout storyHash={storyHash} story={story}>
<StoryView />
</Layout>
</LayoutProvider>
<StorageProvider storage={storage}>
<LayoutProvider>
<Layout storyHash={storyHash} story={story}>
<StoryView />
</Layout>
</LayoutProvider>
</StorageProvider>
</BottomSheetModalProvider>
</GestureHandlerRootView>
</SafeAreaProvider>
Expand Down

0 comments on commit 55ef108

Please sign in to comment.