1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- import {useCallback, useEffect, useState} from 'react';
- import storiesContext from 'sentry/views/stories/storiesContext';
- import type {ResolvedStoryModule} from 'sentry/views/stories/types';
- interface Props {
- filename: string;
- }
- interface EmptyState {
- error: undefined;
- filename: undefined | string;
- resolved: undefined;
- }
- interface ResolvedState {
- error: undefined;
- filename: string;
- resolved: ResolvedStoryModule;
- }
- interface ErrorState {
- error: Error;
- filename: undefined | string;
- resolved: undefined;
- }
- type State = EmptyState | ResolvedState | ErrorState;
- export default function useStoriesLoader({filename}: Props) {
- const [mod, setMod] = useState<State>({
- error: undefined,
- filename,
- resolved: undefined,
- });
- const asyncImportStory = useCallback(async () => {
- if (!filename) {
- return;
- }
- try {
- const resolved = await storiesContext().importStory(filename);
- setMod({
- error: undefined,
- filename,
- resolved,
- });
- } catch (error) {
- setMod({
- error,
- filename,
- resolved: undefined,
- });
- }
- }, [filename]);
- useEffect(() => {
- asyncImportStory();
- }, [asyncImportStory]);
- return mod;
- }
|