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

Docs2: Refactor manager to use new index data #18023

Merged
merged 28 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e2f55d4
Refactor storiesHash data structure to (mostly) deal with new index
tmeasday Apr 22, 2022
c5c541f
Got tests working again
tmeasday Apr 22, 2022
712a911
Got lib/api building
tmeasday Apr 22, 2022
b0395c5
Various ui fixes
tmeasday Apr 23, 2022
95feebc
Change logic for detecting docs-only items
tmeasday Apr 23, 2022
3515ad5
Deepscan fixes
tmeasday Apr 23, 2022
0aeb9b0
Merge remote-tracking branch 'origin/future-hybrid-stories-index' int…
tmeasday Apr 23, 2022
4134f6b
Get API building
tmeasday Apr 23, 2022
c778bb5
Fixes for build
tmeasday Apr 26, 2022
a11c662
Refactor `StoriesHash` to be simpler to use and more intuitive
tmeasday Apr 27, 2022
a525c30
Some fixes
tmeasday Apr 27, 2022
2d78481
Add story for docs only and fix issue
tmeasday Apr 28, 2022
22d9f84
Pull orphans to the front of the list in the storiesHash
tmeasday Apr 28, 2022
68ed21f
Do single story hoisting on docs only entries
tmeasday Apr 28, 2022
e8fce6d
Explanatory comment
tmeasday Apr 28, 2022
1479659
Deepscan
tmeasday Apr 28, 2022
45897ec
Merge remote-tracking branch 'origin/future-hybrid-stories-index' int…
tmeasday Apr 29, 2022
14e2c3d
Fix `data.test.ts`
tmeasday Apr 29, 2022
cc44374
Fix `data.test.ts`
tmeasday Apr 29, 2022
2f7e71a
Ensure we use the component name when collapsing
tmeasday Apr 29, 2022
b49210b
Merge remote-tracking branch 'origin/future-hybrid-stories-index' int…
tmeasday May 6, 2022
aa1c3c0
Fix merge problems
tmeasday May 6, 2022
c2cfb64
Fix mockdata
tmeasday May 8, 2022
45a04b9
Move rootless stories to the top of the mockdata
tmeasday May 8, 2022
0f4b6bd
Fix search stories
tmeasday May 8, 2022
f23e94d
Fixes based on data changes
tmeasday May 8, 2022
270f466
Fix remaining stories
tmeasday May 9, 2022
349ba7b
You can navigate to story or docs entries
tmeasday May 9, 2022
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
8 changes: 4 additions & 4 deletions addons/storysource/src/StoryPanel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { API, Story, useParameter } from '@storybook/api';
import { API, useParameter } from '@storybook/api';
import { styled } from '@storybook/theming';
import { Link } from '@storybook/router';
import {
Expand Down Expand Up @@ -48,7 +48,7 @@ interface SourceParams {
locationsMap?: LocationsMap;
}
export const StoryPanel: React.FC<StoryPanelProps> = ({ api }) => {
const story: Story | undefined = api.getCurrentStoryData() as Story;
const story = api.getCurrentStoryData();
const selectedStoryRef = React.useRef<HTMLDivElement>(null);
const { source, locationsMap }: SourceParams = useParameter('storySource', {
source: 'loading source...',
Expand Down Expand Up @@ -114,10 +114,10 @@ export const StoryPanel: React.FC<StoryPanelProps> = ({ api }) => {
const location = locationsMap[key];
const first = location.startLoc.line - 1;
const last = location.endLoc.line;
const { kind, refId } = story;
const { title, refId } = story;
// source loader ids are different from story id
const sourceIdParts = key.split('--');
const id = api.storyId(kind, sourceIdParts[sourceIdParts.length - 1]);
const id = api.storyId(title, sourceIdParts[sourceIdParts.length - 1]);
const start = createPart({ rows: rows.slice(lastRow, first), stylesheet, useInlineStyles });
const storyPart = createStoryPart({ rows, stylesheet, useInlineStyles, location, id, refId });

Expand Down
48 changes: 34 additions & 14 deletions lib/api/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ import { Listener } from '@storybook/channels';
import { createContext } from './context';
import Store, { Options } from './store';
import getInitialState from './initial-state';
import type { StoriesHash, Story, Root, Group } from './lib/stories';
import type {
StoriesHash,
RootEntry,
GroupEntry,
ComponentEntry,
DocsEntry,
StoryEntry,
HashEntry,
} from './lib/stories';
import type { ComposedRef, Refs } from './modules/refs';
import { isGroup, isRoot, isStory } from './lib/stories';

import * as provider from './modules/provider';
import * as addons from './modules/addons';
Expand Down Expand Up @@ -139,12 +146,14 @@ export const combineParameters = (...parameterSets: Parameters[]) =>
return undefined;
});

export type ModuleFn = (m: ModuleArgs) => Module;
export type ModuleFn<APIType = unknown, StateType = unknown> = (
m: ModuleArgs
) => Module<APIType, StateType>;

interface Module {
interface Module<APIType = unknown, StateType = unknown> {
init?: () => void;
api?: unknown;
state?: unknown;
api?: APIType;
state?: StateType;
}

class ManagerProvider extends Component<ManagerProviderProps, State> {
Expand Down Expand Up @@ -331,8 +340,18 @@ export function useStorybookApi(): API {
return api;
}

export type { StoriesHash, Story, Root, Group, ComposedRef, Refs };
export { ManagerConsumer as Consumer, ManagerProvider as Provider, isGroup, isRoot, isStory };
export type {
StoriesHash,
RootEntry,
GroupEntry,
ComponentEntry,
DocsEntry,
StoryEntry,
HashEntry,
ComposedRef,
Refs,
};
export { ManagerConsumer as Consumer, ManagerProvider as Provider };

export interface EventMap {
[eventId: string]: Listener;
Expand Down Expand Up @@ -446,13 +465,13 @@ export function useArgs(): [Args, (newArgs: Args) => void, (argNames?: string[])
const { getCurrentStoryData, updateStoryArgs, resetStoryArgs } = useStorybookApi();

const data = getCurrentStoryData();
const args = isStory(data) ? data.args : {};
const args = data.type === 'story' ? data.args : {};
const updateArgs = useCallback(
(newArgs: Args) => updateStoryArgs(data as Story, newArgs),
(newArgs: Args) => updateStoryArgs(data as StoryEntry, newArgs),
[data, updateStoryArgs]
);
const resetArgs = useCallback(
(argNames?: string[]) => resetStoryArgs(data as Story, argNames),
(argNames?: string[]) => resetStoryArgs(data as StoryEntry, argNames),
[data, resetStoryArgs]
);

Expand All @@ -468,12 +487,13 @@ export function useGlobalTypes(): ArgTypes {
return useStorybookApi().getGlobalTypes();
}

function useCurrentStory(): Story {
function useCurrentStory(): StoryEntry | DocsEntry {
const { getCurrentStoryData } = useStorybookApi();

return getCurrentStoryData() as Story;
return getCurrentStoryData();
}

export function useArgTypes(): ArgTypes {
return useCurrentStory()?.argTypes || {};
const current = useCurrentStory();
return (current?.type === 'story' && current.argTypes) || {};
}
2 changes: 1 addition & 1 deletion lib/api/src/initial-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ type Additions = Addition[];

// Returns the initialState of the app
const main = (...additions: Additions): State =>
additions.reduce((acc: State, item) => merge(acc, item), {});
additions.reduce((acc: State, item) => merge<State>(acc, item), {} as any);

export default main;
4 changes: 2 additions & 2 deletions lib/api/src/lib/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import isEqual from 'lodash/isEqual';

import { logger } from '@storybook/client-logger';

export default (a: any, b: any) =>
mergeWith({}, a, b, (objValue: any, srcValue: any) => {
export default <TObj = any>(a: TObj, b: Partial<TObj>) =>
mergeWith({}, a, b, (objValue: TObj, srcValue: Partial<TObj>) => {
if (Array.isArray(srcValue) && Array.isArray(objValue)) {
srcValue.forEach((s) => {
const existing = objValue.find((o) => o === s || isEqual(o, s));
Expand Down
Loading