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

UI: Add links to documentation and videos in UI #25565

Merged
merged 20 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 10 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
99 changes: 99 additions & 0 deletions code/addons/interactions/src/components/EmptyState.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import React, { useEffect, useState } from 'react';
import { Link } from '@storybook/components';
import { DocumentIcon, VideoIcon } from '@storybook/icons';
import { Consumer, useStorybookApi } from '@storybook/manager-api';
import { styled } from '@storybook/theming';

import { DOCUMENTATION_LINK, TUTORIAL_VIDEO_LINK } from '../constants';

const Wrapper = styled.div(({ theme }) => ({
height: '100%',
display: 'flex',
padding: 0,
alignItems: 'center',
justifyContent: 'center',
flexDirection: 'column',
gap: 15,
background: theme.background.content,
}));

const Content = styled.div({
display: 'flex',
flexDirection: 'column',
gap: 4,
maxWidth: 415,
});

const Title = styled.div(({ theme }) => ({
fontWeight: theme.typography.weight.bold,
fontSize: theme.typography.size.s2 - 1,
textAlign: 'center',
color: theme.textColor,
}));

const Description = styled.div(({ theme }) => ({
fontWeight: theme.typography.weight.regular,
fontSize: theme.typography.size.s2 - 1,
textAlign: 'center',
color: theme.textMutedColor,
}));

const Links = styled.div(({ theme }) => ({
display: 'flex',
fontSize: theme.typography.size.s2 - 1,
gap: 25,
}));

const Divider = styled.div(({ theme }) => ({
width: 1,
height: 16,
backgroundColor: theme.appBorderColor,
}));

export const Empty = () => {
const [isLoading, setIsLoading] = useState(true);
const api = useStorybookApi();
const docsUrl = api.getDocsUrl({
subpath: DOCUMENTATION_LINK,
versioned: true,
renderer: true,
});

// We are adding a small delay to avoid flickering when the story is loading.
// It takes a bit of time for the controls to appear, so we don't want
// to show the empty state for a split second.
useEffect(() => {
const load = setTimeout(() => {
setIsLoading(false);
}, 100);

return () => clearTimeout(load);
}, []);

if (isLoading) return null;

return (
<Wrapper>
<Content>
<Title>Interaction testing</Title>
<Description>
Interaction tests allow you to verify the functional aspects of UIs. Write a play function
for your story and you&apos;ll see it run here.
</Description>
</Content>
<Links>
<Link href={TUTORIAL_VIDEO_LINK} target="_blank" withArrow>
<VideoIcon /> Watch 8m video
</Link>
<Divider />
<Consumer>
{({ state }) => (
<Link href={docsUrl} target="_blank" withArrow>
<DocumentIcon /> Read docs
</Link>
)}
</Consumer>
</Links>
</Wrapper>
);
};
18 changes: 4 additions & 14 deletions code/addons/interactions/src/components/InteractionsPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as React from 'react';
import { Link, Placeholder } from '@storybook/components';
import { type Call, CallStates, type ControlStates } from '@storybook/instrumenter';
import { styled } from '@storybook/theming';
import { transparentize } from 'polished';
Expand All @@ -8,6 +7,8 @@ import { Subnav } from './Subnav';

import { Interaction } from './Interaction';
import { isTestAssertionError } from '../utils';
import { Empty } from './EmptyState';
import { DOCUMENTATION_LINK } from '../constants';

export interface Controls {
start: (args: any) => void;
Expand Down Expand Up @@ -40,7 +41,7 @@ interface InteractionsPanelProps {
}

const Container = styled.div(({ theme }) => ({
minHeight: '100%',
height: '100%',
background: theme.background.content,
}));

Expand Down Expand Up @@ -153,18 +154,7 @@ export const InteractionsPanel: React.FC<InteractionsPanelProps> = React.memo(
</CaughtException>
)}
<div ref={endRef} />
{!isPlaying && !caughtException && interactions.length === 0 && (
<Placeholder>
No interactions found
<Link
href="https://storybook.js.org/docs/react/writing-stories/play-function"
target="_blank"
withArrow
>
Learn how to add interactions to your story
</Link>
</Placeholder>
)}
{!isPlaying && !caughtException && interactions.length === 0 && <Empty />}
</Container>
);
}
Expand Down
3 changes: 3 additions & 0 deletions code/addons/interactions/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export const ADDON_ID = 'storybook/interactions';
export const PANEL_ID = `${ADDON_ID}/panel`;

export const TUTORIAL_VIDEO_LINK = 'https://youtu.be/Waht9qq7AoA';
export const DOCUMENTATION_LINK = 'writing-tests/interaction-testing';
37 changes: 35 additions & 2 deletions code/builders/builder-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import type {
import { getData } from './utils/data';
import { safeResolve } from './utils/safeResolve';
import { readOrderedFiles } from './utils/files';
import { normalizeBuilderName, rendererPathToName } from './utils/framework';

let compilation: Compilation;
let asyncIterator: ReturnType<StarterFunction> | ReturnType<BuilderFunction>;
Expand Down Expand Up @@ -163,6 +164,21 @@ const starter: StarterFunction = async function* starterGeneratorFn({

const { cssFiles, jsFiles } = await readOrderedFiles(addonsDir, compilation?.outputFiles);

// Build additional global values
const globals: Record<string, any> = {};

const { renderer: rendererPath, builder: builderValue } = await options.presets.apply('core');

const rendererName = rendererPathToName(rendererPath);
if (rendererName) {
globals.STORYBOOK_RENDERER = rendererName;
}

const builderName = normalizeBuilderName(builderValue);
if (builderName) {
globals.STORYBOOK_BUILDER = builderName;
}

yield;

const html = await renderHTML(
Expand All @@ -177,7 +193,8 @@ const starter: StarterFunction = async function* starterGeneratorFn({
logLevel,
docsOptions,
tagsOptions,
options
options,
globals
);

yield;
Expand Down Expand Up @@ -252,6 +269,21 @@ const builder: BuilderFunction = async function* builderGeneratorFn({ startTime,
});
const { cssFiles, jsFiles } = await readOrderedFiles(addonsDir, compilation?.outputFiles);

// Build additional global values
const globals: Record<string, any> = {};

const { renderer: rendererPath, builder: builderValue } = await options.presets.apply('core');

const rendererName = rendererPathToName(rendererPath);
if (rendererName) {
globals.STORYBOOK_RENDERER = rendererName;
}

const builderName = normalizeBuilderName(builderValue);
if (builderName) {
globals.STORYBOOK_BUILDER = builderName;
}

yield;

const html = await renderHTML(
Expand All @@ -266,7 +298,8 @@ const builder: BuilderFunction = async function* builderGeneratorFn({ startTime,
logLevel,
docsOptions,
tagsOptions,
options
options,
globals
);

await Promise.all([
Expand Down
25 changes: 25 additions & 0 deletions code/builders/builder-manager/src/utils/framework.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import path from 'path';

export const rendererPathToName = (rendererPath?: string) => {
const value = rendererPath?.split(path.sep).pop()?.toLowerCase();

if (!value) {
return undefined;
}

if (value.includes('vue')) {
return 'vue';
}

return value;
};

export const normalizeBuilderName = (builderValue?: string | { name: string }) => {
ShaunEvening marked this conversation as resolved.
Show resolved Hide resolved
if (!builderValue) {
return undefined;
}

const name = typeof builderValue === 'string' ? builderValue : builderValue.name;

return name.includes('webpack5') ? 'webpack5' : name;
};
8 changes: 7 additions & 1 deletion code/builders/builder-manager/src/utils/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ export const renderHTML = async (
logLevel: Promise<string>,
docsOptions: Promise<DocsOptions>,
tagsOptions: Promise<TagsOptions>,
{ versionCheck, previewUrl, configType, ignorePreview }: Options
{ versionCheck, previewUrl, configType, ignorePreview }: Options,
globals: Record<string, any>
) => {
const titleRef = await title;
const templateRef = await template;
const stringifiedGlobals = Object.entries(globals).reduce(
(transformed, [key, value]) => ({ ...transformed, [key]: JSON.stringify(value) }),
{}
);

return render(templateRef, {
title: titleRef ? `${titleRef} - Storybook` : 'Storybook',
Expand All @@ -54,6 +59,7 @@ export const renderHTML = async (
VERSIONCHECK: JSON.stringify(JSON.stringify(versionCheck), null, 2),
PREVIEW_URL: JSON.stringify(previewUrl, null, 2), // global preview URL
TAGS_OPTIONS: JSON.stringify(await tagsOptions, null, 2),
...stringifiedGlobals,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

},
head: (await customHead) || '',
ignorePreview,
Expand Down
36 changes: 36 additions & 0 deletions code/lib/manager-api/src/modules/versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export interface SubAPI {
* @returns {API_Version} The latest version of the Storybook Manager.
*/
getLatestVersion: () => API_Version;
/**
* Returns the URL of the Storybook documentation for the current version.
*
* @returns {string} The URL of the Storybook Manager documentation.
*/
getDocsUrl: (options: { subpath?: string; versioned?: boolean; renderer?: boolean }) => string;
/**
* Checks if an update is available for the Storybook Manager.
*
Expand Down Expand Up @@ -73,6 +79,36 @@ export const init: ModuleFn = ({ store }) => {
}
return latest as API_Version;
},
// TODO: Move this to it's own "info" module later
getDocsUrl: ({ subpath, versioned, renderer }) => {
const {
versions: { latest, current },
} = store.getState();

let url = 'https://storybook.js.org/docs/';

console.log('current', current);
console.log('latest', latest);

if (versioned && current?.version && latest?.version) {
const versionDiff = semver.diff(latest.version, current.version);
const isLatestDocs = versionDiff === 'patch' || versionDiff === null;

if (!isLatestDocs) {
url += `${semver.major(current.version)}.${semver.minor(current.version)}/`;
}
}

if (subpath) {
url += `${subpath}/`;
}

if (renderer && typeof global.STORYBOOK_RENDERER !== 'undefined') {
url += `?renderer=${global.STORYBOOK_RENDERER}`;
}

return url;
},
versionUpdateAvailable: () => {
const latest = api.getLatestVersion();
const current = api.getCurrentVersion();
Expand Down
Loading
Loading