Skip to content

Commit

Permalink
fix: move package.json import to client side
Browse files Browse the repository at this point in the history
try `with` and `assert`
  • Loading branch information
JesusTheHun committed Sep 23, 2024
1 parent 85777c9 commit 9972434
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![Storybook](https://raw.githubusercontent.com/storybookjs/brand/master/badge/badge-storybook.svg?sanitize=true)](https://storybook.js.org)
[![npm](https://img.shields.io/npm/v/storybook-addon-remix-react-router?color=blue)](https://www.npmjs.com/package/storybook-addon-remix-react-router)
[![Release](https://github.com/JesusTheHun/storybook-addon-remix-react-router/actions/workflows/release.yml/badge.svg)](https://github.com/JesusTheHun/storybook-addon-remix-react-router/actions/workflows/release.yml)
![npm](https://img.shields.io/npm/dm/storybook-addon-react-router-v6)
![npm](https://img.shields.io/npm/dm/storybook-addon-remix-react-router)

> Use Remix React Router in your stories.
Expand Down
5 changes: 0 additions & 5 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// @ts-ignore
import packageJson from '../package.json' assert { type: 'json' };

export const ADDON_ID = 'storybook/react-router-v6';
export const PANEL_ID = `${ADDON_ID}/panel`;
export const PARAM_KEY = `reactRouter`;
Expand All @@ -15,5 +12,3 @@ export const EVENTS = {
LOADER_INVOKED: `${ADDON_ID}/loader_invoked`,
LOADER_SETTLED: `${ADDON_ID}/loader_settled`,
} as const;

export const ADDON_VERSION = packageJson.version;
2 changes: 1 addition & 1 deletion src/features/decorator/withRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { PARAM_KEY } from '../../constants';
import { ReactRouterDecorator, ReactRouterDecoratorProps } from './components/ReactRouterDecorator';
import { castParametersV2 } from './utils/castParametersV2';

export const withRouter = makeDecorator({
export const withRouter: () => any = makeDecorator({
name: 'withRouter',
parameterName: PARAM_KEY,
wrapper: (getStory, context, { parameters }) => {
Expand Down
41 changes: 32 additions & 9 deletions src/features/panel/hooks/useAddonVersions.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
import { compareVersions } from 'compare-versions';
import { useEffect, useState } from 'react';
import { ADDON_VERSION } from '../../../constants';

export function useAddonVersions() {
const [version, setVersion] = useState<string>();
const [currentVersion, setCurrentVersion] = useState<string>();
const [latestVersion, setLatestVersion] = useState<string>();

useEffect(() => {
if (version !== undefined) return;

const abortController = new AbortController();
fetch(`https://registry.npmjs.org/storybook-addon-remix-react-router/latest`, { signal: abortController.signal })
.then((b) => b.json())
.then((json) => setVersion(json.version))
.then((json) => setLatestVersion(json.version))
// eslint-disable-next-line @typescript-eslint/no-empty-function
.catch(() => {});

return () => abortController.abort();
}, [version]);
}, []);

useEffect(() => {
getAddonVersion().then((v) => setCurrentVersion(v));
}, []);

const newVersionAvailable = version === undefined ? undefined : compareVersions(version, ADDON_VERSION) === 1;
const newVersionAvailable =
!latestVersion || !currentVersion ? undefined : compareVersions(latestVersion, currentVersion) === 1;

return {
currentAddonVersion: ADDON_VERSION,
latestAddonVersion: version,
currentAddonVersion: currentVersion,
latestAddonVersion: latestVersion,
addonUpdateAvailable: newVersionAvailable,
};
}

async function getAddonVersion() {
try {
const packageJson = await import('../../../../package.json', {
with: {
type: 'json',
},
});

return packageJson.version;
} catch (error) {
const packageJson = await import('../../../../package.json', {
assert: {
type: 'json',
},
});

return packageJson.version;
}
}

0 comments on commit 9972434

Please sign in to comment.