Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort modes by their theme toolbar order #153

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/components/ModeSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@ type ModeData = Pick<TestMode, "name">;

interface ModeSelectorProps {
isAccepted: boolean;
selectedMode: ModeData;
onSelectMode: (mode: ModeData) => void;
modeOrder?: string[];
modeResults: { mode: ModeData; result?: ComparisonResult }[];
onSelectMode: (mode: ModeData) => void;
selectedMode: ModeData;
}

export const ModeSelector = ({
isAccepted,
selectedMode,
modeOrder,
modeResults,
onSelectMode,
selectedMode,
}: ModeSelectorProps) => {
const aggregate = aggregateResult(modeResults.map(({ result }) => result));
if (!aggregate) return null;
Expand All @@ -53,13 +55,20 @@ export const ModeSelector = ({

const links =
modeResults.length > 1 &&
modeResults.map(({ mode, result }) => ({
id: mode.name,
title: mode.name,
right: !isAccepted && result !== ComparisonResult.Equal && <StatusDot status={result} />,
onClick: () => onSelectMode(mode),
active: selectedMode.name === mode.name,
}));
modeResults
.map(({ mode, result }) => ({
id: mode.name,
title: mode.name,
right: !isAccepted && result !== ComparisonResult.Equal && <StatusDot status={result} />,
onClick: () => onSelectMode(mode),
active: selectedMode.name === mode.name,
}))
.sort((a, b) => {
if (!modeOrder) return 0;
const ia = modeOrder.indexOf(a.title);
const ib = modeOrder.indexOf(b.title);
return ia !== -1 && ib !== -1 ? ia - ib : 0;
});

return (
<WithTooltip
Expand Down
1 change: 1 addition & 0 deletions src/screens/VisualTests/BuildResultsFooter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const BuildResultsFooter = ({
{modeResults.length > 0 && (
<ModeSelector
isAccepted={storyState.summary.status === TestStatus.Accepted}
modeOrder={storyState.modeOrder}
selectedMode={storyState.selectedTest.mode}
modeResults={modeResults}
onSelectMode={storyState.onSelectMode}
Expand Down
10 changes: 6 additions & 4 deletions src/utils/useTests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useGlobals } from "@storybook/manager-api";
import { useGlobals, useGlobalTypes } from "@storybook/manager-api";
import { useCallback, useState } from "react";

import { BrowserInfo, StoryTestFieldsFragment, TestMode, TestStatus } from "../gql/graphql";
Expand All @@ -8,9 +8,10 @@ type ModeData = Pick<TestMode, "name">;

const useGlobalValue = (key: string) => {
try {
return useGlobals()[0][key];
// eslint-disable-next-line react-hooks/rules-of-hooks
return [useGlobals()[0][key], useGlobalTypes()[key]];
} catch (e) {
return null;
return [null, null];
}
};

Expand All @@ -19,7 +20,7 @@ const useGlobalValue = (key: string) => {
* for the same story.
*/
export function useTests(tests: StoryTestFieldsFragment[]) {
const theme = useGlobalValue("theme");
const [theme, themeType] = useGlobalValue("theme");
const [selectedBrowserId, onSelectBrowserId] = useState<BrowserData["id"]>(() => {
const test = tests.find(({ status }) => status !== TestStatus.Passed) || tests[0];
return test?.comparisons[0]?.browser.id;
Expand All @@ -40,6 +41,7 @@ export function useTests(tests: StoryTestFieldsFragment[]) {
selectedTest?.comparisons[0];

return {
modeOrder: themeType?.toolbar?.items?.map((item: { title: string }) => item.title),
selectedTest,
selectedComparison,
onSelectBrowser,
Expand Down