Skip to content

Commit

Permalink
Show main location on linking screens
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Aug 28, 2023
1 parent f9f5238 commit 138a2df
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/Panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,16 @@ export const Panel = ({ active, api }: PanelProps) => {
},
[api]
);
const [
const {
projectId,
projectToken,
configDir,
mainPath,
updateProject,
projectUpdatingFailed,
projectIdUpdated,
clearProjectIdUpdated,
] = useProjectId();
} = useProjectId();

// Render a hidden element when the addon panel is not active.
// Storybook's AddonPanel component does the same but it's not styleable so we don't use it.
Expand All @@ -109,6 +110,7 @@ export const Panel = ({ active, api }: PanelProps) => {
<LinkingProjectFailed
projectId={projectId}
projectToken={projectToken}
mainPath={mainPath}
configDir={configDir}
/>
);
Expand All @@ -119,6 +121,7 @@ export const Panel = ({ active, api }: PanelProps) => {
<Provider key={PANEL_ID} value={client}>
<LinkedProject
projectId={projectId}
mainPath={mainPath}
goToNext={clearProjectIdUpdated}
setAccessToken={setAccessToken}
/>
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { Channel } from "@storybook/channels";
// eslint-disable-next-line import/no-unresolved
import { getGitInfo, GitInfo, run } from "chromatic/node";
import { relative } from "path";
import { basename, relative } from "path";

import {
BUILD_ANNOUNCED,
Expand Down Expand Up @@ -102,13 +102,13 @@ async function serverChannel(
mainPath = await findConfig(configDir, "main");
await updateMain({ mainPath, projectId, projectToken });
channel.emit(PROJECT_UPDATED, {
mainPath,
mainPath: basename(mainPath),
configDir: relativeConfigDir,
} satisfies ProjectUpdatedPayload);
} catch (err) {
console.warn(`Failed to update your main configuration:\n\n ${err}`);
channel.emit(PROJECT_UPDATING_FAILED, {
mainPath,
mainPath: basename(mainPath),
configDir: relativeConfigDir,
} satisfies ProjectUpdatingFailedPayload);
}
Expand Down
1 change: 1 addition & 0 deletions src/screens/LinkProject/LinkedProject.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const meta = {
component: LinkedProject,
args: {
projectId: "Project:abc123",
mainPath: "main.ts",
goToNext: action("goToNext"),
setAccessToken: action("setAccessToken"),
},
Expand Down
6 changes: 4 additions & 2 deletions src/screens/LinkProject/LinkedProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ const ProjectQuery = graphql(/* GraphQL */ `

export const LinkedProject = ({
projectId,
mainPath,
goToNext,
setAccessToken,
}: {
projectId: string;
mainPath: string;
goToNext: () => void;
setAccessToken: (accessToken: string | null) => void;
}) => {
Expand All @@ -62,8 +64,8 @@ export const LinkedProject = ({
<Heading>Project linked!</Heading>
<Text style={{ maxWidth: 380 }}>
The <code>projectId</code> for {data.project.name} has been added to this
Storybook&apos;s <code>main.js</code>. This will be used to sync with Chromatic.
Please commit this change to continue using this addon.
Storybook&apos;s <code>{mainPath}</code>. This will be used to sync with
Chromatic. Please commit this change to continue using this addon.
</Text>
<Button secondary onClick={() => goToNext()}>
Catch a UI change
Expand Down
6 changes: 6 additions & 0 deletions src/screens/LinkProject/LinkingProjectFailed.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {};

export const WithMainPath: Story = {
args: {
mainPath: "main.ts",
},
};
4 changes: 3 additions & 1 deletion src/screens/LinkProject/LinkingProjectFailed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type LinkingProjectFailedProps = {
projectId: string;
projectToken: string;
configDir: string;
mainPath?: string;
};

const addonName = "@chromaui/addon-visual-tests";
Expand All @@ -20,6 +21,7 @@ export function LinkingProjectFailed({
projectId,
projectToken,
configDir,
mainPath,
}: LinkingProjectFailedProps) {
return (
<Sections>
Expand All @@ -32,7 +34,7 @@ export function LinkingProjectFailed({
</CenterText>
<Code>
{dedent`
// ${configDir}/main.js|ts|tsx
// ${configDir}/${mainPath ?? `main.js|ts|tsx`}
module.exports = {
// ...,
Expand Down
26 changes: 13 additions & 13 deletions src/utils/useProjectId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@ import React from "react";
import {
PROJECT_UPDATED,
PROJECT_UPDATING_FAILED,
ProjectUpdatedPayload,
ProjectUpdatingFailedPayload,
UPDATE_PROJECT,
UpdateProjectPayload,
} from "../constants";

const { CHROMATIC_PROJECT_ID } = process.env;

export const useProjectId = (): [
projectId: string,
projectToken: string,
configDir: string,
updateProject: (projectId: string, projectToken?: string) => void,
projectUpdatingFailed: boolean,
projectIdUpdated: boolean,
clearProjectIdUpdated: () => void
] => {
export const useProjectId = () => {
const [projectId, setProjectId] = React.useState<string | null>(CHROMATIC_PROJECT_ID);
const [projectToken, setProjectToken] = React.useState<string | null>();
const [projectIdUpdated, setProjectIdUpdated] = React.useState(false);
const [projectUpdatingFailed, setProjectUpdatingFailed] = React.useState(false);
const [mainPath, setMainPath] = React.useState<string | null>();
const [configDir, setConfigDir] = React.useState<string | null>();

const emit = useChannel({
[PROJECT_UPDATED]: () => setProjectIdUpdated(true),
[PROJECT_UPDATED]: (payload: ProjectUpdatedPayload) => {
setProjectIdUpdated(true);
setMainPath(payload.mainPath);
setConfigDir(payload.configDir);
},
[PROJECT_UPDATING_FAILED]: (payload: ProjectUpdatingFailedPayload) => {
setProjectUpdatingFailed(true);
setMainPath(payload.mainPath);
setConfigDir(payload.configDir);
},
});
Expand All @@ -42,13 +41,14 @@ export const useProjectId = (): [
setProjectId(newProjectId);
setProjectToken(newProjectToken);
};
return [
return {
projectId,
projectToken,
configDir,
mainPath,
updateProject,
projectUpdatingFailed,
projectIdUpdated,
() => setProjectIdUpdated(false),
];
clearProjectIdUpdated: () => setProjectIdUpdated(false),
};
};

0 comments on commit 138a2df

Please sign in to comment.