-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
Refs.tsx
210 lines (184 loc) · 6.34 KB
/
Refs.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import type { FC, MutableRefObject } from 'react';
import React, { useEffect, useMemo, useState, useRef, useCallback } from 'react';
import type { State } from '@storybook/manager-api';
import { useStorybookApi, useStorybookState } from '@storybook/manager-api';
import { styled } from '@storybook/theming';
import { transparentize } from 'polished';
import { AuthBlock, ErrorBlock, LoaderBlock, EmptyBlock } from './RefBlocks';
import { RefIndicator } from './RefIndicator';
import { Tree } from './Tree';
import { DEFAULT_REF_ID } from './Sidebar';
import type { Highlight, RefType } from './types';
import { getStateType } from '../../utils/tree';
import { CollapseIcon } from './components/CollapseIcon';
import type { API_IndexHash } from '@storybook/types';
export interface RefProps {
isLoading: boolean;
isBrowsing: boolean;
selectedStoryId: string | null;
highlightedRef: MutableRefObject<Highlight>;
setHighlighted: (highlight: Highlight) => void;
}
const Wrapper = styled.div<{ isMain: boolean }>(({ isMain }) => ({
position: 'relative',
marginTop: isMain ? undefined : 0,
}));
const RefHead = styled.div(({ theme }) => ({
fontWeight: theme.typography.weight.bold,
fontSize: theme.typography.size.s2,
// Similar to ListItem.tsx
textDecoration: 'none',
lineHeight: '16px',
display: 'flex',
alignItems: 'center',
justifyContent: 'space-between',
background: 'transparent',
width: '100%',
marginTop: 20,
paddingTop: 16,
paddingBottom: 12,
borderTop: `1px solid ${theme.appBorderColor}`,
color:
theme.base === 'light' ? theme.color.defaultText : transparentize(0.2, theme.color.defaultText),
}));
const RefTitle = styled.div({
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
flex: 1,
overflow: 'hidden',
marginLeft: 2,
});
const CollapseButton = styled.button(({ theme }) => ({
all: 'unset',
display: 'flex',
padding: '0px 8px',
gap: 6,
alignItems: 'center',
cursor: 'pointer',
overflow: 'hidden',
'&:focus': {
borderColor: theme.color.secondary,
'span:first-of-type': {
borderLeftColor: theme.color.secondary,
},
},
}));
export const Ref: FC<RefType & RefProps & { status?: State['status'] }> = React.memo(
function Ref(props) {
const { docsOptions } = useStorybookState();
const api = useStorybookApi();
const {
index,
id: refId,
title = refId,
isLoading: isLoadingMain,
isBrowsing,
selectedStoryId,
highlightedRef,
setHighlighted,
loginUrl,
type,
expanded = true,
indexError,
previewInitialized,
} = props;
const length = useMemo(() => (index ? Object.keys(index).length : 0), [index]);
const indicatorRef = useRef<HTMLElement>(null);
const isMain = refId === DEFAULT_REF_ID;
const isLoadingInjected =
(type === 'auto-inject' && !previewInitialized) || type === 'server-checked';
const isLoading = isLoadingMain || isLoadingInjected || type === 'unknown';
const isError = !!indexError;
const isEmpty = !isLoading && length === 0;
const isAuthRequired = !!loginUrl && length === 0;
const state = getStateType(isLoading, isAuthRequired, isError, isEmpty);
const [isExpanded, setExpanded] = useState<boolean>(expanded);
useEffect(() => {
if (index && selectedStoryId && index[selectedStoryId]) {
setExpanded(true);
}
}, [setExpanded, index, selectedStoryId]);
const handleClick = useCallback(() => setExpanded((value) => !value), [setExpanded]);
const setHighlightedItemId = useCallback(
(itemId: string) => setHighlighted({ itemId, refId }),
[setHighlighted]
);
const onSelectStoryId = useCallback(
(storyId: string) => api && api.selectStory(storyId, undefined, { ref: !isMain && refId }),
[api, isMain, refId]
);
const filteredIndex = {} as API_IndexHash;
function getLastWord(str: string) {
const words = str.split(' ');
return words[words.length - 1];
}
// Iterate through the index data
for (const key in index) {
// Check if the key starts with id of the error message
if (
indexError &&
indexError.hasOwnProperty('message') &&
key.startsWith(getLastWord(indexError.message))
) {
// Do not include this item in the filtered index
continue;
}
// Otherwise, include it in the filtered index
filteredIndex[key] = index[key];
}
return (
<>
{isMain || (
<RefHead
aria-label={`${isExpanded ? 'Hide' : 'Show'} ${title} stories`}
aria-expanded={isExpanded}
>
<CollapseButton data-action="collapse-ref" onClick={handleClick}>
<CollapseIcon isExpanded={isExpanded} />
<RefTitle title={title}>{title}</RefTitle>
</CollapseButton>
<RefIndicator {...props} state={state} ref={indicatorRef} />
</RefHead>
)}
{isExpanded && (
<Wrapper data-title={title} isMain={isMain}>
{state === 'auth' && <AuthBlock id={refId} loginUrl={loginUrl} />}
{state === 'error' && (
<>
<ErrorBlock error={indexError} />
<Tree
status={props.status}
isBrowsing={isBrowsing}
isMain={isMain}
refId={refId}
data={filteredIndex}
docsMode={docsOptions.docsMode}
selectedStoryId={selectedStoryId}
onSelectStoryId={onSelectStoryId}
highlightedRef={highlightedRef}
setHighlightedItemId={setHighlightedItemId}
/>
</>
)}
{state === 'loading' && <LoaderBlock isMain={isMain} />}
{state === 'empty' && <EmptyBlock isMain={isMain} />}
{state === 'ready' && (
<Tree
status={props.status}
isBrowsing={isBrowsing}
isMain={isMain}
refId={refId}
data={index}
docsMode={docsOptions.docsMode}
selectedStoryId={selectedStoryId}
onSelectStoryId={onSelectStoryId}
highlightedRef={highlightedRef}
setHighlightedItemId={setHighlightedItemId}
/>
)}
</Wrapper>
)}
</>
);
}
);