feat(widgets): consolidate integration metadata + shared response caching (#6691)
This commit is contained in:
1 parent
7d1d9062ed
commit
8d8dec5749
206 files changed
+3893
-1946
No files matched your search
@@ -5,32 +5,40 @@ import type { WidgetKind } from "@homarr/definitions";
|
||||
import { createSettings } from "@homarr/settings/creator";
|
||||
|
||||
import { loadWidgetDefinition, reduceWidgetOptionsWithDefinition } from "./manifest";
|
||||
import prefetchForApps from "./app/prefetch";
|
||||
import prefetchForBookmarks from "./bookmarks/prefetch";
|
||||
import type { Prefetch, WidgetOptionsRecordOf } from "./definition";
|
||||
import type { inferOptionsFromCreator } from "./options";
|
||||
import type { PrefetchLoader } from "./definition";
|
||||
|
||||
const prefetchCallbacks: Partial<{
|
||||
[TKind in WidgetKind]: Prefetch<TKind>;
|
||||
}> = {
|
||||
bookmarks: prefetchForBookmarks,
|
||||
app: prefetchForApps,
|
||||
};
|
||||
const definePrefetchLoaders = <TLoaders extends Partial<Record<WidgetKind, PrefetchLoader>>>(loaders: TLoaders) =>
|
||||
loaders;
|
||||
|
||||
export const prefetchForKindAsync = async <TKind extends WidgetKind>(
|
||||
kind: TKind,
|
||||
// Keep these imports explicit so Next.js can trace each optional prefetch
|
||||
// module without loading its database-specific implementation eagerly.
|
||||
const prefetchLoaders = definePrefetchLoaders({
|
||||
app: () => import("./app/prefetch"),
|
||||
bookmarks: () => import("./bookmarks/prefetch"),
|
||||
});
|
||||
|
||||
type PrefetchWidgetKind = keyof typeof prefetchLoaders;
|
||||
|
||||
const hasPrefetchLoader = (kind: WidgetKind): kind is PrefetchWidgetKind =>
|
||||
Object.prototype.hasOwnProperty.call(prefetchLoaders, kind);
|
||||
|
||||
export const prefetchForKindAsync = async (
|
||||
kind: WidgetKind,
|
||||
queryClient: QueryClient,
|
||||
items: {
|
||||
options: inferOptionsFromCreator<WidgetOptionsRecordOf<TKind>>;
|
||||
options: Record<string, unknown>;
|
||||
integrationIds: string[];
|
||||
}[],
|
||||
) => {
|
||||
const callback = prefetchCallbacks[kind];
|
||||
if (!callback) {
|
||||
if (!hasPrefetchLoader(kind)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const [serverSettings, definition] = await Promise.all([getRscServerSettingsAsync(), loadWidgetDefinition(kind)]);
|
||||
const [{ default: callback }, serverSettings, definition] = await Promise.all([
|
||||
prefetchLoaders[kind](),
|
||||
getRscServerSettingsAsync(),
|
||||
loadWidgetDefinition(kind),
|
||||
]);
|
||||
|
||||
const itemsWithDefaultOptions = items.map((item) => ({
|
||||
...item,
|
||||
@@ -41,5 +49,5 @@ export const prefetchForKindAsync = async <TKind extends WidgetKind>(
|
||||
),
|
||||
}));
|
||||
|
||||
await callback(queryClient, itemsWithDefaultOptions as never[]);
|
||||
await callback(queryClient, itemsWithDefaultOptions);
|
||||
};
|
||||
Reference in new issue
Block a user