import type React from "react"; import type { LoaderComponent } from "next/dynamic"; import type { QueryClient, QueryKey } from "@tanstack/react-query"; import type { DefaultErrorData } from "@trpc/server/unstable-core-do-not-import"; import type { IntegrationKind, WidgetKind } from "@homarr/definitions"; import type { ServerSettings } from "@homarr/server-settings"; import type { SettingsContextProps } from "@homarr/settings/creator"; import type { stringOrTranslation } from "@homarr/translation"; import type { TablerIcon } from "@homarr/ui"; import type { WidgetImports } from "."; import type { inferOptionsFromCreator, inferOptionsFromDefinition, WidgetOptionsRecord } from "./options"; export interface WidgetContextMenuAction { key: string; label: stringOrTranslation; icon?: TablerIcon; onClick: () => void; hidden?: boolean; disabled?: boolean; color?: string; } export interface WidgetContextMenuContext { isEditMode: boolean; boardId: string | undefined; itemId: string | undefined; } export interface WidgetContextActionProps< TKind extends WidgetKind, TOptions extends WidgetOptionsRecord = WidgetOptionsRecord, > { kind: TKind; options: inferOptionsFromDefinition; setOptions: (partial: Partial>) => void; integrationIds: string[]; context: WidgetContextMenuContext; widgetStateRef: React.MutableRefObject | null>; } const createWithDynamicImport = (kind: TKind, definition: TDefinition) => (componentLoader: () => LoaderComponent>) => ({ definition: { ...definition, kind, }, kind, componentLoader, }); export type PrefetchLoader = () => Promise<{ default: Prefetch }>; export type Prefetch = ( queryClient: QueryClient, items: { options: inferOptionsFromCreator>; integrationIds: string[]; }[], ) => Promise; export const createWidgetDefinition = ( kind: TKind, definition: TDefinition, ) => ({ withDynamicImport: createWithDynamicImport(kind, definition), }); export interface WidgetDefinition { icon: TablerIcon; queryKey?: QueryKey; queryKeys?: QueryKey[]; refetchInterval?: number | null; supportedIntegrations?: IntegrationKind[]; integrationsRequired?: boolean; maxIntegrations?: number; createOptions: ( settings: Pick, ) => WidgetOptionsRecord; errors?: Partial< Record< DefaultErrorData["code"], { icon: TablerIcon; message: stringOrTranslation; hideLogsLink?: boolean; } > >; contextActions?: (props: Omit, "kind">) => WidgetContextMenuAction[]; } export const getWidgetQueryKeys = (definition: WidgetDefinition, kind: WidgetKind): QueryKey[] => definition.queryKeys ?? [definition.queryKey ?? [["widget", kind]]]; export interface WidgetProps { options: inferOptionsFromCreator>; integrationIds: string[]; itemId: string | undefined; // undefined when in preview mode } export type WidgetComponentProps = WidgetProps & { boardId: string | undefined; // undefined when in preview mode isEditMode: boolean; setOptions: ({ newOptions }: { newOptions: Partial>> }) => void; width: number; height: number; widgetStateRef?: React.MutableRefObject | null>; removeItem?: () => void; }; export type WidgetOptionsRecordOf = WidgetImports[TKind]["definition"]["createOptions"]; /** * The following type should only include values that can be available for user (including anonymous). * Because they need to be provided to the client to for example set certain default values. */ export interface WidgetOptionsSettings { server: { board: Pick; }; }