fix: address widget reload review findings

This commit is contained in:
Thomas Camlong committed 2026-08-23 09:00:33 +02:00
1 parent 357355283c
commit 65a96b6b14
10 files changed
+206 -29

No files matched your search

@@ -4,6 +4,8 @@ import { Suspense, use, useMemo } from "react";
import type { ToolCallMessagePartProps } from "@assistant-ui/react";
import { Alert, Box, Button, Group, Skeleton, Stack, Text, ThemeIcon } from "@mantine/core";
import { IconAlertTriangle, IconCheck, IconSettings } from "@tabler/icons-react";
import { ErrorBoundary } from "react-error-boundary";
import type { FallbackProps } from "react-error-boundary";
import { clientApi } from "@homarr/api/client";
import { useSession } from "@homarr/auth/client";
@@ -73,9 +75,27 @@ export const AssistantConfigureWidgetTool = (props: AssistantConfigureWidgetTool
if (!completeArgs) return <AssistantConfigureWidgetToolContent {...props} definition={undefined} />;
return (
<Suspense fallback={<AssistantConfigureWidgetSkeleton />}>
<LoadedAssistantConfigureWidgetTool {...props} args={completeArgs} />
</Suspense>
<ErrorBoundary FallbackComponent={AssistantWidgetDefinitionErrorFallback} resetKeys={[completeArgs.kind]}>
<Suspense fallback={<AssistantConfigureWidgetSkeleton />}>
<LoadedAssistantConfigureWidgetTool {...props} args={completeArgs} />
</Suspense>
</ErrorBoundary>
);
};
const AssistantWidgetDefinitionErrorFallback = ({ resetErrorBoundary }: FallbackProps) => {
const t = useI18n("assistant.configureWidget");
const actionT = useI18n("common.action");
return (
<Alert color="red" variant="light" title={t("definitionLoadErrorTitle")} icon={<IconAlertTriangle size={18} />}>
<Stack gap="sm">
<Text size="sm">{t("definitionLoadErrorDescription")}</Text>
<Button variant="light" color="red" size="compact-sm" w="fit-content" onClick={resetErrorBoundary}>
{actionT("tryAgain")}
</Button>
</Stack>
</Alert>
);
};
@@ -1,6 +1,7 @@
import { readdirSync, readFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { parse } from "@babel/parser";
import { describe, expect, test } from "vitest";
const sourceRoot = join(dirname(fileURLToPath(import.meta.url)), "../..");
@@ -13,18 +14,105 @@ const collectRuntimeSourceFiles = (directory: string): string[] =>
return [path];
});
const rootWidgetsModule = "@homarr/widgets";
interface AstNode {
type: string;
[key: string]: unknown;
}
const isAstNode = (value: unknown): value is AstNode =>
typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
const getAstNode = (value: unknown) => (isAstNode(value) ? value : undefined);
const getAstNodes = (value: unknown) => (Array.isArray(value) ? value.filter(isAstNode) : []);
const isRootWidgetsModule = (node: unknown) => {
const astNode = getAstNode(node);
return astNode?.type === "StringLiteral" && astNode.value === rootWidgetsModule;
};
const hasRuntimeRootWidgetImport = (source: string, fileName: string): boolean => {
const sourceFile = parse(source, {
sourceFilename: fileName,
sourceType: "unambiguous",
plugins: ["typescript", "jsx", "decorators-legacy", "importAttributes"],
});
let found = false;
const visit = (node: AstNode) => {
if (found) return;
if (node.type === "ImportDeclaration" && isRootWidgetsModule(node.source)) {
const specifiers = getAstNodes(node.specifiers);
if (
node.importKind !== "type" &&
(specifiers.length === 0 ||
specifiers.some((specifier) => specifier.type !== "ImportSpecifier" || specifier.importKind !== "type"))
) {
found = true;
}
} else if (
(node.type === "ExportNamedDeclaration" || node.type === "ExportAllDeclaration") &&
isRootWidgetsModule(node.source)
) {
const specifiers = getAstNodes(node.specifiers);
if (
node.exportKind !== "type" &&
(node.type === "ExportAllDeclaration" ||
specifiers.length === 0 ||
specifiers.some((specifier) => specifier.exportKind !== "type"))
) {
found = true;
}
} else if (node.type === "TSImportEqualsDeclaration" && node.importKind !== "type") {
const moduleReference = getAstNode(node.moduleReference);
if (moduleReference?.type === "TSExternalModuleReference" && isRootWidgetsModule(moduleReference.expression)) {
found = true;
}
} else if (node.type === "CallExpression") {
const callee = getAstNode(node.callee);
const argument = getAstNodes(node.arguments)[0];
if (
isRootWidgetsModule(argument) &&
(callee?.type === "Import" || (callee?.type === "Identifier" && callee.name === "require"))
) {
found = true;
}
}
for (const value of Object.values(node)) {
if (isAstNode(value)) visit(value);
else if (Array.isArray(value)) value.filter(isAstNode).forEach(visit);
}
};
visit(sourceFile as unknown as AstNode);
return found;
};
describe("widget import boundary", () => {
test("application runtime code uses narrow widget exports", () => {
const offenders = collectRuntimeSourceFiles(sourceRoot).filter((path) => {
const source = readFileSync(path, "utf8");
const importStatements = source.match(/import[\s\S]*?;/g) ?? [];
return (
importStatements.some(
(statement) => !/^import\s+type\b/.test(statement) && /\sfrom\s+["']@homarr\/widgets["']/.test(statement),
) || /import\(["']@homarr\/widgets["']\)/.test(source)
);
return hasRuntimeRootWidgetImport(source, path);
});
expect(offenders).toEqual([]);
});
test.each([
['import type { WidgetDefinition } from "@homarr/widgets";', false],
['import { type WidgetDefinition } from "@homarr/widgets";', false],
['export type { WidgetDefinition } from "@homarr/widgets";', false],
["const example = 'import { widgets } from \"@homarr/widgets\";';", false],
['// import { widgets } from "@homarr/widgets";', false],
['import { widgets } from "@homarr/widgets";', true],
['import { type WidgetDefinition, widgets } from "@homarr/widgets";', true],
['export * from "@homarr/widgets";', true],
['import /* chunk */ ( /* source */ "@homarr/widgets" );', true],
['require("@homarr/widgets");', true],
])("detects runtime root imports in %s", (source, expected) => {
expect(hasRuntimeRootWidgetImport(source, "fixture.ts")).toBe(expected);
});
});