-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: search data * chore: fill content * chore: fix loading * chore: support loading state
- Loading branch information
Showing
14 changed files
with
227 additions
and
85 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
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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { loadFilesMeta, useSiteData } from 'dumi'; | ||
import React from 'react'; | ||
import type { DemoInfo } from '../context'; | ||
import type { IRouteMeta } from '../types'; | ||
import { useLocaleDocRoutes } from '../utils'; | ||
|
||
type RoutesData = Record<string, IRouteMeta & { demos: DemoInfo[] }>; | ||
|
||
type Demos = Record<string, DemoInfo>; | ||
|
||
type ReturnData = [ | ||
routes: ReturnType<typeof useLocaleDocRoutes> | null, | ||
demo: Demos | null, | ||
]; | ||
|
||
export default function useSearchData(enabled: boolean): ReturnData { | ||
const routes = useLocaleDocRoutes(); | ||
const [filesMeta, setFilesMeta] = React.useState<RoutesData | null>(null); | ||
const { tabs } = useSiteData(); | ||
|
||
React.useEffect(() => { | ||
if (enabled) { | ||
loadFilesMeta().then((data) => { | ||
setFilesMeta(data); | ||
}); | ||
} | ||
}, [enabled]); | ||
|
||
return React.useMemo(() => { | ||
if (!filesMeta) { | ||
return [null, null]; | ||
} | ||
|
||
// Route Meta | ||
const mergedRoutes: typeof routes = {}; | ||
|
||
Object.keys(routes).forEach((routeId) => { | ||
mergedRoutes[routeId] = { | ||
...routes[routeId], | ||
}; | ||
|
||
// Fill routes meta | ||
if (mergedRoutes[routeId].meta) { | ||
mergedRoutes[routeId].meta = { | ||
...mergedRoutes[routeId].meta, | ||
...filesMeta[routeId], | ||
tabs: mergedRoutes[routeId].tabs?.map((id: string) => { | ||
const meta = { | ||
frontmatter: { title: tabs[id].title }, | ||
toc: [], | ||
texts: [], | ||
}; | ||
return { | ||
...tabs[id], | ||
meta: filesMeta[id] || meta, | ||
}; | ||
}), | ||
}; | ||
} | ||
}); | ||
|
||
// Demos | ||
const demos: Demos = Object.entries(filesMeta).reduce((acc, [id, meta]) => { | ||
// append route id to demo | ||
Object.values(meta.demos).forEach((demo) => { | ||
demo.routeId = id; | ||
}); | ||
// merge demos | ||
Object.assign(acc, meta.demos); | ||
|
||
return acc; | ||
}, {}); | ||
|
||
return [mergedRoutes, demos]; | ||
}, [filesMeta, routes, tabs]); | ||
} |
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
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
Oops, something went wrong.