-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Sidebar.tsx
156 lines (144 loc) · 4.19 KB
/
Sidebar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { DOCS_MODE } from 'global';
import React, { FunctionComponent, useMemo } from 'react';
import { styled } from '@storybook/theming';
import { ScrollArea, Spaced } from '@storybook/components';
import type { StoriesHash, State } from '@storybook/api';
import { Heading } from './Heading';
import { DEFAULT_REF_ID, collapseAllStories, collapseDocsOnlyStories } from './data';
import { Explorer } from './Explorer';
import { Search } from './Search';
import { SearchResults } from './SearchResults';
import { Refs, CombinedDataset, Selection } from './types';
import { useLastViewed } from './useLastViewed';
const Container = styled.nav({
position: 'absolute',
zIndex: 1,
left: 0,
top: 0,
bottom: 0,
right: 0,
width: '100%',
height: '100%',
});
const StyledSpaced = styled(Spaced)({
paddingBottom: '2.5rem',
});
const CustomScrollArea = styled(ScrollArea)({
'&&&&& .os-scrollbar-handle:before': {
left: -12,
},
'&&&&& .os-scrollbar-vertical': {
right: 5,
},
padding: 20,
});
const Swap = React.memo<{ children: React.ReactNode; condition: boolean }>(
({ children, condition }) => {
const [a, b] = React.Children.toArray(children);
return (
<>
<div style={{ display: condition ? 'block' : 'none' }}>{a}</div>
<div style={{ display: condition ? 'none' : 'block' }}>{b}</div>
</>
);
}
);
const useCombination = (
stories: StoriesHash,
ready: boolean,
error: Error | undefined,
refs: Refs
): CombinedDataset => {
const hash = useMemo(
() => ({
[DEFAULT_REF_ID]: {
stories,
title: null,
id: DEFAULT_REF_ID,
url: 'iframe.html',
ready,
error,
},
...refs,
}),
[refs, stories]
);
return useMemo(() => ({ hash, entries: Object.entries(hash) }), [hash]);
};
export interface SidebarProps {
stories: StoriesHash;
storiesConfigured: boolean;
storiesFailed?: Error;
refs: State['refs'];
menu: any[];
storyId?: string;
refId?: string;
menuHighlighted?: boolean;
enableShortcuts?: boolean;
}
export const Sidebar: FunctionComponent<SidebarProps> = React.memo(
({
storyId = null,
refId = DEFAULT_REF_ID,
stories: storiesHash,
storiesConfigured,
storiesFailed,
menu,
menuHighlighted = false,
enableShortcuts = true,
refs = {},
}) => {
const selected: Selection = useMemo(() => storyId && { storyId, refId }, [storyId, refId]);
const stories = useMemo(
() => (DOCS_MODE ? collapseAllStories : collapseDocsOnlyStories)(storiesHash),
[DOCS_MODE, storiesHash]
);
const dataset = useCombination(stories, storiesConfigured, storiesFailed, refs);
const isLoading = !dataset.hash[DEFAULT_REF_ID].ready;
const lastViewedProps = useLastViewed(selected);
return (
<Container className="container sidebar-container">
<CustomScrollArea vertical>
<StyledSpaced row={1.6}>
<Heading className="sidebar-header" menuHighlighted={menuHighlighted} menu={menu} />
<Search
dataset={dataset}
isLoading={isLoading}
enableShortcuts={enableShortcuts}
{...lastViewedProps}
>
{({
query,
results,
isBrowsing,
closeMenu,
getMenuProps,
getItemProps,
highlightedIndex,
}) => (
<Swap condition={isBrowsing}>
<Explorer
dataset={dataset}
selected={selected}
isLoading={isLoading}
isBrowsing={isBrowsing}
/>
<SearchResults
query={query}
results={results}
closeMenu={closeMenu}
getMenuProps={getMenuProps}
getItemProps={getItemProps}
highlightedIndex={highlightedIndex}
enableShortcuts={enableShortcuts}
isLoading={isLoading}
/>
</Swap>
)}
</Search>
</StyledSpaced>
</CustomScrollArea>
</Container>
);
}
);