-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Make searchable again #1898
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import type { PICKED_PKG_FIELDS } from '@/constants'; | ||
import type { AtomComponentAsset } from 'dumi-assets-types'; | ||
import { createContext, useContext, type ComponentType } from 'react'; | ||
import type { ILocalesConfig, IPreviewerProps, IThemeConfig } from '../types'; | ||
import type { | ||
ILocalesConfig, | ||
IPreviewerProps, | ||
IRouteMeta, | ||
IThemeConfig, | ||
} from '../types'; | ||
import use from './use'; | ||
|
||
export type DemoInfo = { | ||
|
@@ -16,6 +21,7 @@ interface ISiteContext { | |
entryExports: Record<string, any>; | ||
demos: Record<string, DemoInfo>; | ||
components: Record<string, AtomComponentAsset>; | ||
tabs: IRouteMeta['tabs']; | ||
locales: ILocalesConfig; | ||
themeConfig: IThemeConfig; | ||
hostname?: string; | ||
|
@@ -44,6 +50,7 @@ export const SiteContext = createContext<ISiteContext>({ | |
setLoading: () => {}, | ||
_2_level_nav_available: true, | ||
getDemoById: async () => null, | ||
tabs: [], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getAllRoutesMeta 的返回值里不包含 tabs 数据么 |
||
}); | ||
|
||
export const useSiteData = () => { | ||
|
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 = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个兜底逻辑在 meta runtime 插件里有的,是不是可以不用做兜底了:https://github.com/umijs/dumi/blob/feature/2.3.0/src/templates/meta-runtime.ts.tpl#L31-L35 以及这里要遍历的应该是 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. runtime 里这个已经是空的 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 之后再起个新的 PR 把 runtime 清理掉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. runtime 删了会影响页面 Tab 展示的,因为 useRouteMeta 必须得返回 tabs 数据,页面上才有数据源展示 Tab |
||
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]); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 exports.ts 里导出以后应该也是能拿到类型的?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个是兜
client
的,打包出的dist
会跟着exports
走。