-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseStoryLoad.ts
40 lines (30 loc) · 1.12 KB
/
useStoryLoad.ts
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
import { useState, useEffect } from 'react';
import { setExampleState } from '../../store/store';
import { EXAMPLE_STATE } from '../../shared/globals';
import type { ComponentStruct, Story } from '../types';
export const useStoryLoad = (
name: string,
componentName: string,
dir: string | undefined,
): [ComponentStruct, Story, boolean] => {
const componentDir = dir || componentName;
const [data, setData] = useState<Story>({});
const [struct, setStruct] = useState<Story>({});
const [isLoading, setIsLoading] = useState<boolean>(true);
useEffect(() => {
async function load() {
const { [name]: _data } = await import(
`../../../../../packages/dss-ui/src/components/${componentDir}/${componentName}.stories.tsx`
);
const { default: _struct } = await import(
`../../../../../packages/dss-ui/src/components/${componentDir}/${componentName}.stories.tsx`
);
setIsLoading(false);
setData(_data);
setStruct(_struct);
setExampleState(EXAMPLE_STATE.idle);
}
load();
}, [name, componentDir, componentName]);
return [struct, data, isLoading];
};