feat: expand advanced widget experiences
This commit is contained in:
1 parent
55226842f9
commit
65a7e84bea
137 files changed
+3684
-850
No files matched your search
@@ -1,6 +1,23 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { getAdvancedColumnWidth, getCompactSectionVisibility } from "./component";
|
||||
import { getAdvancedColumnWidth, getCompactSectionVisibility, getTracearrSectionVisibility } from "./component";
|
||||
import { attachTracearrSource } from "./source";
|
||||
|
||||
describe("Tracearr source ownership", () => {
|
||||
it("uses integration-scoped keys for overlapping upstream ids", () => {
|
||||
const first = attachTracearrSource([{ id: "same" }], {
|
||||
integrationId: "tracearr-a",
|
||||
integrationName: "Tracearr A",
|
||||
});
|
||||
const second = attachTracearrSource([{ id: "same" }], {
|
||||
integrationId: "tracearr-b",
|
||||
integrationName: "Tracearr B",
|
||||
});
|
||||
|
||||
expect([first[0]?.key, second[0]?.key]).toEqual(["tracearr-a:same", "tracearr-b:same"]);
|
||||
expect(second[0]?.integrationName).toBe("Tracearr B");
|
||||
});
|
||||
});
|
||||
|
||||
describe("Tracearr advanced layout", () => {
|
||||
it("subtracts grid padding and the inter-column gap", () => {
|
||||
@@ -38,3 +55,14 @@ describe("Tracearr compact disclosure", () => {
|
||||
).toEqual({ violations: true, recentActivity: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("Tracearr advanced disclosure", () => {
|
||||
it("shows all sections even when compact presentation toggles are disabled", () => {
|
||||
expect(
|
||||
getTracearrSectionVisibility(
|
||||
{ showStats: false, showStreams: false, showViolations: false, showRecentActivity: false },
|
||||
true,
|
||||
),
|
||||
).toEqual({ stats: true, streams: true, violations: true, recentActivity: true });
|
||||
});
|
||||
});
|
||||
@@ -1,16 +1,19 @@
|
||||
"use client";
|
||||
|
||||
import { ScrollArea, SimpleGrid, Stack, Text } from "@mantine/core";
|
||||
import { Box, Group, ScrollArea, SimpleGrid, Stack, Text } from "@mantine/core";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import type { TracearrDashboardData } from "@homarr/integrations/types";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
|
||||
import type { WidgetComponentProps } from "../definition";
|
||||
import { IntegrationErrorIndicator } from "../common/integration-error-indicator";
|
||||
import { getUsableWidgetQueryData } from "../common/query-state";
|
||||
import { WidgetQueryErrorIndicator } from "../common/query-state-indicator";
|
||||
import { NoIntegrationDataError } from "../errors/no-data-integration";
|
||||
import { formatTotalTracearrBitrate } from "./bitrate";
|
||||
import { RecentActivityList } from "./recent-activity-section";
|
||||
import { attachTracearrSource } from "./source";
|
||||
import { StatsBar } from "./stats-section";
|
||||
import { StreamsList } from "./streams-section";
|
||||
import { ViolationsList } from "./violations-section";
|
||||
@@ -19,6 +22,18 @@ const ADVANCED_GRID_BREAKPOINT = 800;
|
||||
const ADVANCED_GRID_GAP_PX = 16;
|
||||
const ADVANCED_GRID_PADDING_PX = 16;
|
||||
|
||||
type TracearrSectionOptions = Pick<
|
||||
WidgetComponentProps<"tracearr">["options"],
|
||||
"showStats" | "showStreams" | "showViolations" | "showRecentActivity"
|
||||
>;
|
||||
|
||||
export const getTracearrSectionVisibility = (options: TracearrSectionOptions, isAdvanced: boolean) => ({
|
||||
stats: isAdvanced || options.showStats,
|
||||
streams: isAdvanced || options.showStreams,
|
||||
violations: isAdvanced || options.showViolations,
|
||||
recentActivity: isAdvanced || options.showRecentActivity,
|
||||
});
|
||||
|
||||
export const getCompactSectionVisibility = ({
|
||||
height,
|
||||
showStreams,
|
||||
@@ -68,13 +83,16 @@ interface TracearrContentProps {
|
||||
|
||||
function TracearrContent({ integrationIds, options, width, height, displayMode }: TracearrContentProps) {
|
||||
const t = useScopedI18n("widget.tracearr");
|
||||
const dashboardData =
|
||||
getUsableWidgetQueryData(clientApi.widget.tracearr.getDashboard.useQuery({ integrationIds })) ?? [];
|
||||
const dashboardQuery = clientApi.widget.tracearr.getDashboard.useQuery({ integrationIds });
|
||||
const dashboardData = getUsableWidgetQueryData(dashboardQuery) ?? [];
|
||||
const successfulDashboards = dashboardData.flatMap((item) =>
|
||||
item.dashboard === null ? [] : [{ ...item, dashboard: item.dashboard }],
|
||||
);
|
||||
|
||||
// Merge data from all integrations
|
||||
const combinedData = dashboardData.reduce<TracearrDashboardData>(
|
||||
(acc, item) => {
|
||||
const { stats, streams, violations, recentActivity } = item.dashboard;
|
||||
const combinedData = successfulDashboards.reduce<TracearrDashboardData>(
|
||||
(acc, { dashboard }) => {
|
||||
const { stats, streams, violations, recentActivity } = dashboard;
|
||||
const vData = violations ?? { data: [], meta: { total: 0, page: 1, pageSize: 5 } };
|
||||
const aData = recentActivity ?? { data: [], meta: { total: 0, page: 1, pageSize: 5 } };
|
||||
|
||||
@@ -136,60 +154,101 @@ function TracearrContent({ integrationIds, options, width, height, displayMode }
|
||||
},
|
||||
};
|
||||
|
||||
const noSectionsEnabled =
|
||||
!options.showStats && !options.showStreams && !options.showRecentActivity && !options.showViolations;
|
||||
const isAdvanced = displayMode === "advanced";
|
||||
const sectionVisibility = getTracearrSectionVisibility(options, isAdvanced);
|
||||
const noSectionsEnabled = Object.values(sectionVisibility).every((isVisible) => !isVisible);
|
||||
|
||||
const compactLimit = height < 220 ? 2 : height < 360 ? 4 : 8;
|
||||
const streams = displayMode === "advanced" ? combined.streams.data : combined.streams.data.slice(0, compactLimit);
|
||||
const violations = combined.violations?.data ?? [];
|
||||
const recentActivity = combined.recentActivity?.data ?? [];
|
||||
const sourcedStreams = successfulDashboards.flatMap(({ integrationId, integrationName, dashboard }) =>
|
||||
attachTracearrSource(dashboard.streams.data, { integrationId, integrationName }),
|
||||
);
|
||||
const sourcedViolations = successfulDashboards.flatMap(({ integrationId, integrationName, dashboard }) =>
|
||||
attachTracearrSource(dashboard.violations?.data ?? [], { integrationId, integrationName }),
|
||||
);
|
||||
const sourcedRecentActivity = successfulDashboards.flatMap(({ integrationId, integrationName, dashboard }) =>
|
||||
attachTracearrSource(dashboard.recentActivity?.data ?? [], { integrationId, integrationName }),
|
||||
);
|
||||
const streams = isAdvanced ? sourcedStreams : sourcedStreams.slice(0, compactLimit);
|
||||
const showSource = isAdvanced || dashboardData.length > 1;
|
||||
const compactSections = getCompactSectionVisibility({
|
||||
height,
|
||||
showStreams: options.showStreams,
|
||||
showViolations: options.showViolations,
|
||||
hasViolations: violations.length > 0,
|
||||
showRecentActivity: options.showRecentActivity,
|
||||
showStreams: sectionVisibility.streams,
|
||||
showViolations: sectionVisibility.violations,
|
||||
hasViolations: sourcedViolations.length > 0,
|
||||
showRecentActivity: sectionVisibility.recentActivity,
|
||||
});
|
||||
|
||||
if (displayMode === "advanced") {
|
||||
if (isAdvanced) {
|
||||
const isTwoColumn = width >= ADVANCED_GRID_BREAKPOINT;
|
||||
const columnWidth = getAdvancedColumnWidth(width, isTwoColumn);
|
||||
return (
|
||||
<ScrollArea h="100%">
|
||||
<SimpleGrid
|
||||
cols={isTwoColumn ? 2 : 1}
|
||||
spacing={`${ADVANCED_GRID_GAP_PX}px`}
|
||||
p={`${ADVANCED_GRID_PADDING_PX}px`}
|
||||
>
|
||||
<Stack gap="sm">
|
||||
{options.showStats && (
|
||||
<StatsBar stats={combined.stats} summary={combined.streams.summary} width={columnWidth} />
|
||||
)}
|
||||
{options.showStreams && <StreamsList streams={streams} width={columnWidth} />}
|
||||
</Stack>
|
||||
<Stack gap="sm">
|
||||
{options.showViolations && <ViolationsList violations={violations} />}
|
||||
{options.showRecentActivity && <RecentActivityList sessions={recentActivity} />}
|
||||
</Stack>
|
||||
</SimpleGrid>
|
||||
</ScrollArea>
|
||||
<Box h="100%" pos="relative">
|
||||
<Group pos="absolute" top={4} right={8} gap={0} style={{ zIndex: 2 }}>
|
||||
<IntegrationErrorIndicator results={dashboardData} />
|
||||
<WidgetQueryErrorIndicator error={dashboardQuery.error} label={t("name")} />
|
||||
</Group>
|
||||
<ScrollArea h="100%">
|
||||
<SimpleGrid
|
||||
cols={isTwoColumn ? 2 : 1}
|
||||
spacing={`${ADVANCED_GRID_GAP_PX}px`}
|
||||
p={`${ADVANCED_GRID_PADDING_PX}px`}
|
||||
>
|
||||
<Stack gap="sm">
|
||||
<Text size="xs" c="dimmed" lineClamp={1}>
|
||||
{successfulDashboards.map(({ integrationName }) => integrationName).join(" · ")}
|
||||
</Text>
|
||||
{sectionVisibility.stats && (
|
||||
<StatsBar stats={combined.stats} summary={combined.streams.summary} width={columnWidth} />
|
||||
)}
|
||||
{sectionVisibility.streams && (
|
||||
<StreamsList streams={streams} width={columnWidth} showSource={showSource} />
|
||||
)}
|
||||
</Stack>
|
||||
<Stack gap="sm">
|
||||
{sectionVisibility.violations && (
|
||||
<ViolationsList violations={sourcedViolations} showSource={showSource} />
|
||||
)}
|
||||
{sectionVisibility.recentActivity && (
|
||||
<RecentActivityList sessions={sourcedRecentActivity} showSource={showSource} />
|
||||
)}
|
||||
</Stack>
|
||||
</SimpleGrid>
|
||||
</ScrollArea>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollArea h="100%">
|
||||
<Stack gap="xs" p="xs">
|
||||
{options.showStats && <StatsBar stats={combined.stats} summary={combined.streams.summary} width={width} />}
|
||||
{compactSections.violations && <ViolationsList violations={violations.slice(0, compactLimit)} />}
|
||||
{options.showStreams && <StreamsList streams={streams} width={width} />}
|
||||
{compactSections.recentActivity && <RecentActivityList sessions={recentActivity.slice(0, compactLimit)} />}
|
||||
{noSectionsEnabled && (
|
||||
<Text c="dimmed" ta="center">
|
||||
{t("noSectionsEnabled")}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
</ScrollArea>
|
||||
<Box h="100%" pos="relative">
|
||||
<Group pos="absolute" top={4} right={8} gap={0} style={{ zIndex: 2 }}>
|
||||
<IntegrationErrorIndicator results={dashboardData} />
|
||||
<WidgetQueryErrorIndicator error={dashboardQuery.error} label={t("name")} />
|
||||
</Group>
|
||||
<ScrollArea h="100%">
|
||||
<Stack gap="xs" p="xs">
|
||||
{showSource && (
|
||||
<Text size="xs" c="dimmed" lineClamp={1}>
|
||||
{successfulDashboards.map(({ integrationName }) => integrationName).join(" · ")}
|
||||
</Text>
|
||||
)}
|
||||
{sectionVisibility.stats && (
|
||||
<StatsBar stats={combined.stats} summary={combined.streams.summary} width={width} />
|
||||
)}
|
||||
{compactSections.violations && (
|
||||
<ViolationsList violations={sourcedViolations.slice(0, compactLimit)} showSource={showSource} />
|
||||
)}
|
||||
{sectionVisibility.streams && <StreamsList streams={streams} width={width} showSource={showSource} />}
|
||||
{compactSections.recentActivity && (
|
||||
<RecentActivityList sessions={sourcedRecentActivity.slice(0, compactLimit)} showSource={showSource} />
|
||||
)}
|
||||
{noSectionsEnabled && (
|
||||
<Text c="dimmed" ta="center">
|
||||
{t("noSectionsEnabled")}
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
</ScrollArea>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,17 @@ import { Avatar, Badge, Group, Paper, Stack, Text } from "@mantine/core";
|
||||
import type { TracearrHistorySession } from "@homarr/integrations/types";
|
||||
import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client";
|
||||
|
||||
export function RecentActivityList({ sessions }: { sessions: TracearrHistorySession[] }) {
|
||||
import type { SourcedTracearrItem } from "./source";
|
||||
|
||||
type SourcedTracearrHistorySession = SourcedTracearrItem<TracearrHistorySession>;
|
||||
|
||||
export function RecentActivityList({
|
||||
sessions,
|
||||
showSource,
|
||||
}: {
|
||||
sessions: SourcedTracearrHistorySession[];
|
||||
showSource: boolean;
|
||||
}) {
|
||||
const t = useScopedI18n("widget.tracearr");
|
||||
const locale = useCurrentIntlLocale();
|
||||
|
||||
@@ -25,7 +35,7 @@ export function RecentActivityList({ sessions }: { sessions: TracearrHistorySess
|
||||
: session.mediaTitle;
|
||||
|
||||
return (
|
||||
<Paper key={session.id} p="sm" radius="lg">
|
||||
<Paper key={session.key} p="sm" radius="lg">
|
||||
<Group justify="space-between" wrap="nowrap">
|
||||
<Group gap="xs" wrap="nowrap" style={{ overflow: "hidden" }}>
|
||||
<Avatar src={session.user.avatarUrl} alt={session.user.username} radius="xl" size="sm" />
|
||||
@@ -35,6 +45,7 @@ export function RecentActivityList({ sessions }: { sessions: TracearrHistorySess
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed" lineClamp={1}>
|
||||
{session.user.username} • {session.serverName}
|
||||
{showSource ? ` • ${session.integrationName}` : ""}
|
||||
</Text>
|
||||
</Stack>
|
||||
</Group>
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
export interface TracearrSource {
|
||||
integrationId: string;
|
||||
integrationName: string;
|
||||
}
|
||||
|
||||
export type SourcedTracearrItem<T extends { id: string }> = T &
|
||||
TracearrSource & {
|
||||
key: string;
|
||||
};
|
||||
|
||||
export const attachTracearrSource = <T extends { id: string }>(
|
||||
items: readonly T[],
|
||||
source: TracearrSource,
|
||||
): SourcedTracearrItem<T>[] =>
|
||||
items.map((item) => ({
|
||||
...item,
|
||||
...source,
|
||||
key: `${source.integrationId}:${item.id}`,
|
||||
}));
|
||||
@@ -5,7 +5,19 @@ import { formatDuration } from "@homarr/common";
|
||||
import type { TracearrStream } from "@homarr/integrations/types";
|
||||
import { useScopedI18n } from "@homarr/translation/client";
|
||||
|
||||
export function StreamsList({ streams, width }: { streams: TracearrStream[]; width: number }) {
|
||||
import type { SourcedTracearrItem } from "./source";
|
||||
|
||||
type SourcedTracearrStream = SourcedTracearrItem<TracearrStream>;
|
||||
|
||||
export function StreamsList({
|
||||
streams,
|
||||
width,
|
||||
showSource,
|
||||
}: {
|
||||
streams: SourcedTracearrStream[];
|
||||
width: number;
|
||||
showSource: boolean;
|
||||
}) {
|
||||
const t = useScopedI18n("widget.tracearr");
|
||||
|
||||
if (streams.length === 0) {
|
||||
@@ -19,13 +31,21 @@ export function StreamsList({ streams, width }: { streams: TracearrStream[]; wid
|
||||
return (
|
||||
<Stack gap="xs">
|
||||
{streams.map((stream) => (
|
||||
<StreamCard key={stream.id} stream={stream} compact={width < 300} />
|
||||
<StreamCard key={stream.key} stream={stream} compact={width < 300} showSource={showSource} />
|
||||
))}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
|
||||
function StreamCard({ stream, compact }: { stream: TracearrStream; compact: boolean }) {
|
||||
function StreamCard({
|
||||
stream,
|
||||
compact,
|
||||
showSource,
|
||||
}: {
|
||||
stream: SourcedTracearrStream;
|
||||
compact: boolean;
|
||||
showSource: boolean;
|
||||
}) {
|
||||
const t = useScopedI18n("widget.tracearr");
|
||||
const progressPercent =
|
||||
stream.durationMs && stream.durationMs > 0 ? (stream.progressMs / stream.durationMs) * 100 : 0;
|
||||
@@ -115,13 +135,13 @@ function StreamCard({ stream, compact }: { stream: TracearrStream; compact: bool
|
||||
</Group>
|
||||
)}
|
||||
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
{stream.device && (
|
||||
{(stream.device || showSource) && (
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
<Text size="xs" c="dimmed" lineClamp={1}>
|
||||
{stream.player ?? stream.device}
|
||||
{[stream.player ?? stream.device, showSource ? stream.integrationName : null].filter(Boolean).join(" · ")}
|
||||
</Text>
|
||||
)}
|
||||
</Group>
|
||||
</Group>
|
||||
)}
|
||||
</Stack>
|
||||
</Paper>
|
||||
);
|
||||
|
||||
@@ -4,7 +4,17 @@ import { IconAlertTriangle } from "@tabler/icons-react";
|
||||
import type { TracearrViolation } from "@homarr/integrations/types";
|
||||
import { useCurrentIntlLocale, useScopedI18n } from "@homarr/translation/client";
|
||||
|
||||
export function ViolationsList({ violations }: { violations: TracearrViolation[] }) {
|
||||
import type { SourcedTracearrItem } from "./source";
|
||||
|
||||
type SourcedTracearrViolation = SourcedTracearrItem<TracearrViolation>;
|
||||
|
||||
export function ViolationsList({
|
||||
violations,
|
||||
showSource,
|
||||
}: {
|
||||
violations: SourcedTracearrViolation[];
|
||||
showSource: boolean;
|
||||
}) {
|
||||
const t = useScopedI18n("widget.tracearr");
|
||||
const locale = useCurrentIntlLocale();
|
||||
|
||||
@@ -20,7 +30,7 @@ export function ViolationsList({ violations }: { violations: TracearrViolation[]
|
||||
) : (
|
||||
<Stack gap="xs">
|
||||
{violations.map((violation) => (
|
||||
<Paper key={violation.id} p="xs" radius="lg">
|
||||
<Paper key={violation.key} p="xs" radius="lg">
|
||||
<Group justify="space-between" wrap="nowrap">
|
||||
<Group gap="xs" wrap="nowrap" style={{ overflow: "hidden" }}>
|
||||
<IconAlertTriangle
|
||||
@@ -40,6 +50,7 @@ export function ViolationsList({ violations }: { violations: TracearrViolation[]
|
||||
</Text>
|
||||
<Text size="xs" c="dimmed" lineClamp={1}>
|
||||
{t("violations.rule")}: {violation.rule.name}
|
||||
{showSource ? ` · ${violation.integrationName}` : ""}
|
||||
</Text>
|
||||
</Stack>
|
||||
</Group>
|
||||
|
||||
Reference in new issue
Block a user