fix: simplify custom widget preview controls

This commit is contained in:
Thomas Camlong committed 2026-08-10 17:22:40 +02:00
1 parent 893fcd6ed1
commit be93fb330a
8 files changed
+116 -120

No files matched your search

@@ -25,10 +25,10 @@ Every option has a default. Supported controls include text, textarea, number, s
## ChatGPT or Claude ## ChatGPT or Claude
Enter the desired widget and optional API documentation URL, then select **Copy AI prompt**. The single self-contained prompt is below 12,000 characters, starts with your request, includes the lean contract and two polished examples, and never claims that an offline chat has MCP tools. Open **Create or fix with any AI**, enter the desired widget and optional API documentation URL, then select the copy action beside the accordion title. The single self-contained prompt is below 12,000 characters, starts with your request, includes the lean contract and two polished examples, and never claims that an offline chat has MCP tools.
The model must return one fenced `json` block containing the complete widget, including the JSX in its `template` The model must return one fenced `json` block containing the complete widget, including the JSX in its `template`
field. Copy that single block and select **Paste AI response**. Homarr fills the visual builders and the JSX editor field. Copy that single block and select **Paste**. Homarr fills the visual builders and the JSX editor
without any placeholder replacement or second paste. without any placeholder replacement or second paste.
Homarr shows all actionable schema or JSX errors inline. Enter credentials separately. The prompt deliberately Homarr shows all actionable schema or JSX errors inline. Enter credentials separately. The prompt deliberately
@@ -30,7 +30,13 @@ describe("Copy AI prompt", () => {
`${process.cwd()}/apps/nextjs/src/app/[locale]/manage/custom-widgets/_custom-widget-form.module.css`, `${process.cwd()}/apps/nextjs/src/app/[locale]/manage/custom-widgets/_custom-widget-form.module.css`,
"utf8", "utf8",
); );
expect(card).toContain("<SimpleGrid cols={{ base: 1, sm: 2 }}>"); const actions = readFileSync(
`${process.cwd()}/apps/nextjs/src/app/[locale]/manage/custom-widgets/_copy-ai-prompt-button.tsx`,
"utf8",
);
expect(card).toContain('<Accordion variant="contained">');
expect(card).not.toContain("defaultValue");
expect(actions).toContain("<ActionIcon");
expect(styles).toContain(".sectionNav > a"); expect(styles).toContain(".sectionNav > a");
expect(styles).toContain("flex: 0 0 auto"); expect(styles).toContain("flex: 0 0 auto");
}); });
@@ -1,7 +1,7 @@
"use client"; "use client";
import { Button } from "@mantine/core"; import { ActionIcon, Tooltip } from "@mantine/core";
import { IconSparkles } from "@tabler/icons-react"; import { IconCopy } from "@tabler/icons-react";
import { buildCustomWidgetAiPrompt } from "@homarr/custom-widgets/authoring-prompt"; import { buildCustomWidgetAiPrompt } from "@homarr/custom-widgets/authoring-prompt";
import { showErrorNotification, showSuccessNotification } from "@homarr/notifications"; import { showErrorNotification, showSuccessNotification } from "@homarr/notifications";
@@ -36,15 +36,16 @@ export const CopyAiPromptButton = ({
}; };
return ( return (
<Button <Tooltip label={t("action.copyAiPrompt")}>
type="button" <ActionIcon
variant="light" type="button"
leftSection={<IconSparkles size={16} />} variant="subtle"
onClick={() => void handleCopy()} size="lg"
fullWidth aria-label={t("action.copyAiPrompt")}
size="sm" onClick={() => void handleCopy()}
> >
{t("action.copyAiPrompt")} <IconCopy size={16} />
</Button> </ActionIcon>
</Tooltip>
); );
}; };
@@ -1,7 +1,7 @@
"use client"; "use client";
import { Button, Card, Group, SimpleGrid, Stack, Text, TextInput, Textarea } from "@mantine/core"; import { Accordion, Button, Group, Stack, Text, TextInput, Textarea } from "@mantine/core";
import { IconSparkles } from "@tabler/icons-react"; import { IconClipboard, IconSparkles } from "@tabler/icons-react";
import type { HomarrCustomWidgetV2 } from "@homarr/custom-widgets/core"; import type { HomarrCustomWidgetV2 } from "@homarr/custom-widgets/core";
import { useScopedI18n } from "@homarr/translation/client"; import { useScopedI18n } from "@homarr/translation/client";
@@ -20,38 +20,41 @@ interface AiCardProps {
export function CustomWidgetAiCard(props: AiCardProps) { export function CustomWidgetAiCard(props: AiCardProps) {
const t = useScopedI18n("customWidget.workbench.ai"); const t = useScopedI18n("customWidget.workbench.ai");
return ( return (
<Card withBorder p="md"> <Accordion variant="contained">
<Stack gap="sm"> <Accordion.Item value="ai">
<Group gap="xs"> <Group gap={0} wrap="nowrap">
<IconSparkles size={18} /> <Accordion.Control icon={<IconSparkles size={18} />} style={{ flex: 1 }}>
<Text fw={600}>{t("title")}</Text> {t("title")}
</Group> </Accordion.Control>
<Text size="sm" c="dimmed">
{t("description")}
</Text>
<Textarea
label={t("request")}
value={props.request}
onChange={(event) => props.onRequestChange(event.currentTarget.value)}
autosize
minRows={2}
/>
<TextInput
label={t("documentationUrl")}
value={props.documentationUrl}
onChange={(event) => props.onDocumentationUrlChange(event.currentTarget.value)}
/>
<SimpleGrid cols={{ base: 1, sm: 2 }}>
<CopyAiPromptButton <CopyAiPromptButton
currentConfig={props.candidate} currentConfig={props.candidate}
request={props.request} request={props.request}
documentationUrl={props.documentationUrl} documentationUrl={props.documentationUrl}
/> />
<Button type="button" variant="light" leftSection={<IconSparkles size={16} />} onClick={props.onPaste}> </Group>
{t("paste")} <Accordion.Panel>
</Button> <Stack gap="sm">
</SimpleGrid> <Text size="sm" c="dimmed">
</Stack> {t("description")}
</Card> </Text>
<Textarea
label={t("request")}
value={props.request}
onChange={(event) => props.onRequestChange(event.currentTarget.value)}
autosize
minRows={2}
/>
<TextInput
label={t("documentationUrl")}
value={props.documentationUrl}
onChange={(event) => props.onDocumentationUrlChange(event.currentTarget.value)}
/>
<Button type="button" variant="light" leftSection={<IconClipboard size={16} />} onClick={props.onPaste}>
{t("paste")}
</Button>
</Stack>
</Accordion.Panel>
</Accordion.Item>
</Accordion>
); );
} }
@@ -67,7 +67,6 @@ export function CustomWidgetForm({ mode, initialValues, definitionId }: CustomWi
const [documentationUrl, setDocumentationUrl] = useState(""); const [documentationUrl, setDocumentationUrl] = useState("");
const [preview, setPreview] = useState<PreviewState>({ data: {}, status: {}, session: null, outcome: "idle" }); const [preview, setPreview] = useState<PreviewState>({ data: {}, status: {}, session: null, outcome: "idle" });
const [previewSize, setPreviewSize] = useState("standard"); const [previewSize, setPreviewSize] = useState("standard");
const [previewTheme, setPreviewTheme] = useState<"light" | "dark">("dark");
const [optionsSnapshot, setOptionsSnapshot] = useState<Record<string, unknown>>(() => { const [optionsSnapshot, setOptionsSnapshot] = useState<Record<string, unknown>>(() => {
const initialDefinition = buildDefinition(formInitialValues); const initialDefinition = buildDefinition(formInitialValues);
return initialDefinition.success ? getDefinitionDefaults(initialDefinition.data) : {}; return initialDefinition.success ? getDefinitionDefaults(initialDefinition.data) : {};
@@ -267,8 +266,6 @@ export function CustomWidgetForm({ mode, initialValues, definitionId }: CustomWi
preview={preview} preview={preview}
size={previewSize} size={previewSize}
onSizeChange={setPreviewSize} onSizeChange={setPreviewSize}
theme={previewTheme}
onThemeChange={setPreviewTheme}
optionsSnapshot={optionsSnapshot} optionsSnapshot={optionsSnapshot}
onOptionsChange={setOptionsSnapshot} onOptionsChange={setOptionsSnapshot}
onLiveActionsChange={(enabled) => onLiveActionsChange={(enabled) =>
@@ -1,5 +1,6 @@
// @vitest-environment jsdom // @vitest-environment jsdom
import { readFileSync } from "node:fs";
import { act } from "react"; import { act } from "react";
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import type { Root } from "react-dom/client"; import type { Root } from "react-dom/client";
@@ -69,6 +70,16 @@ afterEach(async () => {
}); });
describe("Custom Widget preview panel", () => { describe("Custom Widget preview panel", () => {
test("inherits the application theme instead of forcing a reload-sensitive preview theme", () => {
const source = readFileSync(
`${process.cwd()}/apps/nextjs/src/app/[locale]/manage/custom-widgets/_custom-widget-preview-panel.tsx`,
"utf8",
);
expect(source).not.toContain("forceColorScheme");
expect(source).not.toContain("onThemeChange");
});
test("contains invalid definitions inside the widget canvas while keeping every inspection tab", async () => { test("contains invalid definitions inside the widget canvas while keeping every inspection tab", async () => {
await act(async () => { await act(async () => {
root.render( root.render(
@@ -79,8 +90,6 @@ describe("Custom Widget preview panel", () => {
preview={{ data: { previous: true }, status: {}, session: null, outcome: "idle" }} preview={{ data: { previous: true }, status: {}, session: null, outcome: "idle" }}
size="standard" size="standard"
onSizeChange={vi.fn()} onSizeChange={vi.fn()}
theme="dark"
onThemeChange={vi.fn()}
optionsSnapshot={{}} optionsSnapshot={{}}
onOptionsChange={vi.fn()} onOptionsChange={vi.fn()}
onLiveActionsChange={vi.fn()} onLiveActionsChange={vi.fn()}
@@ -124,6 +133,8 @@ describe("Custom Widget preview panel", () => {
expect(host.textContent).toContain("sources.0.baseUrl"); expect(host.textContent).toContain("sources.0.baseUrl");
expect(host.textContent).toContain("Enter a complete URL."); expect(host.textContent).toContain("Enter a complete URL.");
expect(host.textContent).not.toContain("sessionActive");
expect(host.textContent).not.toContain("simulationPending");
}); });
test("contains renderer failures inside the widget tab and keeps the inspection tabs usable", async () => { test("contains renderer failures inside the widget tab and keeps the inspection tabs usable", async () => {
@@ -149,8 +160,6 @@ describe("Custom Widget preview panel", () => {
preview={preview} preview={preview}
size="standard" size="standard"
onSizeChange={vi.fn()} onSizeChange={vi.fn()}
theme="dark"
onThemeChange={vi.fn()}
optionsSnapshot={{ ...failingOptions }} optionsSnapshot={{ ...failingOptions }}
onOptionsChange={vi.fn()} onOptionsChange={vi.fn()}
onLiveActionsChange={vi.fn()} onLiveActionsChange={vi.fn()}
@@ -175,8 +184,6 @@ describe("Custom Widget preview panel", () => {
preview={preview} preview={preview}
size="standard" size="standard"
onSizeChange={vi.fn()} onSizeChange={vi.fn()}
theme="dark"
onThemeChange={vi.fn()}
optionsSnapshot={failingOptions} optionsSnapshot={failingOptions}
onOptionsChange={vi.fn()} onOptionsChange={vi.fn()}
onLiveActionsChange={vi.fn()} onLiveActionsChange={vi.fn()}
@@ -211,8 +218,6 @@ describe("Custom Widget preview panel", () => {
preview={preview} preview={preview}
size="standard" size="standard"
onSizeChange={vi.fn()} onSizeChange={vi.fn()}
theme="dark"
onThemeChange={vi.fn()}
optionsSnapshot={{}} optionsSnapshot={{}}
onOptionsChange={vi.fn()} onOptionsChange={vi.fn()}
onLiveActionsChange={vi.fn()} onLiveActionsChange={vi.fn()}
@@ -1,20 +1,7 @@
"use client"; "use client";
import { useState } from "react"; import { useState } from "react";
import { import { Alert, Box, Card, Group, Paper, SegmentedControl, Stack, Switch, Tabs, Text } from "@mantine/core";
Alert,
Badge,
Box,
Card,
Group,
MantineProvider,
Paper,
SegmentedControl,
Stack,
Switch,
Tabs,
Text,
} from "@mantine/core";
import { clientApi } from "@homarr/api/client"; import { clientApi } from "@homarr/api/client";
import type { HomarrCustomWidgetV2 } from "@homarr/custom-widgets/core"; import type { HomarrCustomWidgetV2 } from "@homarr/custom-widgets/core";
@@ -44,8 +31,6 @@ interface PreviewPanelProps {
preview: PreviewState; preview: PreviewState;
size: string; size: string;
onSizeChange(value: string): void; onSizeChange(value: string): void;
theme: "light" | "dark";
onThemeChange(value: "light" | "dark"): void;
optionsSnapshot: Record<string, unknown>; optionsSnapshot: Record<string, unknown>;
onOptionsChange(value: Record<string, unknown>): void; onOptionsChange(value: Record<string, unknown>): void;
onLiveActionsChange(enabled: boolean): void; onLiveActionsChange(enabled: boolean): void;
@@ -98,39 +83,51 @@ export function CustomWidgetPreviewPanel(props: PreviewPanelProps) {
return ( return (
<Card withBorder p="md"> <Card withBorder p="md">
<Stack gap="md"> <Stack gap="md">
<Group grow> <Group gap="xs" justify="space-between">
<SegmentedControl <SegmentedControl
size="xs" size="xs"
value={props.size} value={props.size}
onChange={props.onSizeChange} onChange={props.onSizeChange}
data={[ data={[
{ value: "compact", label: t("size.compact") }, {
{ value: "standard", label: t("size.standard") }, value: "compact",
{ value: "wide", label: t("size.wide") }, label: (
<span aria-label={t("size.compact")} title={t("size.compact")}>
S
</span>
),
},
{
value: "standard",
label: (
<span aria-label={t("size.standard")} title={t("size.standard")}>
M
</span>
),
},
{
value: "wide",
label: (
<span aria-label={t("size.wide")} title={t("size.wide")}>
L
</span>
),
},
]} ]}
/> />
<SegmentedControl <SegmentedControl
size="xs" size="xs"
value={props.theme} value={fixture}
onChange={(value) => props.onThemeChange(value as "light" | "dark")} disabled={!candidate}
onChange={(value) => setFixture(value as typeof fixture)}
data={[ data={[
{ value: "light", label: t("theme.light") }, { value: "live", label: t("fixture.live") },
{ value: "dark", label: t("theme.dark") }, { value: "loading", label: t("fixture.loading") },
{ value: "empty", label: t("fixture.empty") },
{ value: "error", label: t("fixture.error") },
]} ]}
/> />
</Group> </Group>
<SegmentedControl
size="xs"
value={fixture}
disabled={!candidate}
onChange={(value) => setFixture(value as typeof fixture)}
data={[
{ value: "live", label: t("fixture.live") },
{ value: "loading", label: t("fixture.loading") },
{ value: "empty", label: t("fixture.empty") },
{ value: "error", label: t("fixture.error") },
]}
/>
{candidate && ( {candidate && (
<PreviewResult <PreviewResult
outcome={props.preview.outcome} outcome={props.preview.outcome}
@@ -153,22 +150,20 @@ export function CustomWidgetPreviewPanel(props: PreviewPanelProps) {
</Tabs.List> </Tabs.List>
<Tabs.Panel value="widget" pt="sm"> <Tabs.Panel value="widget" pt="sm">
<Box className={classes.previewCanvas}> <Box className={classes.previewCanvas}>
<MantineProvider forceColorScheme={props.theme}> <Paper withBorder p="sm" h={360} w={widths[props.size] ?? 480} maw="100%" style={{ overflow: "auto" }}>
<Paper withBorder p="sm" h={360} w={widths[props.size] ?? 480} maw="100%" style={{ overflow: "auto" }}> {displayData ? (
{displayData ? ( <PreviewErrorBoundary
<PreviewErrorBoundary title={errorBoundaryMessages("title")}
title={errorBoundaryMessages("title")} description={t("containedFailures")}
description={t("containedFailures")} retryLabel={errorBoundaryMessages("retry")}
retryLabel={errorBoundaryMessages("retry")} resetKeys={[rendererResetKey]}
resetKeys={[rendererResetKey]} >
> <CustomJsxDisplay data={displayData} />
<CustomJsxDisplay data={displayData} /> </PreviewErrorBoundary>
</PreviewErrorBoundary> ) : (
) : ( <Alert color="yellow">{t("invalid")}</Alert>
<Alert color="yellow">{t("invalid")}</Alert> )}
)} </Paper>
</Paper>
</MantineProvider>
</Box> </Box>
</Tabs.Panel> </Tabs.Panel>
<Tabs.Panel value="data" pt="sm"> <Tabs.Panel value="data" pt="sm">
@@ -221,11 +216,6 @@ export function CustomWidgetPreviewPanel(props: PreviewPanelProps) {
</Tabs.Panel> </Tabs.Panel>
<Tabs.Panel value="diagnostics" pt="sm"> <Tabs.Panel value="diagnostics" pt="sm">
<Stack gap="xs"> <Stack gap="xs">
{props.preview.session ? (
<Badge color="green">{t("sessionActive")}</Badge>
) : (
<Badge color="gray">{t("simulationPending")}</Badge>
)}
<Text size="sm" c="dimmed"> <Text size="sm" c="dimmed">
{t("containedFailures")} {t("containedFailures")}
</Text> </Text>
+1 -7
View File
@@ -976,7 +976,7 @@
"description": "Copy one prompt, then paste the AI's single JSON block. Homarr loads its JSX into the editor automatically.", "description": "Copy one prompt, then paste the AI's single JSON block. Homarr loads its JSX into the editor automatically.",
"request": "What should this widget do?", "request": "What should this widget do?",
"documentationUrl": "API documentation URL (optional)", "documentationUrl": "API documentation URL (optional)",
"paste": "Paste AI response", "paste": "Paste",
"response": "AI response", "response": "AI response",
"loaded": "Widget loaded into the editor", "loaded": "Widget loaded into the editor",
"invalidResponse": "Clipboard does not contain a valid v2 widget", "invalidResponse": "Clipboard does not contain a valid v2 widget",
@@ -1010,8 +1010,6 @@
"liveActions": "Run actions against the configured API", "liveActions": "Run actions against the configured API",
"liveActionsDescription": "Off is safe simulation. Turning this on affects the real service.", "liveActionsDescription": "Off is safe simulation. Turning this on affects the real service.",
"noActions": "No actions are defined.", "noActions": "No actions are defined.",
"sessionActive": "Preview session active",
"simulationPending": "Actions are simulated until you run preview",
"containedFailures": "Runtime failures are contained inside this widget. Request journals never include secret values.", "containedFailures": "Runtime failures are contained inside this widget. Request journals never include secret values.",
"validation": { "validation": {
"title": "Validation", "title": "Validation",
@@ -1046,10 +1044,6 @@
"standard": "Standard", "standard": "Standard",
"wide": "Wide" "wide": "Wide"
}, },
"theme": {
"light": "Light",
"dark": "Dark"
},
"fixture": { "fixture": {
"live": "Live", "live": "Live",
"loading": "Loading", "loading": "Loading",