-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref(storybook) use react query for module loading (#82931)
Use react query for story loading
- Loading branch information
Showing
3 changed files
with
18 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,28 @@ | ||
import {useCallback, useEffect, useState} from 'react'; | ||
|
||
import {useQuery} from 'sentry/utils/queryClient'; | ||
import storiesContext from 'sentry/views/stories/storiesContext'; | ||
import type {ResolvedStoryModule} from 'sentry/views/stories/types'; | ||
|
||
interface Props { | ||
interface UseStoriesLoaderOptions { | ||
filename: string; | ||
} | ||
|
||
interface EmptyState { | ||
error: undefined; | ||
filename: undefined | string; | ||
resolved: undefined; | ||
} | ||
|
||
interface ResolvedState { | ||
error: undefined; | ||
interface StoryDescriptor { | ||
filename: string; | ||
resolved: ResolvedStoryModule; | ||
} | ||
|
||
interface ErrorState { | ||
error: Error; | ||
filename: undefined | string; | ||
resolved: undefined; | ||
function importStory(filename: string): Promise<StoryDescriptor> { | ||
return storiesContext() | ||
.importStory(filename) | ||
.then(story => ({ | ||
filename, | ||
resolved: story, | ||
})); | ||
} | ||
|
||
type State = EmptyState | ResolvedState | ErrorState; | ||
|
||
export default function useStoriesLoader({filename}: Props) { | ||
const [mod, setMod] = useState<State>({ | ||
error: undefined, | ||
filename, | ||
resolved: undefined, | ||
export default function useStoriesLoader({filename}: UseStoriesLoaderOptions) { | ||
return useQuery({ | ||
queryKey: [filename], | ||
queryFn: () => importStory(filename), | ||
}); | ||
|
||
const asyncImportStory = useCallback(async () => { | ||
if (!filename) { | ||
return; | ||
} | ||
try { | ||
setMod({ | ||
error: undefined, | ||
filename, | ||
resolved: undefined, | ||
}); | ||
const resolved = await storiesContext().importStory(filename); | ||
setMod({ | ||
error: undefined, | ||
filename, | ||
|
||
resolved, | ||
}); | ||
} catch (error) { | ||
setMod({ | ||
error, | ||
filename, | ||
|
||
resolved: undefined, | ||
}); | ||
} | ||
}, [filename]); | ||
|
||
useEffect(() => { | ||
asyncImportStory(); | ||
}, [asyncImportStory]); | ||
|
||
return mod; | ||
} |