generated from storybookjs/addon-kit
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show Errors filter in sidebar when there's component errors
- Loading branch information
1 parent
d37a74d
commit addd9fe
Showing
5 changed files
with
101 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,71 @@ | ||
import { type API, useStorybookState } from "@storybook/manager-api"; | ||
import { styled } from "@storybook/theming"; | ||
import type { API_FilterFunction } from "@storybook/types"; | ||
import React, { useCallback } from "react"; | ||
import React, { useCallback, useEffect } from "react"; | ||
|
||
import { SidebarToggleButton } from "./components/SidebarToggleButton"; | ||
import { ADDON_ID } from "./constants"; | ||
import { ADDON_ID, ENABLE_FILTER } from "./constants"; | ||
|
||
const filterNone: API_FilterFunction = () => true; | ||
const filterWarn: API_FilterFunction = ({ status }) => status?.[ADDON_ID]?.status === "warn"; | ||
const filterError: API_FilterFunction = ({ status }) => status?.[ADDON_ID]?.status === "error"; | ||
const filterBoth: API_FilterFunction = ({ status }) => | ||
status?.[ADDON_ID]?.status === "warn" || status?.[ADDON_ID]?.status === "error"; | ||
|
||
const Wrapper = styled.div({ | ||
display: "flex", | ||
gap: 5, | ||
}); | ||
|
||
interface SidebarBottomProps { | ||
api: API; | ||
} | ||
|
||
export const ENABLE_FILTER = "enableFilter"; | ||
|
||
export const SidebarBottom = ({ api }: SidebarBottomProps) => { | ||
const onEnable = useCallback(() => { | ||
api.experimental_setFilter(ADDON_ID, filterWarn); | ||
// Used internally to trigger next step in guided tour | ||
api.emit(ENABLE_FILTER, ADDON_ID, filterWarn); | ||
}, [api]); | ||
const onDisable = useCallback(() => api.experimental_setFilter(ADDON_ID, filterNone), [api]); | ||
const [showWarnings, setShowWarnings] = React.useState(false); | ||
const [showErrors, setShowErrors] = React.useState(false); | ||
|
||
const { status } = useStorybookState(); | ||
const warnings = Object.values(status).filter((value) => value[ADDON_ID]?.status === "warn"); | ||
if (!warnings.length) return null; | ||
const errors = Object.values(status).filter((value) => value[ADDON_ID]?.status === "error"); | ||
const hasWarnings = warnings.length > 0; | ||
const hasErrors = errors.length > 0; | ||
|
||
const toggleWarnings = useCallback(() => setShowWarnings((shown) => !shown), []); | ||
const toggleErrors = useCallback(() => setShowErrors((shown) => !shown), []); | ||
|
||
useEffect(() => { | ||
let filter = filterNone; | ||
if (hasWarnings && showWarnings) filter = filterWarn; | ||
if (hasErrors && showErrors) filter = filter === filterWarn ? filterBoth : filterError; | ||
api.experimental_setFilter(ADDON_ID, filter); | ||
api.emit(ENABLE_FILTER, filter); | ||
}, [api, hasWarnings, hasErrors, showWarnings, showErrors]); | ||
|
||
if (!hasWarnings && !hasErrors) return null; | ||
|
||
return ( | ||
<span id="sidebar-bottom-wrapper"> | ||
<SidebarToggleButton count={warnings.length} onEnable={onEnable} onDisable={onDisable} /> | ||
</span> | ||
<Wrapper id="sidebar-bottom-wrapper"> | ||
{hasWarnings && ( | ||
<SidebarToggleButton | ||
id="changes-found-filter" | ||
active={showWarnings} | ||
count={warnings.length} | ||
label="Change" | ||
status="warning" | ||
onClick={toggleWarnings} | ||
/> | ||
)} | ||
{hasErrors && ( | ||
<SidebarToggleButton | ||
id="errors-found-filter" | ||
active={showErrors} | ||
count={errors.length} | ||
label="Error" | ||
status="critical" | ||
onClick={toggleErrors} | ||
/> | ||
)} | ||
</Wrapper> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
import { action } from "@storybook/addon-actions"; | ||
import { type StoryObj } from "@storybook/react"; | ||
import { findByRole, userEvent } from "@storybook/testing-library"; | ||
|
||
import { playAll } from "../utils/playAll"; | ||
import { SidebarToggleButton } from "./SidebarToggleButton"; | ||
|
||
export default { | ||
component: SidebarToggleButton, | ||
args: { | ||
active: false, | ||
onClick: action("onClick"), | ||
}, | ||
}; | ||
|
||
export const Changes = { | ||
args: { | ||
count: 12, | ||
onEnable: action("onEnable"), | ||
onDisable: action("onDisable"), | ||
label: "Change", | ||
status: "warning", | ||
}, | ||
}; | ||
|
||
export const Default = {}; | ||
export const ChangesActive = { | ||
args: { | ||
...Changes.args, | ||
active: true, | ||
}, | ||
}; | ||
|
||
export const Enabled: StoryObj = { | ||
play: playAll(async ({ canvasElement }) => { | ||
const button = await findByRole(canvasElement, "button"); | ||
await userEvent.click(button); | ||
}), | ||
export const Errors = { | ||
args: { | ||
count: 2, | ||
label: "Error", | ||
status: "critical", | ||
}, | ||
}; | ||
|
||
export const ErrorsActive = { | ||
args: { | ||
...Errors.args, | ||
active: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters