-
-
Notifications
You must be signed in to change notification settings - Fork 169
/
Copy pathRefs.tsx
83 lines (75 loc) · 2.27 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
import type { FC } from 'react';
import React, { useMemo, useCallback, useEffect, useState } from 'react';
import type { State } from '@storybook/core/manager-api';
import { styled } from '@storybook/react-native-theming';
import { Tree } from './Tree';
import type { RefType } from './types';
import { getStateType } from './util/tree';
export interface RefProps {
isLoading: boolean;
isBrowsing: boolean;
selectedStoryId: string | null;
setSelection: (selection: { refId: string; storyId: string }) => void;
}
const Wrapper = styled.View<{ isMain: boolean }>(({}) => ({
position: 'relative',
flex: 1,
}));
export const Ref: FC<RefType & RefProps & { status?: State['status'] }> = React.memo(function Ref(
props
) {
const {
index,
id: refId,
title = refId,
isLoading: isLoadingMain,
isBrowsing,
selectedStoryId,
loginUrl,
type,
expanded = true,
indexError,
previewInitialized,
setSelection,
} = props;
const length = useMemo(() => (index ? Object.keys(index).length : 0), [index]);
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 onSelectStoryId = useCallback(
(storyId: string) => {
setSelection({ refId, storyId });
},
[refId, setSelection]
);
return (
<>
{isExpanded && (
<Wrapper data-title={title} isMain={true}>
{state === 'ready' && (
<Tree
status={props.status}
isBrowsing={isBrowsing}
isMain={true}
refId={refId}
data={index}
docsMode={false}
selectedStoryId={selectedStoryId}
onSelectStoryId={onSelectStoryId}
/>
)}
</Wrapper>
)}
</>
);
});