Skip to content

Commit

Permalink
fix cookie not loading on initial load (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
josephsilcock authored Oct 29, 2024
1 parent de49d69 commit 8eed0f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
42 changes: 17 additions & 25 deletions packages/addon/src/components/PanelContent.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,41 @@
import React, { useEffect, useState } from 'react';
import {
useGlobals,
useParameter,
useStorybookState,
} from '@storybook/manager-api';
import { useGlobals, useParameter } from '@storybook/manager-api';
import Icons from '@storybook/icons';
import { ObjectControl } from '@storybook/blocks';

import { PARAM_ENCONDING_KEY, PARAM_KEY } from '../constants';
import { PARAM_ENCODING_KEY, PARAM_KEY } from '../constants';
import { Cookie } from '../types';
import { clearCookies, setCookies } from '../utils';

export const PanelContent: React.FC = () => {
const { path } = useStorybookState();
const defaultCookie = useParameter<Cookie>(PARAM_KEY, {}); // TODO: Doesn't work on initial load
const encoding = useParameter<boolean>(PARAM_ENCONDING_KEY, false);
const defaultCookie = useParameter<Cookie>(PARAM_KEY, null);
const encoding = useParameter<boolean>(PARAM_ENCODING_KEY, false);

const [story, setStory] = useState<string>();
const [value, setValue] = useState<Cookie>();
const [globals, updateGlobals] = useGlobals();

const initCookie = () => {
setStory(path);
setValue(defaultCookie);
const updateCookieValue = (newValue: Cookie | null) => {
setValue(newValue ?? {});
clearCookies();
setCookies(defaultCookie, encoding);
if (newValue) {
setCookies(newValue, encoding);
}
updateGlobals({ ...globals });
};

useEffect(() => {
if (story !== path) {
initCookie();
if (!defaultCookie && !value) {
return;
}
}, [path, story]);
updateCookieValue(defaultCookie);
}, [defaultCookie, encoding]);

const handleChange = (newValue: Cookie) => {
clearCookies();
setCookies(newValue, encoding);
setValue(newValue);
updateGlobals({ ...globals });
updateCookieValue(newValue);
};

const handleClear = () => {
clearCookies();
setValue({});
updateGlobals({ ...globals });
updateCookieValue(null);
};

return (
Expand All @@ -55,7 +47,7 @@ export const PanelContent: React.FC = () => {
<ObjectControl
name="cookie"
onChange={handleChange}
value={value || defaultCookie}
value={value}
theme=""
/>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/addon/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export const ADDON_ID = 'storybook-addon-cookie';
export const PANEL_ID = `${ADDON_ID}/panel`;
export const PARAM_KEY = `cookie`;
export const ADDON_TITLE = 'Cookie';
export const PARAM_ENCONDING_KEY = 'cookieEncoding';
export const PARAM_ENCODING_KEY = 'cookieEncoding';
export const PARAM_PRESERVE_KEY = 'cookiePreserve';

0 comments on commit 8eed0f4

Please sign in to comment.