Skip to content

Commit

Permalink
feat(version-picker): reintroduce the version picker for storybook
Browse files Browse the repository at this point in the history
- add better error handling for failed requests
- add mock data as a fallback for testing purposes
- add the version picker addon back into the storybook manager
  • Loading branch information
nineteen88 committed Sep 26, 2024
1 parent d83f547 commit 9d8ccf0
Show file tree
Hide file tree
Showing 4 changed files with 729 additions and 9 deletions.
15 changes: 14 additions & 1 deletion .storybook/manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
import { addons } from "@storybook/manager-api";
import { addons, types } from "@storybook/manager-api";
import sageTheme from "./sage-docs-theme";
import { ADDON_ID, TOOL_ID } from "./version-picker/constants";
import { VersionPicker } from "./version-picker";
import { API_PreparedIndexEntry, API_StatusObject } from "@storybook/types";

if (process.env.NODE_ENV === "production") {
addons.register(ADDON_ID, () => {
addons.add(TOOL_ID, {
type: types.TOOL,
title: "Version picker",
match: ({ viewMode }) => !!(viewMode && viewMode.match(/^(story|docs)$/)),
render: VersionPicker,
});
});
}

addons.setConfig({
theme: sageTheme,
panelPosition: "bottom",
Expand Down
25 changes: 19 additions & 6 deletions .storybook/version-picker/fetch-data.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
import mockData from "./mock-data/metadata.json";

interface Metadata {
versions: Record<string, string>;
}

const fetchData = async () => {
const response = await fetch(
"https://carbon.sage.com/metadata/metadata.json"
);
const fetchData = async (): Promise<Metadata | null> => {
try {
const response = await fetch(
"https://carbon.sage.com/metadata/metadata.json"
);

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const metadata: Metadata = await response.json();

const metadata: Metadata = await response.json();
return metadata;
} catch (error) {
console.error("Failed to fetch metadata:", error);

return metadata;
console.log("Returning mock data as fallback");
return mockData as Metadata;
}
};

export default fetchData;
13 changes: 11 additions & 2 deletions .storybook/version-picker/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@ export const VersionPicker = () => {
}

const getData = async () => {
const data = await fetchData();
setVersions(data.versions);
try {
const data = await fetchData();
if (data) {
setVersions(data.versions);
} else {
throw new Error("Failed to fetch metadata");
}
} catch (error) {
console.error("Failed to retrieve version data:", error);
setVersions(undefined);
}
};

getData();
Expand Down
Loading

0 comments on commit 9d8ccf0

Please sign in to comment.