feat: modernize widget experience
This commit is contained in:
1 parent
6331c88911
commit
1fa345edd1
165 files changed
+3698
-1649
No files matched your search
@@ -6,23 +6,23 @@ tags:
|
|||||||
|
|
||||||
Homarr has the following keyboard shortcuts:
|
Homarr has the following keyboard shortcuts:
|
||||||
|
|
||||||
| Shortcut | Description |
|
| Shortcut | Description |
|
||||||
| ------------------------------------------ | ---------------------------------------------------------- |
|
| ---------------------------------------------- | ---------------------------------------------------------- |
|
||||||
| `MOD` + `K` | Opens the search bar |
|
| `MOD` + `K` | Opens the search bar |
|
||||||
| `MOD` + `J` | Toggles Dark Mode |
|
| `MOD` + `J` | Toggles Dark Mode |
|
||||||
| `MOD` + `E` | Toggles Edit Mode |
|
| `MOD` + `E` | Toggles Edit Mode |
|
||||||
| Hold `Shift` while hovering a widget | Opens its temporary advanced view |
|
| Hold `Shift` while hovering a supported widget | Opens its temporary advanced view |
|
||||||
| `Shift` + `Enter` on a focused widget | Opens its advanced view until it is closed |
|
| `Shift` + `Enter` on a supported widget | Opens its advanced view until it is closed |
|
||||||
| `Esc` | Closes an open advanced view or board menu |
|
| `Esc` | Closes an open advanced view or board menu |
|
||||||
| `Shift` + `F10` on a focused board section | Opens the precise add-item menu at the next available cell |
|
| `Shift` + `F10` on a focused board section | Opens the precise add-item menu at the next available cell |
|
||||||
|
|
||||||
:::tip
|
:::tip
|
||||||
|
|
||||||
Here `MOD` refers to the modifier key, which is `Ctrl` on Windows and `Cmd` on macOS.
|
Here `MOD` refers to the modifier key, which is `Ctrl` on Windows and `Cmd` on macOS.
|
||||||
|
|
||||||
Advanced view and precise placement are available outside edit mode. App tiles stay compact and do not have an
|
Advanced view and precise placement are available outside edit mode. Widgets without a distinct advanced experience
|
||||||
advanced view. Precise placement still requires permission to change the board and the right-click interaction
|
adapt to their normal dimensions instead. Precise placement still requires permission to change the board and the
|
||||||
preference to be enabled.
|
right-click interaction preference to be enabled.
|
||||||
|
|
||||||
:::
|
:::
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ import widgetOverview from "./img/widget-overview.png";
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ This widget displays time-series charts for a selected Beszel system, including
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ The Coolify widget displays a compact overview of your Coolify instance. It can
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import { technitiumDnsIntegration } from "@site/docs/integrations/technitium-dns
|
|||||||
|
|
||||||
<WidgetHeader widget={dnsHoleControlsWidget} categories={["DNS Hole"]} />
|
<WidgetHeader widget={dnsHoleControlsWidget} categories={["DNS Hole"]} />
|
||||||
|
|
||||||
This widget lets permitted users enable, disable, or temporarily pause one or more DNS services. It shares the summary query and cache with DNS Hole Summary. Advanced view exposes every selected source, while compact layouts reduce controls according to both widget width and height.
|
This widget lets permitted users enable, disable, or temporarily pause one or more DNS services. It shares the summary query and cache with DNS Hole Summary. Every selected source stays available in the normal scrollable view, while cards and controls adapt to the widget's width and height.
|
||||||
|
|
||||||
### Screenshots
|
### Screenshots
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ import smallSize from "./img/small.png";
|
|||||||
- Apply an action to all available services from the top action row. Homarr keeps successful results visible and reports how many sources failed.
|
- Apply an action to all available services from the top action row. Homarr keeps successful results visible and reports how many sources failed.
|
||||||
- Interaction controls only appear when the current user has permission for that integration.
|
- Interaction controls only appear when the current user has permission for that integration.
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import { dnsHoleSummaryWidget } from ".";
|
|||||||
|
|
||||||
<WidgetHeader widget={dnsHoleSummaryWidget} categories={["DNS Hole"]} />
|
<WidgetHeader widget={dnsHoleSummaryWidget} categories={["DNS Hole"]} />
|
||||||
|
|
||||||
This widget combines statistics from every selected DNS service, including total queries, blocked queries, and blocklist size. Its data is cached under the same DNS query family as DNS Hole Controls, so placing both widgets on a board does not create a second integration request. Advanced view keeps the aggregate metrics visible and adds a named status chip for each source.
|
This widget combines statistics from every selected DNS service, including total queries, blocked queries, and blocklist size. Its data is cached under the same DNS query family as DNS Hole Controls, so placing both widgets on a board does not create a second integration request. When the widget has enough room, it adds a scrollable status row naming each selected source.
|
||||||
|
|
||||||
### Screenshots
|
### Screenshots
|
||||||
|
|
||||||
@@ -52,7 +52,7 @@ import { technitiumDnsIntegration } from "@site/docs/integrations/technitium-dns
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ import youtubeStream from "./img/nasa-iss-stream.png";
|
|||||||
<img src={youtubeStream} alt="iframe youtube-iss-stream" width={128 * 3} />
|
<img src={youtubeStream} alt="iframe youtube-iss-stream" width={128 * 3} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ import { prowlarrIntegration } from "@site/docs/integrations/prowlarr";
|
|||||||
|
|
||||||
- Test all indexers by clicking on the test button.
|
- Test all indexers by clicking on the test button.
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ import mediaRequestList from "./img/list.png";
|
|||||||
- Hover over the widget to reveal a **search button** that opens Spotlight in media request search mode, scoped to the widget's integrations.
|
- Hover over the widget to reveal a **search button** that opens Spotlight in media request search mode, scoped to the widget's integrations.
|
||||||
- A warning indicator identifies unavailable integrations without hiding requests returned by healthy integrations.
|
- A warning indicator identifies unavailable integrations without hiding requests returned by healthy integrations.
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ import { overseerrIntegration } from "@site/docs/integrations/overseerr";
|
|||||||
|
|
||||||
<WidgetHeader widget={mediaRequestStatsWidget} categories={["Media requests"]} />
|
<WidgetHeader widget={mediaRequestStatsWidget} categories={["Media requests"]} />
|
||||||
|
|
||||||
Provide an overview of your media requests, including total requests, open requests, approvals, and rejections.
|
Provides an overview of media requests, including totals, availability, approvals, and rejections. Resize the widget to reveal denser stat tiles; the user ranking remains scrollable instead of being truncated.
|
||||||
|
|
||||||
### Supported Integrations
|
### Supported Integrations
|
||||||
|
|
||||||
@@ -45,7 +45,7 @@ import mediaRequestStats from "./img/stats.png";
|
|||||||
- Hover over the widget to reveal a **search button** that opens Spotlight in media request search mode, scoped to the widget's integrations.
|
- Hover over the widget to reveal a **search button** that opens Spotlight in media request search mode, scoped to the widget's integrations.
|
||||||
- A warning indicator identifies unavailable integrations while totals from healthy integrations remain visible.
|
- A warning indicator identifies unavailable integrations while totals from healthy integrations remain visible.
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ import bedrockItem from "./img/bedrock.png";
|
|||||||
<img src={bedrockItem} alt="minecraft-server-status bedrock-edition" width={128 * 1.5} height={128 * 1.5} />
|
<img src={bedrockItem} alt="minecraft-server-status bedrock-edition" width={128 * 1.5} height={128 * 1.5} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ import { networkControllerSummaryWidget } from ".";
|
|||||||
|
|
||||||
<WidgetHeader widget={networkControllerSummaryWidget} categories={["Network controller"]} />
|
<WidgetHeader widget={networkControllerSummaryWidget} categories={["Network controller"]} />
|
||||||
|
|
||||||
Shows information whether your WAN is connected, Wi-Fi is active and several other information.
|
Shows whether your WAN, internet, Wi-Fi, LAN, and VPN are available. Wider or taller layouts progressively reveal latency, ping, and connected-client counts without a separate view.
|
||||||
|
|
||||||
### Screenshots
|
### Screenshots
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ import summaryCard from "./img/summary.png";
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import editOn from "./img/edit.png";
|
|||||||
<img src={editOn} alt="notebook edit-on" width={128 * 3} />
|
<img src={editOn} alt="notebook edit-on" width={128 * 3} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,7 @@ import notificationsScreenshot from "./img/notifications.png";
|
|||||||
items={[{ integration: ntfyIntegration }, { integration: nextcloudIntegration }, { integration: gotifyIntegration }]}
|
items={[{ integration: ntfyIntegration }, { integration: nextcloudIntegration }, { integration: gotifyIntegration }]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ This widget displays statistics from your Paperless-ngx instance, including an i
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -83,7 +83,7 @@ When the widget has enough space, a "last updated" timestamp appears at the bott
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ GitHub, Docker Hub, GitLab, NPM, Codeberg, GitHub Container Registry, Quay, and
|
|||||||
For GitHub Container Registry, use `owner/package` identifiers such as `homarr-labs/homarr`.
|
For GitHub Container Registry, use `owner/package` identifiers such as `homarr-labs/homarr`.
|
||||||
Full image references such as `ghcr.io/homarr-labs/homarr:latest` are also accepted.
|
Full image references such as `ghcr.io/homarr-labs/homarr:latest` are also accepted.
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ import screenshot from "./img/screenshot.png";
|
|||||||
|
|
||||||
<img src={screenshot} alt="rss-feed noted-lol" width={128 * 4} />
|
<img src={screenshot} alt="rss-feed noted-lol" width={128 * 4} />
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ import { stockPriceWidget } from ".";
|
|||||||
<WidgetHeader widget={stockPriceWidget} categories={["Finance", "Market"]} />
|
<WidgetHeader widget={stockPriceWidget} categories={["Finance", "Market"]} />
|
||||||
|
|
||||||
Using the Stocks widget, you can show the current stock prices of your favorite companies on your dashboard.
|
Using the Stocks widget, you can show the current stock prices of your favorite companies on your dashboard.
|
||||||
The compact view prioritizes the symbol, current price, and trend. Company name, change, and time range appear only when both the widget width and height can support them; the advanced view always shows the complete summary.
|
The widget prioritizes the symbol, current price, and trend. Company name, change, time range, and a taller chart appear progressively when both widget dimensions can support them.
|
||||||
|
|
||||||
### Screenshots
|
### Screenshots
|
||||||
|
|
||||||
@@ -34,7 +34,7 @@ import nvidia from "./img/nvidia.png";
|
|||||||
<img src={nvidia} alt="stock-price nvidia" width={128 * 2 + 2} height={128 * 2 + 4} />
|
<img src={nvidia} alt="stock-price nvidia" width={128 * 2 + 2} height={128 * 2 + 4} />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ This widget displays filesystem and SMART disk usage for supported server and NA
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ import singleIntegrationImage from "./img/single.png";
|
|||||||
]}
|
]}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<WidgetInteractionGuide />
|
<WidgetInteractionGuide advancedView={false} />
|
||||||
|
|
||||||
### Adding the widget
|
### Adding the widget
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,13 @@
|
|||||||
import { IconArrowsMaximize, IconLayoutGrid, IconMouse2, IconPlus } from "@tabler/icons-react";
|
import { IconArrowsMaximize, IconLayoutGrid, IconMouse2, IconPlus } from "@tabler/icons-react";
|
||||||
|
|
||||||
const interactions = [
|
const advancedInteraction = {
|
||||||
{
|
icon: IconArrowsMaximize,
|
||||||
icon: IconArrowsMaximize,
|
title: "Advanced view",
|
||||||
title: "Advanced view",
|
description:
|
||||||
description:
|
"Hold Shift while hovering for an in-place preview, press Shift + Enter, or choose Open advanced view from the widget menu.",
|
||||||
"Hold Shift while hovering for an in-place preview, press Shift + Enter, or choose Open advanced view from the widget menu.",
|
} as const;
|
||||||
},
|
|
||||||
|
const standardInteractions = [
|
||||||
{
|
{
|
||||||
icon: IconMouse2,
|
icon: IconMouse2,
|
||||||
title: "Widget actions",
|
title: "Widget actions",
|
||||||
@@ -27,27 +28,35 @@ const interactions = [
|
|||||||
},
|
},
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export const WidgetInteractionGuide = () => (
|
export const WidgetInteractionGuide = ({ advancedView = true }: { advancedView?: boolean }) => {
|
||||||
<details className="group my-6 overflow-hidden rounded-xl border border-solid border-[#e5e7eb] bg-slate-50/70 dark:border-[#333] dark:bg-gray-900/40">
|
const interactions = advancedView ? [advancedInteraction, ...standardInteractions] : standardInteractions;
|
||||||
<summary className="flex cursor-pointer list-none items-center justify-between gap-3 px-4 py-3 font-semibold marker:hidden">
|
|
||||||
<span>Power-user controls</span>
|
return (
|
||||||
<span className="text-xs font-normal text-[#696969] group-open:hidden dark:text-[#999]">Show shortcuts</span>
|
<details className="group my-6 overflow-hidden rounded-xl border border-solid border-[#e5e7eb] bg-slate-50/70 dark:border-[#333] dark:bg-gray-900/40">
|
||||||
<span className="hidden text-xs font-normal text-[#696969] group-open:inline dark:text-[#999]">
|
<summary className="flex cursor-pointer list-none items-center justify-between gap-3 px-4 py-3 font-semibold marker:hidden">
|
||||||
Hide shortcuts
|
<span>Power-user controls</span>
|
||||||
</span>
|
<span className="text-xs font-normal text-[#696969] group-open:hidden dark:text-[#999]">Show shortcuts</span>
|
||||||
</summary>
|
<span className="hidden text-xs font-normal text-[#696969] group-open:inline dark:text-[#999]">
|
||||||
<div className="grid gap-3 border-0 border-t border-solid border-[#e5e7eb] p-4 md:grid-cols-2 xl:grid-cols-4 dark:border-[#333]">
|
Hide shortcuts
|
||||||
{interactions.map(({ icon: Icon, title, description }) => (
|
</span>
|
||||||
<div key={title} className="rounded-lg bg-white p-3 shadow-sm dark:bg-gray-950">
|
</summary>
|
||||||
<Icon aria-hidden size={20} stroke={1.5} className="mb-2 stroke-red-500" />
|
<div
|
||||||
<div className="font-semibold">{title}</div>
|
className={`grid gap-3 border-0 border-t border-solid border-[#e5e7eb] p-4 md:grid-cols-2 ${advancedView ? "xl:grid-cols-4" : "xl:grid-cols-3"} dark:border-[#333]`}
|
||||||
<p className="mb-0 mt-1 text-sm text-[#696969] dark:text-[#999]">{description}</p>
|
>
|
||||||
</div>
|
{interactions.map(({ icon: Icon, title, description }) => (
|
||||||
))}
|
<div key={title} className="rounded-lg bg-white p-3 shadow-sm dark:bg-gray-950">
|
||||||
</div>
|
<Icon aria-hidden size={20} stroke={1.5} className="mb-2 stroke-red-500" />
|
||||||
<p className="m-0 px-4 pb-4 text-xs text-[#696969] dark:text-[#999]">
|
<div className="font-semibold">{title}</div>
|
||||||
Advanced view reuses the widget's live data and cache. The rest of the board stays visible but dimmed. Touch
|
<p className="mb-0 mt-1 text-sm text-[#696969] dark:text-[#999]">{description}</p>
|
||||||
devices expose a maximize control; app tiles stay compact.
|
</div>
|
||||||
</p>
|
))}
|
||||||
</details>
|
</div>
|
||||||
);
|
{advancedView && (
|
||||||
|
<p className="m-0 px-4 pb-4 text-xs text-[#696969] dark:text-[#999]">
|
||||||
|
Advanced view reuses the widget's live data and cache. The rest of the board stays visible but dimmed.
|
||||||
|
Touch devices expose a maximize control; compact-only widgets adapt in place instead.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</details>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -47,7 +47,6 @@ interface CustomWidgetFormProps {
|
|||||||
initialValues?: Partial<CustomWidgetFormValues>;
|
initialValues?: Partial<CustomWidgetFormValues>;
|
||||||
definitionId?: string;
|
definitionId?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
const sectionLinks = [
|
const sectionLinks = [
|
||||||
["general", "section.general", IconSettings],
|
["general", "section.general", IconSettings],
|
||||||
["sources", "section.sources", IconApi],
|
["sources", "section.sources", IconApi],
|
||||||
|
|||||||
@@ -3,6 +3,10 @@
|
|||||||
animation: focus-backdrop-in 180ms cubic-bezier(0.16, 1, 0.3, 1);
|
animation: focus-backdrop-in 180ms cubic-bezier(0.16, 1, 0.3, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.manualBackdrop {
|
||||||
|
pointer-events: auto;
|
||||||
|
}
|
||||||
|
|
||||||
.surface {
|
.surface {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
@@ -15,6 +19,25 @@
|
|||||||
transform-origin: top left;
|
transform-origin: top left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.manualSurface {
|
||||||
|
position: fixed;
|
||||||
|
max-width: none;
|
||||||
|
max-height: none;
|
||||||
|
overflow: visible;
|
||||||
|
background: transparent;
|
||||||
|
box-shadow: none;
|
||||||
|
overscroll-behavior: contain;
|
||||||
|
animation: focus-surface-in 180ms cubic-bezier(0.16, 1, 0.3, 1);
|
||||||
|
transform-origin: top left;
|
||||||
|
}
|
||||||
|
|
||||||
|
.manualSurface > div {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
max-height: none !important;
|
||||||
|
overflow: visible;
|
||||||
|
}
|
||||||
|
|
||||||
.backdropClosing {
|
.backdropClosing {
|
||||||
animation: focus-backdrop-out 180ms ease-in forwards;
|
animation: focus-backdrop-out 180ms ease-in forwards;
|
||||||
}
|
}
|
||||||
@@ -32,6 +55,18 @@
|
|||||||
height: 100%;
|
height: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.surfacePortalTarget,
|
||||||
|
.manualSurfaceMount {
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
min-height: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.manualSurfaceMount {
|
||||||
|
overflow: hidden;
|
||||||
|
border-radius: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
.surfaceHostAdvanced {
|
.surfaceHostAdvanced {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
inset: 0;
|
inset: 0;
|
||||||
@@ -49,7 +84,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.closeButton {
|
.closeButton {
|
||||||
position: fixed;
|
position: absolute;
|
||||||
z-index: 2;
|
z-index: 2;
|
||||||
pointer-events: auto;
|
pointer-events: auto;
|
||||||
}
|
}
|
||||||
@@ -99,7 +134,8 @@
|
|||||||
|
|
||||||
@media (prefers-reduced-motion: reduce) {
|
@media (prefers-reduced-motion: reduce) {
|
||||||
.backdrop,
|
.backdrop,
|
||||||
.surface {
|
.surface,
|
||||||
|
.manualSurface {
|
||||||
animation: none;
|
animation: none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ interface ActiveFocus {
|
|||||||
sourceRect: FocusRect;
|
sourceRect: FocusRect;
|
||||||
activation: "preview" | "manual";
|
activation: "preview" | "manual";
|
||||||
autofocusClose: boolean;
|
autofocusClose: boolean;
|
||||||
|
restorePreviewFocus: boolean;
|
||||||
phase: "visible" | "closing";
|
phase: "visible" | "closing";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,6 +80,7 @@ export const BoardAdvancedFocusProvider = ({ children }: PropsWithChildren) => {
|
|||||||
sourceRect: { left: rect.left, top: rect.top, width: rect.width, height: rect.height },
|
sourceRect: { left: rect.left, top: rect.top, width: rect.width, height: rect.height },
|
||||||
activation: options.activation ?? "manual",
|
activation: options.activation ?? "manual",
|
||||||
autofocusClose: options.autofocusClose ?? false,
|
autofocusClose: options.autofocusClose ?? false,
|
||||||
|
restorePreviewFocus: options.activation === "preview" && document.activeElement === source,
|
||||||
phase: "visible",
|
phase: "visible",
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -118,12 +120,46 @@ export const BoardAdvancedFocusProvider = ({ children }: PropsWithChildren) => {
|
|||||||
if (current?.activation === "preview" && current.itemId !== itemId) {
|
if (current?.activation === "preview" && current.itemId !== itemId) {
|
||||||
cancelCloseTimer();
|
cancelCloseTimer();
|
||||||
updateActive(null);
|
updateActive(null);
|
||||||
|
if (current.restorePreviewFocus && current.source.isConnected) current.source.focus({ preventScroll: true });
|
||||||
}
|
}
|
||||||
startHoverTimer();
|
startHoverTimer();
|
||||||
},
|
},
|
||||||
[cancelCloseTimer, startHoverTimer, updateActive],
|
[cancelCloseTimer, startHoverTimer, updateActive],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handlePointerMove = (event: PointerEvent) => {
|
||||||
|
const current = activeRef.current;
|
||||||
|
if (!shiftHeldRef.current || current?.activation !== "preview") return;
|
||||||
|
|
||||||
|
const underlyingSource = document
|
||||||
|
.elementsFromPoint(event.clientX, event.clientY)
|
||||||
|
.map((element) =>
|
||||||
|
element instanceof HTMLElement
|
||||||
|
? element.matches(".grid-stack-item-content")
|
||||||
|
? element
|
||||||
|
: element.closest<HTMLElement>(".grid-stack-item-content")
|
||||||
|
: null,
|
||||||
|
)
|
||||||
|
.find((source) => source !== null && source !== current.source);
|
||||||
|
const itemId = underlyingSource?.closest<HTMLElement>(".grid-stack-item[data-id]")?.dataset.id;
|
||||||
|
if (!underlyingSource || !itemId) return;
|
||||||
|
|
||||||
|
if (underlyingSource.hasAttribute("aria-keyshortcuts")) {
|
||||||
|
hover(itemId, underlyingSource);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
hoveredRef.current = null;
|
||||||
|
cancelHoverTimer();
|
||||||
|
cancelCloseTimer();
|
||||||
|
updateActive(null);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("pointermove", handlePointerMove, { capture: true });
|
||||||
|
return () => window.removeEventListener("pointermove", handlePointerMove, { capture: true });
|
||||||
|
}, [cancelCloseTimer, cancelHoverTimer, hover, updateActive]);
|
||||||
|
|
||||||
const leave = useCallback(
|
const leave = useCallback(
|
||||||
(itemId: string) => {
|
(itemId: string) => {
|
||||||
if (hoveredRef.current?.itemId === itemId) hoveredRef.current = null;
|
if (hoveredRef.current?.itemId === itemId) hoveredRef.current = null;
|
||||||
@@ -145,7 +181,7 @@ export const BoardAdvancedFocusProvider = ({ children }: PropsWithChildren) => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleKeyDown = (event: KeyboardEvent) => {
|
const handleKeyDown = (event: KeyboardEvent) => {
|
||||||
if (event.key === "Escape" && activeRef.current) {
|
if (event.key === "Escape" && activeRef.current) {
|
||||||
const advancedSurface = document.querySelector(`.${classes.surface}`);
|
const advancedSurface = document.querySelector("[data-advanced-focus-surface]");
|
||||||
if (event.defaultPrevented || event.isComposing || isEscapeOwnedByNestedOverlay(event.target, advancedSurface))
|
if (event.defaultPrevented || event.isComposing || isEscapeOwnedByNestedOverlay(event.target, advancedSurface))
|
||||||
return;
|
return;
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
@@ -170,11 +206,11 @@ export const BoardAdvancedFocusProvider = ({ children }: PropsWithChildren) => {
|
|||||||
if (activeRef.current?.activation === "preview") close(false);
|
if (activeRef.current?.activation === "preview") close(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
window.addEventListener("keydown", handleKeyDown);
|
window.addEventListener("keydown", handleKeyDown, { capture: true });
|
||||||
window.addEventListener("keyup", handleKeyUp);
|
window.addEventListener("keyup", handleKeyUp);
|
||||||
window.addEventListener("blur", handleBlur);
|
window.addEventListener("blur", handleBlur);
|
||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("keydown", handleKeyDown);
|
window.removeEventListener("keydown", handleKeyDown, { capture: true });
|
||||||
window.removeEventListener("keyup", handleKeyUp);
|
window.removeEventListener("keyup", handleKeyUp);
|
||||||
window.removeEventListener("blur", handleBlur);
|
window.removeEventListener("blur", handleBlur);
|
||||||
cancelHoverTimer();
|
cancelHoverTimer();
|
||||||
@@ -194,7 +230,7 @@ export const BoardAdvancedFocusProvider = ({ children }: PropsWithChildren) => {
|
|||||||
return (
|
return (
|
||||||
<AdvancedFocusContext.Provider value={value}>
|
<AdvancedFocusContext.Provider value={value}>
|
||||||
{children}
|
{children}
|
||||||
{active && (
|
{active?.activation === "preview" && (
|
||||||
<Portal>
|
<Portal>
|
||||||
<Overlay
|
<Overlay
|
||||||
fixed
|
fixed
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { isEscapeOwnedByNestedOverlay } from "./escape";
|
|||||||
describe("isEscapeOwnedByNestedOverlay", () => {
|
describe("isEscapeOwnedByNestedOverlay", () => {
|
||||||
test("allows Escape from the advanced surface itself", () => {
|
test("allows Escape from the advanced surface itself", () => {
|
||||||
const surface = document.createElement("section");
|
const surface = document.createElement("section");
|
||||||
surface.setAttribute("role", "region");
|
surface.setAttribute("role", "dialog");
|
||||||
const button = document.createElement("button");
|
const button = document.createElement("button");
|
||||||
surface.append(button);
|
surface.append(button);
|
||||||
|
|
||||||
@@ -44,6 +44,24 @@ describe("isEscapeOwnedByNestedOverlay", () => {
|
|||||||
expect(isEscapeOwnedByNestedOverlay(target, surface)).toBe(true);
|
expect(isEscapeOwnedByNestedOverlay(target, surface)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("leaves Escape to a visible portalled menu when focus stays in the advanced surface", () => {
|
||||||
|
const surface = document.createElement("section");
|
||||||
|
surface.setAttribute("role", "dialog");
|
||||||
|
const target = document.createElement("button");
|
||||||
|
surface.append(target);
|
||||||
|
document.body.append(surface);
|
||||||
|
|
||||||
|
const menu = document.createElement("div");
|
||||||
|
menu.setAttribute("role", "menu");
|
||||||
|
menu.getClientRects = () => [{ width: 100, height: 100 }] as unknown as DOMRectList;
|
||||||
|
document.body.append(menu);
|
||||||
|
|
||||||
|
expect(isEscapeOwnedByNestedOverlay(target, surface)).toBe(true);
|
||||||
|
|
||||||
|
menu.remove();
|
||||||
|
surface.remove();
|
||||||
|
});
|
||||||
|
|
||||||
test("does not mistake the compact widget source for a nested overlay", () => {
|
test("does not mistake the compact widget source for a nested overlay", () => {
|
||||||
const surface = document.createElement("section");
|
const surface = document.createElement("section");
|
||||||
surface.setAttribute("role", "region");
|
surface.setAttribute("role", "region");
|
||||||
|
|||||||
@@ -6,8 +6,14 @@ const escapeOwnerSelector = [
|
|||||||
].join(", ");
|
].join(", ");
|
||||||
|
|
||||||
export const isEscapeOwnedByNestedOverlay = (target: EventTarget | null, advancedSurface: Element | null) => {
|
export const isEscapeOwnedByNestedOverlay = (target: EventTarget | null, advancedSurface: Element | null) => {
|
||||||
if (!(target instanceof Element)) return false;
|
const escapeOwner = target instanceof Element ? target.closest(escapeOwnerSelector) : null;
|
||||||
|
if (escapeOwner !== null && escapeOwner !== advancedSurface) return true;
|
||||||
|
|
||||||
const escapeOwner = target.closest(escapeOwnerSelector);
|
return Array.from(document.querySelectorAll(escapeOwnerSelector)).some(
|
||||||
return escapeOwner !== null && escapeOwner !== advancedSurface;
|
(owner) =>
|
||||||
|
owner !== advancedSurface &&
|
||||||
|
owner.getAttribute("aria-hidden") !== "true" &&
|
||||||
|
owner.closest('[aria-hidden="true"]') === null &&
|
||||||
|
owner.getClientRects().length > 0,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
@@ -0,0 +1,107 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { act, createElement, Fragment, useRef, useState } from "react";
|
||||||
|
import { createRoot } from "react-dom/client";
|
||||||
|
import { MantineProvider } from "@mantine/core";
|
||||||
|
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||||
|
|
||||||
|
import { AdvancedFocusManualSurface } from "./manual-surface";
|
||||||
|
|
||||||
|
const sourceRect = { left: 100, top: 120, width: 240, height: 160 };
|
||||||
|
const rect = { left: 40, top: 80, width: 640, height: 480 };
|
||||||
|
const closePosition = { left: 688, top: 80 };
|
||||||
|
|
||||||
|
describe("AdvancedFocusManualSurface", () => {
|
||||||
|
let host: HTMLDivElement | undefined;
|
||||||
|
let root: ReturnType<typeof createRoot> | undefined;
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
if (root) await act(async () => root?.unmount());
|
||||||
|
host?.remove();
|
||||||
|
document.querySelectorAll("[data-portal]").forEach((portal) => portal.remove());
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("portals dialog semantics, traps focus, closes from the backdrop, and restores focus", async () => {
|
||||||
|
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
||||||
|
vi.stubGlobal(
|
||||||
|
"matchMedia",
|
||||||
|
vi.fn().mockImplementation((query: string) => ({
|
||||||
|
matches: false,
|
||||||
|
media: query,
|
||||||
|
onchange: null,
|
||||||
|
addEventListener: vi.fn(),
|
||||||
|
removeEventListener: vi.fn(),
|
||||||
|
addListener: vi.fn(),
|
||||||
|
removeListener: vi.fn(),
|
||||||
|
dispatchEvent: vi.fn(),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
host = document.createElement("div");
|
||||||
|
document.body.append(host);
|
||||||
|
root = createRoot(host);
|
||||||
|
|
||||||
|
const Harness = () => {
|
||||||
|
const [opened, setOpened] = useState(false);
|
||||||
|
const contentRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
return createElement(
|
||||||
|
Fragment,
|
||||||
|
null,
|
||||||
|
createElement("button", { type: "button", onClick: () => setOpened(true) }, "Open advanced view"),
|
||||||
|
createElement(
|
||||||
|
AdvancedFocusManualSurface,
|
||||||
|
{
|
||||||
|
opened,
|
||||||
|
phase: "visible",
|
||||||
|
id: "advanced-focus-test",
|
||||||
|
label: "Test widget advanced view",
|
||||||
|
closeLabel: "Close advanced view",
|
||||||
|
rect,
|
||||||
|
closePosition,
|
||||||
|
sourceRect,
|
||||||
|
radius: "md",
|
||||||
|
contentRef,
|
||||||
|
onClose: () => setOpened(false),
|
||||||
|
},
|
||||||
|
createElement("button", { type: "button" }, "First widget action"),
|
||||||
|
createElement("button", { type: "button" }, "Last widget action"),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
await act(async () => root?.render(createElement(MantineProvider, null, createElement(Harness))));
|
||||||
|
const trigger = document.querySelector<HTMLButtonElement>("button");
|
||||||
|
expect(trigger).not.toBeNull();
|
||||||
|
trigger?.focus();
|
||||||
|
await act(async () => trigger?.click());
|
||||||
|
await act(async () => await new Promise((resolve) => setTimeout(resolve, 20)));
|
||||||
|
|
||||||
|
const dialog = document.querySelector<HTMLElement>("[role='dialog']");
|
||||||
|
const closeButton = dialog?.querySelector<HTMLButtonElement>("[aria-label='Close advanced view']");
|
||||||
|
const actions = dialog?.querySelectorAll<HTMLButtonElement>("button");
|
||||||
|
expect(dialog?.id).toBe("advanced-focus-test");
|
||||||
|
expect(dialog?.getAttribute("aria-modal")).toBe("true");
|
||||||
|
expect(dialog?.closest("[data-portal]")).not.toBeNull();
|
||||||
|
expect(dialog?.closest(".grid-stack-item")).toBeNull();
|
||||||
|
expect(document.getElementById(dialog?.getAttribute("aria-labelledby") ?? "")?.textContent).toBe(
|
||||||
|
"Test widget advanced view",
|
||||||
|
);
|
||||||
|
expect(document.activeElement).toBe(closeButton);
|
||||||
|
|
||||||
|
const lastAction = actions?.item((actions?.length ?? 1) - 1);
|
||||||
|
lastAction?.focus();
|
||||||
|
document.dispatchEvent(new KeyboardEvent("keydown", { key: "Tab", bubbles: true, cancelable: true }));
|
||||||
|
expect(document.activeElement).toBe(closeButton);
|
||||||
|
closeButton?.focus();
|
||||||
|
document.dispatchEvent(
|
||||||
|
new KeyboardEvent("keydown", { key: "Tab", shiftKey: true, bubbles: true, cancelable: true }),
|
||||||
|
);
|
||||||
|
expect(document.activeElement).toBe(lastAction);
|
||||||
|
|
||||||
|
const backdrop = document.querySelector<HTMLElement>("[data-advanced-focus-overlay]");
|
||||||
|
await act(async () => backdrop?.click());
|
||||||
|
await act(async () => await new Promise((resolve) => setTimeout(resolve, 20)));
|
||||||
|
expect(document.activeElement).toBe(trigger);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,101 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import type { CSSProperties, PropsWithChildren, Ref } from "react";
|
||||||
|
import { Modal, VisuallyHidden } from "@mantine/core";
|
||||||
|
import { useReducedMotion } from "@mantine/hooks";
|
||||||
|
|
||||||
|
import classes from "./advanced-focus.module.css";
|
||||||
|
import type { FocusClosePosition, FocusRect } from "./geometry";
|
||||||
|
|
||||||
|
interface AdvancedFocusManualSurfaceProps extends PropsWithChildren {
|
||||||
|
opened: boolean;
|
||||||
|
phase: "visible" | "closing";
|
||||||
|
id: string;
|
||||||
|
label: string;
|
||||||
|
closeLabel: string;
|
||||||
|
rect: FocusRect;
|
||||||
|
closePosition: FocusClosePosition;
|
||||||
|
sourceRect: FocusRect;
|
||||||
|
radius: string | number;
|
||||||
|
contentRef: Ref<HTMLDivElement>;
|
||||||
|
onClose: () => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const AdvancedFocusManualSurface = ({
|
||||||
|
opened,
|
||||||
|
phase,
|
||||||
|
id,
|
||||||
|
label,
|
||||||
|
closeLabel,
|
||||||
|
rect,
|
||||||
|
closePosition,
|
||||||
|
sourceRect,
|
||||||
|
radius,
|
||||||
|
contentRef,
|
||||||
|
onClose,
|
||||||
|
children,
|
||||||
|
}: AdvancedFocusManualSurfaceProps) => {
|
||||||
|
const reduceMotion = useReducedMotion();
|
||||||
|
const transitionDuration = reduceMotion ? 0 : 180;
|
||||||
|
const geometryStyle = {
|
||||||
|
left: rect.left,
|
||||||
|
top: rect.top,
|
||||||
|
width: rect.width,
|
||||||
|
height: rect.height,
|
||||||
|
"--focus-translate-x": `${sourceRect.left - rect.left}px`,
|
||||||
|
"--focus-translate-y": `${sourceRect.top - rect.top}px`,
|
||||||
|
"--focus-scale-x": rect.width > 0 ? sourceRect.width / rect.width : 1,
|
||||||
|
"--focus-scale-y": rect.height > 0 ? sourceRect.height / rect.height : 1,
|
||||||
|
} as CSSProperties;
|
||||||
|
const relativeClosePosition = {
|
||||||
|
left: closePosition.left - rect.left,
|
||||||
|
top: closePosition.top - rect.top,
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Modal.Root
|
||||||
|
opened={opened}
|
||||||
|
onClose={onClose}
|
||||||
|
radius={radius}
|
||||||
|
padding={0}
|
||||||
|
shadow="none"
|
||||||
|
zIndex="calc(var(--mantine-z-index-modal) - 1)"
|
||||||
|
closeOnEscape={false}
|
||||||
|
closeOnClickOutside
|
||||||
|
lockScroll
|
||||||
|
trapFocus
|
||||||
|
returnFocus
|
||||||
|
removeScrollProps={{ allowPinchZoom: true }}
|
||||||
|
transitionProps={{ duration: transitionDuration, transition: "fade" }}
|
||||||
|
>
|
||||||
|
<Modal.Overlay
|
||||||
|
data-advanced-focus-overlay
|
||||||
|
backgroundOpacity={0.38}
|
||||||
|
blur={0}
|
||||||
|
className={`${classes.backdrop} ${classes.manualBackdrop} ${
|
||||||
|
phase === "closing" ? classes.backdropClosing : ""
|
||||||
|
}`}
|
||||||
|
/>
|
||||||
|
<Modal.Content
|
||||||
|
ref={contentRef}
|
||||||
|
id={id}
|
||||||
|
data-advanced-focus-surface
|
||||||
|
className={`${classes.manualSurface} ${phase === "closing" ? classes.surfaceClosing : ""}`}
|
||||||
|
style={geometryStyle}
|
||||||
|
>
|
||||||
|
<Modal.Title>
|
||||||
|
<VisuallyHidden>{label}</VisuallyHidden>
|
||||||
|
</Modal.Title>
|
||||||
|
<Modal.CloseButton
|
||||||
|
className={classes.closeButton}
|
||||||
|
variant="default"
|
||||||
|
size={44}
|
||||||
|
aria-label={closeLabel}
|
||||||
|
aria-controls={id}
|
||||||
|
style={relativeClosePosition}
|
||||||
|
/>
|
||||||
|
{children}
|
||||||
|
</Modal.Content>
|
||||||
|
</Modal.Root>
|
||||||
|
);
|
||||||
|
};
|
||||||
@@ -13,10 +13,8 @@ export const redirectShiftWheel = (root: HTMLElement, target: EventTarget | null
|
|||||||
if (delta === 0) return false;
|
if (delta === 0) return false;
|
||||||
|
|
||||||
let current = target instanceof Element ? target : null;
|
let current = target instanceof Element ? target : null;
|
||||||
let startedInScrollContainer = false;
|
|
||||||
while (current && root.contains(current)) {
|
while (current && root.contains(current)) {
|
||||||
const isCurrentScrollContainer = isVerticalScrollContainer(current);
|
const isCurrentScrollContainer = isVerticalScrollContainer(current);
|
||||||
startedInScrollContainer ||= isCurrentScrollContainer;
|
|
||||||
if (isCurrentScrollContainer && canScrollVertically(current, delta)) {
|
if (isCurrentScrollContainer && canScrollVertically(current, delta)) {
|
||||||
current.scrollTop += delta;
|
current.scrollTop += delta;
|
||||||
return true;
|
return true;
|
||||||
@@ -24,8 +22,9 @@ export const redirectShiftWheel = (root: HTMLElement, target: EventTarget | null
|
|||||||
if (current === root) break;
|
if (current === root) break;
|
||||||
current = current.parentElement;
|
current = current.parentElement;
|
||||||
}
|
}
|
||||||
if (startedInScrollContainer) return false;
|
|
||||||
|
|
||||||
|
// A nested control can reach its edge before the widget's main Mantine viewport.
|
||||||
|
// Hand the gesture to that viewport instead of making Shift+wheel feel stuck.
|
||||||
const viewport = [...root.querySelectorAll<HTMLElement>("[data-scrollbars='y'], [data-scrollbars='xy']")].find(
|
const viewport = [...root.querySelectorAll<HTMLElement>("[data-scrollbars='y'], [data-scrollbars='xy']")].find(
|
||||||
(element) => isVerticalScrollContainer(element) && canScrollVertically(element, delta),
|
(element) => isVerticalScrollContainer(element) && canScrollVertically(element, delta),
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ export const BoardItemMenu = (props: BoardItemMenuProps) => {
|
|||||||
|
|
||||||
return <BoardItemMenuInner {...props} />;
|
return <BoardItemMenuInner {...props} />;
|
||||||
};
|
};
|
||||||
|
|
||||||
const BoardItemMenuInner = ({ item, definition, resetErrorBoundary }: BoardItemMenuProps) => {
|
const BoardItemMenuInner = ({ item, definition, resetErrorBoundary }: BoardItemMenuProps) => {
|
||||||
const { data: session } = useSession();
|
const { data: session } = useSession();
|
||||||
const canDuplicate = item.kind !== "customApi" || (session?.user.permissions.includes("admin") ?? false);
|
const canDuplicate = item.kind !== "customApi" || (session?.user.permissions.includes("admin") ?? false);
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import {
|
|||||||
import type { SqliteDatabase } from "./shared/e2e-db";
|
import type { SqliteDatabase } from "./shared/e2e-db";
|
||||||
import { createSqliteDbFileAsync } from "./shared/e2e-db";
|
import { createSqliteDbFileAsync } from "./shared/e2e-db";
|
||||||
import { createHomarrContainer } from "./shared/create-homarr-container";
|
import { createHomarrContainer } from "./shared/create-homarr-container";
|
||||||
|
import { loginAsync } from "./shared/login";
|
||||||
import { seedAdminUserAsync } from "./shared/seed-admin-user";
|
import { seedAdminUserAsync } from "./shared/seed-admin-user";
|
||||||
|
|
||||||
const ownerCredentials = { username: "owner", password: "Comp(exP4sswOrd" };
|
const ownerCredentials = { username: "owner", password: "Comp(exP4sswOrd" };
|
||||||
@@ -68,11 +69,12 @@ describe("Board advanced interactions", () => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const widget = page.locator(".grid-stack-item[data-kind='clock'] > .grid-stack-item-content").first();
|
const widget = page.locator(".grid-stack-item[data-kind='clock'] > .grid-stack-item-content").first();
|
||||||
const compactSurface = widget.locator(".clock-wrapper");
|
const compactSurface = page.locator(".clock-wrapper").first();
|
||||||
const advancedSurface = page.getByRole("region", { name: "Date and time advanced view" });
|
const previewSurface = page.getByRole("region", { name: "Date and time advanced view" });
|
||||||
const iframeAdvancedSurface = page.getByRole("region", { name: "iFrame advanced view" });
|
const manualSurface = page.getByRole("dialog", { name: "Date and time advanced view" });
|
||||||
|
const bookmarksPreviewSurface = page.getByRole("region", { name: "Bookmarks advanced view" });
|
||||||
const dimmingOverlay = page.locator("[data-advanced-focus-overlay]");
|
const dimmingOverlay = page.locator("[data-advanced-focus-overlay]");
|
||||||
const otherWidget = page.locator(".grid-stack-item[data-kind='iframe'] > .grid-stack-item-content").first();
|
const otherWidget = page.locator(".grid-stack-item[data-kind='bookmarks'] > .grid-stack-item-content").first();
|
||||||
|
|
||||||
await compactSurface.evaluate((element) => {
|
await compactSurface.evaluate((element) => {
|
||||||
element.setAttribute("data-lifecycle-probe", "same-instance");
|
element.setAttribute("data-lifecycle-probe", "same-instance");
|
||||||
@@ -80,42 +82,54 @@ describe("Board advanced interactions", () => {
|
|||||||
await widget.focus();
|
await widget.focus();
|
||||||
await page.keyboard.down("Shift");
|
await page.keyboard.down("Shift");
|
||||||
await widget.hover();
|
await widget.hover();
|
||||||
await expect(advancedSurface).toBeVisible({ timeout: 2_000 });
|
await expect(previewSurface).toBeVisible({ timeout: 2_000 });
|
||||||
await expect(advancedSurface).toHaveCSS("animation-name", "none");
|
await expect(previewSurface).toHaveCSS("animation-name", "none");
|
||||||
const compactBounds = await widget.boundingBox();
|
const compactBounds = await widget.boundingBox();
|
||||||
const advancedBounds = await advancedSurface.boundingBox();
|
const advancedBounds = await previewSurface.boundingBox();
|
||||||
expect(advancedBounds?.width).toBeGreaterThan(compactBounds?.width ?? 0);
|
expect(advancedBounds?.width).toBeGreaterThan(compactBounds?.width ?? 0);
|
||||||
await expect(advancedSurface).toHaveAttribute("data-lifecycle-probe", "same-instance");
|
await expect(previewSurface).toHaveAttribute("data-lifecycle-probe", "same-instance");
|
||||||
await expect(dimmingOverlay).toBeVisible();
|
await expect(dimmingOverlay).toBeVisible();
|
||||||
await expect(dimmingOverlay).toHaveCSS("pointer-events", "none");
|
await expect(dimmingOverlay).toHaveCSS("pointer-events", "none");
|
||||||
await expect(otherWidget).toBeVisible();
|
await expect(otherWidget).toBeVisible();
|
||||||
await expect(widget).toBeFocused();
|
await expect(widget).toBeFocused();
|
||||||
|
|
||||||
await advancedSurface.click({ button: "right", position: { x: 80, y: 80 } });
|
await previewSurface.click({ button: "right", position: { x: 80, y: 80 } });
|
||||||
const portalledMenuItem = page.getByRole("menuitem", { name: "Open advanced view" });
|
const portalledMenuItem = page.getByRole("menuitem", { name: "Open advanced view" });
|
||||||
await expect(portalledMenuItem).toBeVisible();
|
await expect(portalledMenuItem).toBeVisible();
|
||||||
await portalledMenuItem.hover();
|
await portalledMenuItem.hover();
|
||||||
await expect(advancedSurface).toBeVisible();
|
await expect(previewSurface).toBeVisible();
|
||||||
await page.keyboard.press("Escape");
|
await page.keyboard.press("Escape");
|
||||||
await expect(portalledMenuItem).toBeHidden();
|
await expect(portalledMenuItem).toBeHidden();
|
||||||
await expect(advancedSurface).toBeVisible();
|
await expect(previewSurface).toBeVisible();
|
||||||
|
|
||||||
await otherWidget.hover();
|
const otherBounds = await otherWidget.boundingBox();
|
||||||
await expect(advancedSurface).toBeHidden();
|
expect(otherBounds).not.toBeNull();
|
||||||
await expect(iframeAdvancedSurface).toBeVisible({ timeout: 2_000 });
|
if (!otherBounds) throw new Error("Bookmarks widget has no bounds");
|
||||||
|
await page.mouse.move(otherBounds.x + otherBounds.width / 2, otherBounds.y + otherBounds.height / 2);
|
||||||
|
await expect(previewSurface).toBeHidden();
|
||||||
|
await expect(bookmarksPreviewSurface).toBeVisible({ timeout: 2_000 });
|
||||||
await page.keyboard.up("Shift");
|
await page.keyboard.up("Shift");
|
||||||
await expect(iframeAdvancedSurface).toBeHidden();
|
await expect(bookmarksPreviewSurface).toBeHidden();
|
||||||
await expect(dimmingOverlay).toBeHidden();
|
await expect(dimmingOverlay).toBeHidden();
|
||||||
await expect(compactSurface).toHaveAttribute("data-lifecycle-probe", "same-instance");
|
await expect(compactSurface).toHaveAttribute("data-lifecycle-probe", "same-instance");
|
||||||
await expect(widget).toBeFocused();
|
await expect(widget).toBeFocused();
|
||||||
|
|
||||||
await page.keyboard.press("Shift+Enter");
|
await page.keyboard.press("Shift+Enter");
|
||||||
await expect(advancedSurface).toBeVisible();
|
await expect(manualSurface).toBeVisible();
|
||||||
|
await expect(manualSurface).toHaveAttribute("aria-modal", "true");
|
||||||
|
expect(await manualSurface.evaluate((element) => element.closest(".grid-stack-item"))).toBeNull();
|
||||||
|
await expect(manualSurface.locator(".clock-wrapper")).toHaveAttribute("data-lifecycle-probe", "same-instance");
|
||||||
|
await expect(dimmingOverlay).toHaveCSS("pointer-events", "auto");
|
||||||
|
expect(await page.evaluate(() => getComputedStyle(document.body).overflow)).toBe("hidden");
|
||||||
|
await expect(page.getByRole("button", { name: "Close advanced view" })).toBeFocused();
|
||||||
|
await page.keyboard.press("Tab");
|
||||||
|
await expect(page.getByRole("button", { name: "Close advanced view" })).toBeFocused();
|
||||||
|
await page.keyboard.press("Shift+Tab");
|
||||||
await expect(page.getByRole("button", { name: "Close advanced view" })).toBeFocused();
|
await expect(page.getByRole("button", { name: "Close advanced view" })).toBeFocused();
|
||||||
await expect(page.getByRole("button", { name: /advanced view is pinned|keep advanced view open/i })).toHaveCount(
|
await expect(page.getByRole("button", { name: /advanced view is pinned|keep advanced view open/i })).toHaveCount(
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
await advancedSurface.evaluate((element) => {
|
await manualSurface.evaluate((element) => {
|
||||||
const probe = document.createElement("div");
|
const probe = document.createElement("div");
|
||||||
probe.dataset.shiftScrollProbe = "true";
|
probe.dataset.shiftScrollProbe = "true";
|
||||||
probe.style.cssText = "height:80px;overflow-y:auto";
|
probe.style.cssText = "height:80px;overflow-y:auto";
|
||||||
@@ -123,11 +137,19 @@ describe("Board advanced interactions", () => {
|
|||||||
probe.addEventListener("wheel", (event) => event.stopPropagation());
|
probe.addEventListener("wheel", (event) => event.stopPropagation());
|
||||||
element.append(probe);
|
element.append(probe);
|
||||||
|
|
||||||
const unrelated = probe.cloneNode(true) as HTMLDivElement;
|
const mainViewport = document.createElement("div");
|
||||||
|
mainViewport.dataset.scrollbars = "y";
|
||||||
|
mainViewport.style.cssText = "height:80px;overflow-y:auto";
|
||||||
|
mainViewport.innerHTML = '<div style="height:600px"></div>';
|
||||||
|
element.append(mainViewport);
|
||||||
|
|
||||||
|
const unrelated = document.createElement("div");
|
||||||
unrelated.dataset.shiftScrollProbe = "unrelated";
|
unrelated.dataset.shiftScrollProbe = "unrelated";
|
||||||
|
unrelated.style.cssText = "height:80px;overflow-y:hidden";
|
||||||
|
unrelated.innerHTML = '<div style="height:600px"></div>';
|
||||||
element.append(unrelated);
|
element.append(unrelated);
|
||||||
});
|
});
|
||||||
const scrollProbe = advancedSurface.locator("[data-shift-scroll-probe='true']");
|
const scrollProbe = manualSurface.locator("[data-shift-scroll-probe='true']");
|
||||||
await page.keyboard.down("Shift");
|
await page.keyboard.down("Shift");
|
||||||
const pixelEventCancelled = await scrollProbe.evaluate(
|
const pixelEventCancelled = await scrollProbe.evaluate(
|
||||||
(element) =>
|
(element) =>
|
||||||
@@ -156,56 +178,78 @@ describe("Board advanced interactions", () => {
|
|||||||
new WheelEvent("wheel", { bubbles: true, cancelable: true, shiftKey: true, deltaX: 120 }),
|
new WheelEvent("wheel", { bubbles: true, cancelable: true, shiftKey: true, deltaX: 120 }),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
expect(await manualSurface.locator("[data-scrollbars='y']").evaluate((element) => element.scrollTop)).toBe(120);
|
||||||
expect(
|
expect(
|
||||||
await advancedSurface.locator("[data-shift-scroll-probe='unrelated']").evaluate((element) => element.scrollTop),
|
await manualSurface.locator("[data-shift-scroll-probe='unrelated']").evaluate((element) => element.scrollTop),
|
||||||
).toBe(0);
|
).toBe(0);
|
||||||
|
|
||||||
|
await manualSurface.click({ button: "right", position: { x: 80, y: 80 } });
|
||||||
|
await expect(portalledMenuItem).toBeVisible();
|
||||||
await page.keyboard.press("Escape");
|
await page.keyboard.press("Escape");
|
||||||
await expect(advancedSurface).toBeHidden();
|
await expect(portalledMenuItem).toBeHidden();
|
||||||
|
await expect(manualSurface).toBeVisible();
|
||||||
|
await page.keyboard.press("Escape");
|
||||||
|
await expect(manualSurface).toBeHidden();
|
||||||
await expect(widget).toBeFocused();
|
await expect(widget).toBeFocused();
|
||||||
|
expect(await page.evaluate(() => getComputedStyle(document.body).overflow)).not.toBe("hidden");
|
||||||
|
|
||||||
await widget.click({ button: "right" });
|
await widget.click({ button: "right" });
|
||||||
await page.getByRole("menuitem", { name: "Open advanced view" }).click();
|
await page.getByRole("menuitem", { name: "Open advanced view" }).click();
|
||||||
await expect(advancedSurface).toBeVisible();
|
await expect(manualSurface).toBeVisible();
|
||||||
await page.keyboard.press("Escape");
|
const viewport = page.viewportSize();
|
||||||
await expect(advancedSurface).toBeHidden();
|
if (!viewport) throw new Error("Board page has no viewport");
|
||||||
|
await page.mouse.click(4, viewport.height - 4);
|
||||||
|
await expect(manualSurface).toBeHidden();
|
||||||
|
await expect(widget).toBeFocused();
|
||||||
|
|
||||||
const appWidget = page.locator(".grid-stack-item[data-kind='app'] > .grid-stack-item-content").first();
|
for (const compactOnlyKind of ["app", "iframe"]) {
|
||||||
await expect(appWidget).not.toHaveAttribute("aria-keyshortcuts", "Shift+Enter");
|
const compactOnlyWidget = page
|
||||||
await appWidget.click({ button: "right" });
|
.locator(`.grid-stack-item[data-kind='${compactOnlyKind}'] > .grid-stack-item-content`)
|
||||||
await expect(page.getByRole("menuitem", { name: "Open advanced view" })).toHaveCount(0);
|
.first();
|
||||||
await page.keyboard.press("Escape");
|
await expect(compactOnlyWidget).not.toHaveAttribute("aria-keyshortcuts", "Shift+Enter");
|
||||||
await page.keyboard.down("Shift");
|
await compactOnlyWidget.dispatchEvent("contextmenu", { button: 2 });
|
||||||
await appWidget.hover();
|
await expect(page.getByRole("menuitem", { name: "Open advanced view" })).toHaveCount(0);
|
||||||
await expect(advancedSurface).toBeHidden();
|
await page.keyboard.press("Escape");
|
||||||
await page.keyboard.up("Shift");
|
await page.keyboard.down("Shift");
|
||||||
|
await compactOnlyWidget.hover({ position: { x: 4, y: 4 } });
|
||||||
|
await expect(previewSurface).toBeHidden();
|
||||||
|
await expect(dimmingOverlay).toBeHidden();
|
||||||
|
await page.keyboard.up("Shift");
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
await context.close();
|
await context.close();
|
||||||
}
|
}
|
||||||
}, 60_000);
|
}, 60_000);
|
||||||
|
|
||||||
test("preserves iframe browsing state and dismisses focus when a responsive layout remounts", async () => {
|
test("keeps compact-only iframe state and dismisses retained focus when a responsive layout remounts", async () => {
|
||||||
const { context, page } = await openBoardAsync(browser, baseUrl, ownerCredentials);
|
const { context, page } = await openBoardAsync(browser, baseUrl, ownerCredentials);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const iframeSlot = page.locator(".grid-stack-item[data-kind='iframe'] > .grid-stack-item-content").first();
|
const iframeSlot = page.locator(".grid-stack-item[data-kind='iframe'] > .grid-stack-item-content").first();
|
||||||
const iframe = iframeSlot.locator("iframe");
|
const iframe = iframeSlot.locator("iframe");
|
||||||
await iframe.evaluate((element) => {
|
await iframe.evaluate((element) => {
|
||||||
element.srcdoc = '<label>State <input id="state" value="initial"></label>';
|
(element as HTMLIFrameElement).srcdoc = '<label>State <input id="state" value="initial"></label>';
|
||||||
|
(element as HTMLIFrameElement).dataset.advancedFocusProbe = "true";
|
||||||
});
|
});
|
||||||
const frame = page.frameLocator(".grid-stack-item[data-kind='iframe'] iframe");
|
const frame = page.frameLocator("iframe[data-advanced-focus-probe='true']");
|
||||||
await frame.locator("#state").fill("changed");
|
await frame.locator("#state").fill("changed");
|
||||||
|
|
||||||
await iframeSlot.focus();
|
await expect(iframeSlot).not.toHaveAttribute("aria-keyshortcuts", "Shift+Enter");
|
||||||
await page.keyboard.press("Shift+Enter");
|
await iframeSlot.dispatchEvent("contextmenu", { button: 2 });
|
||||||
await expect(page.getByRole("region", { name: "iFrame advanced view" })).toBeVisible();
|
await expect(page.getByRole("menuitem", { name: "Open advanced view" })).toHaveCount(0);
|
||||||
await expect(frame.locator("#state")).toHaveValue("changed");
|
|
||||||
await page.keyboard.press("Escape");
|
await page.keyboard.press("Escape");
|
||||||
|
await page.keyboard.down("Shift");
|
||||||
|
await iframeSlot.hover({ position: { x: 4, y: 4 } });
|
||||||
|
await expect(page.getByRole("region", { name: "iFrame advanced view" })).toHaveCount(0);
|
||||||
|
await page.keyboard.up("Shift");
|
||||||
await expect(frame.locator("#state")).toHaveValue("changed");
|
await expect(frame.locator("#state")).toHaveValue("changed");
|
||||||
|
|
||||||
await iframeSlot.focus();
|
const bookmarksSlot = page.locator(".grid-stack-item[data-kind='bookmarks'] > .grid-stack-item-content").first();
|
||||||
|
await bookmarksSlot.focus();
|
||||||
await page.keyboard.press("Shift+Enter");
|
await page.keyboard.press("Shift+Enter");
|
||||||
|
await expect(page.getByRole("dialog", { name: "Bookmarks advanced view" })).toBeVisible();
|
||||||
await page.setViewportSize({ width: 800, height: 900 });
|
await page.setViewportSize({ width: 800, height: 900 });
|
||||||
await expect(page.getByRole("region", { name: "iFrame advanced view" })).toBeHidden();
|
await expect(page.getByRole("dialog", { name: "Bookmarks advanced view" })).toBeHidden();
|
||||||
await expect(page.locator(".grid-stack-item[data-kind='iframe'] iframe")).toBeVisible();
|
await expect(page.locator(".grid-stack-item[data-kind='iframe'] iframe")).toBeVisible();
|
||||||
} finally {
|
} finally {
|
||||||
await context.close();
|
await context.close();
|
||||||
@@ -229,7 +273,7 @@ describe("Board advanced interactions", () => {
|
|||||||
expect(bounds.height).toBeGreaterThanOrEqual(44);
|
expect(bounds.height).toBeGreaterThanOrEqual(44);
|
||||||
expect(bounds.x + bounds.width).toBeLessThanOrEqual(1366);
|
expect(bounds.x + bounds.width).toBeLessThanOrEqual(1366);
|
||||||
expect(bounds.y + bounds.height).toBeLessThanOrEqual(768);
|
expect(bounds.y + bounds.height).toBeLessThanOrEqual(768);
|
||||||
const surfaceBounds = await page.getByRole("region", { name: "Date and time advanced view" }).boundingBox();
|
const surfaceBounds = await page.getByRole("dialog", { name: "Date and time advanced view" }).boundingBox();
|
||||||
const contentBounds = await page.locator(".clock-widget-container").boundingBox();
|
const contentBounds = await page.locator(".clock-widget-container").boundingBox();
|
||||||
expect(surfaceBounds).not.toBeNull();
|
expect(surfaceBounds).not.toBeNull();
|
||||||
expect(contentBounds).not.toBeNull();
|
expect(contentBounds).not.toBeNull();
|
||||||
@@ -240,7 +284,7 @@ describe("Board advanced interactions", () => {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
await closeButton.click();
|
await closeButton.click();
|
||||||
await expect(page.getByRole("region", { name: "Date and time advanced view" })).toBeHidden();
|
await expect(page.getByRole("dialog", { name: "Date and time advanced view" })).toBeHidden();
|
||||||
} finally {
|
} finally {
|
||||||
await context.close();
|
await context.close();
|
||||||
}
|
}
|
||||||
@@ -304,7 +348,7 @@ describe("Board advanced interactions", () => {
|
|||||||
|
|
||||||
await widget.click({ button: "right" });
|
await widget.click({ button: "right" });
|
||||||
await page.getByRole("menuitem", { name: "Open advanced view" }).click();
|
await page.getByRole("menuitem", { name: "Open advanced view" }).click();
|
||||||
await expect(page.getByRole("region", { name: "Date and time advanced view" })).toBeVisible();
|
await expect(page.getByRole("dialog", { name: "Date and time advanced view" })).toBeVisible();
|
||||||
await page.keyboard.press("Escape");
|
await page.keyboard.press("Escape");
|
||||||
|
|
||||||
await expect(grid).not.toHaveAttribute("aria-label", "Add item here");
|
await expect(grid).not.toHaveAttribute("aria-label", "Add item here");
|
||||||
@@ -325,14 +369,8 @@ const openBoardAsync = async (
|
|||||||
const context = await browser.newContext({ reducedMotion: "reduce", viewport });
|
const context = await browser.newContext({ reducedMotion: "reduce", viewport });
|
||||||
const page = await context.newPage();
|
const page = await context.newPage();
|
||||||
|
|
||||||
await page.goto(`${baseUrl}/auth/login`);
|
await loginAsync({ page, baseUrl, credentials, destination: `/boards/${boardName}` });
|
||||||
await page.getByLabel("Username").fill(credentials.username);
|
await expect(page.locator(".clock-wrapper").first()).toBeVisible({
|
||||||
await page.locator("#password").fill(credentials.password);
|
|
||||||
const loginRedirect = page.waitForURL(baseUrl, { timeout: 15_000, waitUntil: "commit" });
|
|
||||||
await page.locator("css=button[type='submit']").click();
|
|
||||||
await loginRedirect;
|
|
||||||
await page.goto(`${baseUrl}/boards/${boardName}`);
|
|
||||||
await expect(page.locator(".grid-stack-item[data-kind='clock'] .clock-wrapper").first()).toBeVisible({
|
|
||||||
timeout: 15_000,
|
timeout: 15_000,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -368,6 +406,7 @@ const seedInteractionBoardAsync = async (db: SqliteDatabase, ownerId: string) =>
|
|||||||
const desktopLayoutId = createId();
|
const desktopLayoutId = createId();
|
||||||
const sectionId = createId();
|
const sectionId = createId();
|
||||||
const clockItemId = createId();
|
const clockItemId = createId();
|
||||||
|
const bookmarksItemId = createId();
|
||||||
const iframeItemId = createId();
|
const iframeItemId = createId();
|
||||||
const appItemId = createId();
|
const appItemId = createId();
|
||||||
const appId = createId();
|
const appId = createId();
|
||||||
@@ -392,6 +431,22 @@ const seedInteractionBoardAsync = async (db: SqliteDatabase, ownerId: string) =>
|
|||||||
options: stringify({ is24HourFormat: true, showSeconds: false, showDate: true }),
|
options: stringify({ is24HourFormat: true, showSeconds: false, showDate: true }),
|
||||||
advancedOptions: stringify({ title: null, customCssClasses: [], borderColor: "" }),
|
advancedOptions: stringify({ title: null, customCssClasses: [], borderColor: "" }),
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: bookmarksItemId,
|
||||||
|
kind: "bookmarks",
|
||||||
|
boardId,
|
||||||
|
options: stringify({
|
||||||
|
title: "",
|
||||||
|
layout: "column",
|
||||||
|
hideTitle: false,
|
||||||
|
hideIcon: false,
|
||||||
|
hideHostname: false,
|
||||||
|
openNewTab: true,
|
||||||
|
withBorder: false,
|
||||||
|
items: [appId],
|
||||||
|
}),
|
||||||
|
advancedOptions: stringify({ title: null, customCssClasses: [], borderColor: "" }),
|
||||||
|
},
|
||||||
{
|
{
|
||||||
id: iframeItemId,
|
id: iframeItemId,
|
||||||
kind: "iframe",
|
kind: "iframe",
|
||||||
@@ -418,6 +473,15 @@ const seedInteractionBoardAsync = async (db: SqliteDatabase, ownerId: string) =>
|
|||||||
width: 4,
|
width: 4,
|
||||||
height: 3,
|
height: 3,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
itemId: bookmarksItemId,
|
||||||
|
sectionId,
|
||||||
|
layoutId,
|
||||||
|
xOffset: 0,
|
||||||
|
yOffset: 3,
|
||||||
|
width: 4,
|
||||||
|
height: 3,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
itemId: iframeItemId,
|
itemId: iframeItemId,
|
||||||
sectionId,
|
sectionId,
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import type { Page } from "@playwright/test";
|
||||||
|
|
||||||
|
interface LoginInput {
|
||||||
|
page: Page;
|
||||||
|
baseUrl: string;
|
||||||
|
credentials: {
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
};
|
||||||
|
destination?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const loginAsync = async ({ page, baseUrl, credentials, destination = "/" }: LoginInput) => {
|
||||||
|
const destinationUrl = new URL(destination, baseUrl);
|
||||||
|
const loginUrl = new URL("/auth/login", baseUrl);
|
||||||
|
loginUrl.searchParams.set("callbackUrl", destinationUrl.href);
|
||||||
|
|
||||||
|
await page.goto(loginUrl.href, { waitUntil: "load" });
|
||||||
|
await page.waitForFunction(() => {
|
||||||
|
const form = document.querySelector("form");
|
||||||
|
return form !== null && Object.keys(form).some((key) => key.startsWith("__reactProps$"));
|
||||||
|
});
|
||||||
|
await page.getByLabel("Username").fill(credentials.username);
|
||||||
|
await page.locator("#password").fill(credentials.password);
|
||||||
|
|
||||||
|
await page.locator("button[type='submit']").click();
|
||||||
|
await waitForAuthenticatedSessionAsync(page, baseUrl);
|
||||||
|
|
||||||
|
await page.goto(destinationUrl.href, { waitUntil: "domcontentloaded" });
|
||||||
|
const currentUrl = new URL(page.url());
|
||||||
|
if (currentUrl.origin !== destinationUrl.origin || currentUrl.pathname !== destinationUrl.pathname) {
|
||||||
|
throw new Error("Authenticated destination was not reached");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const waitForAuthenticatedSessionAsync = async (page: Page, baseUrl: string) => {
|
||||||
|
const sessionUrl = new URL("/api/auth/session", baseUrl).href;
|
||||||
|
const deadline = Date.now() + 30_000;
|
||||||
|
|
||||||
|
while (Date.now() < deadline) {
|
||||||
|
const response = await page.request.get(sessionUrl);
|
||||||
|
if (response.ok()) {
|
||||||
|
const session = (await response.json()) as { user?: unknown } | null;
|
||||||
|
if (session?.user !== undefined) return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await page.waitForTimeout(100);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("Authentication session was not established");
|
||||||
|
};
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { parse } from "superjson";
|
||||||
|
import { z } from "zod/v4";
|
||||||
|
|
||||||
|
import type { Session } from "@homarr/auth";
|
||||||
|
import type { Database } from "@homarr/db";
|
||||||
|
import { and, eq } from "@homarr/db";
|
||||||
|
import { boards, items } from "@homarr/db/schema";
|
||||||
|
import type { BoardPermission } from "@homarr/definitions";
|
||||||
|
|
||||||
|
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
||||||
|
|
||||||
|
export const customApiItemInputSchema = z.object({
|
||||||
|
boardId: z.string().min(1).max(100),
|
||||||
|
itemId: z.string().min(1).max(100),
|
||||||
|
definitionId: z.string().min(1).max(100),
|
||||||
|
});
|
||||||
|
|
||||||
|
const customApiOptionsSchema = z.object({ definitionId: z.string() }).passthrough();
|
||||||
|
|
||||||
|
export async function assertCustomApiItemBindingAsync(
|
||||||
|
ctx: { db: Database; session: Session | null },
|
||||||
|
input: z.infer<typeof customApiItemInputSchema>,
|
||||||
|
permission?: BoardPermission,
|
||||||
|
) {
|
||||||
|
const item = await ctx.db.query.items.findFirst({
|
||||||
|
columns: { options: true },
|
||||||
|
where: and(eq(items.id, input.itemId), eq(items.boardId, input.boardId), eq(items.kind, "customApi")),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!item) throwCustomApiWidgetNotFound();
|
||||||
|
if (permission) await throwIfActionForbiddenAsync(ctx, eq(boards.id, input.boardId), permission);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const options = customApiOptionsSchema.parse(parse<unknown>(item.options));
|
||||||
|
if (options.definitionId !== input.definitionId) throwCustomApiWidgetNotFound();
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof TRPCError) throw error;
|
||||||
|
throwCustomApiWidgetNotFound();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function throwCustomApiWidgetNotFound(): never {
|
||||||
|
throw new TRPCError({ code: "NOT_FOUND", message: "Custom API widget not found" });
|
||||||
|
}
|
||||||
@@ -37,11 +37,10 @@ export const locationRouter = createTRPCRouter({
|
|||||||
.input(locationSearchCityInput)
|
.input(locationSearchCityInput)
|
||||||
.output(locationSearchCityOutput)
|
.output(locationSearchCityOutput)
|
||||||
.query(async ({ input }) => {
|
.query(async ({ input }) => {
|
||||||
|
const url = new URL("https://geocoding-api.open-meteo.com/v1/search");
|
||||||
|
url.searchParams.set("name", input.query);
|
||||||
const res = await withTimeoutAsync(async (signal) => {
|
const res = await withTimeoutAsync(async (signal) => {
|
||||||
return await fetchWithTrustedCertificatesAsync(
|
return await fetchWithTrustedCertificatesAsync(url.toString(), { signal });
|
||||||
`https://geocoding-api.open-meteo.com/v1/search?name=${input.query}`,
|
|
||||||
{ signal },
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
return (await res.json()) as z.infer<typeof locationSearchCityOutput>;
|
return (await res.json()) as z.infer<typeof locationSearchCityOutput>;
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -0,0 +1,376 @@
|
|||||||
|
// @vitest-environment node
|
||||||
|
|
||||||
|
import { stringify } from "superjson";
|
||||||
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||||
|
|
||||||
|
import type { Session } from "@homarr/auth";
|
||||||
|
import { createId } from "@homarr/common";
|
||||||
|
import type { Database } from "@homarr/db";
|
||||||
|
import { eq } from "@homarr/db";
|
||||||
|
import { boards, boardUserPermissions, customWidgetDefinitions, items, users } from "@homarr/db/schema";
|
||||||
|
import { createDb } from "@homarr/db/test";
|
||||||
|
import type { GroupPermissionKey } from "@homarr/definitions";
|
||||||
|
|
||||||
|
import { customWidgetRouter } from "../custom-widget/custom-widget-router";
|
||||||
|
import { customApiRouter } from "../widgets/custom-api";
|
||||||
|
|
||||||
|
const createSession = (userId: string, permissions: GroupPermissionKey[] = []): Session => ({
|
||||||
|
user: { id: userId, permissions, colorScheme: "light" },
|
||||||
|
expires: new Date().toISOString(),
|
||||||
|
});
|
||||||
|
|
||||||
|
const createCustomWidgetCaller = (db: Database, session: Session | null) =>
|
||||||
|
customWidgetRouter.createCaller({ db, deviceType: undefined, session });
|
||||||
|
|
||||||
|
const createCustomApiCaller = (db: Database, session: Session | null) =>
|
||||||
|
customApiRouter.createCaller({ db, deviceType: undefined, session });
|
||||||
|
|
||||||
|
async function createFixtureAsync(db: Database) {
|
||||||
|
const ownerId = createId();
|
||||||
|
const viewerId = createId();
|
||||||
|
const modifierId = createId();
|
||||||
|
const unrelatedId = createId();
|
||||||
|
const adminId = createId();
|
||||||
|
const boardId = createId();
|
||||||
|
const itemId = createId();
|
||||||
|
const definitionId = createId();
|
||||||
|
|
||||||
|
await db.insert(users).values(
|
||||||
|
[ownerId, viewerId, modifierId, unrelatedId, adminId].map((id) => ({
|
||||||
|
id,
|
||||||
|
name: id,
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
await db.insert(boards).values({ id: boardId, name: `board-${boardId}`, creatorId: ownerId, isPublic: false });
|
||||||
|
await db.insert(boardUserPermissions).values([
|
||||||
|
{ boardId, userId: viewerId, permission: "view" },
|
||||||
|
{ boardId, userId: modifierId, permission: "modify" },
|
||||||
|
]);
|
||||||
|
await db.insert(customWidgetDefinitions).values({
|
||||||
|
id: definitionId,
|
||||||
|
name: "Restart service",
|
||||||
|
description: "Restarts a service",
|
||||||
|
url: "https://api.example.com/restart",
|
||||||
|
method: "POST",
|
||||||
|
displayType: "actionButton",
|
||||||
|
displayConfig: stringify({
|
||||||
|
type: "actionButton",
|
||||||
|
buttonLabel: "Restart",
|
||||||
|
confirmText: "Continue?",
|
||||||
|
}),
|
||||||
|
creatorId: adminId,
|
||||||
|
});
|
||||||
|
await db.insert(items).values({
|
||||||
|
id: itemId,
|
||||||
|
boardId,
|
||||||
|
kind: "customApi",
|
||||||
|
options: stringify({ definitionId, refreshInterval: 30 }),
|
||||||
|
});
|
||||||
|
|
||||||
|
return { ownerId, viewerId, modifierId, unrelatedId, adminId, boardId, itemId, definitionId };
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.restoreAllMocks();
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
vi.useRealTimers();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("custom widget definition authorization", () => {
|
||||||
|
it("restricts the full definition list to administrators", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
|
||||||
|
await expect(createCustomWidgetCaller(db, null).all()).rejects.toMatchObject({ code: "UNAUTHORIZED" });
|
||||||
|
for (const userId of [fixture.ownerId, fixture.viewerId, fixture.modifierId, fixture.unrelatedId]) {
|
||||||
|
await expect(createCustomWidgetCaller(db, createSession(userId)).all()).rejects.toMatchObject({
|
||||||
|
code: "FORBIDDEN",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
await expect(createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"])).all()).resolves.toEqual([
|
||||||
|
expect.objectContaining({ id: fixture.definitionId, url: "https://api.example.com/restart" }),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns only safe definition summaries to board modifiers", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
|
||||||
|
const result = await createCustomWidgetCaller(db, createSession(fixture.modifierId)).available({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
});
|
||||||
|
expect(result).toEqual([
|
||||||
|
{
|
||||||
|
id: fixture.definitionId,
|
||||||
|
name: "Restart service",
|
||||||
|
description: "Restarts a service",
|
||||||
|
iconUrl: null,
|
||||||
|
displayType: "actionButton",
|
||||||
|
enabled: true,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(JSON.stringify(result)).not.toContain("api.example.com");
|
||||||
|
|
||||||
|
for (const userId of [fixture.viewerId, fixture.unrelatedId]) {
|
||||||
|
await expect(
|
||||||
|
createCustomWidgetCaller(db, createSession(userId)).available({ boardId: fixture.boardId }),
|
||||||
|
).rejects.toMatchObject({ code: "NOT_FOUND" });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("custom widget action authorization", () => {
|
||||||
|
it("requires an administrator even for users who can view or modify the board", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
const input = {
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
};
|
||||||
|
|
||||||
|
await expect(createCustomWidgetCaller(db, null).execute(input)).rejects.toMatchObject({ code: "UNAUTHORIZED" });
|
||||||
|
for (const userId of [fixture.ownerId, fixture.viewerId, fixture.modifierId, fixture.unrelatedId]) {
|
||||||
|
await expect(createCustomWidgetCaller(db, createSession(userId)).execute(input)).rejects.toMatchObject({
|
||||||
|
code: "FORBIDDEN",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("executes for an administrator only when board, item, and definition are bound", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 204 }));
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
const caller = createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"]));
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
caller.execute({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
}),
|
||||||
|
).resolves.toEqual({ success: true, responseInfo: { status: 204 } });
|
||||||
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
|
"https://api.example.com/restart",
|
||||||
|
expect.objectContaining({ method: "POST", redirect: "error", signal: expect.any(AbortSignal) }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it.each(["board", "item", "definition"] as const)("rejects a mismatched %s binding", async (mismatch) => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
const fetchMock = vi.fn();
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
const input = {
|
||||||
|
boardId: mismatch === "board" ? createId() : fixture.boardId,
|
||||||
|
itemId: mismatch === "item" ? createId() : fixture.itemId,
|
||||||
|
definitionId: mismatch === "definition" ? createId() : fixture.definitionId,
|
||||||
|
};
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"])).execute(input),
|
||||||
|
).rejects.toMatchObject({ code: "NOT_FOUND" });
|
||||||
|
expect(fetchMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects disabled definitions before sending a request", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
await db
|
||||||
|
.update(customWidgetDefinitions)
|
||||||
|
.set({ enabled: false })
|
||||||
|
.where(eq(customWidgetDefinitions.id, fixture.definitionId));
|
||||||
|
const fetchMock = vi.fn();
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"])).execute({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
}),
|
||||||
|
).rejects.toMatchObject({ code: "FORBIDDEN" });
|
||||||
|
expect(fetchMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("custom API outbound request hardening", () => {
|
||||||
|
it("rejects credential-bearing action URLs without exposing the credential", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
await db
|
||||||
|
.update(customWidgetDefinitions)
|
||||||
|
.set({ url: "https://operator:super-secret@private.example.com/restart" })
|
||||||
|
.where(eq(customWidgetDefinitions.id, fixture.definitionId));
|
||||||
|
const fetchMock = vi.fn();
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
|
||||||
|
const result = await createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"])).execute({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
});
|
||||||
|
expect(result).toEqual({ success: false, errorCode: "REQUEST_FAILED", responseInfo: null });
|
||||||
|
expect(JSON.stringify(result)).not.toMatch(/super-secret|private\.example\.com/);
|
||||||
|
expect(fetchMock).not.toHaveBeenCalled();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects redirects and redacts fetch errors", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
const fetchMock = vi
|
||||||
|
.fn()
|
||||||
|
.mockRejectedValue(
|
||||||
|
new TypeError("Redirect to http://token:secret@internal.example.local/admin?apiKey=top-secret was rejected"),
|
||||||
|
);
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
|
||||||
|
const result = await createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"])).execute({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
});
|
||||||
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
|
"https://api.example.com/restart",
|
||||||
|
expect.objectContaining({ redirect: "error" }),
|
||||||
|
);
|
||||||
|
expect(result).toEqual({ success: false, errorCode: "REQUEST_FAILED", responseInfo: null });
|
||||||
|
expect(JSON.stringify(result)).not.toMatch(/top-secret|internal\.example\.local|apiKey/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("aborts timed-out action requests and returns a stable failure", async () => {
|
||||||
|
vi.useFakeTimers();
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
const started = Promise.withResolvers<void>();
|
||||||
|
const fetchMock = vi.fn((_url: string, init: RequestInit) => {
|
||||||
|
started.resolve();
|
||||||
|
return new Promise<Response>((_resolve, reject) => {
|
||||||
|
init.signal?.addEventListener("abort", () => {
|
||||||
|
reject(new Error("Timed out calling http://internal.example.local?secret=timeout-secret"));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
|
||||||
|
const request = createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"])).execute({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
});
|
||||||
|
await started.promise;
|
||||||
|
await vi.advanceTimersByTimeAsync(10_000);
|
||||||
|
|
||||||
|
const result = await request;
|
||||||
|
expect(result).toEqual({ success: false, errorCode: "REQUEST_FAILED", responseInfo: null });
|
||||||
|
expect(JSON.stringify(result)).not.toMatch(/timeout-secret|internal\.example\.local/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("bounds action responses before reporting success", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response("x".repeat(2 * 1024 * 1024 + 1))));
|
||||||
|
|
||||||
|
await expect(
|
||||||
|
createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"])).execute({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
}),
|
||||||
|
).resolves.toEqual({ success: false, errorCode: "REQUEST_FAILED", responseInfo: null });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("binds data requests to a viewable item and never exposes backend error text", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
await db
|
||||||
|
.update(customWidgetDefinitions)
|
||||||
|
.set({
|
||||||
|
displayType: "singleValue",
|
||||||
|
method: "GET",
|
||||||
|
displayConfig: stringify({ type: "singleValue", jsonPath: "$.value" }),
|
||||||
|
})
|
||||||
|
.where(eq(customWidgetDefinitions.id, fixture.definitionId));
|
||||||
|
vi.stubGlobal(
|
||||||
|
"fetch",
|
||||||
|
vi.fn().mockRejectedValue(new Error("Backend http://private.example.local/path?token=super-secret\nstack trace")),
|
||||||
|
);
|
||||||
|
|
||||||
|
const input = {
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
};
|
||||||
|
const error = await createCustomApiCaller(db, createSession(fixture.viewerId))
|
||||||
|
.getData(input)
|
||||||
|
.catch((caught: unknown) => caught);
|
||||||
|
expect(error).toMatchObject({ code: "INTERNAL_SERVER_ERROR", message: "Custom API request failed" });
|
||||||
|
expect(JSON.stringify(error)).not.toMatch(/super-secret|private\.example\.local|stack trace/);
|
||||||
|
|
||||||
|
await expect(createCustomApiCaller(db, createSession(fixture.unrelatedId)).getData(input)).rejects.toMatchObject({
|
||||||
|
code: "NOT_FOUND",
|
||||||
|
});
|
||||||
|
await expect(
|
||||||
|
createCustomApiCaller(db, createSession(fixture.viewerId)).getData({ ...input, definitionId: createId() }),
|
||||||
|
).rejects.toMatchObject({ code: "NOT_FOUND" });
|
||||||
|
});
|
||||||
|
|
||||||
|
it("bounds and disposes non-success data responses", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
await db
|
||||||
|
.update(customWidgetDefinitions)
|
||||||
|
.set({
|
||||||
|
displayType: "singleValue",
|
||||||
|
method: "GET",
|
||||||
|
displayConfig: stringify({ type: "singleValue", jsonPath: "$.value" }),
|
||||||
|
})
|
||||||
|
.where(eq(customWidgetDefinitions.id, fixture.definitionId));
|
||||||
|
vi.stubGlobal("fetch", vi.fn().mockResolvedValue(new Response("x".repeat(2 * 1024 * 1024 + 1), { status: 502 })));
|
||||||
|
|
||||||
|
const error = await createCustomApiCaller(db, createSession(fixture.viewerId))
|
||||||
|
.getData({
|
||||||
|
boardId: fixture.boardId,
|
||||||
|
itemId: fixture.itemId,
|
||||||
|
definitionId: fixture.definitionId,
|
||||||
|
})
|
||||||
|
.catch((caught: unknown) => caught);
|
||||||
|
|
||||||
|
expect(error).toMatchObject({ code: "INTERNAL_SERVER_ERROR", message: "Custom API request failed" });
|
||||||
|
expect(JSON.stringify(error)).not.toContain("x".repeat(100));
|
||||||
|
});
|
||||||
|
|
||||||
|
it("redacts unsafe preview failures and rejects redirects", async () => {
|
||||||
|
const db = createDb();
|
||||||
|
const fixture = await createFixtureAsync(db);
|
||||||
|
const fetchMock = vi.fn().mockRejectedValue(new Error("redirected to http://private.local?secret=value"));
|
||||||
|
vi.stubGlobal("fetch", fetchMock);
|
||||||
|
const caller = createCustomWidgetCaller(db, createSession(fixture.adminId, ["admin"]));
|
||||||
|
|
||||||
|
const result = await caller.preview({
|
||||||
|
url: "https://api.example.com/data",
|
||||||
|
method: "GET",
|
||||||
|
authType: "none",
|
||||||
|
displayType: "singleValue",
|
||||||
|
displayConfig: { type: "singleValue", jsonPath: "$.value", label: "Value", unit: "" },
|
||||||
|
secrets: [],
|
||||||
|
});
|
||||||
|
expect(fetchMock).toHaveBeenCalledWith(
|
||||||
|
"https://api.example.com/data",
|
||||||
|
expect.objectContaining({ redirect: "error" }),
|
||||||
|
);
|
||||||
|
expect(result).toEqual({
|
||||||
|
success: false,
|
||||||
|
error: "Custom API preview failed",
|
||||||
|
responseInfo: null,
|
||||||
|
rawResponse: null,
|
||||||
|
});
|
||||||
|
expect(JSON.stringify(result)).not.toMatch(/private\.local|secret=value/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
// @vitest-environment node
|
||||||
|
|
||||||
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||||
|
|
||||||
|
import { createDb } from "@homarr/db/test";
|
||||||
|
|
||||||
|
import { locationRouter } from "../location";
|
||||||
|
|
||||||
|
const mocks = vi.hoisted(() => ({ fetch: vi.fn() }));
|
||||||
|
|
||||||
|
vi.mock("@homarr/core/infrastructure/http", () => ({
|
||||||
|
fetchWithTrustedCertificatesAsync: mocks.fetch,
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@homarr/core/infrastructure/http/timeout", () => ({
|
||||||
|
withTimeoutAsync: async (callback: (signal: AbortSignal) => Promise<Response>) =>
|
||||||
|
await callback(new AbortController().signal),
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.clearAllMocks();
|
||||||
|
mocks.fetch.mockResolvedValue(new Response(JSON.stringify({ results: [] })));
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("locationRouter", () => {
|
||||||
|
test("encodes the complete search text as one name parameter", async () => {
|
||||||
|
const caller = locationRouter.createCaller({ db: createDb(), deviceType: undefined, session: null });
|
||||||
|
|
||||||
|
await caller.searchCity({ query: "Paris&language=xx&token=secret" });
|
||||||
|
|
||||||
|
const requestedUrl = new URL(mocks.fetch.mock.calls[0]?.[0] as string);
|
||||||
|
expect(requestedUrl.searchParams.get("name")).toBe("Paris&language=xx&token=secret");
|
||||||
|
expect(requestedUrl.searchParams.has("language")).toBe(false);
|
||||||
|
expect(requestedUrl.searchParams.has("token")).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -12,8 +12,14 @@ import type { MediaRequest, MediaRequestStats } from "@homarr/integrations/types
|
|||||||
import type * as MediaRequestListHandlerModule from "@homarr/request-handler/media-request-list";
|
import type * as MediaRequestListHandlerModule from "@homarr/request-handler/media-request-list";
|
||||||
|
|
||||||
import { calendarRouter } from "../../widgets/calendar";
|
import { calendarRouter } from "../../widgets/calendar";
|
||||||
|
import { beszelRouter } from "../../widgets/beszel";
|
||||||
|
import { firewallRouter } from "../../widgets/firewall";
|
||||||
|
import { indexerManagerRouter } from "../../widgets/indexer-manager";
|
||||||
|
import { mediaOrganizerRouter } from "../../widgets/media-organizer";
|
||||||
import { mediaRequestsRouter } from "../../widgets/media-requests";
|
import { mediaRequestsRouter } from "../../widgets/media-requests";
|
||||||
import { mediaServerRouter } from "../../widgets/media-server";
|
import { mediaServerRouter } from "../../widgets/media-server";
|
||||||
|
import { notificationsRouter } from "../../widgets/notifications";
|
||||||
|
import { vpnRouter } from "../../widgets/vpn";
|
||||||
|
|
||||||
vi.hoisted(() => {
|
vi.hoisted(() => {
|
||||||
process.env.SKIP_ENV_VALIDATION = "true";
|
process.env.SKIP_ENV_VALIDATION = "true";
|
||||||
@@ -21,7 +27,11 @@ vi.hoisted(() => {
|
|||||||
|
|
||||||
const createHandler = <T>(integration: { name: string }, data: T) => ({
|
const createHandler = <T>(integration: { name: string }, data: T) => ({
|
||||||
getDataAsync: async () => {
|
getDataAsync: async () => {
|
||||||
if (integration.name === "Offline") throw new Error("offline https://internal.example?token=secret");
|
if (integration.name === "Offline") {
|
||||||
|
throw new Error(
|
||||||
|
"GET http://admin:password@internal.example/private/path?token=secret returned\nprivate response body",
|
||||||
|
);
|
||||||
|
}
|
||||||
return { data, timestamp: new Date() };
|
return { data, timestamp: new Date() };
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -34,6 +44,40 @@ vi.mock("@homarr/request-handler/media-server", () => ({
|
|||||||
mediaServerRequestHandler: { handler: (integration: { name: string }) => createHandler(integration, []) },
|
mediaServerRequestHandler: { handler: (integration: { name: string }) => createHandler(integration, []) },
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
vi.mock("@homarr/request-handler/media-organizer", () => ({
|
||||||
|
mediaOrganizerRequestHandler: {
|
||||||
|
handler: (integration: { name: string }) =>
|
||||||
|
createHandler(integration, { missing: [], missingCount: 0, queued: [], queuedCount: 0 }),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@homarr/request-handler/beszel", () => ({
|
||||||
|
beszelSystemsRequestHandler: { handler: (integration: { name: string }) => createHandler(integration, []) },
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@homarr/request-handler/firewall", () => ({
|
||||||
|
firewallCpuRequestHandler: { handler: (integration: { name: string }) => createHandler(integration, { total: 1 }) },
|
||||||
|
firewallInterfacesRequestHandler: { handler: (integration: { name: string }) => createHandler(integration, []) },
|
||||||
|
firewallMemoryRequestHandler: {
|
||||||
|
handler: (integration: { name: string }) => createHandler(integration, { used: 1, total: 2, percent: 50 }),
|
||||||
|
},
|
||||||
|
firewallVersionRequestHandler: {
|
||||||
|
handler: (integration: { name: string }) => createHandler(integration, { version: "1.0" }),
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@homarr/request-handler/indexer-manager", () => ({
|
||||||
|
indexerManagerRequestHandler: { handler: (integration: { name: string }) => createHandler(integration, []) },
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@homarr/request-handler/notifications", () => ({
|
||||||
|
notificationsRequestHandler: { handler: (integration: { name: string }) => createHandler(integration, []) },
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("@homarr/request-handler/vpn", () => ({
|
||||||
|
vpnSummaryHandler: { handler: (integration: { name: string }) => createHandler(integration, { connected: true }) },
|
||||||
|
}));
|
||||||
|
|
||||||
vi.mock("@homarr/request-handler/media-request-list", async (importOriginal) => {
|
vi.mock("@homarr/request-handler/media-request-list", async (importOriginal) => {
|
||||||
const actual = await importOriginal<typeof MediaRequestListHandlerModule>();
|
const actual = await importOriginal<typeof MediaRequestListHandlerModule>();
|
||||||
return {
|
return {
|
||||||
@@ -121,7 +165,7 @@ describe("partial integration failures", () => {
|
|||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
integration: expect.objectContaining({ id: calendar.offlineId }),
|
integration: expect.objectContaining({ id: calendar.offlineId }),
|
||||||
error: "Integration request failed",
|
error: "INTEGRATION_REQUEST_FAILED",
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
@@ -136,7 +180,7 @@ describe("partial integration failures", () => {
|
|||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
integrationId: mediaServer.offlineId,
|
integrationId: mediaServer.offlineId,
|
||||||
sessions: [],
|
sessions: [],
|
||||||
error: "Integration request failed",
|
error: "INTEGRATION_REQUEST_FAILED",
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
);
|
);
|
||||||
@@ -165,13 +209,13 @@ describe("partial integration failures", () => {
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
expect(latest.failedIntegrations).toEqual([
|
expect(latest.failedIntegrations).toEqual([
|
||||||
expect.objectContaining({ integrationId: mediaRequests.offlineId, error: "Integration request failed" }),
|
expect.objectContaining({ integrationId: mediaRequests.offlineId, error: "INTEGRATION_REQUEST_FAILED" }),
|
||||||
]);
|
]);
|
||||||
expect(JSON.stringify(latest)).not.toContain("secret");
|
expect(JSON.stringify(latest)).not.toContain("secret");
|
||||||
|
|
||||||
const stats = await caller.getStats({ integrationIds: mediaRequests.integrationIds });
|
const stats = await caller.getStats({ integrationIds: mediaRequests.integrationIds });
|
||||||
expect(stats.failedIntegrations).toEqual([
|
expect(stats.failedIntegrations).toEqual([
|
||||||
expect.objectContaining({ integrationId: mediaRequests.offlineId, error: "Integration request failed" }),
|
expect.objectContaining({ integrationId: mediaRequests.offlineId, error: "INTEGRATION_REQUEST_FAILED" }),
|
||||||
]);
|
]);
|
||||||
expect(JSON.stringify(stats)).not.toContain("secret");
|
expect(JSON.stringify(stats)).not.toContain("secret");
|
||||||
expect(stats.stats).toHaveLength(1);
|
expect(stats.stats).toHaveLength(1);
|
||||||
@@ -183,4 +227,58 @@ describe("partial integration failures", () => {
|
|||||||
}),
|
}),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("aggregate widget payloads never expose sensitive partial-failure details", async () => {
|
||||||
|
const cases = [
|
||||||
|
{
|
||||||
|
kind: "sonarr" as const,
|
||||||
|
run: async (setup: Awaited<ReturnType<typeof setupAsync>>) =>
|
||||||
|
await mediaOrganizerRouter
|
||||||
|
.createCaller({ db: setup.db, deviceType: undefined, session: setup.session })
|
||||||
|
.getData({ integrationIds: setup.integrationIds, pageSize: 10 }),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: "beszel" as const,
|
||||||
|
run: async (setup: Awaited<ReturnType<typeof setupAsync>>) =>
|
||||||
|
await beszelRouter
|
||||||
|
.createCaller({ db: setup.db, deviceType: undefined, session: setup.session })
|
||||||
|
.getSystems({ integrationIds: setup.integrationIds }),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: "opnsense" as const,
|
||||||
|
run: async (setup: Awaited<ReturnType<typeof setupAsync>>) =>
|
||||||
|
await firewallRouter
|
||||||
|
.createCaller({ db: setup.db, deviceType: undefined, session: setup.session })
|
||||||
|
.getFirewallCpuStatus({ integrationIds: setup.integrationIds }),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: "prowlarr" as const,
|
||||||
|
run: async (setup: Awaited<ReturnType<typeof setupAsync>>) =>
|
||||||
|
await indexerManagerRouter
|
||||||
|
.createCaller({ db: setup.db, deviceType: undefined, session: setup.session })
|
||||||
|
.getIndexersStatus({ integrationIds: setup.integrationIds }),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: "ntfy" as const,
|
||||||
|
run: async (setup: Awaited<ReturnType<typeof setupAsync>>) =>
|
||||||
|
await notificationsRouter
|
||||||
|
.createCaller({ db: setup.db, deviceType: undefined, session: setup.session })
|
||||||
|
.getNotifications({ integrationIds: setup.integrationIds }),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
kind: "gluetun" as const,
|
||||||
|
run: async (setup: Awaited<ReturnType<typeof setupAsync>>) =>
|
||||||
|
await vpnRouter
|
||||||
|
.createCaller({ db: setup.db, deviceType: undefined, session: setup.session })
|
||||||
|
.getSummaries({ integrationIds: setup.integrationIds }),
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const testCase of cases) {
|
||||||
|
const setup = await setupAsync(testCase.kind);
|
||||||
|
const payload = JSON.stringify(await testCase.run(setup));
|
||||||
|
expect(payload).toContain("INTEGRATION_REQUEST_FAILED");
|
||||||
|
expect(payload).not.toMatch(/internal\.example|token=secret|\/private\/|response body|password/i);
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// @vitest-environment node
|
||||||
|
|
||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
import { readBoundedJsonResponseAsync, validateCustomApiUrl } from "../custom-api-security";
|
import { readBoundedJsonResponseAsync, validateCustomApiUrl } from "../custom-api-security";
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import {
|
|||||||
beszelSystemsRequestHandler,
|
beszelSystemsRequestHandler,
|
||||||
} from "@homarr/request-handler/beszel";
|
} from "@homarr/request-handler/beszel";
|
||||||
|
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { settleIntegrationQueries, toPublicIntegrationError } from "../../settle-integrations";
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
import { BoundedAsyncQueue } from "./bounded-async-queue";
|
import { BoundedAsyncQueue } from "./bounded-async-queue";
|
||||||
@@ -18,8 +18,6 @@ import { BoundedAsyncQueue } from "./bounded-async-queue";
|
|||||||
const logger = createLogger({ module: "beszelRouter" });
|
const logger = createLogger({ module: "beszelRouter" });
|
||||||
const MAX_PENDING_LIVE_EVENTS = 4;
|
const MAX_PENDING_LIVE_EVENTS = 4;
|
||||||
|
|
||||||
const errorMessage = (error: unknown) => (error instanceof Error ? error.message : String(error));
|
|
||||||
|
|
||||||
export const beszelRouter = createTRPCRouter({
|
export const beszelRouter = createTRPCRouter({
|
||||||
getSystems: publicProcedure
|
getSystems: publicProcedure
|
||||||
.meta({
|
.meta({
|
||||||
@@ -41,7 +39,6 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
return {
|
return {
|
||||||
integrationId: integration.id,
|
integrationId: integration.id,
|
||||||
integrationName: integration.name,
|
integrationName: integration.name,
|
||||||
integrationUrl: integration.url,
|
|
||||||
systems: data,
|
systems: data,
|
||||||
updatedAt: timestamp,
|
updatedAt: timestamp,
|
||||||
};
|
};
|
||||||
@@ -50,10 +47,9 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
fallback: (integration, error) => ({
|
fallback: (integration, error) => ({
|
||||||
integrationId: integration.id,
|
integrationId: integration.id,
|
||||||
integrationName: integration.name,
|
integrationName: integration.name,
|
||||||
integrationUrl: integration.url,
|
|
||||||
systems: [],
|
systems: [],
|
||||||
updatedAt: new Date(0),
|
updatedAt: new Date(0),
|
||||||
error: errorMessage(error),
|
error: toPublicIntegrationError(error),
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
@@ -127,7 +123,7 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
history: [],
|
history: [],
|
||||||
systemNameMap: {},
|
systemNameMap: {},
|
||||||
updatedAt: new Date(0),
|
updatedAt: new Date(0),
|
||||||
error: errorMessage(error),
|
error: toPublicIntegrationError(error),
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
@@ -194,7 +190,7 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
systemStats: [],
|
systemStats: [],
|
||||||
containerStats: [],
|
containerStats: [],
|
||||||
updatedAt: new Date(0),
|
updatedAt: new Date(0),
|
||||||
error: errorMessage(error),
|
error: toPublicIntegrationError(error),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
@@ -271,15 +267,14 @@ export const beszelRouter = createTRPCRouter({
|
|||||||
integrationId: integration.id,
|
integrationId: integration.id,
|
||||||
systemId: input.systemId,
|
systemId: input.systemId,
|
||||||
emittedEventCount,
|
emittedEventCount,
|
||||||
error: errorMessage(error),
|
error,
|
||||||
});
|
});
|
||||||
queue.fail(
|
queue.fail(
|
||||||
error instanceof TRPCError
|
new TRPCError({
|
||||||
? error
|
code: "BAD_GATEWAY",
|
||||||
: new TRPCError({
|
message: "Live integration request failed",
|
||||||
code: "INTERNAL_SERVER_ERROR",
|
cause: error,
|
||||||
message: error instanceof Error ? error.message : String(error),
|
}),
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { radarrReleaseTypes } from "@homarr/integrations/types";
|
|||||||
import { calendarMonthRequestHandler } from "@homarr/request-handler/calendar";
|
import { calendarMonthRequestHandler } from "@homarr/request-handler/calendar";
|
||||||
|
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { PUBLIC_INTEGRATION_ERROR, settleIntegrationQueries } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
export const calendarRouter = createTRPCRouter({
|
export const calendarRouter = createTRPCRouter({
|
||||||
@@ -52,7 +52,7 @@ export const calendarRouter = createTRPCRouter({
|
|||||||
name: integration.name,
|
name: integration.name,
|
||||||
kind: integration.kind,
|
kind: integration.kind,
|
||||||
},
|
},
|
||||||
error: "Integration request failed",
|
error: PUBLIC_INTEGRATION_ERROR,
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { readBoundedJsonResponseAsync as readBoundedJsonResponse } from "@homarr/common/server";
|
import { readBoundedJsonResponseAsync as readBoundedJsonResponse } from "@homarr/common/server";
|
||||||
|
|
||||||
export const MAX_CUSTOM_API_RESPONSE_BYTES = 2 * 1024 * 1024;
|
export const MAX_CUSTOM_API_RESPONSE_BYTES = 2 * 1024 * 1024;
|
||||||
|
export const CUSTOM_API_FETCH_TIMEOUT_MS = 10_000;
|
||||||
|
|
||||||
export function validateCustomApiUrl(urlString: string): URL {
|
export function validateCustomApiUrl(urlString: string): URL {
|
||||||
if (urlString.length > 2_048) throw new Error("Custom API URL is too long");
|
if (urlString.length > 2_048) throw new Error("Custom API URL is too long");
|
||||||
@@ -19,3 +20,48 @@ export async function readBoundedJsonResponseAsync(
|
|||||||
): Promise<unknown> {
|
): Promise<unknown> {
|
||||||
return await readBoundedJsonResponse(response, maxBytes, "Custom API response");
|
return await readBoundedJsonResponse(response, maxBytes, "Custom API response");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function consumeBoundedResponseAsync(
|
||||||
|
response: Response,
|
||||||
|
maxBytes = MAX_CUSTOM_API_RESPONSE_BYTES,
|
||||||
|
): Promise<void> {
|
||||||
|
const declaredLength = Number(response.headers.get("content-length"));
|
||||||
|
if (Number.isFinite(declaredLength) && declaredLength > maxBytes) {
|
||||||
|
await response.body?.cancel();
|
||||||
|
throw new Error(`Custom API response exceeds ${maxBytes} bytes`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!response.body) return;
|
||||||
|
const reader = response.body.getReader();
|
||||||
|
let byteLength = 0;
|
||||||
|
for (;;) {
|
||||||
|
const { done, value } = await reader.read();
|
||||||
|
if (done) return;
|
||||||
|
byteLength += value?.byteLength ?? 0;
|
||||||
|
if (byteLength > maxBytes) {
|
||||||
|
await reader.cancel();
|
||||||
|
throw new Error(`Custom API response exceeds ${maxBytes} bytes`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function withCustomApiResponseAsync<TResult>(
|
||||||
|
url: URL,
|
||||||
|
init: Omit<RequestInit, "redirect" | "signal">,
|
||||||
|
consumeResponseAsync: (response: Response) => Promise<TResult>,
|
||||||
|
timeoutMs = CUSTOM_API_FETCH_TIMEOUT_MS,
|
||||||
|
): Promise<TResult> {
|
||||||
|
const controller = new AbortController();
|
||||||
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(url.toString(), {
|
||||||
|
...init,
|
||||||
|
redirect: "error",
|
||||||
|
signal: controller.signal,
|
||||||
|
});
|
||||||
|
return await consumeResponseAsync(response);
|
||||||
|
} finally {
|
||||||
|
clearTimeout(timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
} from "@homarr/request-handler/firewall";
|
} from "@homarr/request-handler/firewall";
|
||||||
|
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { settleIntegrationQueries, toPublicIntegrationError } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
const firewallMiddleware = createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("firewall"));
|
const firewallMiddleware = createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("firewall"));
|
||||||
@@ -35,7 +35,7 @@ const queryFirewall = <
|
|||||||
fallback: (integration, error) => ({
|
fallback: (integration, error) => ({
|
||||||
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: new Date(0) },
|
integration: { id: integration.id, name: integration.name, kind: integration.kind, updatedAt: new Date(0) },
|
||||||
summary: createFallback(),
|
summary: createFallback(),
|
||||||
error: error instanceof Error ? error.message : "Integration request failed",
|
error: toPublicIntegrationError(error),
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import { indexerManagerRequestHandler } from "@homarr/request-handler/indexer-ma
|
|||||||
|
|
||||||
import type { IntegrationAction } from "../../middlewares/integration";
|
import type { IntegrationAction } from "../../middlewares/integration";
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { settleIntegrationQueries, toPublicIntegrationError } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
const createIndexerManagerIntegrationMiddleware = (action: IntegrationAction) =>
|
const createIndexerManagerIntegrationMiddleware = (action: IntegrationAction) =>
|
||||||
@@ -34,7 +34,7 @@ export const indexerManagerRouter = createTRPCRouter({
|
|||||||
integrationId: integration.id,
|
integrationId: integration.id,
|
||||||
integrationName: integration.name,
|
integrationName: integration.name,
|
||||||
indexers: [],
|
indexers: [],
|
||||||
error: error instanceof Error ? error.message : String(error),
|
error: toPublicIntegrationError(error),
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
|||||||
import { mediaOrganizerRequestHandler } from "@homarr/request-handler/media-organizer";
|
import { mediaOrganizerRequestHandler } from "@homarr/request-handler/media-organizer";
|
||||||
|
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { settleIntegrationQueries, toPublicIntegrationError } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
export const mediaOrganizerRouter = createTRPCRouter({
|
export const mediaOrganizerRouter = createTRPCRouter({
|
||||||
@@ -39,7 +39,7 @@ export const mediaOrganizerRouter = createTRPCRouter({
|
|||||||
missingCount: 0,
|
missingCount: 0,
|
||||||
queued: [],
|
queued: [],
|
||||||
queuedCount: 0,
|
queuedCount: 0,
|
||||||
error: error instanceof Error ? error.message : String(error),
|
error: toPublicIntegrationError(error),
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import {
|
|||||||
import { mediaRequestStatsRequestHandler } from "@homarr/request-handler/media-request-stats";
|
import { mediaRequestStatsRequestHandler } from "@homarr/request-handler/media-request-stats";
|
||||||
|
|
||||||
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware, createOneIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { PUBLIC_INTEGRATION_ERROR, settleIntegrationQueries } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
interface IntegrationQueryResult<TData> {
|
interface IntegrationQueryResult<TData> {
|
||||||
@@ -47,7 +47,7 @@ export const mediaRequestsRouter = createTRPCRouter({
|
|||||||
fallback: (integration) => ({
|
fallback: (integration) => ({
|
||||||
integration: { id: integration.id, name: integration.name, kind: integration.kind },
|
integration: { id: integration.id, name: integration.name, kind: integration.kind },
|
||||||
data: [],
|
data: [],
|
||||||
error: "Integration request failed",
|
error: PUBLIC_INTEGRATION_ERROR,
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
@@ -104,7 +104,7 @@ export const mediaRequestsRouter = createTRPCRouter({
|
|||||||
fallback: (integration) => ({
|
fallback: (integration) => ({
|
||||||
integration: { id: integration.id, name: integration.name, kind: integration.kind },
|
integration: { id: integration.id, name: integration.name, kind: integration.kind },
|
||||||
data: { stats: null, users: [] },
|
data: { stats: null, users: [] },
|
||||||
error: "Integration request failed",
|
error: PUBLIC_INTEGRATION_ERROR,
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { mediaServerRequestHandler } from "@homarr/request-handler/media-server"
|
|||||||
|
|
||||||
import type { IntegrationAction } from "../../middlewares/integration";
|
import type { IntegrationAction } from "../../middlewares/integration";
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { PUBLIC_INTEGRATION_ERROR, settleIntegrationQueries } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
const createMediaServerIntegrationMiddleware = (action: IntegrationAction) =>
|
const createMediaServerIntegrationMiddleware = (action: IntegrationAction) =>
|
||||||
@@ -43,7 +43,7 @@ export const mediaServerRouter = createTRPCRouter({
|
|||||||
integrationName: integration.name,
|
integrationName: integration.name,
|
||||||
integrationKind: integration.kind,
|
integrationKind: integration.kind,
|
||||||
sessions: [],
|
sessions: [],
|
||||||
error: "Integration request failed",
|
error: PUBLIC_INTEGRATION_ERROR,
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { notificationsRequestHandler } from "@homarr/request-handler/notificatio
|
|||||||
|
|
||||||
import type { IntegrationAction } from "../../middlewares/integration";
|
import type { IntegrationAction } from "../../middlewares/integration";
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { settleIntegrationQueries, toPublicIntegrationError } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
const createNotificationsIntegrationMiddleware = (action: IntegrationAction) =>
|
const createNotificationsIntegrationMiddleware = (action: IntegrationAction) =>
|
||||||
@@ -39,7 +39,7 @@ export const notificationsRouter = createTRPCRouter({
|
|||||||
updatedAt: new Date(0),
|
updatedAt: new Date(0),
|
||||||
},
|
},
|
||||||
data: [],
|
data: [],
|
||||||
error: error instanceof Error ? error.message : String(error),
|
error: toPublicIntegrationError(error),
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import { createTRPCRouter, publicProcedure } from "../../trpc";
|
|||||||
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
import { throwIfActionForbiddenAsync } from "../board/board-access";
|
||||||
|
|
||||||
const logger = createLogger({ module: "releasesRouter" });
|
const logger = createLogger({ module: "releasesRouter" });
|
||||||
|
const PUBLIC_RELEASE_ERROR_MESSAGE: string = "Release provider request failed";
|
||||||
|
|
||||||
const formatVersionFilterRegex = (versionFilter: z.infer<typeof releaseVersionFilterSchema> | undefined) => {
|
const formatVersionFilterRegex = (versionFilter: z.infer<typeof releaseVersionFilterSchema> | undefined) => {
|
||||||
if (!versionFilter) return undefined;
|
if (!versionFilter) return undefined;
|
||||||
@@ -100,19 +101,38 @@ export const releasesRouter = createTRPCRouter({
|
|||||||
})
|
})
|
||||||
.getDataAsync();
|
.getDataAsync();
|
||||||
|
|
||||||
|
const data = response.data.success
|
||||||
|
? response.data
|
||||||
|
: {
|
||||||
|
success: false as const,
|
||||||
|
error: { code: response.data.error.code, message: PUBLIC_RELEASE_ERROR_MESSAGE },
|
||||||
|
};
|
||||||
|
if (!response.data.success) {
|
||||||
|
logger.warn("Release provider request failed", {
|
||||||
|
repositoryId,
|
||||||
|
provider: repository.provider,
|
||||||
|
error: response.data.error,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: repositoryId,
|
id: repositoryId,
|
||||||
provider: repository.provider,
|
provider: repository.provider,
|
||||||
timestamp: response.timestamp,
|
timestamp: response.timestamp,
|
||||||
...response.data,
|
...data,
|
||||||
};
|
};
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
logger.warn("Release provider request failed", {
|
||||||
|
repositoryId,
|
||||||
|
provider: repository.provider,
|
||||||
|
error,
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
id: repositoryId,
|
id: repositoryId,
|
||||||
provider: repository.provider,
|
provider: repository.provider,
|
||||||
timestamp: new Date(),
|
timestamp: new Date(),
|
||||||
success: false as const,
|
success: false as const,
|
||||||
error: { code: "unexpected" as const, message: String(error) },
|
error: { code: "unexpected" as const, message: PUBLIC_RELEASE_ERROR_MESSAGE },
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
|||||||
import { vpnSummaryHandler } from "@homarr/request-handler/vpn";
|
import { vpnSummaryHandler } from "@homarr/request-handler/vpn";
|
||||||
|
|
||||||
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
||||||
import { settleIntegrationQueries } from "../../settle-integrations";
|
import { settleIntegrationQueries, toPublicIntegrationError } from "../../settle-integrations";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
export const vpnRouter = createTRPCRouter({
|
export const vpnRouter = createTRPCRouter({
|
||||||
@@ -33,7 +33,7 @@ export const vpnRouter = createTRPCRouter({
|
|||||||
updatedAt: new Date(0),
|
updatedAt: new Date(0),
|
||||||
},
|
},
|
||||||
summary: null,
|
summary: null,
|
||||||
error: error instanceof Error ? error.message : String(error),
|
error: toPublicIntegrationError(error),
|
||||||
}),
|
}),
|
||||||
throwOnAllFailures: true,
|
throwOnAllFailures: true,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
|||||||
|
|
||||||
const logger = createLogger({ module: "settleIntegrations" });
|
const logger = createLogger({ module: "settleIntegrations" });
|
||||||
|
|
||||||
|
export const PUBLIC_INTEGRATION_ERROR: string = "INTEGRATION_REQUEST_FAILED";
|
||||||
|
|
||||||
|
export const toPublicIntegrationError = (_error: unknown): string => PUBLIC_INTEGRATION_ERROR;
|
||||||
|
|
||||||
interface IntegrationLike {
|
interface IntegrationLike {
|
||||||
id: string;
|
id: string;
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { beforeEach, describe, expect, test, vi } from "vitest";
|
import { beforeEach, describe, expect, test, vi } from "vitest";
|
||||||
|
|
||||||
import { settleIntegrationQueries } from "../settle-integrations";
|
import { PUBLIC_INTEGRATION_ERROR, settleIntegrationQueries, toPublicIntegrationError } from "../settle-integrations";
|
||||||
|
|
||||||
const mocks = vi.hoisted(() => ({
|
const mocks = vi.hoisted(() => ({
|
||||||
logger: { warn: vi.fn() },
|
logger: { warn: vi.fn() },
|
||||||
@@ -22,6 +22,17 @@ const integrations = [
|
|||||||
beforeEach(() => vi.clearAllMocks());
|
beforeEach(() => vi.clearAllMocks());
|
||||||
|
|
||||||
describe("settleIntegrationQueries", () => {
|
describe("settleIntegrationQueries", () => {
|
||||||
|
test("maps sensitive integration failures to a stable public code", () => {
|
||||||
|
const error = new Error(
|
||||||
|
"GET http://admin:password@nas.internal.local/private/path?token=secret returned\nprivate response body",
|
||||||
|
);
|
||||||
|
|
||||||
|
const publicError = toPublicIntegrationError(error);
|
||||||
|
|
||||||
|
expect(publicError).toBe(PUBLIC_INTEGRATION_ERROR);
|
||||||
|
expect(publicError).not.toMatch(/admin|password|nas\.internal|private|token|secret|response body/i);
|
||||||
|
});
|
||||||
|
|
||||||
test("returns an empty result when no integrations are configured", async () => {
|
test("returns an empty result when no integrations are configured", async () => {
|
||||||
await expect(settleIntegrationQueries([], vi.fn())).resolves.toEqual([]);
|
await expect(settleIntegrationQueries([], vi.fn())).resolves.toEqual([]);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -48,3 +48,38 @@ const absoluteUrlRegex = /^[a-z]+:(\/\/)?/;
|
|||||||
export const isAbsoluteUrl = (urlOrPath: string): boolean => {
|
export const isAbsoluteUrl = (urlOrPath: string): boolean => {
|
||||||
return absoluteUrlRegex.test(urlOrPath.toLowerCase());
|
return absoluteUrlRegex.test(urlOrPath.toLowerCase());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const SAFE_NEW_TAB_REL = "noopener noreferrer";
|
||||||
|
|
||||||
|
interface SafeApplicationUrlOptions {
|
||||||
|
baseUrl?: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Returns an absolute, credential-free HTTP(S) URL suitable for application navigation. */
|
||||||
|
export const getSafeApplicationUrl = (value: unknown, options: SafeApplicationUrlOptions = {}): string | undefined => {
|
||||||
|
if (typeof value !== "string" || value.trim() === "") return undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
let baseUrl: string | undefined;
|
||||||
|
if (options.baseUrl !== undefined) {
|
||||||
|
const parsedBase = parseHttpUrl(options.baseUrl);
|
||||||
|
if (!parsedBase) return undefined;
|
||||||
|
parsedBase.search = "";
|
||||||
|
parsedBase.hash = "";
|
||||||
|
baseUrl = parsedBase.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return parseHttpUrl(value, baseUrl)?.toString();
|
||||||
|
} catch {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const parseHttpUrl = (value: unknown, baseUrl?: string): URL | undefined => {
|
||||||
|
if (typeof value !== "string") return undefined;
|
||||||
|
const url = new URL(value, baseUrl);
|
||||||
|
if ((url.protocol !== "http:" && url.protocol !== "https:") || url.username !== "" || url.password !== "") {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return url;
|
||||||
|
};
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Spotlight } from "@mantine/spotlight";
|
import { Spotlight } from "@mantine/spotlight";
|
||||||
|
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "@homarr/common";
|
||||||
import { Link } from "@homarr/ui";
|
import { Link } from "@homarr/ui";
|
||||||
|
|
||||||
import type { inferSearchInteractionOptions } from "../../../lib/interaction";
|
import type { inferSearchInteractionOptions } from "../../../lib/interaction";
|
||||||
@@ -15,10 +16,18 @@ interface ChildrenActionItemProps {
|
|||||||
export const ChildrenActionItem = ({ childrenOptions, action, query, setChildrenOptions }: ChildrenActionItemProps) => {
|
export const ChildrenActionItem = ({ childrenOptions, action, query, setChildrenOptions }: ChildrenActionItemProps) => {
|
||||||
const interaction = action.useInteraction(childrenOptions.option, query);
|
const interaction = action.useInteraction(childrenOptions.option, query);
|
||||||
|
|
||||||
|
const safeHref = interaction.type === "link" ? getSafeApplicationUrl(interaction.href) : undefined;
|
||||||
const renderRoot =
|
const renderRoot =
|
||||||
interaction.type === "link"
|
interaction.type === "link" && safeHref
|
||||||
? (props: Record<string, unknown>) => {
|
? (props: Record<string, unknown>) => {
|
||||||
return <Link href={interaction.href} target={interaction.newTab ? "_blank" : undefined} {...props} />;
|
return (
|
||||||
|
<Link
|
||||||
|
href={safeHref}
|
||||||
|
target={interaction.newTab ? "_blank" : undefined}
|
||||||
|
rel={interaction.newTab ? SAFE_NEW_TAB_REL : undefined}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { Spotlight } from "@mantine/spotlight";
|
|||||||
|
|
||||||
import type { TranslationObject } from "@homarr/translation";
|
import type { TranslationObject } from "@homarr/translation";
|
||||||
import { Link } from "@homarr/ui";
|
import { Link } from "@homarr/ui";
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "@homarr/common";
|
||||||
|
|
||||||
import type { SearchGroup } from "../../../lib/group";
|
import type { SearchGroup } from "../../../lib/group";
|
||||||
import type { inferSearchInteractionOptions } from "../../../lib/interaction";
|
import type { inferSearchInteractionOptions } from "../../../lib/interaction";
|
||||||
@@ -30,10 +31,18 @@ export const SpotlightGroupActionItem = <TOption extends Record<string, unknown>
|
|||||||
|
|
||||||
const { key: _reactKey, ...optionProps } = option as unknown as { key?: unknown } & Record<string, unknown>;
|
const { key: _reactKey, ...optionProps } = option as unknown as { key?: unknown } & Record<string, unknown>;
|
||||||
|
|
||||||
|
const safeHref = interaction.type === "link" ? getSafeApplicationUrl(interaction.href) : undefined;
|
||||||
const renderRoot =
|
const renderRoot =
|
||||||
interaction.type === "link"
|
interaction.type === "link" && safeHref
|
||||||
? (props: Record<string, unknown>) => {
|
? (props: Record<string, unknown>) => {
|
||||||
return <Link href={interaction.href} target={interaction.newTab ? "_blank" : undefined} {...props} />;
|
return (
|
||||||
|
<Link
|
||||||
|
href={safeHref}
|
||||||
|
target={interaction.newTab ? "_blank" : undefined}
|
||||||
|
rel={interaction.newTab ? SAFE_NEW_TAB_REL : undefined}
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
: undefined;
|
: undefined;
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,9 @@ import { IconClick, IconListSearch } from "@tabler/icons-react";
|
|||||||
import type { RouterOutputs } from "@homarr/api";
|
import type { RouterOutputs } from "@homarr/api";
|
||||||
import { clientApi } from "@homarr/api/client";
|
import { clientApi } from "@homarr/api/client";
|
||||||
import { createModal, useModalAction } from "@homarr/modals";
|
import { createModal, useModalAction } from "@homarr/modals";
|
||||||
import { useScopedI18n } from "@homarr/translation/client";
|
import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
|
import { formatLocalizedCompactNumber } from "../common/locale";
|
||||||
import type { OptionLocation } from "../options";
|
import type { OptionLocation } from "../options";
|
||||||
import type { CommonWidgetInputProps } from "./common";
|
import type { CommonWidgetInputProps } from "./common";
|
||||||
import { useWidgetInputTranslation } from "./common";
|
import { useWidgetInputTranslation } from "./common";
|
||||||
@@ -128,7 +129,7 @@ const LocationSearchModal = createModal<LocationSearchInnerProps>(({ actions, in
|
|||||||
if (error) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
<Alert title={tCommon("error")} color="red">
|
<Alert title={tCommon("error")} color="red">
|
||||||
{error.message}
|
{tCommon("action.tryAgain")}
|
||||||
</Alert>
|
</Alert>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -187,6 +188,7 @@ interface LocationSearchTableRowProps {
|
|||||||
|
|
||||||
const LocationSelectTableRow = ({ city, onLocationSelect, closeModal }: LocationSearchTableRowProps) => {
|
const LocationSelectTableRow = ({ city, onLocationSelect, closeModal }: LocationSearchTableRowProps) => {
|
||||||
const t = useScopedI18n("widget.common.location.table");
|
const t = useScopedI18n("widget.common.location.table");
|
||||||
|
const locale = useCurrentIntlLocale();
|
||||||
const onSelect = useCallback(() => {
|
const onSelect = useCallback(() => {
|
||||||
onLocationSelect({
|
onLocationSelect({
|
||||||
name: city.name,
|
name: city.name,
|
||||||
@@ -196,8 +198,6 @@ const LocationSelectTableRow = ({ city, onLocationSelect, closeModal }: Location
|
|||||||
closeModal();
|
closeModal();
|
||||||
}, [city, onLocationSelect, closeModal]);
|
}, [city, onLocationSelect, closeModal]);
|
||||||
|
|
||||||
const formatter = Intl.NumberFormat("en", { notation: "compact" });
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Table.Tr>
|
<Table.Tr>
|
||||||
<Table.Td>
|
<Table.Td>
|
||||||
@@ -215,7 +215,7 @@ const LocationSelectTableRow = ({ city, onLocationSelect, closeModal }: Location
|
|||||||
</Table.Td>
|
</Table.Td>
|
||||||
<Table.Td>
|
<Table.Td>
|
||||||
{city.population ? (
|
{city.population ? (
|
||||||
<Text style={{ whiteSpace: "nowrap" }}>{formatter.format(city.population)}</Text>
|
<Text style={{ whiteSpace: "nowrap" }}>{formatLocalizedCompactNumber(city.population, locale)}</Text>
|
||||||
) : (
|
) : (
|
||||||
<Text c="gray"> {t("population.fallback")}</Text>
|
<Text c="gray"> {t("population.fallback")}</Text>
|
||||||
)}
|
)}
|
||||||
|
|||||||
@@ -271,19 +271,19 @@ const ProviderTokensSection = ({ itemId, repositories }: { itemId: string; repos
|
|||||||
const { data: configuredKinds = [], refetch } = clientApi.widget.secrets.getConfiguredKinds.useQuery({ itemId });
|
const { data: configuredKinds = [], refetch } = clientApi.widget.secrets.getConfiguredKinds.useQuery({ itemId });
|
||||||
const setSecret = clientApi.widget.secrets.setSecret.useMutation({
|
const setSecret = clientApi.widget.secrets.setSecret.useMutation({
|
||||||
onSuccess: () => refetch(),
|
onSuccess: () => refetch(),
|
||||||
onError: (error) => {
|
onError: () => {
|
||||||
showErrorNotification({
|
showErrorNotification({
|
||||||
title: "Failed to save token",
|
title: tRepository("tokens.label"),
|
||||||
message: error.message,
|
message: tRepository("tokens.notConfigured"),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
const deleteSecret = clientApi.widget.secrets.deleteSecret.useMutation({
|
const deleteSecret = clientApi.widget.secrets.deleteSecret.useMutation({
|
||||||
onSuccess: () => refetch(),
|
onSuccess: () => refetch(),
|
||||||
onError: (error) => {
|
onError: () => {
|
||||||
showErrorNotification({
|
showErrorNotification({
|
||||||
title: "Failed to delete token",
|
title: tRepository("tokens.label"),
|
||||||
message: error.message,
|
message: tRepository("tokens.notConfigured"),
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,42 @@
|
|||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
import { supportsAdvancedFocus } from "./definition";
|
||||||
|
import { widgetImports } from ".";
|
||||||
|
|
||||||
|
const compactOnlyWidgetKinds = [
|
||||||
|
"app",
|
||||||
|
"bazarr",
|
||||||
|
"beszelSystemGrid",
|
||||||
|
"beszelSystemStats",
|
||||||
|
"beszelSystemTable",
|
||||||
|
"coolify",
|
||||||
|
"dnsHoleControls",
|
||||||
|
"dnsHoleSummary",
|
||||||
|
"iframe",
|
||||||
|
"indexerManager",
|
||||||
|
"mediaRequests-requestList",
|
||||||
|
"mediaRequests-requestStats",
|
||||||
|
"minecraftServerStatus",
|
||||||
|
"networkControllerSummary",
|
||||||
|
"notebook",
|
||||||
|
"notifications",
|
||||||
|
"paperlessNgx",
|
||||||
|
"patchmon",
|
||||||
|
"releases",
|
||||||
|
"rssFeed",
|
||||||
|
"stockPrice",
|
||||||
|
"systemDisks",
|
||||||
|
"video",
|
||||||
|
"vpn",
|
||||||
|
] as const;
|
||||||
|
|
||||||
|
describe("advanced focus support", () => {
|
||||||
|
test("is reserved for widgets with a distinct advanced experience", () => {
|
||||||
|
const actualCompactOnlyKinds = Object.entries(widgetImports)
|
||||||
|
.filter(([, widgetImport]) => !supportsAdvancedFocus(widgetImport.definition))
|
||||||
|
.map(([kind]) => kind)
|
||||||
|
.toSorted();
|
||||||
|
|
||||||
|
expect(actualCompactOnlyKinds).toEqual([...compactOnlyWidgetKinds].toSorted());
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -18,6 +18,7 @@ import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client"
|
|||||||
|
|
||||||
import { WidgetEmptyState } from "../common/empty-state";
|
import { WidgetEmptyState } from "../common/empty-state";
|
||||||
import type { WidgetComponentProps } from "../definition";
|
import type { WidgetComponentProps } from "../definition";
|
||||||
|
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||||
import actionTargetClasses from "../common/action-target.module.css";
|
import actionTargetClasses from "../common/action-target.module.css";
|
||||||
|
|
||||||
import "react-quill-new/dist/quill.snow.css";
|
import "react-quill-new/dist/quill.snow.css";
|
||||||
@@ -181,10 +182,12 @@ const AnchorNoteWidgetContent = ({
|
|||||||
}: AnchorNoteWidgetContentProps) => {
|
}: AnchorNoteWidgetContentProps) => {
|
||||||
const t = useScopedI18n("widget.anchorNote");
|
const t = useScopedI18n("widget.anchorNote");
|
||||||
const locale = useCurrentIntlLocale();
|
const locale = useCurrentIntlLocale();
|
||||||
const { data: note, refetch } = clientApi.widget.anchorNotes.getNote.useQuery({
|
const noteQuery = clientApi.widget.anchorNotes.getNote.useQuery({
|
||||||
integrationId,
|
integrationId,
|
||||||
noteId,
|
noteId,
|
||||||
});
|
});
|
||||||
|
const note = getUsableWidgetQueryData(noteQuery);
|
||||||
|
const { refetch } = noteQuery;
|
||||||
const { mutateAsync: updateNoteAsync, isPending: isUpdating } = clientApi.widget.anchorNotes.updateNote.useMutation();
|
const { mutateAsync: updateNoteAsync, isPending: isUpdating } = clientApi.widget.anchorNotes.updateNote.useMutation();
|
||||||
|
|
||||||
const [isEditing, setIsEditing] = useState(false);
|
const [isEditing, setIsEditing] = useState(false);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
import type { PropsWithChildren } from "react";
|
import type { PropsWithChildren } from "react";
|
||||||
import { Fragment, Suspense } from "react";
|
import { Fragment, Suspense } from "react";
|
||||||
import { Flex, rem, Stack, Text, Tooltip, UnstyledButton } from "@mantine/core";
|
import { Box, Flex, Stack, Text, Tooltip, UnstyledButton } from "@mantine/core";
|
||||||
import { IconMinus } from "@tabler/icons-react";
|
import { IconMinus } from "@tabler/icons-react";
|
||||||
import combineClasses from "clsx";
|
import combineClasses from "clsx";
|
||||||
|
|
||||||
@@ -14,6 +14,9 @@ import { useI18n } from "@homarr/translation/client";
|
|||||||
import { MaskedOrNormalImage } from "@homarr/ui";
|
import { MaskedOrNormalImage } from "@homarr/ui";
|
||||||
|
|
||||||
import { WidgetEmptyState } from "../common/empty-state";
|
import { WidgetEmptyState } from "../common/empty-state";
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
|
||||||
|
import { getUsableWidgetQueryData, isInitialWidgetQueryPending } from "../common/query-state";
|
||||||
|
import { WidgetQueryErrorIndicator, WidgetQueryLoadingState } from "../common/query-state-indicator";
|
||||||
import type { WidgetComponentProps } from "../definition";
|
import type { WidgetComponentProps } from "../definition";
|
||||||
import classes from "./app.module.css";
|
import classes from "./app.module.css";
|
||||||
import { PingDot } from "./ping/ping-dot";
|
import { PingDot } from "./ping/ping-dot";
|
||||||
@@ -23,12 +26,12 @@ export default function AppWidget({ options, isEditMode, height, width }: Widget
|
|||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
const settings = useSettings();
|
const settings = useSettings();
|
||||||
const board = useRequiredBoard();
|
const board = useRequiredBoard();
|
||||||
const { data: app } = clientApi.app.byId.useQuery({
|
const appQuery = clientApi.app.byId.useQuery({ id: options.appId });
|
||||||
id: options.appId,
|
const app = getUsableWidgetQueryData(appQuery);
|
||||||
});
|
const href = getSafeApplicationUrl(app?.href);
|
||||||
useRegisterSpotlightContextResults(
|
useRegisterSpotlightContextResults(
|
||||||
`app-${app?.id ?? options.appId}`,
|
`app-${app?.id ?? options.appId}`,
|
||||||
app?.href
|
app && href
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
id: app.id,
|
id: app.id,
|
||||||
@@ -37,99 +40,102 @@ export default function AppWidget({ options, isEditMode, height, width }: Widget
|
|||||||
interaction() {
|
interaction() {
|
||||||
return {
|
return {
|
||||||
type: "link",
|
type: "link",
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
href,
|
||||||
href: app.href!,
|
|
||||||
newTab: options.openInNewTab,
|
newTab: options.openInNewTab,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
: [],
|
: [],
|
||||||
[app, options.openInNewTab],
|
[app, href, options.openInNewTab],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (isInitialWidgetQueryPending(appQuery)) return <WidgetQueryLoadingState />;
|
||||||
if (!app) return <WidgetEmptyState />;
|
if (!app) return <WidgetEmptyState />;
|
||||||
|
|
||||||
const isTiny = height < 100 || width < 100;
|
const isTiny = height < 100 || width < 100;
|
||||||
const isColumnLayout = options.layout.startsWith("column");
|
const isColumnLayout = options.layout.startsWith("column");
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppLink
|
<Box h="100%" w="100%" pos="relative">
|
||||||
href={app.href ?? undefined}
|
<AppLink href={href} openInNewTab={options.openInNewTab} enabled={Boolean(href) && !isEditMode}>
|
||||||
openInNewTab={options.openInNewTab}
|
<Tooltip.Floating
|
||||||
enabled={Boolean(app.href) && !isEditMode}
|
label={app.description?.split("\n").map((line, index) => (
|
||||||
>
|
<Fragment key={index}>
|
||||||
<Tooltip.Floating
|
{line}
|
||||||
label={app.description?.split("\n").map((line, index) => (
|
<br />
|
||||||
<Fragment key={index}>
|
</Fragment>
|
||||||
{line}
|
))}
|
||||||
<br />
|
position="right-start"
|
||||||
</Fragment>
|
multiline
|
||||||
))}
|
disabled={options.descriptionDisplayMode !== "tooltip" || !app.description || isEditMode}
|
||||||
position="right-start"
|
styles={{ tooltip: { maxWidth: 300 } }}
|
||||||
multiline
|
|
||||||
disabled={options.descriptionDisplayMode !== "tooltip" || !app.description || isEditMode}
|
|
||||||
styles={{ tooltip: { maxWidth: 300 } }}
|
|
||||||
>
|
|
||||||
<Flex
|
|
||||||
p={isTiny ? 4 : "sm"}
|
|
||||||
className={combineClasses("app-flex-wrapper", app.name, app.id, app.href && classes.appWithUrl)}
|
|
||||||
h="100%"
|
|
||||||
w="100%"
|
|
||||||
direction={options.layout}
|
|
||||||
justify="center"
|
|
||||||
align="center"
|
|
||||||
gap={isColumnLayout ? 0 : "sm"}
|
|
||||||
onContextMenu={isEditMode ? (e) => e.preventDefault() : undefined}
|
|
||||||
>
|
>
|
||||||
<Stack gap={0}>
|
<Flex
|
||||||
{options.showTitle && (
|
p={isTiny ? 4 : "sm"}
|
||||||
<Text
|
className={combineClasses("app-flex-wrapper", app.name, app.id, href && classes.appWithUrl)}
|
||||||
className="app-title"
|
h="100%"
|
||||||
fw={700}
|
w="100%"
|
||||||
size={isTiny ? rem(8) : "sm"}
|
direction={options.layout}
|
||||||
ta={isColumnLayout ? "center" : undefined}
|
justify="center"
|
||||||
>
|
align="center"
|
||||||
{app.name}
|
gap={isColumnLayout ? 0 : "sm"}
|
||||||
</Text>
|
onContextMenu={isEditMode ? (e) => e.preventDefault() : undefined}
|
||||||
)}
|
>
|
||||||
{options.descriptionDisplayMode === "normal" && (
|
<Stack gap={0}>
|
||||||
<Text
|
{options.showTitle && (
|
||||||
className="app-description"
|
<Text
|
||||||
size={isTiny ? rem(8) : "sm"}
|
className="app-title"
|
||||||
ta={isColumnLayout ? "center" : undefined}
|
fw={700}
|
||||||
c="dimmed"
|
size={isTiny ? "xs" : "sm"}
|
||||||
lineClamp={Math.max(1, Math.floor((height - 48) / 18))}
|
ta={isColumnLayout ? "center" : undefined}
|
||||||
>
|
>
|
||||||
{app.description?.split("\n").map((line, index) => (
|
{app.name}
|
||||||
<Fragment key={index}>
|
</Text>
|
||||||
{line}
|
)}
|
||||||
<br />
|
{options.descriptionDisplayMode === "normal" && !isTiny && (
|
||||||
</Fragment>
|
<Text
|
||||||
))}
|
className="app-description"
|
||||||
</Text>
|
size="sm"
|
||||||
)}
|
ta={isColumnLayout ? "center" : undefined}
|
||||||
</Stack>
|
c="dimmed"
|
||||||
<MaskedOrNormalImage
|
lineClamp={Math.max(1, Math.floor((height - 48) / 18))}
|
||||||
imageUrl={app.iconUrl}
|
>
|
||||||
hasColor={board.iconColor !== null}
|
{app.description?.split("\n").map((line, index) => (
|
||||||
alt={app.name}
|
<Fragment key={index}>
|
||||||
className={combineClasses(classes.appIcon, "app-icon")}
|
{line}
|
||||||
style={{
|
<br />
|
||||||
height: "100%",
|
</Fragment>
|
||||||
width: "100%",
|
))}
|
||||||
minWidth: "20%",
|
</Text>
|
||||||
maxWidth: isColumnLayout ? undefined : isTiny ? "38%" : "50%",
|
)}
|
||||||
}}
|
</Stack>
|
||||||
/>
|
<MaskedOrNormalImage
|
||||||
</Flex>
|
imageUrl={app.iconUrl}
|
||||||
</Tooltip.Floating>
|
hasColor={board.iconColor !== null}
|
||||||
{options.pingEnabled && !settings.forceDisableStatus && !board.disableStatus && app.href ? (
|
alt={app.name}
|
||||||
<Suspense fallback={<PingDot icon={IconMinus} color="gray" tooltip={`${t("common.action.loading")}…`} />}>
|
className={combineClasses(classes.appIcon, "app-icon")}
|
||||||
<PingIndicator appId={app.id} />
|
style={{
|
||||||
</Suspense>
|
height: "100%",
|
||||||
) : null}
|
width: "100%",
|
||||||
</AppLink>
|
minWidth: "20%",
|
||||||
|
maxWidth: isColumnLayout ? undefined : isTiny ? "38%" : "50%",
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</Flex>
|
||||||
|
</Tooltip.Floating>
|
||||||
|
{options.pingEnabled && !settings.forceDisableStatus && !board.disableStatus && app.href ? (
|
||||||
|
<Suspense fallback={<PingDot icon={IconMinus} color="gray" tooltip={`${t("common.action.loading")}…`} />}>
|
||||||
|
<PingIndicator appId={app.id} />
|
||||||
|
</Suspense>
|
||||||
|
) : null}
|
||||||
|
</AppLink>
|
||||||
|
{appQuery.error && (
|
||||||
|
<Box pos="absolute" top={4} right={4}>
|
||||||
|
<WidgetQueryErrorIndicator error={appQuery.error} label={t("widget.app.name")} />
|
||||||
|
</Box>
|
||||||
|
)}
|
||||||
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +151,7 @@ const AppLink = ({ href, openInNewTab, enabled, children }: PropsWithChildren<Ap
|
|||||||
component="a"
|
component="a"
|
||||||
href={href}
|
href={href}
|
||||||
target={openInNewTab ? "_blank" : undefined}
|
target={openInNewTab ? "_blank" : undefined}
|
||||||
rel="noreferrer"
|
rel={openInNewTab ? SAFE_NEW_TAB_REL : undefined}
|
||||||
h="100%"
|
h="100%"
|
||||||
w="100%"
|
w="100%"
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -11,7 +11,9 @@ interface PingIndicatorProps {
|
|||||||
|
|
||||||
export const PingIndicator = ({ appId }: PingIndicatorProps) => {
|
export const PingIndicator = ({ appId }: PingIndicatorProps) => {
|
||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
const { data: pingResult } = clientApi.widget.app.ping.useQuery({ id: appId }, { refetchOnMount: false });
|
const { data: pingResult, error } = clientApi.widget.app.ping.useQuery({ id: appId }, { refetchOnMount: false });
|
||||||
|
|
||||||
|
if (error) return <PingDot icon={IconX} color="red" tooltip={t("common.error")} />;
|
||||||
|
|
||||||
if (!pingResult) return <PingDot icon={IconMinus} color="gray" tooltip={`${t("common.action.loading")}…`} />;
|
if (!pingResult) return <PingDot icon={IconMinus} color="gray" tooltip={`${t("common.action.loading")}…`} />;
|
||||||
|
|
||||||
|
|||||||
@@ -36,19 +36,14 @@ const iconSizeByWidth = [
|
|||||||
{ minWidth: 0, size: 16 },
|
{ minWidth: 0, size: 16 },
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export default function BazarrWidget({
|
export default function BazarrWidget({ integrationIds, options, width, height }: WidgetComponentProps<"bazarr">) {
|
||||||
integrationIds,
|
|
||||||
options,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
displayMode,
|
|
||||||
}: WidgetComponentProps<"bazarr">) {
|
|
||||||
const t = useScopedI18n("widget.bazarr");
|
const t = useScopedI18n("widget.bazarr");
|
||||||
const { data: badges } = clientApi.widget.bazarr.getBadges.useQuery(
|
const { data: badges, error } = clientApi.widget.bazarr.getBadges.useQuery(
|
||||||
{ integrationId: integrationIds[0] ?? "" },
|
{ integrationId: integrationIds[0] ?? "" },
|
||||||
{ enabled: Boolean(integrationIds[0]) },
|
{ enabled: Boolean(integrationIds[0]) },
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (error && !badges) throw error;
|
||||||
if (!badges) return <WidgetEmptyState />;
|
if (!badges) return <WidgetEmptyState />;
|
||||||
|
|
||||||
const statValues = {
|
const statValues = {
|
||||||
@@ -59,10 +54,10 @@ export default function BazarrWidget({
|
|||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
const visibleStatKeys = Object.entries(statVisibilityByOption)
|
const visibleStatKeys = Object.entries(statVisibilityByOption)
|
||||||
.filter(([optionKey]) => displayMode === "advanced" || options[optionKey as keyof typeof options])
|
.filter(([optionKey]) => options[optionKey as keyof typeof options])
|
||||||
.map(([, statKey]) => statKey);
|
.map(([, statKey]) => statKey);
|
||||||
|
|
||||||
const gridCols = getGridCols(width, height, visibleStatKeys.length, displayMode === "advanced");
|
const gridCols = getGridCols(width, height, visibleStatKeys.length);
|
||||||
const iconSize = getIconSize(Math.min(width, height));
|
const iconSize = getIconSize(Math.min(width, height));
|
||||||
|
|
||||||
if (visibleStatKeys.length === 0) {
|
if (visibleStatKeys.length === 0) {
|
||||||
@@ -100,14 +95,14 @@ export default function BazarrWidget({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getGridCols(width: number, height: number, itemCount: number, isAdvanced = false): number {
|
export function getGridCols(width: number, height: number, itemCount: number): number {
|
||||||
if (itemCount <= 1) return 1;
|
if (itemCount <= 1) return 1;
|
||||||
if (isAdvanced && width >= 640) return Math.min(itemCount, 4);
|
|
||||||
|
|
||||||
const widthMatch = gridColsByWidth.find(({ minWidth }) => width >= minWidth)?.cols ?? 1;
|
const preferredColumns = gridColsByWidth.find(({ minWidth }) => width >= minWidth)?.cols ?? 1;
|
||||||
const rowsAtWidth = Math.ceil(itemCount / widthMatch);
|
const maxColumnsByWidth = Math.max(1, Math.floor(width / 160));
|
||||||
const rowHeight = height / rowsAtWidth;
|
const maxRowsByHeight = Math.max(1, Math.floor(height / 72));
|
||||||
return rowHeight >= 72 ? widthMatch : Math.min(itemCount, Math.max(widthMatch, 2));
|
const columnsNeededToFit = Math.ceil(itemCount / maxRowsByHeight);
|
||||||
|
return Math.min(itemCount, maxColumnsByWidth, Math.max(preferredColumns, columnsNeededToFit));
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getIconSize(width: number): number {
|
export function getIconSize(width: number): number {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { createWidgetDefinition } from "../definition";
|
|||||||
import { optionsBuilder } from "../options";
|
import { optionsBuilder } from "../options";
|
||||||
|
|
||||||
export const { definition, componentLoader } = createWidgetDefinition("bazarr", {
|
export const { definition, componentLoader } = createWidgetDefinition("bazarr", {
|
||||||
|
supportsAdvancedFocus: false,
|
||||||
icon: IconBadgeCc,
|
icon: IconBadgeCc,
|
||||||
supportedIntegrations: ["bazarr"],
|
supportedIntegrations: ["bazarr"],
|
||||||
integrationsRequired: true,
|
integrationsRequired: true,
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ import { getGridCols, getIconSize } from "./component";
|
|||||||
|
|
||||||
describe("Bazarr responsive layout", () => {
|
describe("Bazarr responsive layout", () => {
|
||||||
test("uses two columns for a short widget so all stats remain visible", () => {
|
test("uses two columns for a short widget so all stats remain visible", () => {
|
||||||
expect(getGridCols(220, 100, 4)).toBe(2);
|
expect(getGridCols(220, 100, 4)).toBe(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("uses four columns in a wide advanced surface", () => {
|
test("uses four columns in a wide advanced surface", () => {
|
||||||
expect(getGridCols(800, 400, 4, true)).toBe(4);
|
expect(getGridCols(800, 400, 4)).toBe(2);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("sizes icons from the constrained dimension", () => {
|
test("sizes icons from the constrained dimension", () => {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import { useScopedI18n } from "@homarr/translation/client";
|
|||||||
|
|
||||||
import type { WidgetComponentProps } from "../definition";
|
import type { WidgetComponentProps } from "../definition";
|
||||||
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
||||||
|
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||||
|
|
||||||
const alertIconMap: Record<string, LucideIcon> = {
|
const alertIconMap: Record<string, LucideIcon> = {
|
||||||
CPU: Cpu,
|
CPU: Cpu,
|
||||||
@@ -56,7 +57,9 @@ export default function BeszelAlertsWidget({
|
|||||||
() => ({ integrationIds, includeHistory: options.showHistory, maxHistoryItems: options.maxHistoryItems }),
|
() => ({ integrationIds, includeHistory: options.showHistory, maxHistoryItems: options.maxHistoryItems }),
|
||||||
[integrationIds, options.showHistory, options.maxHistoryItems],
|
[integrationIds, options.showHistory, options.maxHistoryItems],
|
||||||
);
|
);
|
||||||
const { data: results = [], error: alertsError, isPending } = clientApi.widget.beszel.getAlerts.useQuery(alertsInput);
|
const alertsQuery = clientApi.widget.beszel.getAlerts.useQuery(alertsInput);
|
||||||
|
const results = getUsableWidgetQueryData(alertsQuery) ?? [];
|
||||||
|
const { isPending } = alertsQuery;
|
||||||
|
|
||||||
const systemNameMap = useMemo(() => {
|
const systemNameMap = useMemo(() => {
|
||||||
const map: Record<string, string> = {};
|
const map: Record<string, string> = {};
|
||||||
@@ -86,8 +89,6 @@ export default function BeszelAlertsWidget({
|
|||||||
const showHistory = options.showHistory && (displayMode === "advanced" || height >= 360);
|
const showHistory = options.showHistory && (displayMode === "advanced" || height >= 360);
|
||||||
const showAlertDescriptions = displayMode === "advanced" || height >= 190;
|
const showAlertDescriptions = displayMode === "advanced" || height >= 190;
|
||||||
|
|
||||||
if (alertsError) throw alertsError;
|
|
||||||
|
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
return (
|
return (
|
||||||
<Center h="100%">
|
<Center h="100%">
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ import {
|
|||||||
} from "../beszel/_shared/format";
|
} from "../beszel/_shared/format";
|
||||||
import { useBeszelFilteredSystems } from "../beszel/_shared/hooks";
|
import { useBeszelFilteredSystems } from "../beszel/_shared/hooks";
|
||||||
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
||||||
|
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||||
import { BeszelSystemStatsModal } from "../beszel/_shared/system-stats-modal";
|
import { BeszelSystemStatsModal } from "../beszel/_shared/system-stats-modal";
|
||||||
import { DiskUsage } from "../beszel/_shared/disk-usage";
|
import { DiskUsage } from "../beszel/_shared/disk-usage";
|
||||||
|
|
||||||
@@ -385,22 +386,17 @@ export default function BeszelSystemGridWidget({
|
|||||||
isEditMode,
|
isEditMode,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
displayMode = "compact",
|
|
||||||
}: WidgetComponentProps<"beszelSystemGrid">) {
|
}: WidgetComponentProps<"beszelSystemGrid">) {
|
||||||
const t = useScopedI18n("widget.beszelSystemGrid");
|
const t = useScopedI18n("widget.beszelSystemGrid");
|
||||||
const board = useRequiredBoard();
|
const board = useRequiredBoard();
|
||||||
const { openModal } = useModalAction(BeszelSystemStatsModal);
|
const { openModal } = useModalAction(BeszelSystemStatsModal);
|
||||||
|
|
||||||
const {
|
const systemsQuery = clientApi.widget.beszel.getSystems.useQuery({ integrationIds });
|
||||||
data: results = [],
|
const results = getUsableWidgetQueryData(systemsQuery) ?? [];
|
||||||
error: systemsError,
|
const { isPending } = systemsQuery;
|
||||||
isPending,
|
|
||||||
} = clientApi.widget.beszel.getSystems.useQuery({ integrationIds });
|
|
||||||
|
|
||||||
const filteredSystems = useBeszelFilteredSystems(results, options.statusFilter);
|
const filteredSystems = useBeszelFilteredSystems(results, options.statusFilter);
|
||||||
|
|
||||||
if (systemsError) throw systemsError;
|
|
||||||
|
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
return (
|
return (
|
||||||
<Center h="100%">
|
<Center h="100%">
|
||||||
@@ -425,20 +421,15 @@ export default function BeszelSystemGridWidget({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const advanced = displayMode === "advanced";
|
const cols = getColCount(width, height, filteredSystems.length);
|
||||||
const cols = advanced
|
|
||||||
? Math.min(filteredSystems.length, width >= 900 ? 2 : 1)
|
|
||||||
: getColCount(width, height, filteredSystems.length);
|
|
||||||
const rows = Math.ceil(filteredSystems.length / cols) || 1;
|
const rows = Math.ceil(filteredSystems.length / cols) || 1;
|
||||||
const rawCellHeight = height / rows;
|
const rawCellHeight = height / rows;
|
||||||
const minimumCellHeight = advanced ? 320 : MIN_CELL_HEIGHT;
|
const minimumCellHeight = MIN_CELL_HEIGHT;
|
||||||
const scrollEnabled = rawCellHeight < minimumCellHeight;
|
const scrollEnabled = rawCellHeight < minimumCellHeight;
|
||||||
const effectiveCellHeight = scrollEnabled ? minimumCellHeight : rawCellHeight;
|
const effectiveCellHeight = scrollEnabled ? minimumCellHeight : rawCellHeight;
|
||||||
const cellWidth = width / cols;
|
const cellWidth = width / cols;
|
||||||
const size = getSizeConfig(cellWidth, effectiveCellHeight);
|
const size = getSizeConfig(cellWidth, effectiveCellHeight);
|
||||||
const maxMetrics = advanced
|
const maxMetrics = getMaxVisibleMetrics(effectiveCellHeight, size);
|
||||||
? Math.min(metricRenderers.length, getMaxVisibleMetrics(effectiveCellHeight, size))
|
|
||||||
: getMaxVisibleMetrics(effectiveCellHeight, size);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box h="100%" pos="relative" style={{ pointerEvents: isEditMode ? "none" : undefined }}>
|
<Box h="100%" pos="relative" style={{ pointerEvents: isEditMode ? "none" : undefined }}>
|
||||||
|
|||||||
@@ -14,9 +14,10 @@ import { useScopedI18n } from "@homarr/translation/client";
|
|||||||
import classes from "./component.module.css";
|
import classes from "./component.module.css";
|
||||||
|
|
||||||
import type { WidgetComponentProps } from "../definition";
|
import type { WidgetComponentProps } from "../definition";
|
||||||
import { setWidgetRuntimeQueries } from "../definition";
|
import { useWidgetRuntimeQueries } from "../runtime-hooks";
|
||||||
import type { BeszelTimePeriod } from "../beszel/_shared/chart";
|
import type { BeszelTimePeriod } from "../beszel/_shared/chart";
|
||||||
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
||||||
|
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||||
import { BeszelStatsView } from "../beszel/_shared/stats-view";
|
import { BeszelStatsView } from "../beszel/_shared/stats-view";
|
||||||
import { createBeszelSystemChoices, resolveBeszelSystemChoice } from "./selection";
|
import { createBeszelSystemChoices, resolveBeszelSystemChoice } from "./selection";
|
||||||
|
|
||||||
@@ -25,12 +26,10 @@ export default function BeszelSystemStatsWidget({
|
|||||||
integrationIds,
|
integrationIds,
|
||||||
isEditMode,
|
isEditMode,
|
||||||
width,
|
width,
|
||||||
height,
|
|
||||||
displayMode = "compact",
|
|
||||||
boardId,
|
boardId,
|
||||||
itemId,
|
itemId,
|
||||||
setOptions,
|
setOptions,
|
||||||
widgetStateRef,
|
widgetRuntimeRef,
|
||||||
}: WidgetComponentProps<"beszelSystemStats">) {
|
}: WidgetComponentProps<"beszelSystemStats">) {
|
||||||
const t = useScopedI18n("widget.beszelSystemStats");
|
const t = useScopedI18n("widget.beszelSystemStats");
|
||||||
const board = useOptionalBoard();
|
const board = useOptionalBoard();
|
||||||
@@ -45,22 +44,26 @@ export default function BeszelSystemStatsWidget({
|
|||||||
message: t("error.selectionSaveMessage"),
|
message: t("error.selectionSaveMessage"),
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
const {
|
const systemsQuery = clientApi.widget.beszel.getSystems.useQuery({ integrationIds });
|
||||||
data: systemsResult = [],
|
const systemsResult = getUsableWidgetQueryData(systemsQuery) ?? [];
|
||||||
isPending: systemsPending,
|
const { isPending: systemsPending } = systemsQuery;
|
||||||
error: systemsError,
|
|
||||||
} = clientApi.widget.beszel.getSystems.useQuery({ integrationIds });
|
|
||||||
|
|
||||||
const systems = useMemo(() => createBeszelSystemChoices(systemsResult), [systemsResult]);
|
const systems = useMemo(() => createBeszelSystemChoices(systemsResult), [systemsResult]);
|
||||||
const selectedSystem = resolveBeszelSystemChoice(systems, options.systemId);
|
const selectedSystem = resolveBeszelSystemChoice(systems, options.systemId);
|
||||||
const selectedValue = selectedSystem?.value ?? "";
|
const selectedValue = selectedSystem?.value ?? "";
|
||||||
const selectedLabel = selectedSystem?.label;
|
const selectedLabel = selectedSystem?.label;
|
||||||
const systemExists = selectedSystem !== undefined;
|
const systemExists = selectedSystem !== undefined;
|
||||||
const includeDocker =
|
const includeDocker = options.showDockerCpu || options.showDockerMemory || options.showDockerNetwork;
|
||||||
(displayMode === "advanced" || height >= 380) &&
|
const periodOptions = [
|
||||||
(options.showDockerCpu || options.showDockerMemory || options.showDockerNetwork);
|
{ value: "1m", label: t("period.live") },
|
||||||
setWidgetRuntimeQueries(
|
{ value: "1h", label: t("period.oneHour") },
|
||||||
widgetStateRef,
|
{ value: "12h", label: t("period.twelveHours") },
|
||||||
|
{ value: "24h", label: t("period.oneDay") },
|
||||||
|
{ value: "1w", label: t("period.oneWeek") },
|
||||||
|
{ value: "30d", label: t("period.thirtyDays") },
|
||||||
|
];
|
||||||
|
useWidgetRuntimeQueries(
|
||||||
|
widgetRuntimeRef,
|
||||||
selectedSystem && options.timePeriod !== "1m"
|
selectedSystem && options.timePeriod !== "1m"
|
||||||
? [
|
? [
|
||||||
{
|
{
|
||||||
@@ -125,7 +128,6 @@ export default function BeszelSystemStatsWidget({
|
|||||||
[boardId, hasChangeAccess, itemId, options.timePeriod, saveItemOptions, setOptions],
|
[boardId, hasChangeAccess, itemId, options.timePeriod, saveItemOptions, setOptions],
|
||||||
);
|
);
|
||||||
|
|
||||||
if (systemsError) throw systemsError;
|
|
||||||
if (systemsPending)
|
if (systemsPending)
|
||||||
return (
|
return (
|
||||||
<Center h="100%">
|
<Center h="100%">
|
||||||
@@ -210,36 +212,39 @@ export default function BeszelSystemStatsWidget({
|
|||||||
</Menu.Dropdown>
|
</Menu.Dropdown>
|
||||||
</Menu>
|
</Menu>
|
||||||
)}
|
)}
|
||||||
{!isEditMode && displayMode === "advanced" && (
|
{!isEditMode &&
|
||||||
<SegmentedControl
|
(width >= 560 ? (
|
||||||
size="xs"
|
<SegmentedControl
|
||||||
value={options.timePeriod}
|
size="xs"
|
||||||
onChange={handleTimePeriod}
|
fullWidth
|
||||||
disabled={isSelectionSavePending}
|
value={options.timePeriod}
|
||||||
data={[
|
onChange={handleTimePeriod}
|
||||||
{ value: "1m", label: t("period.live") },
|
disabled={isSelectionSavePending}
|
||||||
{ value: "1h", label: t("period.oneHour") },
|
data={periodOptions}
|
||||||
{ value: "12h", label: t("period.twelveHours") },
|
/>
|
||||||
{ value: "24h", label: t("period.oneDay") },
|
) : (
|
||||||
{ value: "1w", label: t("period.oneWeek") },
|
<Select
|
||||||
{ value: "30d", label: t("period.thirtyDays") },
|
size="xs"
|
||||||
]}
|
value={options.timePeriod}
|
||||||
/>
|
onChange={(value) => value && handleTimePeriod(value)}
|
||||||
)}
|
disabled={isSelectionSavePending}
|
||||||
|
data={periodOptions}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
<BeszelStatsView
|
<BeszelStatsView
|
||||||
integrationIds={selectedSystem ? [selectedSystem.integrationId] : []}
|
integrationIds={selectedSystem ? [selectedSystem.integrationId] : []}
|
||||||
systemId={selectedSystem?.systemId ?? ""}
|
systemId={selectedSystem?.systemId ?? ""}
|
||||||
timePeriod={options.timePeriod as BeszelTimePeriod}
|
timePeriod={options.timePeriod as BeszelTimePeriod}
|
||||||
columns={displayMode === "advanced" || width > 600 ? 2 : 1}
|
columns={width > 600 ? 2 : 1}
|
||||||
visibility={{
|
visibility={{
|
||||||
cpu: options.showCpu,
|
cpu: options.showCpu,
|
||||||
memory: options.showMemory,
|
memory: options.showMemory,
|
||||||
disk: options.showDisk,
|
disk: options.showDisk,
|
||||||
diskIO: options.showDiskIO && (displayMode === "advanced" || height >= 260),
|
diskIO: options.showDiskIO,
|
||||||
network: options.showNetwork && (displayMode === "advanced" || height >= 260),
|
network: options.showNetwork,
|
||||||
dockerCpu: options.showDockerCpu && (displayMode === "advanced" || height >= 380),
|
dockerCpu: options.showDockerCpu,
|
||||||
dockerMemory: options.showDockerMemory && (displayMode === "advanced" || height >= 380),
|
dockerMemory: options.showDockerMemory,
|
||||||
dockerNetwork: options.showDockerNetwork && (displayMode === "advanced" || height >= 380),
|
dockerNetwork: options.showDockerNetwork,
|
||||||
}}
|
}}
|
||||||
onSwitchToHistorical={() => handleTimePeriod("1h")}
|
onSwitchToHistorical={() => handleTimePeriod("1h")}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ const timePeriodOptions = [
|
|||||||
];
|
];
|
||||||
|
|
||||||
export const { definition, componentLoader } = createWidgetDefinition("beszelSystemStats", {
|
export const { definition, componentLoader } = createWidgetDefinition("beszelSystemStats", {
|
||||||
|
supportsAdvancedFocus: false,
|
||||||
icon: IconChartAreaLine,
|
icon: IconChartAreaLine,
|
||||||
queryKeys: [[["widget", "beszel", "getSystems"]], [["widget", "beszel", "getSystemStats"]]],
|
queryKeys: [[["widget", "beszel", "getSystems"]], [["widget", "beszel", "getSystemStats"]]],
|
||||||
queryMatcher(query, scope) {
|
queryMatcher(query, scope) {
|
||||||
|
|||||||
@@ -8,33 +8,38 @@ import dayjs from "dayjs";
|
|||||||
|
|
||||||
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
|
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
|
||||||
|
|
||||||
|
import { formatLocalizedDate } from "../../common/locale";
|
||||||
|
|
||||||
export type BeszelTimePeriod = "1m" | "1h" | "12h" | "24h" | "1w" | "30d";
|
export type BeszelTimePeriod = "1m" | "1h" | "12h" | "24h" | "1w" | "30d";
|
||||||
|
|
||||||
const timeFormats: Record<BeszelTimePeriod, string> = {
|
const timeFormatOptions: Record<BeszelTimePeriod, Intl.DateTimeFormatOptions> = {
|
||||||
"1m": "HH:mm:ss",
|
"1m": { hour: "numeric", minute: "2-digit", second: "2-digit" },
|
||||||
"1h": "HH:mm",
|
"1h": { hour: "numeric", minute: "2-digit" },
|
||||||
"12h": "HH:mm",
|
"12h": { hour: "numeric", minute: "2-digit" },
|
||||||
"24h": "HH:mm",
|
"24h": { hour: "numeric", minute: "2-digit" },
|
||||||
"1w": "MMM D",
|
"1w": { month: "short", day: "numeric" },
|
||||||
"30d": "MMM D",
|
"30d": { month: "short", day: "numeric" },
|
||||||
};
|
};
|
||||||
|
|
||||||
const periodDays: Partial<Record<BeszelTimePeriod, number>> = { "1w": 7, "30d": 30 };
|
const periodDays: Partial<Record<BeszelTimePeriod, number>> = { "1w": 7, "30d": 30 };
|
||||||
|
|
||||||
function prepareRecords<T>(records: T[], timePeriod: BeszelTimePeriod) {
|
export const formatBeszelChartTimestamp = (timestamp: string, timePeriod: BeszelTimePeriod, locale: string): string =>
|
||||||
const fmt = (timestamp: string) => dayjs(timestamp).format(timeFormats[timePeriod]);
|
formatLocalizedDate(timestamp, locale, timeFormatOptions[timePeriod]);
|
||||||
|
|
||||||
|
function prepareRecords<T>(records: T[], timePeriod: BeszelTimePeriod, locale: string) {
|
||||||
|
const fmt = (timestamp: string) => formatBeszelChartTimestamp(timestamp, timePeriod, locale);
|
||||||
if (timePeriod === "1m") {
|
if (timePeriod === "1m") {
|
||||||
return { fmt, ordered: records };
|
return { fmt, ordered: records };
|
||||||
}
|
}
|
||||||
return { fmt, ordered: [...records].toReversed() };
|
return { fmt, ordered: [...records].toReversed() };
|
||||||
}
|
}
|
||||||
|
|
||||||
function padTimeGrid(data: Record<string, unknown>[], timePeriod: BeszelTimePeriod) {
|
function padTimeGrid(data: Record<string, unknown>[], timePeriod: BeszelTimePeriod, locale: string) {
|
||||||
if (timePeriod === "1m") return padLiveTimeGrid(data);
|
if (timePeriod === "1m") return padLiveTimeGrid(data, 60, locale);
|
||||||
const days = periodDays[timePeriod];
|
const days = periodDays[timePeriod];
|
||||||
if (!days) return data;
|
if (!days) return data;
|
||||||
|
|
||||||
const fmt = (ts: string) => dayjs(ts).format(timeFormats[timePeriod]);
|
const fmt = (timestamp: string) => formatBeszelChartTimestamp(timestamp, timePeriod, locale);
|
||||||
const now = dayjs();
|
const now = dayjs();
|
||||||
const existingDays = new Set(data.map((d) => dayjs(d.rawTime as string).format("YYYY-MM-DD")));
|
const existingDays = new Set(data.map((d) => dayjs(d.rawTime as string).format("YYYY-MM-DD")));
|
||||||
const result = [...data];
|
const result = [...data];
|
||||||
@@ -51,7 +56,7 @@ function padTimeGrid(data: Record<string, unknown>[], timePeriod: BeszelTimePeri
|
|||||||
return result.toSorted((a, b) => new Date(a.rawTime as string).getTime() - new Date(b.rawTime as string).getTime());
|
return result.toSorted((a, b) => new Date(a.rawTime as string).getTime() - new Date(b.rawTime as string).getTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function padLiveTimeGrid(data: Record<string, unknown>[], pointCount = 60) {
|
export function padLiveTimeGrid(data: Record<string, unknown>[], pointCount = 60, locale = "en-US") {
|
||||||
if (data.length === 0) return data;
|
if (data.length === 0) return data;
|
||||||
|
|
||||||
const end = dayjs(
|
const end = dayjs(
|
||||||
@@ -76,7 +81,7 @@ export function padLiveTimeGrid(data: Record<string, unknown>[], pointCount = 60
|
|||||||
const timestamp = end.subtract(pointCount - index - 1, "second");
|
const timestamp = end.subtract(pointCount - index - 1, "second");
|
||||||
return (
|
return (
|
||||||
pointsBySecond.get(timestamp.valueOf()) ?? {
|
pointsBySecond.get(timestamp.valueOf()) ?? {
|
||||||
time: timestamp.format(timeFormats["1m"]),
|
time: formatBeszelChartTimestamp(timestamp.toISOString(), "1m", locale),
|
||||||
rawTime: timestamp.toISOString(),
|
rawTime: timestamp.toISOString(),
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
@@ -162,17 +167,18 @@ export const useSystemChartData = (
|
|||||||
systemStats: BeszelSystemStatsRecord[] | undefined,
|
systemStats: BeszelSystemStatsRecord[] | undefined,
|
||||||
mapFn: (stats: BeszelSystemStatsRecord["stats"]) => Record<string, unknown>,
|
mapFn: (stats: BeszelSystemStatsRecord["stats"]) => Record<string, unknown>,
|
||||||
timePeriod: BeszelTimePeriod = "1h",
|
timePeriod: BeszelTimePeriod = "1h",
|
||||||
|
locale = "en-US",
|
||||||
) =>
|
) =>
|
||||||
useMemo(() => {
|
useMemo(() => {
|
||||||
if (!systemStats) return [];
|
if (!systemStats) return [];
|
||||||
const { fmt, ordered } = prepareRecords(systemStats, timePeriod);
|
const { fmt, ordered } = prepareRecords(systemStats, timePeriod, locale);
|
||||||
const mapped = ordered.map((r) => ({
|
const mapped = ordered.map((r) => ({
|
||||||
time: fmt(r.created),
|
time: fmt(r.created),
|
||||||
rawTime: r.created,
|
rawTime: r.created,
|
||||||
...mapFn(r.stats),
|
...mapFn(r.stats),
|
||||||
}));
|
}));
|
||||||
return padTimeGrid(mapped, timePeriod);
|
return padTimeGrid(mapped, timePeriod, locale);
|
||||||
}, [systemStats, mapFn, timePeriod]);
|
}, [systemStats, mapFn, timePeriod, locale]);
|
||||||
|
|
||||||
export const useContainerNames = (containerStats: BeszelContainerStatsRecord[] | undefined, max = 15) => {
|
export const useContainerNames = (containerStats: BeszelContainerStatsRecord[] | undefined, max = 15) => {
|
||||||
const prevRef = useRef<string[]>([]);
|
const prevRef = useRef<string[]>([]);
|
||||||
@@ -212,10 +218,11 @@ export const useDiskChartData = (
|
|||||||
efsPaths: string[],
|
efsPaths: string[],
|
||||||
rootSeriesName: string,
|
rootSeriesName: string,
|
||||||
timePeriod: BeszelTimePeriod = "1h",
|
timePeriod: BeszelTimePeriod = "1h",
|
||||||
|
locale = "en-US",
|
||||||
) =>
|
) =>
|
||||||
useMemo(
|
useMemo(
|
||||||
() => buildDiskChartData(systemStats, efsPaths, rootSeriesName, timePeriod),
|
() => buildDiskChartData(systemStats, efsPaths, rootSeriesName, timePeriod, locale),
|
||||||
[systemStats, efsPaths, rootSeriesName, timePeriod],
|
[systemStats, efsPaths, rootSeriesName, timePeriod, locale],
|
||||||
);
|
);
|
||||||
|
|
||||||
export const buildDiskChartData = (
|
export const buildDiskChartData = (
|
||||||
@@ -223,9 +230,10 @@ export const buildDiskChartData = (
|
|||||||
efsPaths: string[],
|
efsPaths: string[],
|
||||||
rootSeriesName: string,
|
rootSeriesName: string,
|
||||||
timePeriod: BeszelTimePeriod = "1h",
|
timePeriod: BeszelTimePeriod = "1h",
|
||||||
|
locale = "en-US",
|
||||||
) => {
|
) => {
|
||||||
if (!systemStats?.length) return [];
|
if (!systemStats?.length) return [];
|
||||||
const { fmt, ordered } = prepareRecords(systemStats, timePeriod);
|
const { fmt, ordered } = prepareRecords(systemStats, timePeriod, locale);
|
||||||
const mapped = ordered.map((record) => {
|
const mapped = ordered.map((record) => {
|
||||||
const point: Record<string, unknown> = {
|
const point: Record<string, unknown> = {
|
||||||
time: fmt(record.created),
|
time: fmt(record.created),
|
||||||
@@ -238,7 +246,7 @@ export const buildDiskChartData = (
|
|||||||
}
|
}
|
||||||
return point;
|
return point;
|
||||||
});
|
});
|
||||||
return padTimeGrid(mapped, timePeriod);
|
return padTimeGrid(mapped, timePeriod, locale);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useDockerChartData = (
|
export const useDockerChartData = (
|
||||||
@@ -246,12 +254,13 @@ export const useDockerChartData = (
|
|||||||
containerNames: string[],
|
containerNames: string[],
|
||||||
metric: "cpu" | "memory" | "network",
|
metric: "cpu" | "memory" | "network",
|
||||||
timePeriod: BeszelTimePeriod = "1h",
|
timePeriod: BeszelTimePeriod = "1h",
|
||||||
|
locale = "en-US",
|
||||||
) =>
|
) =>
|
||||||
useMemo(() => {
|
useMemo(() => {
|
||||||
if (!containerStats?.length) return [];
|
if (!containerStats?.length) return [];
|
||||||
const extract = defaultContainerExtractors[metric];
|
const extract = defaultContainerExtractors[metric];
|
||||||
if (!extract) return [];
|
if (!extract) return [];
|
||||||
const { fmt, ordered } = prepareRecords(containerStats, timePeriod);
|
const { fmt, ordered } = prepareRecords(containerStats, timePeriod, locale);
|
||||||
const mapped = ordered.map((record) => {
|
const mapped = ordered.map((record) => {
|
||||||
const point: Record<string, unknown> = { time: fmt(record.created), rawTime: record.created };
|
const point: Record<string, unknown> = { time: fmt(record.created), rawTime: record.created };
|
||||||
const byName = new Map(record.stats.map((c) => [c.n, c]));
|
const byName = new Map(record.stats.map((c) => [c.n, c]));
|
||||||
@@ -260,5 +269,5 @@ export const useDockerChartData = (
|
|||||||
}
|
}
|
||||||
return point;
|
return point;
|
||||||
});
|
});
|
||||||
return padTimeGrid(mapped, timePeriod);
|
return padTimeGrid(mapped, timePeriod, locale);
|
||||||
}, [containerStats, containerNames, metric, timePeriod]);
|
}, [containerStats, containerNames, metric, timePeriod, locale]);
|
||||||
@@ -6,8 +6,9 @@ import { IconPlugConnectedX, IconServerOff } from "@tabler/icons-react";
|
|||||||
|
|
||||||
import { clientApi } from "@homarr/api/client";
|
import { clientApi } from "@homarr/api/client";
|
||||||
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
|
import type { BeszelContainerStatsRecord, BeszelSystemStatsRecord } from "@homarr/integrations/types";
|
||||||
import { useScopedI18n } from "@homarr/translation/client";
|
import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
|
import { getUsableWidgetQueryData } from "../../common/query-state";
|
||||||
import { containerColors } from "./colors";
|
import { containerColors } from "./colors";
|
||||||
import type { BeszelTimePeriod } from "./chart";
|
import type { BeszelTimePeriod } from "./chart";
|
||||||
import {
|
import {
|
||||||
@@ -71,13 +72,15 @@ export function BeszelStatsView({
|
|||||||
onSwitchToHistorical,
|
onSwitchToHistorical,
|
||||||
}: BeszelStatsViewProps) {
|
}: BeszelStatsViewProps) {
|
||||||
const t = useScopedI18n("widget.beszelSystemStats");
|
const t = useScopedI18n("widget.beszelSystemStats");
|
||||||
|
const locale = useCurrentIntlLocale();
|
||||||
const showDocker = visibility.dockerCpu || visibility.dockerMemory || visibility.dockerNetwork;
|
const showDocker = visibility.dockerCpu || visibility.dockerMemory || visibility.dockerNetwork;
|
||||||
const isLive = timePeriod === "1m";
|
const isLive = timePeriod === "1m";
|
||||||
|
|
||||||
const { data: historicalData, error: historicalError } = clientApi.widget.beszel.getSystemStats.useQuery(
|
const historicalQuery = clientApi.widget.beszel.getSystemStats.useQuery(
|
||||||
{ integrationIds, systemId, timePeriod, includeDocker: showDocker },
|
{ integrationIds, systemId, timePeriod, includeDocker: showDocker },
|
||||||
{ refetchInterval: isLive ? false : 5_000, enabled: !isLive && systemId !== "" },
|
{ refetchInterval: isLive ? false : 5_000, enabled: !isLive && systemId !== "" },
|
||||||
);
|
);
|
||||||
|
const historicalData = isLive ? historicalQuery.data : getUsableWidgetQueryData(historicalQuery);
|
||||||
const { data: liveData, error: liveError } = useLiveStats(integrationIds, systemId, isLive && systemId !== "");
|
const { data: liveData, error: liveError } = useLiveStats(integrationIds, systemId, isLive && systemId !== "");
|
||||||
|
|
||||||
const activeStats:
|
const activeStats:
|
||||||
@@ -125,10 +128,25 @@ export function BeszelStatsView({
|
|||||||
|
|
||||||
const systemStats = activeStats?.systemStats;
|
const systemStats = activeStats?.systemStats;
|
||||||
const containerStats = activeStats?.containerStats;
|
const containerStats = activeStats?.containerStats;
|
||||||
const cpuData = useSystemChartData(whenVisible(visibility.cpu, systemStats), mappers.cpu, timePeriod);
|
const cpuData = useSystemChartData(whenVisible(visibility.cpu, systemStats), mappers.cpu, timePeriod, locale);
|
||||||
const memoryData = useSystemChartData(whenVisible(visibility.memory, systemStats), mappers.memory, timePeriod);
|
const memoryData = useSystemChartData(
|
||||||
const diskIOData = useSystemChartData(whenVisible(visibility.diskIO, systemStats), mappers.diskIO, timePeriod);
|
whenVisible(visibility.memory, systemStats),
|
||||||
const networkData = useSystemChartData(whenVisible(visibility.network, systemStats), mappers.network, timePeriod);
|
mappers.memory,
|
||||||
|
timePeriod,
|
||||||
|
locale,
|
||||||
|
);
|
||||||
|
const diskIOData = useSystemChartData(
|
||||||
|
whenVisible(visibility.diskIO, systemStats),
|
||||||
|
mappers.diskIO,
|
||||||
|
timePeriod,
|
||||||
|
locale,
|
||||||
|
);
|
||||||
|
const networkData = useSystemChartData(
|
||||||
|
whenVisible(visibility.network, systemStats),
|
||||||
|
mappers.network,
|
||||||
|
timePeriod,
|
||||||
|
locale,
|
||||||
|
);
|
||||||
|
|
||||||
const efsPaths = useMemo(() => {
|
const efsPaths = useMemo(() => {
|
||||||
const paths = new Set<string>();
|
const paths = new Set<string>();
|
||||||
@@ -138,7 +156,13 @@ export function BeszelStatsView({
|
|||||||
return [...paths];
|
return [...paths];
|
||||||
}, [systemStats]);
|
}, [systemStats]);
|
||||||
const rootSeriesName = t("chart.disk.series");
|
const rootSeriesName = t("chart.disk.series");
|
||||||
const diskData = useDiskChartData(whenVisible(visibility.disk, systemStats), efsPaths, rootSeriesName, timePeriod);
|
const diskData = useDiskChartData(
|
||||||
|
whenVisible(visibility.disk, systemStats),
|
||||||
|
efsPaths,
|
||||||
|
rootSeriesName,
|
||||||
|
timePeriod,
|
||||||
|
locale,
|
||||||
|
);
|
||||||
const diskSeries = useMemo(
|
const diskSeries = useMemo(
|
||||||
() => [
|
() => [
|
||||||
{ name: rootSeriesName, color: "grape.6" },
|
{ name: rootSeriesName, color: "grape.6" },
|
||||||
@@ -161,22 +185,23 @@ export function BeszelStatsView({
|
|||||||
containerNames,
|
containerNames,
|
||||||
"cpu",
|
"cpu",
|
||||||
timePeriod,
|
timePeriod,
|
||||||
|
locale,
|
||||||
);
|
);
|
||||||
const dockerMemoryData = useDockerChartData(
|
const dockerMemoryData = useDockerChartData(
|
||||||
whenVisible(visibility.dockerMemory, containerStats),
|
whenVisible(visibility.dockerMemory, containerStats),
|
||||||
containerNames,
|
containerNames,
|
||||||
"memory",
|
"memory",
|
||||||
timePeriod,
|
timePeriod,
|
||||||
|
locale,
|
||||||
);
|
);
|
||||||
const dockerNetworkData = useDockerChartData(
|
const dockerNetworkData = useDockerChartData(
|
||||||
whenVisible(visibility.dockerNetwork, containerStats),
|
whenVisible(visibility.dockerNetwork, containerStats),
|
||||||
containerNames,
|
containerNames,
|
||||||
"network",
|
"network",
|
||||||
timePeriod,
|
timePeriod,
|
||||||
|
locale,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (historicalError) throw historicalError;
|
|
||||||
|
|
||||||
if (isLive && liveError) {
|
if (isLive && liveError) {
|
||||||
return (
|
return (
|
||||||
<Center h={CHART_HEIGHT}>
|
<Center h={CHART_HEIGHT}>
|
||||||
@@ -198,7 +223,7 @@ export function BeszelStatsView({
|
|||||||
if (!activeStats) {
|
if (!activeStats) {
|
||||||
const visibleChartCount = Object.values(visibility).filter(Boolean).length;
|
const visibleChartCount = Object.values(visibility).filter(Boolean).length;
|
||||||
return (
|
return (
|
||||||
<SimpleGrid cols={columns} spacing="md" aria-label="Loading system statistics">
|
<SimpleGrid cols={columns} spacing="md" aria-label={t("name")}>
|
||||||
{Array.from({ length: visibleChartCount }, (_, index) => (
|
{Array.from({ length: visibleChartCount }, (_, index) => (
|
||||||
<ChartSkeleton key={index} />
|
<ChartSkeleton key={index} />
|
||||||
))}
|
))}
|
||||||
@@ -212,7 +237,7 @@ export function BeszelStatsView({
|
|||||||
<Stack align="center" gap="xs">
|
<Stack align="center" gap="xs">
|
||||||
<IconServerOff size={24} opacity={0.5} />
|
<IconServerOff size={24} opacity={0.5} />
|
||||||
<Text size="sm" c="dimmed">
|
<Text size="sm" c="dimmed">
|
||||||
{activeStats.error}
|
{t("error.internalServerError")}
|
||||||
</Text>
|
</Text>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Center>
|
</Center>
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ import { useRegisterSpotlightContextResults } from "@homarr/spotlight";
|
|||||||
import { MaskedOrNormalImage } from "@homarr/ui";
|
import { MaskedOrNormalImage } from "@homarr/ui";
|
||||||
|
|
||||||
import type { WidgetComponentProps } from "../definition";
|
import type { WidgetComponentProps } from "../definition";
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
|
||||||
|
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||||
import classes from "./bookmark.module.css";
|
import classes from "./bookmark.module.css";
|
||||||
|
|
||||||
type BookmarkLayout = WidgetComponentProps<"bookmarks">["options"]["layout"];
|
type BookmarkLayout = WidgetComponentProps<"bookmarks">["options"]["layout"];
|
||||||
@@ -64,30 +66,36 @@ export default function BookmarksWidget({
|
|||||||
displayMode,
|
displayMode,
|
||||||
}: WidgetComponentProps<"bookmarks">) {
|
}: WidgetComponentProps<"bookmarks">) {
|
||||||
const board = useRequiredBoard();
|
const board = useRequiredBoard();
|
||||||
const { data = [] } = clientApi.app.byIds.useQuery(options.items, {
|
const data =
|
||||||
select(apps) {
|
getUsableWidgetQueryData(
|
||||||
return apps.toSorted((appA, appB) => options.items.indexOf(appA.id) - options.items.indexOf(appB.id));
|
clientApi.app.byIds.useQuery(options.items, {
|
||||||
},
|
select(apps) {
|
||||||
});
|
return apps.toSorted((appA, appB) => options.items.indexOf(appA.id) - options.items.indexOf(appB.id));
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
) ?? [];
|
||||||
|
|
||||||
useRegisterSpotlightContextResults(
|
useRegisterSpotlightContextResults(
|
||||||
`bookmark-${itemId}`,
|
`bookmark-${itemId}`,
|
||||||
data
|
data.flatMap((app) => {
|
||||||
.filter((app) => app.href !== null)
|
const href = getSafeApplicationUrl(app.href);
|
||||||
.map((app) => ({
|
return href
|
||||||
id: app.id,
|
? [
|
||||||
name: app.name,
|
{
|
||||||
icon: app.iconUrl,
|
id: app.id,
|
||||||
interaction() {
|
name: app.name,
|
||||||
return {
|
icon: app.iconUrl,
|
||||||
type: "link",
|
interaction() {
|
||||||
// We checked above that app.href is defined
|
return {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
type: "link",
|
||||||
href: app.href!,
|
href,
|
||||||
newTab: false,
|
newTab: false,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
})),
|
},
|
||||||
|
]
|
||||||
|
: [];
|
||||||
|
}),
|
||||||
[data],
|
[data],
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -174,47 +182,50 @@ const FlexLayout = ({
|
|||||||
const board = useRequiredBoard();
|
const board = useRequiredBoard();
|
||||||
return (
|
return (
|
||||||
<Flex direction={direction} gap={4} w="100%" mih="100%" wrap="nowrap">
|
<Flex direction={direction} gap={4} w="100%" mih="100%" wrap="nowrap">
|
||||||
{data.map((app) => (
|
{data.map((app) => {
|
||||||
<div key={app.id} style={{ display: "flex", flex: `1 0 ${minimumItemSize}px`, flexDirection: direction }}>
|
const href = getSafeApplicationUrl(app.href);
|
||||||
<UnstyledButton
|
return (
|
||||||
className={classes.bookmarkButton}
|
<div key={app.id} style={{ display: "flex", flex: `1 0 ${minimumItemSize}px`, flexDirection: direction }}>
|
||||||
component="a"
|
<UnstyledButton
|
||||||
href={app.href ?? undefined}
|
className={classes.bookmarkButton}
|
||||||
target={openNewTab ? "_blank" : "_self"}
|
component={href ? "a" : "div"}
|
||||||
rel="noopener noreferrer"
|
href={href}
|
||||||
key={app.id}
|
target={href ? (openNewTab ? "_blank" : "_self") : undefined}
|
||||||
w="100%"
|
rel={href && openNewTab ? SAFE_NEW_TAB_REL : undefined}
|
||||||
>
|
key={app.id}
|
||||||
<Card
|
|
||||||
radius={board.itemRadius}
|
|
||||||
className={classes.card}
|
|
||||||
w="100%"
|
w="100%"
|
||||||
display="flex"
|
|
||||||
p={4}
|
|
||||||
h="100%"
|
|
||||||
withBorder={withBorder}
|
|
||||||
>
|
>
|
||||||
{direction === "row" ? (
|
<Card
|
||||||
<VerticalItem
|
radius={board.itemRadius}
|
||||||
app={app}
|
className={classes.card}
|
||||||
hideTitle={hideTitle}
|
w="100%"
|
||||||
hideIcon={hideIcon}
|
display="flex"
|
||||||
hideHostname={hideHostname}
|
p={4}
|
||||||
hasIconColor={hasIconColor}
|
h="100%"
|
||||||
/>
|
withBorder={withBorder}
|
||||||
) : (
|
>
|
||||||
<HorizontalItem
|
{direction === "row" ? (
|
||||||
app={app}
|
<VerticalItem
|
||||||
hideTitle={hideTitle}
|
app={app}
|
||||||
hideIcon={hideIcon}
|
hideTitle={hideTitle}
|
||||||
hideHostname={hideHostname}
|
hideIcon={hideIcon}
|
||||||
hasIconColor={hasIconColor}
|
hideHostname={hideHostname}
|
||||||
/>
|
hasIconColor={hasIconColor}
|
||||||
)}
|
/>
|
||||||
</Card>
|
) : (
|
||||||
</UnstyledButton>
|
<HorizontalItem
|
||||||
</div>
|
app={app}
|
||||||
))}
|
hideTitle={hideTitle}
|
||||||
|
hideIcon={hideIcon}
|
||||||
|
hideHostname={hideHostname}
|
||||||
|
hasIconColor={hasIconColor}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</UnstyledButton>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</Flex>
|
</Flex>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -231,12 +242,8 @@ export const getAdvancedBookmarkColumns = (width: number, itemCount: number): nu
|
|||||||
Math.max(1, Math.min(itemCount || 1, Math.floor(width / 280)));
|
Math.max(1, Math.min(itemCount || 1, Math.floor(width / 280)));
|
||||||
|
|
||||||
export const getBookmarkHostname = (href: string | null): string | undefined => {
|
export const getBookmarkHostname = (href: string | null): string | undefined => {
|
||||||
if (!href) return undefined;
|
const safeHref = getSafeApplicationUrl(href);
|
||||||
try {
|
return safeHref ? new URL(safeHref).hostname : undefined;
|
||||||
return new URL(href).hostname;
|
|
||||||
} catch {
|
|
||||||
return href;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const AdvancedBookmarksLayout = ({
|
const AdvancedBookmarksLayout = ({
|
||||||
@@ -251,41 +258,44 @@ const AdvancedBookmarksLayout = ({
|
|||||||
return (
|
return (
|
||||||
<ScrollArea h="100%" style={{ flex: 1 }}>
|
<ScrollArea h="100%" style={{ flex: 1 }}>
|
||||||
<SimpleGrid cols={getAdvancedBookmarkColumns(width, data.length)} spacing="sm">
|
<SimpleGrid cols={getAdvancedBookmarkColumns(width, data.length)} spacing="sm">
|
||||||
{data.map((app) => (
|
{data.map((app) => {
|
||||||
<UnstyledButton
|
const href = getSafeApplicationUrl(app.href);
|
||||||
key={app.id}
|
return (
|
||||||
className={classes.bookmarkButton}
|
<UnstyledButton
|
||||||
component="a"
|
key={app.id}
|
||||||
href={app.href ?? undefined}
|
className={classes.bookmarkButton}
|
||||||
target={openNewTab ? "_blank" : "_self"}
|
component={href ? "a" : "div"}
|
||||||
rel="noopener noreferrer"
|
href={href}
|
||||||
>
|
target={href ? (openNewTab ? "_blank" : "_self") : undefined}
|
||||||
<Card radius={board.itemRadius} className={classes.card} withBorder={withBorder} p="md" h="100%">
|
rel={href && openNewTab ? SAFE_NEW_TAB_REL : undefined}
|
||||||
<Group align="flex-start" wrap="nowrap">
|
>
|
||||||
<MaskedOrNormalImage
|
<Card radius={board.itemRadius} className={classes.card} withBorder={withBorder} p="md" h="100%">
|
||||||
imageUrl={app.iconUrl}
|
<Group align="flex-start" wrap="nowrap">
|
||||||
hasColor={hasIconColor}
|
<MaskedOrNormalImage
|
||||||
alt={app.name}
|
imageUrl={app.iconUrl}
|
||||||
className={classes.bookmarkIcon}
|
hasColor={hasIconColor}
|
||||||
style={{ width: 40, height: 40, flex: "0 0 auto" }}
|
alt={app.name}
|
||||||
/>
|
className={classes.bookmarkIcon}
|
||||||
<Stack gap={3} miw={0}>
|
style={{ width: 40, height: 40, flex: "0 0 auto" }}
|
||||||
<Text fw={700} size="sm" truncate="end">
|
/>
|
||||||
{app.name}
|
<Stack gap={3} miw={0}>
|
||||||
</Text>
|
<Text fw={700} size="sm" truncate="end">
|
||||||
<Anchor component="span" size="xs" truncate="end">
|
{app.name}
|
||||||
{getBookmarkHostname(app.href)}
|
|
||||||
</Anchor>
|
|
||||||
{app.description && (
|
|
||||||
<Text size="xs" c="dimmed" lineClamp={3}>
|
|
||||||
{app.description}
|
|
||||||
</Text>
|
</Text>
|
||||||
)}
|
<Anchor component="span" size="xs" truncate="end">
|
||||||
</Stack>
|
{getBookmarkHostname(app.href)}
|
||||||
</Group>
|
</Anchor>
|
||||||
</Card>
|
{app.description && (
|
||||||
</UnstyledButton>
|
<Text size="xs" c="dimmed" lineClamp={3}>
|
||||||
))}
|
{app.description}
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
|
</Group>
|
||||||
|
</Card>
|
||||||
|
</UnstyledButton>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</SimpleGrid>
|
</SimpleGrid>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
);
|
);
|
||||||
@@ -327,43 +337,46 @@ const GridLayout = ({
|
|||||||
mih="100%"
|
mih="100%"
|
||||||
style={{ gridAutoRows: `minmax(${minimumItemHeight}px, 1fr)` }}
|
style={{ gridAutoRows: `minmax(${minimumItemHeight}px, 1fr)` }}
|
||||||
>
|
>
|
||||||
{data.map((app) => (
|
{data.map((app) => {
|
||||||
<UnstyledButton
|
const href = getSafeApplicationUrl(app.href);
|
||||||
className={classes.bookmarkButton}
|
return (
|
||||||
component="a"
|
<UnstyledButton
|
||||||
href={app.href ?? undefined}
|
className={classes.bookmarkButton}
|
||||||
target={openNewTab ? "_blank" : "_self"}
|
component={href ? "a" : "div"}
|
||||||
rel="noopener noreferrer"
|
href={href}
|
||||||
key={app.id}
|
target={href ? (openNewTab ? "_blank" : "_self") : undefined}
|
||||||
h="100%"
|
rel={href && openNewTab ? SAFE_NEW_TAB_REL : undefined}
|
||||||
>
|
key={app.id}
|
||||||
<Card
|
|
||||||
h="100%"
|
h="100%"
|
||||||
className={combineClasses(classes.card, classes["card-grid"])}
|
|
||||||
radius={board.itemRadius}
|
|
||||||
withBorder={withBorder}
|
|
||||||
p="xs"
|
|
||||||
>
|
>
|
||||||
{itemDirection === "horizontal" ? (
|
<Card
|
||||||
<HorizontalItem
|
h="100%"
|
||||||
app={app}
|
className={combineClasses(classes.card, classes["card-grid"])}
|
||||||
hideTitle={hideTitle}
|
radius={board.itemRadius}
|
||||||
hideIcon={hideIcon}
|
withBorder={withBorder}
|
||||||
hideHostname={hideHostname}
|
p="xs"
|
||||||
hasIconColor={hasIconColor}
|
>
|
||||||
/>
|
{itemDirection === "horizontal" ? (
|
||||||
) : (
|
<HorizontalItem
|
||||||
<VerticalItem
|
app={app}
|
||||||
app={app}
|
hideTitle={hideTitle}
|
||||||
hideTitle={hideTitle}
|
hideIcon={hideIcon}
|
||||||
hideIcon={hideIcon}
|
hideHostname={hideHostname}
|
||||||
hideHostname={hideHostname}
|
hasIconColor={hasIconColor}
|
||||||
hasIconColor={hasIconColor}
|
/>
|
||||||
/>
|
) : (
|
||||||
)}
|
<VerticalItem
|
||||||
</Card>
|
app={app}
|
||||||
</UnstyledButton>
|
hideTitle={hideTitle}
|
||||||
))}
|
hideIcon={hideIcon}
|
||||||
|
hideHostname={hideHostname}
|
||||||
|
hasIconColor={hasIconColor}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</Card>
|
||||||
|
</UnstyledButton>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</SimpleGrid>
|
</SimpleGrid>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ describe("bookmark display helpers", () => {
|
|||||||
|
|
||||||
test("does not crash on malformed bookmark URLs", () => {
|
test("does not crash on malformed bookmark URLs", () => {
|
||||||
expect(getBookmarkHostname("https://homarr.dev/docs")).toBe("homarr.dev");
|
expect(getBookmarkHostname("https://homarr.dev/docs")).toBe("homarr.dev");
|
||||||
expect(getBookmarkHostname("not a URL")).toBe("not a URL");
|
expect(getBookmarkHostname("not a URL")).toBeUndefined();
|
||||||
expect(getBookmarkHostname(null)).toBeUndefined();
|
expect(getBookmarkHostname(null)).toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -17,9 +17,11 @@ import dayjs from "dayjs";
|
|||||||
|
|
||||||
import { isNullOrWhitespace } from "@homarr/common";
|
import { isNullOrWhitespace } from "@homarr/common";
|
||||||
import type { CalendarEvent } from "@homarr/integrations/types";
|
import type { CalendarEvent } from "@homarr/integrations/types";
|
||||||
import { useI18n } from "@homarr/translation/client";
|
import { useCurrentIntlLocale, useI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
import { groupEventsByDate } from "./calendar-events";
|
import { groupEventsByDate } from "./calendar-events";
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
|
||||||
|
import { formatLocalizedTime } from "../common/locale";
|
||||||
import classes from "./calendar-event-list.module.css";
|
import classes from "./calendar-event-list.module.css";
|
||||||
|
|
||||||
interface CalendarEventListProps {
|
interface CalendarEventListProps {
|
||||||
@@ -36,7 +38,9 @@ export const CalendarEventList = ({
|
|||||||
locale,
|
locale,
|
||||||
}: CalendarEventListProps) => {
|
}: CalendarEventListProps) => {
|
||||||
const headingIdPrefix = useId();
|
const headingIdPrefix = useId();
|
||||||
const dateFormatter = new Intl.DateTimeFormat(locale, {
|
const currentLocale = useCurrentIntlLocale();
|
||||||
|
const effectiveLocale = locale ?? currentLocale;
|
||||||
|
const dateFormatter = new Intl.DateTimeFormat(effectiveLocale, {
|
||||||
weekday: "long",
|
weekday: "long",
|
||||||
year: "numeric",
|
year: "numeric",
|
||||||
month: "long",
|
month: "long",
|
||||||
@@ -66,20 +70,20 @@ export const CalendarEventList = ({
|
|||||||
{dateFormatter.format(groupedEvents[0]?.startDate)}
|
{dateFormatter.format(groupedEvents[0]?.startDate)}
|
||||||
</Text>
|
</Text>
|
||||||
<Stack mt="xs">
|
<Stack mt="xs">
|
||||||
<CalendarEventRows events={groupedEvents} />
|
<CalendarEventRows events={groupedEvents} locale={effectiveLocale} />
|
||||||
</Stack>
|
</Stack>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
) : (
|
) : (
|
||||||
<CalendarEventRows events={events} />
|
<CalendarEventRows events={events} locale={effectiveLocale} />
|
||||||
)}
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const CalendarEventRows = ({ events }: Pick<CalendarEventListProps, "events">) => {
|
const CalendarEventRows = ({ events, locale }: Pick<CalendarEventListProps, "events"> & { locale: string }) => {
|
||||||
const { colorScheme } = useMantineColorScheme();
|
const { colorScheme } = useMantineColorScheme();
|
||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
return (
|
return (
|
||||||
@@ -136,14 +140,14 @@ const CalendarEventRows = ({ events }: Pick<CalendarEventListProps, "events">) =
|
|||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<Text c={"dimmed"} size={"sm"}>
|
<Text c={"dimmed"} size={"sm"}>
|
||||||
{dayjs(event.startDate).format("HH:mm")}
|
{formatLocalizedTime(event.startDate, locale)}
|
||||||
</Text>
|
</Text>
|
||||||
|
|
||||||
{event.endDate !== null && (
|
{event.endDate !== null && (
|
||||||
<>
|
<>
|
||||||
-{" "}
|
-{" "}
|
||||||
<Text c={"dimmed"} size={"sm"}>
|
<Text c={"dimmed"} size={"sm"}>
|
||||||
{dayjs(event.endDate).format("HH:mm")}
|
{formatLocalizedTime(event.endDate, locale)}
|
||||||
</Text>
|
</Text>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
@@ -171,31 +175,35 @@ const CalendarEventRows = ({ events }: Pick<CalendarEventListProps, "events">) =
|
|||||||
<Group pt={5} gap={5} mt={"auto"} wrap="nowrap">
|
<Group pt={5} gap={5} mt={"auto"} wrap="nowrap">
|
||||||
{event.links
|
{event.links
|
||||||
.filter((link) => link.href)
|
.filter((link) => link.href)
|
||||||
.map((link) => (
|
.map((link) => {
|
||||||
<Button
|
const href = getSafeApplicationUrl(link.href.toString());
|
||||||
key={link.href}
|
return (
|
||||||
component={"a"}
|
<Button
|
||||||
href={link.href.toString()}
|
key={link.href}
|
||||||
target={"_blank"}
|
component={href ? "a" : "div"}
|
||||||
size={"xs"}
|
href={href}
|
||||||
radius={"xl"}
|
target={href ? "_blank" : undefined}
|
||||||
variant={link.color ? undefined : "default"}
|
rel={href ? SAFE_NEW_TAB_REL : undefined}
|
||||||
styles={{
|
size={"xs"}
|
||||||
root: {
|
radius={"xl"}
|
||||||
backgroundColor: link.color,
|
variant={link.color ? undefined : "default"}
|
||||||
color: link.isDark && colorScheme === "dark" ? "white" : "black",
|
styles={{
|
||||||
"&:hover": link.color
|
root: {
|
||||||
? {
|
backgroundColor: link.color,
|
||||||
backgroundColor: link.isDark ? lighten(link.color, 0.1) : darken(link.color, 0.1),
|
color: link.isDark && colorScheme === "dark" ? "white" : "black",
|
||||||
}
|
"&:hover": link.color
|
||||||
: undefined,
|
? {
|
||||||
},
|
backgroundColor: link.isDark ? lighten(link.color, 0.1) : darken(link.color, 0.1),
|
||||||
}}
|
}
|
||||||
leftSection={link.logo ? <Image src={link.logo} fit="contain" w={20} h={20} /> : undefined}
|
: undefined,
|
||||||
>
|
},
|
||||||
<Text>{link.name}</Text>
|
}}
|
||||||
</Button>
|
leftSection={link.logo ? <Image src={link.logo} fit="contain" w={20} h={20} /> : undefined}
|
||||||
))}
|
>
|
||||||
|
<Text>{link.name}</Text>
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
})}
|
||||||
</Group>
|
</Group>
|
||||||
)}
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
import { getSafeApplicationUrl } from "./application-url";
|
||||||
|
|
||||||
|
describe("getSafeApplicationUrl", () => {
|
||||||
|
test.each(["javascript:alert(1)", "data:text/html,test", "not a URL", "/relative", "./relative"])(
|
||||||
|
"rejects unsafe or non-absolute URL %s",
|
||||||
|
(value) => expect(getSafeApplicationUrl(value)).toBeUndefined(),
|
||||||
|
);
|
||||||
|
|
||||||
|
test("rejects credential-bearing URLs", () => {
|
||||||
|
expect(getSafeApplicationUrl("https://user:password@example.com/path")).toBeUndefined();
|
||||||
|
expect(getSafeApplicationUrl("/path", { baseUrl: "https://user:password@example.com" })).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("allows absolute HTTP and HTTPS URLs", () => {
|
||||||
|
expect(getSafeApplicationUrl("http://example.com/path")).toBe("http://example.com/path");
|
||||||
|
expect(getSafeApplicationUrl("https://example.com/path?q=1#result")).toBe("https://example.com/path?q=1#result");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("resolves feed-relative URLs only against a safe HTTP(S) base", () => {
|
||||||
|
expect(getSafeApplicationUrl("/story", { baseUrl: "https://example.com/feed.xml?token=secret" })).toBe(
|
||||||
|
"https://example.com/story",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each([undefined, null, ""])("rejects missing URL %s", (value) => {
|
||||||
|
expect(getSafeApplicationUrl(value)).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "@homarr/common";
|
||||||
@@ -0,0 +1,35 @@
|
|||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
import { formatLocalizedCompactNumber, formatLocalizedDate, formatLocalizedTime, getBinaryStatusKey } from "./locale";
|
||||||
|
|
||||||
|
const sample = new Date("2026-01-02T13:05:09.000Z");
|
||||||
|
|
||||||
|
describe("localized widget formatting", () => {
|
||||||
|
test.each([
|
||||||
|
["en-US", /Jan/],
|
||||||
|
["fr-FR", /janv/i],
|
||||||
|
["de-DE", /Jan/],
|
||||||
|
["he-IL", /[\u0590-\u05ff]/],
|
||||||
|
])("formats dates for %s", (locale, expected) => {
|
||||||
|
expect(
|
||||||
|
formatLocalizedDate(sample, locale, { weekday: "short", month: "short", day: "numeric", timeZone: "UTC" }),
|
||||||
|
).toMatch(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("supports explicit 12-hour and 24-hour clocks", () => {
|
||||||
|
expect(formatLocalizedTime(sample, "en-US", { hour12: true, timeZone: "UTC" })).toBe("1:05 PM");
|
||||||
|
expect(formatLocalizedTime(sample, "en-US", { hour12: false, timeZone: "UTC" })).toBe("13:05");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("localizes compact numbers", () => {
|
||||||
|
expect(formatLocalizedCompactNumber(1_234_567, "en-US")).toBe("1.2M");
|
||||||
|
expect(formatLocalizedCompactNumber(1_234_567, "fr-FR")).toContain("1,2");
|
||||||
|
expect(formatLocalizedCompactNumber(1_234_567, "de-DE")).toContain("Mio.");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("normalizes binary status for translated accessibility labels", () => {
|
||||||
|
expect(getBinaryStatusKey("enabled")).toBe("enabled");
|
||||||
|
expect(getBinaryStatusKey("disabled")).toBe("disabled");
|
||||||
|
expect(getBinaryStatusKey()).toBe("unknown");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
export type LocalizedDateValue = Date | number | string;
|
||||||
|
|
||||||
|
interface LocalizedTimeOptions {
|
||||||
|
hour12?: boolean;
|
||||||
|
includeSeconds?: boolean;
|
||||||
|
timeZone?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const toValidDate = (value: LocalizedDateValue): Date | undefined => {
|
||||||
|
const date = value instanceof Date ? value : new Date(value);
|
||||||
|
return Number.isNaN(date.getTime()) ? undefined : date;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const formatLocalizedDate = (
|
||||||
|
value: LocalizedDateValue,
|
||||||
|
locale: string,
|
||||||
|
options: Intl.DateTimeFormatOptions,
|
||||||
|
fallback = "?",
|
||||||
|
): string => {
|
||||||
|
const date = toValidDate(value);
|
||||||
|
return date ? new Intl.DateTimeFormat(locale, options).format(date) : fallback;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const formatLocalizedTime = (
|
||||||
|
value: LocalizedDateValue,
|
||||||
|
locale: string,
|
||||||
|
{ hour12, includeSeconds = false, timeZone }: LocalizedTimeOptions = {},
|
||||||
|
fallback = "?",
|
||||||
|
): string =>
|
||||||
|
formatLocalizedDate(
|
||||||
|
value,
|
||||||
|
locale,
|
||||||
|
{
|
||||||
|
hour: "numeric",
|
||||||
|
minute: "2-digit",
|
||||||
|
second: includeSeconds ? "2-digit" : undefined,
|
||||||
|
hour12,
|
||||||
|
timeZone,
|
||||||
|
},
|
||||||
|
fallback,
|
||||||
|
);
|
||||||
|
|
||||||
|
export const formatLocalizedDateTime = (
|
||||||
|
value: LocalizedDateValue,
|
||||||
|
locale: string,
|
||||||
|
options: Pick<LocalizedTimeOptions, "hour12" | "timeZone"> = {},
|
||||||
|
fallback = "?",
|
||||||
|
): string =>
|
||||||
|
formatLocalizedDate(
|
||||||
|
value,
|
||||||
|
locale,
|
||||||
|
{ dateStyle: "medium", timeStyle: "short", hour12: options.hour12, timeZone: options.timeZone },
|
||||||
|
fallback,
|
||||||
|
);
|
||||||
|
|
||||||
|
export const formatLocalizedCompactNumber = (value: number, locale: string): string =>
|
||||||
|
new Intl.NumberFormat(locale, { notation: "compact" }).format(value);
|
||||||
|
|
||||||
|
export type BinaryStatusKey = "enabled" | "disabled" | "unknown";
|
||||||
|
|
||||||
|
export const getBinaryStatusKey = (status?: "enabled" | "disabled"): BinaryStatusKey => status ?? "unknown";
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { act, createElement } from "react";
|
||||||
|
import { createRoot } from "react-dom/client";
|
||||||
|
import { MantineProvider } from "@mantine/core";
|
||||||
|
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||||
|
|
||||||
|
import { WidgetQueryErrorIndicator } from "./query-state-indicator";
|
||||||
|
|
||||||
|
vi.mock("@homarr/translation/client", () => ({
|
||||||
|
useScopedI18n: () => (key: string) => key,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const matchMedia = (query: string) => ({
|
||||||
|
matches: false,
|
||||||
|
media: query,
|
||||||
|
onchange: null,
|
||||||
|
addListener: () => undefined,
|
||||||
|
removeListener: () => undefined,
|
||||||
|
addEventListener: () => undefined,
|
||||||
|
removeEventListener: () => undefined,
|
||||||
|
dispatchEvent: () => false,
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("WidgetQueryErrorIndicator", () => {
|
||||||
|
let host: HTMLDivElement | undefined;
|
||||||
|
let root: ReturnType<typeof createRoot> | undefined;
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
if (root) await act(async () => root?.unmount());
|
||||||
|
host?.remove();
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("uses a localized label without exposing the raw failure", async () => {
|
||||||
|
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
||||||
|
vi.stubGlobal("matchMedia", matchMedia);
|
||||||
|
host = document.createElement("div");
|
||||||
|
document.body.append(host);
|
||||||
|
root = createRoot(host);
|
||||||
|
const secret = "http://admin:password@private.local/path?token=secret";
|
||||||
|
|
||||||
|
await act(async () =>
|
||||||
|
root?.render(
|
||||||
|
createElement(
|
||||||
|
MantineProvider,
|
||||||
|
null,
|
||||||
|
createElement(WidgetQueryErrorIndicator, { error: new Error(secret), label: "Weather" }),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(host.querySelector("[aria-label='Weather']")).not.toBeNull();
|
||||||
|
expect(host.textContent).not.toMatch(/password|private\.local|token=secret/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { Box, Center, Loader, Tooltip } from "@mantine/core";
|
||||||
|
import { IconAlertTriangle } from "@tabler/icons-react";
|
||||||
|
|
||||||
|
interface WidgetQueryErrorIndicatorProps {
|
||||||
|
error: unknown;
|
||||||
|
label: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Localized warning for stale or optional-query failures. Never renders the raw error. */
|
||||||
|
export function WidgetQueryErrorIndicator({ error, label }: WidgetQueryErrorIndicatorProps) {
|
||||||
|
if (!error) return null;
|
||||||
|
|
||||||
|
const accessibleLabel = label;
|
||||||
|
return (
|
||||||
|
<Tooltip label={accessibleLabel} position="left" withArrow>
|
||||||
|
<Box
|
||||||
|
component="span"
|
||||||
|
c="orange"
|
||||||
|
role="img"
|
||||||
|
aria-label={accessibleLabel}
|
||||||
|
tabIndex={0}
|
||||||
|
style={{ display: "inline-flex", cursor: "help" }}
|
||||||
|
>
|
||||||
|
<IconAlertTriangle aria-hidden size={14} />
|
||||||
|
</Box>
|
||||||
|
</Tooltip>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function WidgetQueryLoadingState() {
|
||||||
|
return (
|
||||||
|
<Center h="100%" w="100%" p="sm" role="status" aria-live="polite">
|
||||||
|
<Loader size="sm" />
|
||||||
|
</Center>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
|
import { getUsableWidgetQueryData, hasStaleWidgetQueryError, isInitialWidgetQueryPending } from "./query-state";
|
||||||
|
|
||||||
|
describe("getUsableWidgetQueryData", () => {
|
||||||
|
test.each([[], null, 0, false, ""])("preserves successful empty value %#", (data) => {
|
||||||
|
expect(getUsableWidgetQueryData({ data, error: null })).toBe(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("preserves stale cached data when a refetch fails", () => {
|
||||||
|
const data = [{ id: "healthy" }];
|
||||||
|
expect(getUsableWidgetQueryData({ data, error: new Error("offline") })).toBe(data);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("throws a terminal failure when no usable data exists", () => {
|
||||||
|
const error = new Error("offline");
|
||||||
|
expect(() => getUsableWidgetQueryData({ data: undefined, error })).toThrow(error);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("keeps the initial pending state when neither data nor an error exists", () => {
|
||||||
|
expect(getUsableWidgetQueryData({ data: undefined, error: null })).toBeUndefined();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("isInitialWidgetQueryPending", () => {
|
||||||
|
test("only marks the unresolved first request as initial pending", () => {
|
||||||
|
expect(isInitialWidgetQueryPending({ data: undefined, error: null, isPending: true })).toBe(true);
|
||||||
|
expect(isInitialWidgetQueryPending({ data: [], error: null, isPending: true })).toBe(false);
|
||||||
|
expect(isInitialWidgetQueryPending({ data: undefined, error: new Error("offline"), isPending: true })).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("hasStaleWidgetQueryError", () => {
|
||||||
|
test("only marks failures that retain usable cached data", () => {
|
||||||
|
expect(hasStaleWidgetQueryError({ data: [{ id: "cached" }], error: new Error("offline") })).toBe(true);
|
||||||
|
expect(hasStaleWidgetQueryError({ data: undefined, error: new Error("offline") })).toBe(false);
|
||||||
|
expect(hasStaleWidgetQueryError({ data: [], error: null })).toBe(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
interface WidgetQueryState {
|
||||||
|
data: unknown;
|
||||||
|
error: unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PendingWidgetQueryState extends WidgetQueryState {
|
||||||
|
isPending: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Throws terminal query failures while preserving successful empty values and stale cached data. */
|
||||||
|
export const getUsableWidgetQueryData = <TQuery extends WidgetQueryState>(query: TQuery): TQuery["data"] => {
|
||||||
|
if (query.error && query.data === undefined) throw query.error;
|
||||||
|
return query.data;
|
||||||
|
};
|
||||||
|
|
||||||
|
/** True only before the first result; successful empty values and cached data are both ready to render. */
|
||||||
|
export const isInitialWidgetQueryPending = ({ data, error, isPending }: PendingWidgetQueryState): boolean =>
|
||||||
|
isPending && data === undefined && !error;
|
||||||
|
|
||||||
|
/** A failed background refresh still has usable cached data, but should remain visible to the user. */
|
||||||
|
export const hasStaleWidgetQueryError = ({ data, error }: WidgetQueryState): boolean =>
|
||||||
|
error !== null && error !== undefined && data !== undefined;
|
||||||
@@ -6,17 +6,12 @@ import { clientApi } from "@homarr/api/client";
|
|||||||
import { useScopedI18n } from "@homarr/translation/client";
|
import { useScopedI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
import type { WidgetComponentProps } from "../definition";
|
import type { WidgetComponentProps } from "../definition";
|
||||||
|
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||||
import { createWidgetKey } from "./coolify-utils";
|
import { createWidgetKey } from "./coolify-utils";
|
||||||
import { InstanceCard } from "./instance-card";
|
import { InstanceCard } from "./instance-card";
|
||||||
import { SingleInstanceLayout } from "./single-instance-layout";
|
import { SingleInstanceLayout } from "./single-instance-layout";
|
||||||
|
|
||||||
export default function CoolifyWidget({
|
export default function CoolifyWidget({ options, integrationIds, width, height }: WidgetComponentProps<"coolify">) {
|
||||||
options,
|
|
||||||
integrationIds,
|
|
||||||
width,
|
|
||||||
height,
|
|
||||||
displayMode,
|
|
||||||
}: WidgetComponentProps<"coolify">) {
|
|
||||||
const t = useScopedI18n("widget.coolify");
|
const t = useScopedI18n("widget.coolify");
|
||||||
|
|
||||||
if (integrationIds.length === 0) {
|
if (integrationIds.length === 0) {
|
||||||
@@ -27,15 +22,7 @@ export default function CoolifyWidget({
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return <CoolifyContent integrationIds={integrationIds} options={options} width={width} height={height} />;
|
||||||
<CoolifyContent
|
|
||||||
integrationIds={integrationIds}
|
|
||||||
options={options}
|
|
||||||
width={width}
|
|
||||||
height={height}
|
|
||||||
isAdvanced={displayMode === "advanced"}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CoolifyContentProps {
|
interface CoolifyContentProps {
|
||||||
@@ -43,17 +30,18 @@ interface CoolifyContentProps {
|
|||||||
options: WidgetComponentProps<"coolify">["options"];
|
options: WidgetComponentProps<"coolify">["options"];
|
||||||
width: number;
|
width: number;
|
||||||
height: number;
|
height: number;
|
||||||
isAdvanced: boolean;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function CoolifyContent({ integrationIds, options, width, height, isAdvanced }: CoolifyContentProps) {
|
function CoolifyContent({ integrationIds, options, width, height }: CoolifyContentProps) {
|
||||||
const t = useScopedI18n("common");
|
const t = useScopedI18n("common");
|
||||||
const { data: instancesData = [], isPending } = clientApi.widget.coolify.getInstancesInfo.useQuery({
|
const instancesQuery = clientApi.widget.coolify.getInstancesInfo.useQuery({
|
||||||
integrationIds,
|
integrationIds,
|
||||||
});
|
});
|
||||||
|
const instancesData = getUsableWidgetQueryData(instancesQuery) ?? [];
|
||||||
|
const { isPending } = instancesQuery;
|
||||||
|
|
||||||
const isTiny = !isAdvanced && (width < 256 || height < 144);
|
const isTiny = width < 256 || height < 144;
|
||||||
const hideFooter = !isAdvanced && height < 112;
|
const hideFooter = height < 112;
|
||||||
const [firstInstance] = instancesData;
|
const [firstInstance] = instancesData;
|
||||||
const widgetKey = createWidgetKey(integrationIds);
|
const widgetKey = createWidgetKey(integrationIds);
|
||||||
|
|
||||||
@@ -74,7 +62,6 @@ function CoolifyContent({ integrationIds, options, width, height, isAdvanced }:
|
|||||||
options={options}
|
options={options}
|
||||||
isTiny={isTiny}
|
isTiny={isTiny}
|
||||||
widgetKey={widgetKey}
|
widgetKey={widgetKey}
|
||||||
isAdvanced={isAdvanced}
|
|
||||||
hideFooter={hideFooter}
|
hideFooter={hideFooter}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
@@ -82,7 +69,7 @@ function CoolifyContent({ integrationIds, options, width, height, isAdvanced }:
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<ScrollArea h="100%">
|
<ScrollArea h="100%">
|
||||||
<SimpleGrid cols={isAdvanced && width >= 760 ? 2 : 1} spacing="sm" p="xs">
|
<SimpleGrid cols={width >= 760 ? 2 : 1} spacing="sm" p="xs">
|
||||||
{instancesData.map((instance) => (
|
{instancesData.map((instance) => (
|
||||||
<InstanceCard
|
<InstanceCard
|
||||||
key={instance.integrationId}
|
key={instance.integrationId}
|
||||||
@@ -90,7 +77,6 @@ function CoolifyContent({ integrationIds, options, width, height, isAdvanced }:
|
|||||||
options={options}
|
options={options}
|
||||||
isTiny={isTiny}
|
isTiny={isTiny}
|
||||||
widgetKey={widgetKey}
|
widgetKey={widgetKey}
|
||||||
isAdvanced={isAdvanced}
|
|
||||||
hideFooter={hideFooter}
|
hideFooter={hideFooter}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import { createWidgetDefinition } from "../definition";
|
|||||||
import { optionsBuilder } from "../options";
|
import { optionsBuilder } from "../options";
|
||||||
|
|
||||||
export const { definition, componentLoader } = createWidgetDefinition("coolify", {
|
export const { definition, componentLoader } = createWidgetDefinition("coolify", {
|
||||||
|
supportsAdvancedFocus: false,
|
||||||
icon: IconCloud,
|
icon: IconCloud,
|
||||||
createOptions() {
|
createOptions() {
|
||||||
return optionsBuilder.from((factory) => ({
|
return optionsBuilder.from((factory) => ({
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
import { useScopedI18n } from "@homarr/translation/client";
|
import { useScopedI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
import actionTargetClasses from "../common/action-target.module.css";
|
import actionTargetClasses from "../common/action-target.module.css";
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
|
||||||
import { cleanFqdn, getResourceTimestamp, getStatusColor, parseStatus } from "./coolify-utils";
|
import { cleanFqdn, getResourceTimestamp, getStatusColor, parseStatus } from "./coolify-utils";
|
||||||
|
|
||||||
interface ResourceRowProps {
|
interface ResourceRowProps {
|
||||||
@@ -59,13 +60,14 @@ export function ResourceRow({ item, baseUrl, isTiny, resourceType }: ResourceRow
|
|||||||
? t("status.restarting")
|
? t("status.restarting")
|
||||||
: t("status.unknown");
|
: t("status.unknown");
|
||||||
|
|
||||||
const resourceUrl =
|
const resourceUrl = getSafeApplicationUrl(
|
||||||
item.projectUuid && item.environmentUuid
|
item.projectUuid && item.environmentUuid
|
||||||
? `${baseUrl}/project/${item.projectUuid}/environment/${item.environmentUuid}/${resourceType}/${item.uuid}`
|
? `${baseUrl}/project/${item.projectUuid}/environment/${item.environmentUuid}/${resourceType}/${item.uuid}`
|
||||||
: undefined;
|
: undefined,
|
||||||
|
);
|
||||||
|
|
||||||
const logsUrl = resourceUrl ? `${resourceUrl}/logs` : undefined;
|
const logsUrl = getSafeApplicationUrl(resourceUrl ? `${resourceUrl}/logs` : undefined);
|
||||||
const publicUrl = cleanFqdn(item.fqdn);
|
const publicUrl = getSafeApplicationUrl(cleanFqdn(item.fqdn));
|
||||||
const resourceTimestamp = getResourceTimestamp(item, resourceType);
|
const resourceTimestamp = getResourceTimestamp(item, resourceType);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -77,7 +79,7 @@ export function ResourceRow({ item, baseUrl, isTiny, resourceType }: ResourceRow
|
|||||||
className={actionTargetClasses.root}
|
className={actionTargetClasses.root}
|
||||||
href={resourceUrl}
|
href={resourceUrl}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel={SAFE_NEW_TAB_REL}
|
||||||
fz="xs"
|
fz="xs"
|
||||||
c="inherit"
|
c="inherit"
|
||||||
truncate="end"
|
truncate="end"
|
||||||
@@ -98,7 +100,7 @@ export function ResourceRow({ item, baseUrl, isTiny, resourceType }: ResourceRow
|
|||||||
component="a"
|
component="a"
|
||||||
href={publicUrl}
|
href={publicUrl}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel={SAFE_NEW_TAB_REL}
|
||||||
aria-label={t("action.openResource", { name: item.name })}
|
aria-label={t("action.openResource", { name: item.name })}
|
||||||
size="xs"
|
size="xs"
|
||||||
variant="subtle"
|
variant="subtle"
|
||||||
@@ -113,7 +115,7 @@ export function ResourceRow({ item, baseUrl, isTiny, resourceType }: ResourceRow
|
|||||||
component="a"
|
component="a"
|
||||||
href={logsUrl}
|
href={logsUrl}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel={SAFE_NEW_TAB_REL}
|
||||||
aria-label={t("action.openLogs", { name: item.name })}
|
aria-label={t("action.openLogs", { name: item.name })}
|
||||||
size="xs"
|
size="xs"
|
||||||
variant="subtle"
|
variant="subtle"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import type { CoolifyServer } from "@homarr/integrations/types";
|
|||||||
import { useScopedI18n } from "@homarr/translation/client";
|
import { useScopedI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
import actionTargetClasses from "../common/action-target.module.css";
|
import actionTargetClasses from "../common/action-target.module.css";
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
|
||||||
import { getBadgeColor } from "./coolify-utils";
|
import { getBadgeColor } from "./coolify-utils";
|
||||||
|
|
||||||
interface ServersSectionProps {
|
interface ServersSectionProps {
|
||||||
@@ -92,7 +93,7 @@ function ServerRow({ server, counts, baseUrl, isTiny, showIp }: ServerRowProps)
|
|||||||
const t = useScopedI18n("widget.coolify");
|
const t = useScopedI18n("widget.coolify");
|
||||||
const isBuildServer = server.settings?.is_build_server === true;
|
const isBuildServer = server.settings?.is_build_server === true;
|
||||||
const isOnline = server.is_reachable !== false;
|
const isOnline = server.is_reachable !== false;
|
||||||
const serverUrl = `${baseUrl}/server/${server.uuid}`;
|
const serverUrl = getSafeApplicationUrl(`${baseUrl}/server/${server.uuid}`);
|
||||||
const StatusIcon = isOnline ? IconCircleCheckFilled : IconCircleXFilled;
|
const StatusIcon = isOnline ? IconCircleCheckFilled : IconCircleXFilled;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -105,9 +106,10 @@ function ServerRow({ server, counts, baseUrl, isTiny, showIp }: ServerRowProps)
|
|||||||
/>
|
/>
|
||||||
<Anchor
|
<Anchor
|
||||||
className={actionTargetClasses.root}
|
className={actionTargetClasses.root}
|
||||||
|
component={serverUrl ? "a" : "span"}
|
||||||
href={serverUrl}
|
href={serverUrl}
|
||||||
target="_blank"
|
target={serverUrl ? "_blank" : undefined}
|
||||||
rel="noopener noreferrer"
|
rel={serverUrl ? SAFE_NEW_TAB_REL : undefined}
|
||||||
fz="xs"
|
fz="xs"
|
||||||
c="inherit"
|
c="inherit"
|
||||||
truncate="end"
|
truncate="end"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useMemo, useRef, useState } from "react";
|
import { useState } from "react";
|
||||||
import { Accordion, Anchor, Group, Image, ScrollArea, Stack, Text } from "@mantine/core";
|
import { Accordion, Anchor, Group, Image, ScrollArea, Stack, Text } from "@mantine/core";
|
||||||
import { useLocalStorage } from "@mantine/hooks";
|
import { useLocalStorage } from "@mantine/hooks";
|
||||||
|
|
||||||
@@ -8,6 +8,7 @@ import { useTimeAgo } from "@homarr/common";
|
|||||||
import { useScopedI18n } from "@homarr/translation/client";
|
import { useScopedI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
import { ApplicationsSection } from "./applications-section";
|
import { ApplicationsSection } from "./applications-section";
|
||||||
|
import { getSafeApplicationUrl, SAFE_NEW_TAB_REL } from "../common/application-url";
|
||||||
import { buildServerResourceCounts } from "./coolify-utils";
|
import { buildServerResourceCounts } from "./coolify-utils";
|
||||||
import { ServersSection } from "./servers-section";
|
import { ServersSection } from "./servers-section";
|
||||||
import { ServicesSection } from "./services-section";
|
import { ServicesSection } from "./services-section";
|
||||||
@@ -18,8 +19,8 @@ interface SingleInstanceLayoutProps {
|
|||||||
instance: InstanceData;
|
instance: InstanceData;
|
||||||
options: CoolifyOptions;
|
options: CoolifyOptions;
|
||||||
isTiny: boolean;
|
isTiny: boolean;
|
||||||
widgetKey: string;
|
|
||||||
isAdvanced: boolean;
|
isAdvanced: boolean;
|
||||||
|
widgetKey: string;
|
||||||
hideFooter: boolean;
|
hideFooter: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,8 +28,8 @@ export function SingleInstanceLayout({
|
|||||||
instance,
|
instance,
|
||||||
options,
|
options,
|
||||||
isTiny,
|
isTiny,
|
||||||
widgetKey,
|
|
||||||
isAdvanced,
|
isAdvanced,
|
||||||
|
widgetKey,
|
||||||
hideFooter,
|
hideFooter,
|
||||||
}: SingleInstanceLayoutProps) {
|
}: SingleInstanceLayoutProps) {
|
||||||
const t = useScopedI18n("widget.coolify");
|
const t = useScopedI18n("widget.coolify");
|
||||||
@@ -40,36 +41,15 @@ export function SingleInstanceLayout({
|
|||||||
key: `coolify-sections-${widgetKey}`,
|
key: `coolify-sections-${widgetKey}`,
|
||||||
defaultValue: ["applications"],
|
defaultValue: ["applications"],
|
||||||
});
|
});
|
||||||
const visibleSections = useMemo(
|
const [advancedOpenSections, setAdvancedOpenSections] = useState(["servers", "applications", "services"]);
|
||||||
() =>
|
|
||||||
[
|
|
||||||
options.showServers ? "servers" : null,
|
|
||||||
options.showApplications ? "applications" : null,
|
|
||||||
options.showServices ? "services" : null,
|
|
||||||
].filter((section): section is string => section !== null),
|
|
||||||
[options.showApplications, options.showServers, options.showServices],
|
|
||||||
);
|
|
||||||
const [advancedOpenSections, setAdvancedOpenSections] = useState(visibleSections);
|
|
||||||
const previousVisibleSectionsRef = useRef(visibleSections);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const newlyVisibleSections = visibleSections.filter(
|
|
||||||
(section) => !previousVisibleSectionsRef.current.includes(section),
|
|
||||||
);
|
|
||||||
previousVisibleSectionsRef.current = visibleSections;
|
|
||||||
if (newlyVisibleSections.length === 0) return;
|
|
||||||
|
|
||||||
setAdvancedOpenSections((current) => [...new Set([...current, ...newlyVisibleSections])]);
|
|
||||||
}, [visibleSections]);
|
|
||||||
|
|
||||||
const serverResourceCounts = buildServerResourceCounts(
|
const serverResourceCounts = buildServerResourceCounts(
|
||||||
instance.instanceInfo.servers,
|
instance.instanceInfo.servers,
|
||||||
instance.instanceInfo.applications,
|
instance.instanceInfo.applications,
|
||||||
instance.instanceInfo.services,
|
instance.instanceInfo.services,
|
||||||
);
|
);
|
||||||
|
|
||||||
const baseUrl = instance.integrationUrl.replace(/\/+$/, "");
|
const baseUrl = getSafeApplicationUrl(instance.integrationUrl)?.replace(/\/+$/, "") ?? "";
|
||||||
const displayUrl = baseUrl.replace(/^https?:\/\//, "");
|
const displayUrl = baseUrl ? baseUrl.replace(/^https?:\/\//, "") : "—";
|
||||||
const relativeTime = useTimeAgo(instance.updatedAt);
|
const relativeTime = useTimeAgo(instance.updatedAt);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -82,17 +62,25 @@ export function SingleInstanceLayout({
|
|||||||
oolify
|
oolify
|
||||||
</Text>
|
</Text>
|
||||||
</Group>
|
</Group>
|
||||||
<Anchor
|
<Stack gap={0} miw={0}>
|
||||||
href={baseUrl}
|
{isAdvanced && (
|
||||||
target="_blank"
|
<Text fz="xs" fw={600} truncate="end">
|
||||||
rel="noopener noreferrer"
|
{instance.integrationName}
|
||||||
fz={isTiny ? "xs" : "sm"}
|
</Text>
|
||||||
fw={500}
|
)}
|
||||||
c="dimmed"
|
<Anchor
|
||||||
lineClamp={1}
|
component={baseUrl ? "a" : "span"}
|
||||||
>
|
href={baseUrl}
|
||||||
{displayUrl}
|
target={baseUrl ? "_blank" : undefined}
|
||||||
</Anchor>
|
rel={baseUrl ? SAFE_NEW_TAB_REL : undefined}
|
||||||
|
fz={isTiny ? "xs" : "sm"}
|
||||||
|
fw={500}
|
||||||
|
c="dimmed"
|
||||||
|
lineClamp={1}
|
||||||
|
>
|
||||||
|
{isAdvanced ? t("source.url", { url: displayUrl }) : displayUrl}
|
||||||
|
</Anchor>
|
||||||
|
</Stack>
|
||||||
</Group>
|
</Group>
|
||||||
|
|
||||||
<Accordion
|
<Accordion
|
||||||
@@ -108,6 +96,7 @@ export function SingleInstanceLayout({
|
|||||||
serverResourceCounts={serverResourceCounts}
|
serverResourceCounts={serverResourceCounts}
|
||||||
baseUrl={baseUrl}
|
baseUrl={baseUrl}
|
||||||
isTiny={isTiny}
|
isTiny={isTiny}
|
||||||
|
isAdvanced={isAdvanced}
|
||||||
showIp={showIp}
|
showIp={showIp}
|
||||||
onToggleIp={() => setShowIp((prev) => !prev)}
|
onToggleIp={() => setShowIp((prev) => !prev)}
|
||||||
/>
|
/>
|
||||||
@@ -116,7 +105,12 @@ export function SingleInstanceLayout({
|
|||||||
<ApplicationsSection applications={instance.instanceInfo.applications} baseUrl={baseUrl} isTiny={isTiny} />
|
<ApplicationsSection applications={instance.instanceInfo.applications} baseUrl={baseUrl} isTiny={isTiny} />
|
||||||
)}
|
)}
|
||||||
{options.showServices && (
|
{options.showServices && (
|
||||||
<ServicesSection services={instance.instanceInfo.services} baseUrl={baseUrl} isTiny={isTiny} />
|
<ServicesSection
|
||||||
|
services={instance.instanceInfo.services}
|
||||||
|
baseUrl={baseUrl}
|
||||||
|
isTiny={isTiny}
|
||||||
|
isAdvanced={isAdvanced}
|
||||||
|
/>
|
||||||
)}
|
)}
|
||||||
</Accordion>
|
</Accordion>
|
||||||
|
|
||||||
|
|||||||
@@ -415,7 +415,6 @@ describe("CustomJsxDisplay", () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Custom JSX component registry", () => {
|
describe("Custom JSX component registry", () => {
|
||||||
it.each([
|
it.each([
|
||||||
["@mantine/core", MantineCore],
|
["@mantine/core", MantineCore],
|
||||||
|
|||||||
@@ -77,7 +77,6 @@ export default function CustomJsxDisplay({ data }: { data: Record<string, unknow
|
|||||||
</WidgetDefinitionProvider>
|
</WidgetDefinitionProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sameStatus(left: CustomWidgetPublishedQueryState["status"], right: CustomWidgetPublishedQueryState["status"]) {
|
function sameStatus(left: CustomWidgetPublishedQueryState["status"], right: CustomWidgetPublishedQueryState["status"]) {
|
||||||
return (
|
return (
|
||||||
left.loading === right.loading &&
|
left.loading === right.loading &&
|
||||||
|
|||||||
@@ -1,28 +1,11 @@
|
|||||||
import { IconApi, IconPlayerPause } from "@tabler/icons-react";
|
import { IconApi } from "@tabler/icons-react";
|
||||||
import { z } from "zod/v4";
|
import { z } from "zod/v4";
|
||||||
|
|
||||||
import { createWidgetDefinition, widgetQueryInputMatches } from "../definition";
|
import { createWidgetDefinition } from "../definition";
|
||||||
import { optionsBuilder } from "../options";
|
import { optionsBuilder } from "../options";
|
||||||
|
|
||||||
export const { definition, componentLoader } = createWidgetDefinition("customApi", {
|
export const { definition, componentLoader } = createWidgetDefinition("customApi", {
|
||||||
icon: IconApi,
|
icon: IconApi,
|
||||||
queryKey: [["widget", "customApi", "getData"]],
|
|
||||||
queryMatcher: ({ input }, scope) => widgetQueryInputMatches(input, { definitionId: scope.options.definitionId }),
|
|
||||||
contextActions({ options, widgetStateRef }) {
|
|
||||||
return [
|
|
||||||
{
|
|
||||||
key: "togglePolling",
|
|
||||||
label: (t) => t("widget.customApi.actions.togglePolling"),
|
|
||||||
icon: IconPlayerPause,
|
|
||||||
hidden: typeof options.definitionId !== "string" || options.definitionId.trim() === "",
|
|
||||||
disabled: typeof widgetStateRef?.current?.togglePolling !== "function",
|
|
||||||
onClick: () => {
|
|
||||||
const action = widgetStateRef?.current?.togglePolling;
|
|
||||||
if (typeof action === "function") action();
|
|
||||||
},
|
|
||||||
},
|
|
||||||
];
|
|
||||||
},
|
|
||||||
createOptions() {
|
createOptions() {
|
||||||
return optionsBuilder.from((factory) => ({
|
return optionsBuilder.from((factory) => ({
|
||||||
definitionId: factory.customWidgetSelect({ defaultValue: "" }),
|
definitionId: factory.customWidgetSelect({ defaultValue: "" }),
|
||||||
|
|||||||
@@ -1,16 +1,16 @@
|
|||||||
import { describe, expect, test } from "vitest";
|
import { describe, expect, test } from "vitest";
|
||||||
|
|
||||||
import { definition as appDefinition } from "./app";
|
import { definition as appDefinition } from "./app";
|
||||||
|
import { definition as beszelAlertsDefinition } from "./beszel-alerts";
|
||||||
import { definition as beszelSystemGridDefinition } from "./beszel-system-grid";
|
import { definition as beszelSystemGridDefinition } from "./beszel-system-grid";
|
||||||
|
import { definition as beszelSystemStatsDefinition } from "./beszel-system-stats";
|
||||||
import { definition as beszelSystemTableDefinition } from "./beszel-system-table";
|
import { definition as beszelSystemTableDefinition } from "./beszel-system-table";
|
||||||
import {
|
import { definition as clockDefinition } from "./clock";
|
||||||
getWidgetQueryKeys,
|
import { definition as customApiDefinition } from "./custom-api";
|
||||||
getWidgetRuntimeQueries,
|
import { getWidgetQueryKeys, supportsAdvancedFocus } from "./definition";
|
||||||
setWidgetRuntimeQueries,
|
import { definition as dockerDefinition } from "./docker";
|
||||||
supportsAdvancedFocus,
|
import { definition as downloadsDefinition } from "./downloads";
|
||||||
} from "./definition";
|
import { definition as indexerManagerDefinition } from "./indexer-manager";
|
||||||
|
import { definition as notebookDefinition } from "./notebook";
|
||||||
const togglePolling = () => undefined;
|
|
||||||
|
|
||||||
describe("getWidgetQueryKeys", () => {
|
describe("getWidgetQueryKeys", () => {
|
||||||
test("falls back to the widget router namespace", () => {
|
test("falls back to the widget router namespace", () => {
|
||||||
@@ -29,23 +29,26 @@ describe("getWidgetQueryKeys", () => {
|
|||||||
|
|
||||||
expect(getWidgetQueryKeys({ kind: "audioStats", queryKeys })).toBe(queryKeys);
|
expect(getWidgetQueryKeys({ kind: "audioStats", queryKeys })).toBe(queryKeys);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("registers runtime queries without replacing imperative widget actions", () => {
|
|
||||||
const widgetStateRef = { current: { togglePolling } };
|
|
||||||
const runtimeQueries = [{ path: ["widget", "calendar", "findAllEvents"], input: { month: 7 } }];
|
|
||||||
|
|
||||||
setWidgetRuntimeQueries(widgetStateRef, runtimeQueries);
|
|
||||||
|
|
||||||
expect(widgetStateRef.current.togglePolling).toBe(togglePolling);
|
|
||||||
expect(getWidgetRuntimeQueries(widgetStateRef)).toEqual(runtimeQueries);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("supportsAdvancedFocus", () => {
|
describe("supportsAdvancedFocus", () => {
|
||||||
test.each([appDefinition, beszelSystemGridDefinition, beszelSystemTableDefinition])(
|
test.each([
|
||||||
"keeps non-enhanced widgets compact",
|
appDefinition,
|
||||||
(widgetDefinition) => {
|
beszelAlertsDefinition,
|
||||||
expect(supportsAdvancedFocus(widgetDefinition)).toBe(false);
|
beszelSystemGridDefinition,
|
||||||
},
|
beszelSystemStatsDefinition,
|
||||||
);
|
beszelSystemTableDefinition,
|
||||||
|
clockDefinition,
|
||||||
|
customApiDefinition,
|
||||||
|
dockerDefinition,
|
||||||
|
indexerManagerDefinition,
|
||||||
|
notebookDefinition,
|
||||||
|
])("keeps non-enhanced widgets compact", (widgetDefinition) => {
|
||||||
|
expect(supportsAdvancedFocus(widgetDefinition)).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("requires an explicit opt-in", () => {
|
||||||
|
expect(supportsAdvancedFocus({})).toBe(false);
|
||||||
|
expect(supportsAdvancedFocus(downloadsDefinition)).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -33,6 +33,7 @@ import { MaskedOrNormalImage } from "@homarr/ui";
|
|||||||
|
|
||||||
import type { widgetKind } from ".";
|
import type { widgetKind } from ".";
|
||||||
import type { WidgetComponentProps } from "../../definition";
|
import type { WidgetComponentProps } from "../../definition";
|
||||||
|
import { getUsableWidgetQueryData } from "../../common/query-state";
|
||||||
import actionTargetClasses from "../../common/action-target.module.css";
|
import actionTargetClasses from "../../common/action-target.module.css";
|
||||||
import classes from "./component.module.css";
|
import classes from "./component.module.css";
|
||||||
import TimerModal from "./TimerModal";
|
import TimerModal from "./TimerModal";
|
||||||
@@ -46,7 +47,6 @@ export default function DnsHoleControlsWidget({
|
|||||||
isEditMode,
|
isEditMode,
|
||||||
width,
|
width,
|
||||||
height,
|
height,
|
||||||
displayMode,
|
|
||||||
}: WidgetComponentProps<typeof widgetKind>) {
|
}: WidgetComponentProps<typeof widgetKind>) {
|
||||||
const board = useRequiredBoard();
|
const board = useRequiredBoard();
|
||||||
// DnsHole integrations with interaction permissions
|
// DnsHole integrations with interaction permissions
|
||||||
@@ -54,9 +54,9 @@ export default function DnsHoleControlsWidget({
|
|||||||
.map(({ id }) => id)
|
.map(({ id }) => id)
|
||||||
.filter((id) => integrationIds.includes(id));
|
.filter((id) => integrationIds.includes(id));
|
||||||
|
|
||||||
const { data: summaries = [], isPending: isSummaryPending } = clientApi.widget.dnsHole.summary.useQuery({
|
const summaryQuery = clientApi.widget.dnsHole.summary.useQuery({ integrationIds });
|
||||||
integrationIds,
|
const summaries = getUsableWidgetQueryData(summaryQuery) ?? [];
|
||||||
});
|
const { isPending: isSummaryPending } = summaryQuery;
|
||||||
const utils = clientApi.useUtils();
|
const utils = clientApi.useUtils();
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -118,8 +118,7 @@ export default function DnsHoleControlsWidget({
|
|||||||
const [bulkFailureCount, setBulkFailureCount] = useState(0);
|
const [bulkFailureCount, setBulkFailureCount] = useState(0);
|
||||||
const [opened, { close, open }] = useDisclosure(false);
|
const [opened, { close, open }] = useDisclosure(false);
|
||||||
|
|
||||||
const controlAllButtonsVisible =
|
const controlAllButtonsVisible = options.showToggleAllButtons && integrationsWithInteractions.length > 0;
|
||||||
(options.showToggleAllButtons || displayMode === "advanced") && integrationsWithInteractions.length > 0;
|
|
||||||
const actionsPending = bulkPending || isEnabling || isDisabling;
|
const actionsPending = bulkPending || isEnabling || isDisabling;
|
||||||
const actionError = enableError ?? disableError;
|
const actionError = enableError ?? disableError;
|
||||||
const runBulkToggle = async (integrationIdsToToggle: string[]) => {
|
const runBulkToggle = async (integrationIdsToToggle: string[]) => {
|
||||||
@@ -229,7 +228,6 @@ export default function DnsHoleControlsWidget({
|
|||||||
hasIconColor={board.iconColor !== null}
|
hasIconColor={board.iconColor !== null}
|
||||||
rootWidth={width}
|
rootWidth={width}
|
||||||
rootHeight={height}
|
rootHeight={height}
|
||||||
isAdvanced={displayMode === "advanced"}
|
|
||||||
actionsPending={actionsPending}
|
actionsPending={actionsPending}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
@@ -242,7 +240,7 @@ export default function DnsHoleControlsWidget({
|
|||||||
</Text>
|
</Text>
|
||||||
)}
|
)}
|
||||||
{actionError && (
|
{actionError && (
|
||||||
<Tooltip label={actionError.message} multiline>
|
<Tooltip label={t("widget.dnsHoleControls.error.internalServerError")}>
|
||||||
<Text size="xs" c="red" ta="center" lineClamp={2} tabIndex={0}>
|
<Text size="xs" c="red" ta="center" lineClamp={2} tabIndex={0}>
|
||||||
{t("widget.dnsHoleControls.error.internalServerError")}
|
{t("widget.dnsHoleControls.error.internalServerError")}
|
||||||
</Text>
|
</Text>
|
||||||
@@ -269,7 +267,6 @@ interface ControlsCardProps {
|
|||||||
hasIconColor: boolean;
|
hasIconColor: boolean;
|
||||||
rootWidth: number;
|
rootWidth: number;
|
||||||
rootHeight: number;
|
rootHeight: number;
|
||||||
isAdvanced: boolean;
|
|
||||||
actionsPending: boolean;
|
actionsPending: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,7 +280,6 @@ const ControlsCard: React.FC<ControlsCardProps> = ({
|
|||||||
hasIconColor,
|
hasIconColor,
|
||||||
rootWidth,
|
rootWidth,
|
||||||
rootHeight,
|
rootHeight,
|
||||||
isAdvanced,
|
|
||||||
actionsPending,
|
actionsPending,
|
||||||
}) => {
|
}) => {
|
||||||
const isConnected = useIntegrationConnected(data.integration.updatedAt, { timeout: 30000 });
|
const isConnected = useIntegrationConnected(data.integration.updatedAt, { timeout: 30000 });
|
||||||
@@ -294,7 +290,7 @@ const ControlsCard: React.FC<ControlsCardProps> = ({
|
|||||||
const board = useRequiredBoard();
|
const board = useRequiredBoard();
|
||||||
|
|
||||||
const iconUrl = integrationDefs[data.integration.kind].iconUrl;
|
const iconUrl = integrationDefs[data.integration.kind].iconUrl;
|
||||||
const layout = !isAdvanced && (rootWidth < 256 || rootHeight < 112) ? "sm" : "md";
|
const layout = rootWidth < 256 || rootHeight < 112 ? "sm" : "md";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Indicator
|
<Indicator
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { optionsBuilder } from "../../options";
|
|||||||
export const widgetKind = "dnsHoleControls";
|
export const widgetKind = "dnsHoleControls";
|
||||||
|
|
||||||
export const { definition, componentLoader } = createWidgetDefinition(widgetKind, {
|
export const { definition, componentLoader } = createWidgetDefinition(widgetKind, {
|
||||||
|
supportsAdvancedFocus: false,
|
||||||
icon: IconDeviceGamepad,
|
icon: IconDeviceGamepad,
|
||||||
queryKey: [["widget", "dnsHole"]],
|
queryKey: [["widget", "dnsHole"]],
|
||||||
refetchInterval: 5,
|
refetchInterval: 5,
|
||||||
|
|||||||
@@ -2,7 +2,19 @@
|
|||||||
|
|
||||||
import { useMemo } from "react";
|
import { useMemo } from "react";
|
||||||
import type { BoxProps } from "@mantine/core";
|
import type { BoxProps } from "@mantine/core";
|
||||||
import { Avatar, AvatarGroup, Badge, Card, Flex, Group, SimpleGrid, Stack, Text, Tooltip } from "@mantine/core";
|
import {
|
||||||
|
Avatar,
|
||||||
|
AvatarGroup,
|
||||||
|
Badge,
|
||||||
|
Card,
|
||||||
|
Flex,
|
||||||
|
Group,
|
||||||
|
ScrollArea,
|
||||||
|
SimpleGrid,
|
||||||
|
Stack,
|
||||||
|
Text,
|
||||||
|
Tooltip,
|
||||||
|
} from "@mantine/core";
|
||||||
import { useElementSize } from "@mantine/hooks";
|
import { useElementSize } from "@mantine/hooks";
|
||||||
import { IconBarrierBlock, IconPercentage, IconSearch, IconWorldWww } from "@tabler/icons-react";
|
import { IconBarrierBlock, IconPercentage, IconSearch, IconWorldWww } from "@tabler/icons-react";
|
||||||
|
|
||||||
@@ -18,20 +30,25 @@ import type { TablerIcon } from "@homarr/ui";
|
|||||||
|
|
||||||
import type { widgetKind } from ".";
|
import type { widgetKind } from ".";
|
||||||
import type { WidgetComponentProps, WidgetProps } from "../../definition";
|
import type { WidgetComponentProps, WidgetProps } from "../../definition";
|
||||||
|
import { getUsableWidgetQueryData } from "../../common/query-state";
|
||||||
|
|
||||||
export default function DnsHoleSummaryWidget({
|
export default function DnsHoleSummaryWidget({
|
||||||
options,
|
options,
|
||||||
integrationIds,
|
integrationIds,
|
||||||
displayMode,
|
width,
|
||||||
|
height,
|
||||||
}: WidgetComponentProps<typeof widgetKind>) {
|
}: WidgetComponentProps<typeof widgetKind>) {
|
||||||
const { data: summaries = [], isPending } = clientApi.widget.dnsHole.summary.useQuery({
|
const summaryQuery = clientApi.widget.dnsHole.summary.useQuery({
|
||||||
integrationIds,
|
integrationIds,
|
||||||
});
|
});
|
||||||
|
const summaries = getUsableWidgetQueryData(summaryQuery) ?? [];
|
||||||
|
const { isPending } = summaryQuery;
|
||||||
|
|
||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
|
|
||||||
const data = useMemo(() => summaries.flatMap(({ summary }) => summary), [summaries]);
|
const data = useMemo(() => summaries.flatMap(({ summary }) => summary), [summaries]);
|
||||||
const layoutProps = boxPropsByLayout(options.layout);
|
const layoutProps = boxPropsByLayout(options.layout);
|
||||||
|
const showSourceStatuses = summaries.length > 1 && width >= 240 && height >= 220;
|
||||||
|
|
||||||
if (isPending) {
|
if (isPending) {
|
||||||
return (
|
return (
|
||||||
@@ -74,18 +91,21 @@ export default function DnsHoleSummaryWidget({
|
|||||||
</Stack>
|
</Stack>
|
||||||
)}
|
)}
|
||||||
</SimpleGrid>
|
</SimpleGrid>
|
||||||
{displayMode === "advanced" && summaries.length > 0 && (
|
{showSourceStatuses && (
|
||||||
<Group px="xs" pb="xs" gap="xs">
|
<ScrollArea type="auto" scrollbarSize={4} px="xs" pb="xs">
|
||||||
{summaries.map(({ integration, summary }) => (
|
<Group gap="xs" wrap="nowrap">
|
||||||
<Badge
|
{summaries.map(({ integration, summary }) => (
|
||||||
key={integration.id}
|
<Badge
|
||||||
variant="light"
|
key={integration.id}
|
||||||
color={summary.status === "enabled" ? "green" : summary.status === "disabled" ? "red" : "gray"}
|
variant="light"
|
||||||
>
|
color={summary.status === "enabled" ? "green" : summary.status === "disabled" ? "red" : "gray"}
|
||||||
{integration.name}: {t(`widget.dnsHoleSummary.status.${summary.status ?? "unknown"}`)}
|
style={{ flexShrink: 0 }}
|
||||||
</Badge>
|
>
|
||||||
))}
|
{integration.name}: {t(`widget.dnsHoleSummary.status.${summary.status ?? "unknown"}` as never)}
|
||||||
</Group>
|
</Badge>
|
||||||
|
))}
|
||||||
|
</Group>
|
||||||
|
</ScrollArea>
|
||||||
)}
|
)}
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
@@ -174,7 +194,7 @@ const StatCard = ({ item, data, usePiHoleColors, t }: StatCardProps) => {
|
|||||||
className="summary-card"
|
className="summary-card"
|
||||||
p="sm"
|
p="sm"
|
||||||
radius={board.itemRadius}
|
radius={board.itemRadius}
|
||||||
bg={usePiHoleColors ? item.color : "rgba(96, 96, 96, 0.1)"}
|
bg={usePiHoleColors ? item.color : "var(--mantine-color-default-hover)"}
|
||||||
style={{
|
style={{
|
||||||
flex: 1,
|
flex: 1,
|
||||||
}}
|
}}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import { optionsBuilder } from "../../options";
|
|||||||
export const widgetKind = "dnsHoleSummary";
|
export const widgetKind = "dnsHoleSummary";
|
||||||
|
|
||||||
export const { definition, componentLoader } = createWidgetDefinition(widgetKind, {
|
export const { definition, componentLoader } = createWidgetDefinition(widgetKind, {
|
||||||
|
supportsAdvancedFocus: false,
|
||||||
icon: IconAd,
|
icon: IconAd,
|
||||||
queryKey: [["widget", "dnsHole"]],
|
queryKey: [["widget", "dnsHole"]],
|
||||||
refetchInterval: 5,
|
refetchInterval: 5,
|
||||||
|
|||||||
@@ -0,0 +1,61 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { act, createElement } from "react";
|
||||||
|
import { createRoot } from "react-dom/client";
|
||||||
|
import { MantineProvider } from "@mantine/core";
|
||||||
|
import { afterEach, describe, expect, test, vi } from "vitest";
|
||||||
|
|
||||||
|
import { WidgetError } from "./component";
|
||||||
|
|
||||||
|
vi.mock("@homarr/translation/client", () => ({
|
||||||
|
useI18n: () => (key: string) => key,
|
||||||
|
}));
|
||||||
|
|
||||||
|
const matchMedia = (query: string) => ({
|
||||||
|
matches: false,
|
||||||
|
media: query,
|
||||||
|
onchange: null,
|
||||||
|
addListener: () => undefined,
|
||||||
|
removeListener: () => undefined,
|
||||||
|
addEventListener: () => undefined,
|
||||||
|
removeEventListener: () => undefined,
|
||||||
|
dispatchEvent: () => false,
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("WidgetError", () => {
|
||||||
|
let host: HTMLDivElement | undefined;
|
||||||
|
let root: ReturnType<typeof createRoot> | undefined;
|
||||||
|
|
||||||
|
afterEach(async () => {
|
||||||
|
if (root) await act(async () => root?.unmount());
|
||||||
|
host?.remove();
|
||||||
|
vi.unstubAllGlobals();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("never renders an unmapped backend error", async () => {
|
||||||
|
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
|
||||||
|
vi.stubGlobal("matchMedia", matchMedia);
|
||||||
|
host = document.createElement("div");
|
||||||
|
document.body.append(host);
|
||||||
|
root = createRoot(host);
|
||||||
|
const secret = "http://admin:password@private.local/path?token=secret";
|
||||||
|
|
||||||
|
await act(async () =>
|
||||||
|
root?.render(
|
||||||
|
createElement(
|
||||||
|
MantineProvider,
|
||||||
|
null,
|
||||||
|
createElement(WidgetError, {
|
||||||
|
kind: "app",
|
||||||
|
error: new Error(secret),
|
||||||
|
resetErrorBoundary: vi.fn(),
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(host.textContent).toContain("common.error");
|
||||||
|
expect(host.textContent).not.toContain(secret);
|
||||||
|
expect(host.textContent).not.toMatch(/password|private\.local|token=secret/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -28,7 +28,14 @@ export const WidgetError = ({ error, resetErrorBoundary, definition }: WidgetErr
|
|||||||
return <BaseWidgetError {...trpcErrorData} onRetry={resetErrorBoundary} />;
|
return <BaseWidgetError {...trpcErrorData} onRetry={resetErrorBoundary} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return <BaseWidgetError icon={IconExclamationCircle} message={String(error)} onRetry={resetErrorBoundary} />;
|
return (
|
||||||
|
<BaseWidgetError
|
||||||
|
icon={IconExclamationCircle}
|
||||||
|
message={(t) => t("common.error")}
|
||||||
|
showLogsLink
|
||||||
|
onRetry={resetErrorBoundary}
|
||||||
|
/>
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleWidgetTrpcError = (
|
const handleWidgetTrpcError = (
|
||||||
@@ -60,7 +67,7 @@ const handleCommonTrpcError = (error: unknown): Omit<BaseWidgetErrorProps, "onRe
|
|||||||
if (errorData.code === "UNAUTHORIZED" || errorData.code === "FORBIDDEN") {
|
if (errorData.code === "UNAUTHORIZED" || errorData.code === "FORBIDDEN") {
|
||||||
return {
|
return {
|
||||||
icon: IconShield,
|
icon: IconShield,
|
||||||
message: "You don't have permission to access this widget",
|
message: (t) => t("integration.testConnection.error.certificate.alert.permission.title"),
|
||||||
showLogsLink: false,
|
showLogsLink: false,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,7 +44,8 @@ export const hasTotalFirewallFailure = (queries: readonly FirewallQueryState[])
|
|||||||
queries.every((query) => !query.isPending && !query.isFetching) &&
|
queries.every((query) => !query.isPending && !query.isFetching) &&
|
||||||
queries.every(
|
queries.every(
|
||||||
(query) =>
|
(query) =>
|
||||||
query.isError || (query.data !== undefined && query.data.length > 0 && query.data.every(({ error }) => error)),
|
(query.isError && query.data === undefined) ||
|
||||||
|
(query.data !== undefined && query.data.length > 0 && query.data.every(({ error }) => error)),
|
||||||
);
|
);
|
||||||
|
|
||||||
export default function FirewallWidget({
|
export default function FirewallWidget({
|
||||||
@@ -58,7 +59,7 @@ export default function FirewallWidget({
|
|||||||
const isAdvanced = displayMode === "advanced";
|
const isAdvanced = displayMode === "advanced";
|
||||||
const isTiny = !isAdvanced && (width < 256 || height < 180);
|
const isTiny = !isAdvanced && (width < 256 || height < 180);
|
||||||
const ringSize = isAdvanced ? 100 : height < 120 ? 44 : isTiny ? 64 : 100;
|
const ringSize = isAdvanced ? 100 : height < 120 ? 44 : isTiny ? 64 : 100;
|
||||||
const showInterfaces = isAdvanced || height >= 120;
|
const showInterfaces = isAdvanced || (!isTiny && height >= 120);
|
||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
|
|
||||||
const handleSelect = useCallback((value: string | null) => {
|
const handleSelect = useCallback((value: string | null) => {
|
||||||
@@ -159,7 +160,6 @@ export default function FirewallWidget({
|
|||||||
interfacesError={interfacesQuery.isError || Boolean(interfaces?.error)}
|
interfacesError={interfacesQuery.isError || Boolean(interfaces?.error)}
|
||||||
hasError={hasError}
|
hasError={hasError}
|
||||||
isAdvanced={isAdvanced}
|
isAdvanced={isAdvanced}
|
||||||
isTiny={isTiny}
|
|
||||||
ringSize={ringSize}
|
ringSize={ringSize}
|
||||||
showInterfaces={showInterfaces}
|
showInterfaces={showInterfaces}
|
||||||
accordionValue={accordionValue}
|
accordionValue={accordionValue}
|
||||||
@@ -190,7 +190,6 @@ interface FirewallPanelProps {
|
|||||||
interfacesError: boolean;
|
interfacesError: boolean;
|
||||||
hasError: boolean;
|
hasError: boolean;
|
||||||
isAdvanced: boolean;
|
isAdvanced: boolean;
|
||||||
isTiny: boolean;
|
|
||||||
ringSize: number;
|
ringSize: number;
|
||||||
showInterfaces: boolean;
|
showInterfaces: boolean;
|
||||||
accordionValue: string | null;
|
accordionValue: string | null;
|
||||||
@@ -214,7 +213,6 @@ const FirewallPanel = ({
|
|||||||
interfacesError,
|
interfacesError,
|
||||||
hasError,
|
hasError,
|
||||||
isAdvanced,
|
isAdvanced,
|
||||||
isTiny,
|
|
||||||
ringSize,
|
ringSize,
|
||||||
showInterfaces,
|
showInterfaces,
|
||||||
accordionValue,
|
accordionValue,
|
||||||
@@ -276,7 +274,6 @@ const FirewallPanel = ({
|
|||||||
hasResult={interfacesLoaded}
|
hasResult={interfacesLoaded}
|
||||||
hasError={interfacesError}
|
hasError={interfacesError}
|
||||||
isAdvanced={isAdvanced}
|
isAdvanced={isAdvanced}
|
||||||
isTiny={isTiny}
|
|
||||||
accordionValue={accordionValue}
|
accordionValue={accordionValue}
|
||||||
setAccordionValue={setAccordionValue}
|
setAccordionValue={setAccordionValue}
|
||||||
errorLabel={errorLabel}
|
errorLabel={errorLabel}
|
||||||
@@ -302,6 +299,8 @@ const MetricRing = ({ value, icon: Icon, size, label, t }: MetricRingProps) => {
|
|||||||
const safeValue = Math.max(0, Math.min(100, Number.isFinite(value) ? value : 0));
|
const safeValue = Math.max(0, Math.min(100, Number.isFinite(value) ? value : 0));
|
||||||
const status = getMetricStatus(safeValue);
|
const status = getMetricStatus(safeValue);
|
||||||
const statusLabel = t(`widget.firewall.status.${status}`);
|
const statusLabel = t(`widget.firewall.status.${status}`);
|
||||||
|
const showIcon = size >= 64;
|
||||||
|
const showStatus = size >= 96;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RingProgress
|
<RingProgress
|
||||||
@@ -315,9 +314,9 @@ const MetricRing = ({ value, icon: Icon, size, label, t }: MetricRingProps) => {
|
|||||||
thickness={size < 72 ? 4 : 8}
|
thickness={size < 72 ? 4 : 8}
|
||||||
label={
|
label={
|
||||||
<Center style={{ flexDirection: "column" }}>
|
<Center style={{ flexDirection: "column" }}>
|
||||||
<Text size={size < 72 ? "8px" : "xs"}>{safeValue.toFixed(1)}%</Text>
|
<Text size="xs">{safeValue.toFixed(1)}%</Text>
|
||||||
<Icon size={size < 72 ? 8 : 16} />
|
{showIcon && <Icon size={size < 96 ? 12 : 16} />}
|
||||||
{size >= 72 && <Text size="8px">{statusLabel}</Text>}
|
{showStatus && <Text size="xs">{statusLabel}</Text>}
|
||||||
</Center>
|
</Center>
|
||||||
}
|
}
|
||||||
sections={[
|
sections={[
|
||||||
@@ -341,7 +340,6 @@ interface InterfacesPanelProps {
|
|||||||
hasResult: boolean;
|
hasResult: boolean;
|
||||||
hasError: boolean;
|
hasError: boolean;
|
||||||
isAdvanced: boolean;
|
isAdvanced: boolean;
|
||||||
isTiny: boolean;
|
|
||||||
accordionValue: string | null;
|
accordionValue: string | null;
|
||||||
setAccordionValue: (value: string | null) => void;
|
setAccordionValue: (value: string | null) => void;
|
||||||
errorLabel: string;
|
errorLabel: string;
|
||||||
@@ -356,7 +354,6 @@ const InterfacesPanel = ({
|
|||||||
hasResult,
|
hasResult,
|
||||||
hasError,
|
hasError,
|
||||||
isAdvanced,
|
isAdvanced,
|
||||||
isTiny,
|
|
||||||
accordionValue,
|
accordionValue,
|
||||||
setAccordionValue,
|
setAccordionValue,
|
||||||
errorLabel,
|
errorLabel,
|
||||||
@@ -374,9 +371,9 @@ const InterfacesPanel = ({
|
|||||||
variant={isAdvanced ? "contained" : "default"}
|
variant={isAdvanced ? "contained" : "default"}
|
||||||
>
|
>
|
||||||
<Accordion.Item value="interfaces">
|
<Accordion.Item value="interfaces">
|
||||||
<Accordion.Control icon={isTiny ? null : <IconTopologyBus size={16} />}>
|
<Accordion.Control icon={<IconTopologyBus size={16} />}>
|
||||||
<Group justify="space-between" wrap="nowrap" gap="xs">
|
<Group justify="space-between" wrap="nowrap" gap="xs">
|
||||||
<Text size={isTiny ? "8px" : "xs"}>{label}</Text>
|
<Text size="xs">{label}</Text>
|
||||||
{hasError && (
|
{hasError && (
|
||||||
<Badge color="red" variant="light" size="xs">
|
<Badge color="red" variant="light" size="xs">
|
||||||
{errorBadgeLabel}
|
{errorBadgeLabel}
|
||||||
@@ -387,19 +384,30 @@ const InterfacesPanel = ({
|
|||||||
<Accordion.Panel>
|
<Accordion.Panel>
|
||||||
<Stack gap={4}>
|
<Stack gap={4}>
|
||||||
{bandwidth.map(({ name, receive, transmit }) => (
|
{bandwidth.map(({ name, receive, transmit }) => (
|
||||||
<Group key={name} gap="xs" wrap={isTiny ? "wrap" : "nowrap"} justify="space-between">
|
<Group key={name} gap="xs" wrap="nowrap" justify="space-between">
|
||||||
<Text size={isTiny ? "8px" : "xs"} c="blue.3" truncate="end" style={{ flex: 1 }}>
|
<Text
|
||||||
|
size="xs"
|
||||||
|
c="light-dark(var(--mantine-color-blue-8), var(--mantine-color-blue-3))"
|
||||||
|
truncate="end"
|
||||||
|
style={{ flex: 1 }}
|
||||||
|
>
|
||||||
{name}
|
{name}
|
||||||
</Text>
|
</Text>
|
||||||
<Group gap={4} wrap="nowrap">
|
<Group gap={4} wrap="nowrap">
|
||||||
<IconArrowBarUp size={isTiny ? 8 : 12} color="lightgreen" />
|
<IconArrowBarUp
|
||||||
<Text size={isTiny ? "8px" : "xs"} c="green.3">
|
size={12}
|
||||||
|
color="light-dark(var(--mantine-color-green-8), var(--mantine-color-green-3))"
|
||||||
|
/>
|
||||||
|
<Text size="xs" c="light-dark(var(--mantine-color-green-8), var(--mantine-color-green-3))">
|
||||||
{formatBitsPerSec(transmit, 2)}
|
{formatBitsPerSec(transmit, 2)}
|
||||||
</Text>
|
</Text>
|
||||||
</Group>
|
</Group>
|
||||||
<Group gap={4} wrap="nowrap">
|
<Group gap={4} wrap="nowrap">
|
||||||
<IconArrowBarDown size={isTiny ? 8 : 12} color="yellow" />
|
<IconArrowBarDown
|
||||||
<Text size={isTiny ? "8px" : "xs"} c="yellow.3">
|
size={12}
|
||||||
|
color="light-dark(var(--mantine-color-yellow-9), var(--mantine-color-yellow-3))"
|
||||||
|
/>
|
||||||
|
<Text size="xs" c="light-dark(var(--mantine-color-yellow-9), var(--mantine-color-yellow-3))">
|
||||||
{formatBitsPerSec(receive, 2)}
|
{formatBitsPerSec(receive, 2)}
|
||||||
</Text>
|
</Text>
|
||||||
</Group>
|
</Group>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { describe, expect, it } from "vitest";
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
import { hasTotalFirewallFailure } from "./component";
|
import { hasFirewallPartialFailure, hasTotalFirewallFailure } from "./component";
|
||||||
|
|
||||||
const failedQuery = {
|
const failedQuery = {
|
||||||
isPending: false,
|
isPending: false,
|
||||||
@@ -27,4 +27,31 @@ describe("firewall failure state", () => {
|
|||||||
false,
|
false,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("preserves stale cached data when a query-wide refetch fails", () => {
|
||||||
|
const staleQuery = {
|
||||||
|
...failedQuery,
|
||||||
|
isError: true,
|
||||||
|
data: [{ integration: { id: "firewall-1" }, error: undefined }],
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(hasTotalFirewallFailure([staleQuery, failedQuery, failedQuery, failedQuery])).toBe(false);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("surfaces query-wide and integration-specific partial failures", () => {
|
||||||
|
const successfulQuery = {
|
||||||
|
isPending: false,
|
||||||
|
isFetching: false,
|
||||||
|
isError: false,
|
||||||
|
data: [{ integration: { id: "firewall-1" }, error: undefined }],
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(hasFirewallPartialFailure("firewall-1", [successfulQuery])).toBe(false);
|
||||||
|
expect(hasFirewallPartialFailure("firewall-1", [{ ...successfulQuery, isError: true }])).toBe(true);
|
||||||
|
expect(
|
||||||
|
hasFirewallPartialFailure("firewall-1", [
|
||||||
|
{ ...successfulQuery, data: [{ integration: { id: "firewall-1" }, error: "offline" }] },
|
||||||
|
]),
|
||||||
|
).toBe(true);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -7,6 +7,7 @@ import type { Resource } from "@homarr/integrations/types";
|
|||||||
import { useI18n } from "@homarr/translation/client";
|
import { useI18n } from "@homarr/translation/client";
|
||||||
|
|
||||||
import { WidgetEmptyState } from "../../common/empty-state";
|
import { WidgetEmptyState } from "../../common/empty-state";
|
||||||
|
import { getUsableWidgetQueryData } from "../../common/query-state";
|
||||||
import type { WidgetComponentProps } from "../../definition";
|
import type { WidgetComponentProps } from "../../definition";
|
||||||
import { formatUptime } from "../system-health";
|
import { formatUptime } from "../system-health";
|
||||||
import { getClusterAccordionDefault } from "./accordion-state";
|
import { getClusterAccordionDefault } from "./accordion-state";
|
||||||
@@ -38,9 +39,9 @@ export const ClusterHealthMonitoring = ({
|
|||||||
displayMode,
|
displayMode,
|
||||||
}: WidgetComponentProps<"healthMonitoring"> & { integrationId: string }) => {
|
}: WidgetComponentProps<"healthMonitoring"> & { integrationId: string }) => {
|
||||||
const t = useI18n();
|
const t = useI18n();
|
||||||
const { data: healthData } = clientApi.widget.healthMonitoring.getClusterHealthStatus.useQuery({
|
const healthData = getUsableWidgetQueryData(
|
||||||
integrationId,
|
clientApi.widget.healthMonitoring.getClusterHealthStatus.useQuery({ integrationId }),
|
||||||
});
|
);
|
||||||
const accordionScope = `${displayMode}:${options.visibleClusterSections.join(",")}`;
|
const accordionScope = `${displayMode}:${options.visibleClusterSections.join(",")}`;
|
||||||
const accordionDefault = getClusterAccordionDefault(displayMode, options.visibleClusterSections);
|
const accordionDefault = getClusterAccordionDefault(displayMode, options.visibleClusterSections);
|
||||||
const [accordionValues, setAccordionValues] = useState<Record<string, string[]>>({});
|
const [accordionValues, setAccordionValues] = useState<Record<string, string[]>>({});
|
||||||
@@ -67,9 +68,9 @@ export const ClusterHealthMonitoring = ({
|
|||||||
const isTiny = displayMode !== "advanced" && width < 256;
|
const isTiny = displayMode !== "advanced" && width < 256;
|
||||||
return (
|
return (
|
||||||
<Stack h={displayMode === "advanced" ? "auto" : "100%"} p="xs" gap={isTiny ? "xs" : "md"}>
|
<Stack h={displayMode === "advanced" ? "auto" : "100%"} p="xs" gap={isTiny ? "xs" : "md"}>
|
||||||
{options.showUptime && (
|
{options.showUptime && !isTiny && (
|
||||||
<Group justify="center" wrap="nowrap">
|
<Group justify="center" wrap="nowrap">
|
||||||
<Text fz={isTiny ? 8 : "xs"} fw={700} c="dimmed" ta="center">
|
<Text fz="xs" fw={700} c="dimmed" ta="center">
|
||||||
{formatUptime(uptime, t)}
|
{formatUptime(uptime, t)}
|
||||||
</Text>
|
</Text>
|
||||||
</Group>
|
</Group>
|
||||||
@@ -190,10 +191,12 @@ const SummaryHeader = ({ cpu, memory, isTiny }: SummaryHeaderProps) => {
|
|||||||
sections={[{ value: cpu.value, color: cpu.value > 75 ? "orange" : "green" }]}
|
sections={[{ value: cpu.value, color: cpu.value > 75 ? "orange" : "green" }]}
|
||||||
/>
|
/>
|
||||||
<Stack align="center" justify="center" gap={0}>
|
<Stack align="center" justify="center" gap={0}>
|
||||||
<Text fw={500} size={isTiny ? "xs" : "sm"}>
|
{!isTiny && (
|
||||||
{t("widget.healthMonitoring.cluster.summary.cpu")}
|
<Text fw={500} size="sm">
|
||||||
</Text>
|
{t("widget.healthMonitoring.cluster.summary.cpu")}
|
||||||
<Text size={isTiny ? "8px" : "xs"}>{cpu.value.toFixed(1)}%</Text>
|
</Text>
|
||||||
|
)}
|
||||||
|
<Text size="xs">{cpu.value.toFixed(1)}%</Text>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Flex>
|
</Flex>
|
||||||
)}
|
)}
|
||||||
@@ -211,10 +214,12 @@ const SummaryHeader = ({ cpu, memory, isTiny }: SummaryHeaderProps) => {
|
|||||||
sections={[{ value: memory.value, color: memory.value > 75 ? "orange" : "green" }]}
|
sections={[{ value: memory.value, color: memory.value > 75 ? "orange" : "green" }]}
|
||||||
/>
|
/>
|
||||||
<Stack align="center" justify="center" gap={0}>
|
<Stack align="center" justify="center" gap={0}>
|
||||||
<Text size={isTiny ? "xs" : "sm"} fw={500}>
|
{!isTiny && (
|
||||||
{t("widget.healthMonitoring.cluster.summary.memory")}
|
<Text size="sm" fw={500}>
|
||||||
</Text>
|
{t("widget.healthMonitoring.cluster.summary.memory")}
|
||||||
<Text size={isTiny ? "8px" : "xs"}>{memory.value.toFixed(1)}%</Text>
|
</Text>
|
||||||
|
)}
|
||||||
|
<Text size="xs">{memory.value.toFixed(1)}%</Text>
|
||||||
</Stack>
|
</Stack>
|
||||||
</Flex>
|
</Flex>
|
||||||
)}
|
)}
|
||||||
|
|||||||
Loaded 100 of 165 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user