1 | import React, { useMemo } from 'react';
|
2 | import { atom, useAtom, useAtomValue, useSetAtom, getDefaultStore } from 'jotai';
|
3 | import { useTheme as useEmotionTheme } from 'emotion-theming';
|
4 | const storyContextAtom = atom(null);
|
5 |
|
6 |
|
7 |
|
8 | export function useSetStoryContext() {
|
9 | return useSetAtom(storyContextAtom);
|
10 | }
|
11 |
|
12 |
|
13 |
|
14 | export function useStoryContext() {
|
15 | return useAtomValue(storyContextAtom);
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 | export function useStoryContextParam(name, defaultValue) {
|
21 | var _a;
|
22 | const paramAtom = useMemo(() => atom((get) => { var _a, _b; return (_b = (_a = get(storyContextAtom)) === null || _a === void 0 ? void 0 : _a.parameters) === null || _b === void 0 ? void 0 : _b[name]; }), [name]);
|
23 | return (_a = useAtomValue(paramAtom)) !== null && _a !== void 0 ? _a : defaultValue;
|
24 | }
|
25 | /**
|
26 | * Hook that indicates if `storyId` is the currently selected story.
|
27 | */
|
28 | export function useIsStorySelected(storyId) {
|
29 | return useAtomValue(useMemo(() => atom((get) => { var _a; return ((_a = get(storyContextAtom)) === null || _a === void 0 ? void 0 : _a.id) === storyId; }), [storyId]));
|
30 | }
|
31 | /**
|
32 | * Hook that indicates if `title` is the currently selected story section.
|
33 | */
|
34 | export function useIsStorySectionSelected(title) {
|
35 | return useAtomValue(useMemo(() => atom((get) => { var _a; return ((_a = get(storyContextAtom)) === null || _a === void 0 ? void 0 : _a.title) === title; }), [title]));
|
36 | }
|
37 | /**
|
38 | * Hook that causes a re-render when the currently selected story is changed.
|
39 | */
|
40 | export function useUpdateOnStoryChanged() {
|
41 | useAtomValue(useMemo(() => atom((get) => { var _a; return (_a = get(storyContextAtom)) === null || _a === void 0 ? void 0 : _a.id; }), []));
|
42 | }
|
43 | /**
|
44 | * Hook that gets the current theme values.
|
45 | */
|
46 | export function useTheme() {
|
47 | return useEmotionTheme();
|
48 | }
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | export function atomWithToggle(initialValue) {
|
56 | const anAtom = atom(initialValue, (get, set, nextValue) => {
|
57 | const update = nextValue !== null && nextValue !== void 0 ? nextValue : !get(anAtom);
|
58 | set(anAtom, update);
|
59 | });
|
60 | return anAtom;
|
61 | }
|
62 | const isUIVisibleAtom = atomWithToggle(true);
|
63 | /**
|
64 | * Hook that retrieves the current state, and a setter, for the `isUIVisible`
|
65 | * atom.
|
66 | */
|
67 | export function useIsUIVisible() {
|
68 | return useAtom(isUIVisibleAtom);
|
69 | }
|
70 | const isSplitPanelVisibleAtom = atomWithToggle(false);
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | export function useIsSplitPanelVisible() {
|
76 | return useAtom(isSplitPanelVisibleAtom);
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 | export function syncExternalUI({ isUIVisible, isSplitPanelVisible }) {
|
82 | const jotaiStore = getDefaultStore();
|
83 | if (isUIVisible !== undefined) {
|
84 | jotaiStore.set(isUIVisibleAtom, isUIVisible);
|
85 | }
|
86 | if (isSplitPanelVisible !== undefined) {
|
87 | jotaiStore.set(isSplitPanelVisibleAtom, isSplitPanelVisible);
|
88 | }
|
89 | }
|
90 | const selectedAddonAtom = atom(undefined);
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | export function useSelectedAddon(initialValue) {
|
98 | const result = useAtom(selectedAddonAtom);
|
99 | const set = result[1];
|
100 | React.useEffect(() => {
|
101 | const jotaiStore = getDefaultStore();
|
102 |
|
103 |
|
104 | if (jotaiStore.get(selectedAddonAtom) === undefined) {
|
105 | set(initialValue);
|
106 | }
|
107 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
108 | }, []);
|
109 | return result;
|
110 | }
|