fix(widgets): restore compact v1 surfaces (#6710)
This commit is contained in:
1 parent
1b2f1bd720
commit
643ecf3df7
105 files changed
+1603
-1285
No files matched your search
@@ -7,16 +7,10 @@ import type { AudiobookshelfDashboardData, NavidromeDashboardData } from "@homar
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
import { zoomCompensatedSize } from "@homarr/ui";
|
||||
|
||||
import { getCompactStatLayout } from "../common/compact-stat-layout";
|
||||
import classes from "./component.module.css";
|
||||
import type { AudioStatsBackend, AudioStatsDisplayOptions } from "./shared";
|
||||
import {
|
||||
AUDIO_STATS_TRANSLATION_SCOPE,
|
||||
getGridCols,
|
||||
getIconSize,
|
||||
prioritizeVisibleStats,
|
||||
getVisibleStatLimit,
|
||||
getVisibleStats,
|
||||
} from "./shared";
|
||||
import { AUDIO_STATS_TRANSLATION_SCOPE, getGridCols, getIconSize, getVisibleStats } from "./shared";
|
||||
|
||||
const rootClassByCompact = {
|
||||
true: classes.rootCompact,
|
||||
@@ -33,11 +27,49 @@ const statTileClassByCompact = {
|
||||
false: "",
|
||||
} as const;
|
||||
|
||||
const statTileClassByAdvanced = {
|
||||
true: classes.statTileAdvanced,
|
||||
false: "",
|
||||
} as const;
|
||||
|
||||
const rootClassByLayout = {
|
||||
default: "",
|
||||
short: classes.rootShort,
|
||||
narrowShort: classes.rootShort,
|
||||
} as const;
|
||||
|
||||
const gridClassByLayout = {
|
||||
default: "",
|
||||
short: classes.gridShort,
|
||||
narrowShort: classes.gridShort,
|
||||
} as const;
|
||||
|
||||
const statTileClassByLayout = {
|
||||
default: "",
|
||||
short: classes.statTileShort,
|
||||
narrowShort: `${classes.statTileShort} ${classes.statTileNarrowShort}`,
|
||||
} as const;
|
||||
|
||||
const statLabelClassByLayout = {
|
||||
default: "",
|
||||
short: classes.statLabelShort,
|
||||
narrowShort: classes.statLabelShort,
|
||||
} as const;
|
||||
|
||||
const statValueClassByLayout = {
|
||||
default: "",
|
||||
short: "",
|
||||
narrowShort: classes.statValueNarrowShort,
|
||||
} as const;
|
||||
|
||||
const COMPACT_SINGLE_COLUMN_BREAKPOINT = 220;
|
||||
|
||||
interface AudioStatsContentProps {
|
||||
backend: AudioStatsBackend;
|
||||
stats: NavidromeDashboardData | AudiobookshelfDashboardData;
|
||||
options: AudioStatsDisplayOptions;
|
||||
showAllStats?: boolean;
|
||||
advanced?: boolean;
|
||||
width: number;
|
||||
height: number;
|
||||
}
|
||||
@@ -47,29 +79,45 @@ export function AudioStatsContent({
|
||||
stats,
|
||||
options,
|
||||
showAllStats = false,
|
||||
advanced = false,
|
||||
width,
|
||||
height,
|
||||
}: AudioStatsContentProps) {
|
||||
const t = useI18n(AUDIO_STATS_TRANSLATION_SCOPE);
|
||||
const compact = options.compactMode ?? false;
|
||||
const enabledStats = prioritizeVisibleStats(getVisibleStats(backend, options, stats, showAllStats), compact);
|
||||
const visibleStats = enabledStats.slice(0, getVisibleStatLimit(width, height, enabledStats.length, compact));
|
||||
const visibleStats = getVisibleStats(backend, options, stats, showAllStats);
|
||||
const compactKey = String(compact) as keyof typeof rootClassByCompact;
|
||||
const gridCols = getGridCols(width, visibleStats.length, compact);
|
||||
const iconSize = getIconSize(width, compact);
|
||||
const advancedKey = String(advanced) as keyof typeof statTileClassByAdvanced;
|
||||
let defaultGridCols = getGridCols(width, visibleStats.length, compact);
|
||||
if (!advanced && width < COMPACT_SINGLE_COLUMN_BREAKPOINT) {
|
||||
defaultGridCols = 1;
|
||||
}
|
||||
const layout = getCompactStatLayout({
|
||||
width,
|
||||
height,
|
||||
visibleCount: visibleStats.length,
|
||||
compactDisplay: !advanced,
|
||||
defaultColumns: defaultGridCols,
|
||||
defaultIconSize: getIconSize(width, compact),
|
||||
});
|
||||
|
||||
return (
|
||||
<div className={`${classes.root} ${rootClassByCompact[compactKey]}`}>
|
||||
<div className={`${classes.root} ${rootClassByCompact[compactKey]} ${rootClassByLayout[layout.state]}`}>
|
||||
{visibleStats.length > 0 && (
|
||||
<div
|
||||
className={`${classes.grid} ${gridClassByCompact[compactKey]}`}
|
||||
style={{ "--stat-cols": gridCols } as CSSProperties}
|
||||
className={`${classes.grid} ${gridClassByCompact[compactKey]} ${gridClassByLayout[layout.state]}`}
|
||||
style={{ "--stat-cols": layout.columns } as CSSProperties}
|
||||
>
|
||||
{visibleStats.map(({ optionKey, statKey, value, Icon }) => (
|
||||
<div key={optionKey} className={`${classes.statTile} ${statTileClassByCompact[compactKey]}`}>
|
||||
<Icon className={classes.statIcon} style={zoomCompensatedSize(iconSize)} stroke={1.5} />
|
||||
<span className={classes.statValue}>{value}</span>
|
||||
<span className={classes.statLabel}>{t(statKey as never)}</span>
|
||||
<div
|
||||
key={optionKey}
|
||||
className={`${classes.statTile} ${statTileClassByCompact[compactKey]} ${statTileClassByAdvanced[advancedKey]} ${statTileClassByLayout[layout.state]}`}
|
||||
>
|
||||
<Icon className={classes.statIcon} style={zoomCompensatedSize(layout.iconSize)} stroke={1.5} />
|
||||
<span className={`${classes.statValue} ${statValueClassByLayout[layout.state]}`}>{value}</span>
|
||||
<span className={`${classes.statLabel} ${statLabelClassByLayout[layout.state]}`}>
|
||||
{t(statKey as never)}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
@@ -2,7 +2,7 @@ import { describe, expect, test } from "vitest";
|
||||
|
||||
import type { AudiobookshelfDashboardData, NavidromeDashboardData } from "@homarr/integrations/types";
|
||||
|
||||
import { getGridCols, getIconSize, getVisibleStatLimit, getVisibleStats, prioritizeVisibleStats } from "./shared";
|
||||
import { getGridCols, getIconSize, getVisibleStats } from "./shared";
|
||||
|
||||
describe("getGridCols", () => {
|
||||
test("caps columns to visible stat count", () => {
|
||||
@@ -37,17 +37,6 @@ describe("getIconSize", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("getVisibleStatLimit", () => {
|
||||
test("keeps compact metrics legible when height is constrained", () => {
|
||||
expect(getVisibleStatLimit(100, 80, 5, true)).toBe(1);
|
||||
expect(getVisibleStatLimit(180, 50, 5, true)).toBe(3);
|
||||
});
|
||||
|
||||
test("preserves every configured metric outside compact mode", () => {
|
||||
expect(getVisibleStatLimit(100, 80, 5, false)).toBe(5);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getVisibleStats", () => {
|
||||
const navidromeStats: NavidromeDashboardData = {
|
||||
artistCount: 100,
|
||||
@@ -117,21 +106,3 @@ describe("getVisibleStats", () => {
|
||||
expect(typeof result[0]?.value).toBe("string");
|
||||
});
|
||||
});
|
||||
|
||||
describe("prioritizeVisibleStats", () => {
|
||||
test("puts an active session first only on compact surfaces", () => {
|
||||
const stats = getVisibleStats(
|
||||
"audiobookshelf",
|
||||
{ showLibraryCount: true, showActiveSessions: true },
|
||||
{
|
||||
libraryCount: 3,
|
||||
totalAudiobooks: 150,
|
||||
totalPodcasts: 20,
|
||||
totalListeningTimeSeconds: 86400,
|
||||
activeSessions: 2,
|
||||
},
|
||||
);
|
||||
expect(prioritizeVisibleStats(stats, true)[0]?.statKey).toBe("activeSessions");
|
||||
expect(prioritizeVisibleStats(stats, false)[0]?.statKey).toBe("libraryCount");
|
||||
});
|
||||
});
|
||||
@@ -14,6 +14,11 @@
|
||||
gap: clamp(2px, 1cqi, 4px);
|
||||
}
|
||||
|
||||
.rootShort {
|
||||
padding: 4px;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.grid {
|
||||
display: grid;
|
||||
flex: 1;
|
||||
@@ -29,6 +34,10 @@
|
||||
gap: clamp(2px, 1cqi, 4px);
|
||||
}
|
||||
|
||||
.gridShort {
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.statTile {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
@@ -40,16 +49,31 @@
|
||||
gap: clamp(2px, 1.5cqi, 4px);
|
||||
padding: clamp(4px, 2cqi, 8px);
|
||||
border-radius: var(--mantine-radius-md);
|
||||
background-color: transparent;
|
||||
background-color: light-dark(var(--mantine-color-gray-0), var(--mantine-color-dark-6));
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.statTileAdvanced {
|
||||
background-color: transparent;
|
||||
}
|
||||
|
||||
.statTileCompact {
|
||||
gap: clamp(1px, 0.8cqi, 2px);
|
||||
padding: clamp(2px, 1.2cqi, 5px);
|
||||
border-radius: var(--mantine-radius-sm);
|
||||
}
|
||||
|
||||
.statTileShort {
|
||||
gap: 2px;
|
||||
padding: 3px;
|
||||
}
|
||||
|
||||
.statTileNarrowShort {
|
||||
flex-direction: row;
|
||||
gap: 2px;
|
||||
padding: 1px 3px;
|
||||
}
|
||||
|
||||
.statIcon {
|
||||
color: var(--mantine-color-dimmed);
|
||||
flex-shrink: 0;
|
||||
@@ -57,7 +81,12 @@
|
||||
}
|
||||
|
||||
.statLabel {
|
||||
font-size: clamp(8px, 2.5cqi, 11px);
|
||||
flex-shrink: 0;
|
||||
font-size: clamp(
|
||||
calc(8px * var(--board-canvas-ui-scale, 1)),
|
||||
calc(2.5cqi * var(--board-canvas-ui-scale, 1)),
|
||||
calc(11px * var(--board-canvas-ui-scale, 1))
|
||||
);
|
||||
color: var(--mantine-color-dimmed);
|
||||
line-height: 1.2;
|
||||
text-align: center;
|
||||
@@ -68,11 +97,32 @@
|
||||
}
|
||||
|
||||
.statTileCompact .statLabel {
|
||||
font-size: clamp(7px, 2cqi, 9px);
|
||||
font-size: clamp(
|
||||
calc(7px * var(--board-canvas-ui-scale, 1)),
|
||||
calc(2cqi * var(--board-canvas-ui-scale, 1)),
|
||||
calc(9px * var(--board-canvas-ui-scale, 1))
|
||||
);
|
||||
}
|
||||
|
||||
.statLabelShort {
|
||||
position: absolute;
|
||||
width: 1px;
|
||||
height: 1px;
|
||||
padding: 0;
|
||||
margin: -1px;
|
||||
overflow: hidden;
|
||||
clip: rect(0, 0, 0, 0);
|
||||
white-space: nowrap;
|
||||
border: 0;
|
||||
}
|
||||
|
||||
.statValue {
|
||||
font-size: clamp(13px, 5cqi, 20px);
|
||||
flex-shrink: 0;
|
||||
font-size: clamp(
|
||||
calc(13px * var(--board-canvas-ui-scale, 1)),
|
||||
calc(5cqi * var(--board-canvas-ui-scale, 1)),
|
||||
calc(20px * var(--board-canvas-ui-scale, 1))
|
||||
);
|
||||
font-weight: 700;
|
||||
color: var(--mantine-primary-color-filled);
|
||||
line-height: 1.1;
|
||||
@@ -84,7 +134,16 @@
|
||||
}
|
||||
|
||||
.statTileCompact .statValue {
|
||||
font-size: clamp(11px, 4cqi, 16px);
|
||||
font-size: clamp(
|
||||
calc(11px * var(--board-canvas-ui-scale, 1)),
|
||||
calc(4cqi * var(--board-canvas-ui-scale, 1)),
|
||||
calc(16px * var(--board-canvas-ui-scale, 1))
|
||||
);
|
||||
}
|
||||
|
||||
.statValueNarrowShort {
|
||||
font-size: calc(11px * var(--board-canvas-ui-scale, 1));
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.emptyState {
|
||||
|
||||
@@ -18,6 +18,7 @@ export default function AudioStatsWidget({
|
||||
options,
|
||||
width,
|
||||
height,
|
||||
displayScale = 1,
|
||||
displayMode = "compact",
|
||||
widgetRuntimeRef,
|
||||
}: WidgetComponentProps<"audioStats">) {
|
||||
@@ -39,14 +40,22 @@ export default function AudioStatsWidget({
|
||||
const currentStreams = streamResults ?? [];
|
||||
if (!response) return <WidgetEmptyState />;
|
||||
|
||||
let responsiveWidth = width;
|
||||
let responsiveHeight = height;
|
||||
if (displayMode === "compact" && Number.isFinite(displayScale) && displayScale > 0) {
|
||||
responsiveWidth *= displayScale;
|
||||
responsiveHeight *= displayScale;
|
||||
}
|
||||
|
||||
const summary = (
|
||||
<AudioStatsContent
|
||||
backend={response.kind}
|
||||
stats={response.data}
|
||||
options={{ ...options, compactMode: displayMode === "compact" && (options.compactMode || height < 160) }}
|
||||
options={{ ...options, compactMode: displayMode === "compact" && options.compactMode }}
|
||||
showAllStats={displayMode === "advanced"}
|
||||
width={width}
|
||||
height={height}
|
||||
advanced={displayMode === "advanced"}
|
||||
width={responsiveWidth}
|
||||
height={responsiveHeight}
|
||||
/>
|
||||
);
|
||||
if (displayMode === "compact") {
|
||||
|
||||
@@ -156,23 +156,6 @@ export const getGridCols = (width: number, visibleCount: number, compact: boolea
|
||||
return Math.min(maxCols, Math.max(visibleCount, 1));
|
||||
};
|
||||
|
||||
export const getVisibleStatLimit = (width: number, height: number, visibleCount: number, compact: boolean): number => {
|
||||
if (!compact) return visibleCount;
|
||||
|
||||
const columns = getGridCols(width, visibleCount, true);
|
||||
const rows = Math.max(1, Math.floor(Math.max(0, height - 8) / 44));
|
||||
return Math.min(visibleCount, columns * rows);
|
||||
};
|
||||
|
||||
export const prioritizeVisibleStats = (stats: VisibleStat[], compact: boolean): VisibleStat[] => {
|
||||
if (!compact) return stats;
|
||||
return stats.toSorted(
|
||||
(left, right) =>
|
||||
Number(right.statKey === "activeSessions" && Number(right.value) > 0) -
|
||||
Number(left.statKey === "activeSessions" && Number(left.value) > 0),
|
||||
);
|
||||
};
|
||||
|
||||
const fallbackIconSize = { true: 14, false: 16 } as const;
|
||||
|
||||
export const getIconSize = (width: number, compact: boolean): number => {
|
||||
|
||||
Reference in new issue
Block a user