feat: add widget server loader (#16)
* wip: Add gridstack board * wip: Centralize board pages, Add board settings page * fix: remove cyclic dependency and rename widget-sort to kind * improve: Add header actions as parallel route * feat: add item select modal, add category edit modal, * feat: add edit item modal * feat: add remove item modal * wip: add category actions * feat: add saving of board, wip: add app widget * Merge branch 'main' into add-board * chore: update turbo dependencies * chore: update mantine dependencies * chore: fix typescript errors, lint and format * feat: add confirm modal to category removal, move items of removed category to above wrapper * feat: remove app widget to continue in another branch * feat: add loading spinner until board is initialized * fix: issue with cellheight of gridstack items * feat: add translations for board * fix: issue with translation for settings page * feat: add widget server loader * fix: typing issue * chore: address pull request feedback * fix: formatting
This commit is contained in:
@@ -10,6 +10,62 @@ import type {
|
||||
} from "./options";
|
||||
import type { IntegrationSelectOption } from "./widget-integration-select";
|
||||
|
||||
type ServerDataLoader<TKind extends WidgetKind> = () => Promise<{
|
||||
default: (props: WidgetProps<TKind>) => Promise<Record<string, unknown>>;
|
||||
}>;
|
||||
|
||||
const createWithDynamicImport =
|
||||
<
|
||||
TKind extends WidgetKind,
|
||||
TDefinition extends WidgetDefinition,
|
||||
TServerDataLoader extends ServerDataLoader<TKind> | undefined,
|
||||
>(
|
||||
kind: TKind,
|
||||
definition: TDefinition,
|
||||
serverDataLoader: TServerDataLoader,
|
||||
) =>
|
||||
(
|
||||
componentLoader: () => LoaderComponent<
|
||||
WidgetComponentProps<TKind> &
|
||||
(TServerDataLoader extends ServerDataLoader<TKind>
|
||||
? {
|
||||
serverData: Awaited<
|
||||
ReturnType<Awaited<ReturnType<TServerDataLoader>>["default"]>
|
||||
>;
|
||||
}
|
||||
: never)
|
||||
>,
|
||||
) => ({
|
||||
definition: {
|
||||
...definition,
|
||||
kind,
|
||||
},
|
||||
kind,
|
||||
serverDataLoader,
|
||||
componentLoader,
|
||||
});
|
||||
|
||||
const createWithServerData =
|
||||
<TKind extends WidgetKind, TDefinition extends WidgetDefinition>(
|
||||
kind: TKind,
|
||||
definition: TDefinition,
|
||||
) =>
|
||||
<TServerDataLoader extends ServerDataLoader<TKind>>(
|
||||
serverDataLoader: TServerDataLoader,
|
||||
) => ({
|
||||
definition: {
|
||||
...definition,
|
||||
kind,
|
||||
},
|
||||
kind,
|
||||
serverDataLoader,
|
||||
withDynamicImport: createWithDynamicImport(
|
||||
kind,
|
||||
definition,
|
||||
serverDataLoader,
|
||||
),
|
||||
});
|
||||
|
||||
export const createWidgetDefinition = <
|
||||
TKind extends WidgetKind,
|
||||
TDefinition extends WidgetDefinition,
|
||||
@@ -17,15 +73,8 @@ export const createWidgetDefinition = <
|
||||
kind: TKind,
|
||||
definition: TDefinition,
|
||||
) => ({
|
||||
withDynamicImport: (
|
||||
componentLoader: () => LoaderComponent<WidgetComponentProps<TKind>>,
|
||||
) => ({
|
||||
definition: {
|
||||
kind,
|
||||
...definition,
|
||||
},
|
||||
componentLoader,
|
||||
}),
|
||||
withServerData: createWithServerData(kind, definition),
|
||||
withDynamicImport: createWithDynamicImport(kind, definition, undefined),
|
||||
});
|
||||
|
||||
export interface WidgetDefinition {
|
||||
@@ -34,13 +83,29 @@ export interface WidgetDefinition {
|
||||
options: WidgetOptionsRecord;
|
||||
}
|
||||
|
||||
export interface WidgetComponentProps<TKind extends WidgetKind> {
|
||||
export interface WidgetProps<TKind extends WidgetKind> {
|
||||
options: inferOptionsFromDefinition<WidgetOptionsRecordOf<TKind>>;
|
||||
integrations: inferIntegrationsFromDefinition<
|
||||
WidgetImports[TKind]["definition"]
|
||||
>;
|
||||
}
|
||||
|
||||
type inferServerDataForKind<TKind extends WidgetKind> =
|
||||
WidgetImports[TKind] extends { serverDataLoader: ServerDataLoader<TKind> }
|
||||
? Awaited<
|
||||
ReturnType<
|
||||
Awaited<
|
||||
ReturnType<WidgetImports[TKind]["serverDataLoader"]>
|
||||
>["default"]
|
||||
>
|
||||
>
|
||||
: undefined;
|
||||
|
||||
export type WidgetComponentProps<TKind extends WidgetKind> =
|
||||
WidgetProps<TKind> & {
|
||||
serverData?: inferServerDataForKind<TKind>;
|
||||
};
|
||||
|
||||
type inferIntegrationsFromDefinition<TDefinition extends WidgetDefinition> =
|
||||
TDefinition extends {
|
||||
supportedIntegrations: infer TSupportedIntegrations;
|
||||
|
||||
Reference in New Issue
Block a user