Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: store panel open state in storage #641

Merged
merged 5 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
Copy link
Member

@dannyhw dannyhw Nov 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this store actually works, I'm realising now that its in the lastViewed hook but I think its just an artifact from me copy/pasting a bit too freely from the web code 😅

We have the storage from the getStorybookUI thats required maybe we can pass that down to the ui

Edit:

Wait i guess if this is for web only that would work 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it's only web, but I can take a look at the other storage solution and migrate to that so maybe we can drop store2 usage later alltogether.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah the thing is that we also want tablets and other wide screen devices to work so that would be ideal


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];
};