* feat: add pi hole summary integration * feat: add pi hole summary widget * fix: type issues with integrations and integrationIds * feat: add middleware for integrations and improve cache redis channel * feat: add error boundary for widgets * fix: broken lock file * fix: format format issues * fix: typecheck issue * fix: deepsource issues * fix: widget sandbox without error boundary * chore: address pull request feedback * chore: remove todo comment and created issue * fix: format issues * fix: deepsource issue
43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { useMemo } from "react";
|
|
import { IconExclamationCircle } from "@tabler/icons-react";
|
|
import { TRPCClientError } from "@trpc/client";
|
|
import type { DefaultErrorData } from "@trpc/server/unstable-core-do-not-import";
|
|
|
|
import type { WidgetKind } from "@homarr/definitions";
|
|
|
|
import { widgetImports } from "..";
|
|
import { ErrorBoundaryError } from "./base";
|
|
import { BaseWidgetError } from "./base-component";
|
|
|
|
interface WidgetErrorProps {
|
|
kind: WidgetKind;
|
|
error: unknown;
|
|
resetErrorBoundary: () => void;
|
|
}
|
|
|
|
export const WidgetError = ({ error, resetErrorBoundary, kind }: WidgetErrorProps) => {
|
|
const currentDefinition = useMemo(() => widgetImports[kind].definition, [kind]);
|
|
|
|
if (error instanceof ErrorBoundaryError) {
|
|
return <BaseWidgetError {...error.getErrorBoundaryData()} onRetry={resetErrorBoundary} />;
|
|
}
|
|
|
|
if (error instanceof TRPCClientError && "code" in error.data) {
|
|
const errorData = error.data as DefaultErrorData;
|
|
|
|
if (!("errors" in currentDefinition && errorData.code in currentDefinition.errors)) return null;
|
|
|
|
const errorDefinition = currentDefinition.errors[errorData.code as keyof typeof currentDefinition.errors];
|
|
|
|
return <BaseWidgetError {...errorDefinition} onRetry={resetErrorBoundary} showLogsLink />;
|
|
}
|
|
|
|
return (
|
|
<BaseWidgetError
|
|
icon={IconExclamationCircle}
|
|
message={(error as { toString: () => string }).toString()}
|
|
onRetry={resetErrorBoundary}
|
|
/>
|
|
);
|
|
};
|