Skip to content

A modern and easy to use dashboard. 80+ integrations. 20K+ icons built in. Authentication out of the box. No YAML, drag and drop configuration.

fix(docker): retain configured columns and compact table widths (#7101)

Thomas1f6ab56157cc
diff --git a/packages/widgets/src/common/homarr-data-table.tsx b/packages/widgets/src/common/homarr-data-table.tsx
index 2aeda2f7d..e21a69d90 100644
--- a/packages/widgets/src/common/homarr-data-table.tsx
+++ b/packages/widgets/src/common/homarr-data-table.tsx
@@ -26,6 +26,9 @@ type HomarrDataTableProps<T> = DataTableProps<T> & {
cellPadding: string;
rowCursor?: "auto" | "default";
preserveColumnWidths?: boolean;
+ fillColumnAccessor?: string;
+ defaultColumnWidths?: readonly Pick<DataTableColumn<T>, "accessor" | "width">[];
+ onColumnWidthsChange?: (widths: { accessor: string; width: number }[]) => void;
};
export function HomarrDataTable<T>({
@@ -33,6 +36,9 @@ export function HomarrDataTable<T>({
cellPadding,
rowCursor = "auto",
preserveColumnWidths = false,
+ fillColumnAccessor,
+ defaultColumnWidths,
+ onColumnWidthsChange,
className,
columns,
defaultColumnProps,
@@ -43,6 +49,8 @@ export function HomarrDataTable<T>({
}: HomarrDataTableProps<T>) {
const [resizePreviewWidths, setResizePreviewWidths] = useState<Record<string, number>>();
const removeResizeListeners = useRef<() => void>(() => undefined);
+ const internalScrollViewportRef = useRef<HTMLDivElement>(null);
+ const scrollViewportRef = props.scrollViewportRef ?? internalScrollViewportRef;
useEffect(() => () => removeResizeListeners.current(), []);
const handleMouseDownCapture = useCallback(
@@ -75,35 +83,94 @@ export function HomarrDataTable<T>({
if (currentWidth === undefined || nextWidth === undefined) return;
const visualScale = currentCell.getBoundingClientRect().width / currentCell.offsetWidth;
- const minimumWidth = fallbackColumnWidth * visualScale;
+ let currentMinimumWidth = fallbackColumnWidth * visualScale;
+ let nextMinimumWidth = currentMinimumWidth;
const startX = event.clientX;
removeResizeListeners.current();
+ const usesDisplayedWidths = Boolean(
+ preserveColumnWidths &&
+ fillColumnAccessor &&
+ onColumnWidthsChange &&
+ columns.some(({ accessor, hidden }) => !hidden && String(accessor) === fillColumnAccessor),
+ );
+ const previousCursor = document.body.style.cursor;
+ const previousUserSelect = document.body.style.userSelect;
+ if (usesDisplayedWidths) {
+ // Fill widths use displayed pixels; original widths keep compact columns resizable.
+ currentMinimumWidth = getColumnWidth(
+ defaultColumnWidths?.find(({ accessor }) => String(accessor) === currentAccessor)?.width,
+ );
+ nextMinimumWidth = getColumnWidth(
+ defaultColumnWidths?.find(({ accessor }) => String(accessor) === nextAccessor)?.width,
+ );
+ if (currentAccessor !== fillColumnAccessor)
+ currentMinimumWidth = Math.min(fallbackColumnWidth, currentMinimumWidth);
+ if (nextAccessor !== fillColumnAccessor) nextMinimumWidth = Math.min(fallbackColumnWidth, nextMinimumWidth);
+ const viewport = scrollViewportRef.current;
+ if (
+ (currentAccessor === fillColumnAccessor || nextAccessor === fillColumnAccessor) &&
+ viewport &&
+ viewport.scrollWidth > viewport.clientWidth
+ ) {
+ // An overflowing table leaves no spare width for its fill column.
+ currentMinimumWidth = currentWidth;
+ nextMinimumWidth = nextWidth;
+ }
+ // The native resize handler saves logical widths; fill mode stores displayed pixels.
+ event.preventDefault();
+ event.stopPropagation();
+ document.body.style.cursor = "col-resize";
+ document.body.style.userSelect = "none";
+ }
+ let previewWidths: Record<string, number> | undefined;
const handleMouseMove = (moveEvent: MouseEvent) => {
const delta = Math.min(
- nextWidth - minimumWidth,
- Math.max(minimumWidth - currentWidth, moveEvent.clientX - startX),
+ nextWidth - nextMinimumWidth,
+ Math.max(currentMinimumWidth - currentWidth, moveEvent.clientX - startX),
);
- setResizePreviewWidths({
+ previewWidths = {
...widths,
[currentAccessor]: currentWidth + delta,
[nextAccessor]: nextWidth - delta,
- });
+ };
+ setResizePreviewWidths(previewWidths);
};
- const handleResizeEnd = () => {
+ const handleResizeEnd = (endEvent: Event) => {
removeResizeListeners.current();
+ if (usesDisplayedWidths && previewWidths && endEvent.type === "mouseup") {
+ const resizedWidths = [currentAccessor, nextAccessor].flatMap((accessor) => {
+ const width = previewWidths?.[accessor];
+ if (accessor === fillColumnAccessor || width === undefined) return [];
+ return [{ accessor, width }];
+ });
+ if (resizedWidths.length > 0) onColumnWidthsChange?.(resizedWidths);
+ }
requestAnimationFrame(() => requestAnimationFrame(() => setResizePreviewWidths(undefined)));
};
removeResizeListeners.current = () => {
window.removeEventListener("mousemove", handleMouseMove);
window.removeEventListener("mouseup", handleResizeEnd);
window.removeEventListener("blur", handleResizeEnd);
+ if (usesDisplayedWidths) {
+ document.body.style.cursor = previousCursor;
+ document.body.style.userSelect = previousUserSelect;
+ }
+ removeResizeListeners.current = () => undefined;
};
window.addEventListener("mousemove", handleMouseMove);
window.addEventListener("mouseup", handleResizeEnd);
window.addEventListener("blur", handleResizeEnd);
},
- [onMouseDownCapture],
+ [
+ columns,
+ defaultColumnWidths,
+ fillColumnAccessor,
+ onColumnWidthsChange,
+ onMouseDownCapture,
+ preserveColumnWidths,
+ scrollViewportRef,
+ ],
);
const mergedClassName = [
@@ -123,7 +190,19 @@ export function HomarrDataTable<T>({
return columns.map((column) => {
if (column.hidden) return column;
- const width = `${(getRenderedWidth(column) / totalWidth) * 100}%`;
+ let width = `${(getRenderedWidth(column) / totalWidth) * 100}%`;
+ if (
+ preserveColumnWidths &&
+ fillColumnAccessor &&
+ visibleColumns.some(({ accessor }) => String(accessor) === fillColumnAccessor)
+ ) {
+ const renderedWidth = getRenderedWidth(column);
+ const inverseScale = "var(--board-canvas-inverse-scale, 1)";
+ width = `calc(${renderedWidth}px * ${inverseScale})`;
+ if (String(column.accessor) === fillColumnAccessor) {
+ width = `calc(100% - ${totalWidth - renderedWidth}px * ${inverseScale})`;
+ }
+ }
const fittedWidth = { width, minWidth: width, maxWidth: width };
const cellsStyle = column.cellsStyle ?? defaultColumnProps?.cellsStyle;
return {
@@ -132,16 +211,30 @@ export function HomarrDataTable<T>({
cellsStyle: (record: T, index: number) => [cellsStyle?.(record, index), fittedWidth, { padding: cellPadding }],
};
});
- }, [cellPadding, columns, defaultColumnProps?.cellsStyle, resizePreviewWidths]);
+ }, [
+ cellPadding,
+ columns,
+ defaultColumnProps?.cellsStyle,
+ fillColumnAccessor,
+ preserveColumnWidths,
+ resizePreviewWidths,
+ ]);
const minimumTableWidth = useMemo(() => {
if (!preserveColumnWidths) return undefined;
const totalWidth = columns
.filter(({ hidden }) => !hidden)
- .reduce((total, column) => total + getColumnWidth(column.width), 0);
+ .reduce((total, column) => {
+ if (String(column.accessor) === fillColumnAccessor) {
+ // Saved fill widths may include spare space from a wider board.
+ const defaultWidth = defaultColumnWidths?.find(({ accessor }) => accessor === column.accessor)?.width;
+ return total + getColumnWidth(defaultWidth);
+ }
+ return total + getColumnWidth(column.width);
+ }, 0);
// Keep the minimum readable in displayed pixels when the board canvas is zoomed.
return `calc(${totalWidth}px * var(--board-canvas-inverse-scale, 1))`;
- }, [columns, preserveColumnWidths]);
+ }, [columns, defaultColumnWidths, fillColumnAccessor, preserveColumnWidths]);
return (
<DataTable<T>
@@ -154,6 +247,7 @@ export function HomarrDataTable<T>({
textSelectionDisabled
height="100%"
{...props}
+ scrollViewportRef={scrollViewportRef}
striped={false}
columns={fittedColumns}
onMouseDownCapture={handleMouseDownCapture}
diff --git a/packages/widgets/src/common/use-persisted-table-layout.ts b/packages/widgets/src/common/use-persisted-table-layout.ts
index 7b221248c..cf19bb9aa 100644
--- a/packages/widgets/src/common/use-persisted-table-layout.ts
+++ b/packages/widgets/src/common/use-persisted-table-layout.ts
@@ -231,5 +231,5 @@ export const usePersistedTableLayout = <T>({
visibleAccessorSet,
]);
- return { effectiveColumns, storeKey };
+ return { effectiveColumns, storeKey, setMultipleColumnWidths };
};
diff --git a/packages/widgets/src/docker/component.tsx b/packages/widgets/src/docker/component.tsx
index c3e3ba915..273c51c4f 100644
--- a/packages/widgets/src/docker/component.tsx
+++ b/packages/widgets/src/docker/component.tsx
@@ -146,14 +146,14 @@ const createColumns = (
{
accessor: "state",
title: t("field.state.label"),
- width: 100,
+ width: 80,
sortable: sortingEnabled,
render: (container) => <ContainerStateBadge state={container.state} />,
},
{
accessor: "host",
title: t("field.host.label"),
- width: 120,
+ width: 100,
sortable: sortingEnabled,
ellipsis: true,
render: (container) => (
@@ -165,7 +165,7 @@ const createColumns = (
{
accessor: "cpuUsage",
title: invariantTechnicalLabels.cpu,
- width: 80,
+ width: 64,
sortable: sortingEnabled,
render: (container) => {
const cpuUsage = safeValue(container.cpuUsage);
@@ -179,7 +179,7 @@ const createColumns = (
{
accessor: "memoryUsage",
title: t("field.stats.memory.label"),
- width: 100,
+ width: 80,
sortable: sortingEnabled,
render: (container) => {
const memoryUsage = safeValue(container.memoryUsage);
@@ -383,8 +383,8 @@ export default function DockerWidget({
);
const columnVisibility = useMemo(
- () => getDockerColumnVisibility(options.columns, responsiveWidth, isAdvanced),
- [isAdvanced, options.columns, responsiveWidth],
+ () => getDockerColumnVisibility(options.columns, isAdvanced),
+ [isAdvanced, options.columns],
);
const columns = useMemo(() => {
const sortingEnabled = (isAdvanced || options.enableRowSorting) && !isEditMode;
@@ -392,7 +392,7 @@ export default function DockerWidget({
({ accessor }) => columnVisibility[String(accessor) as keyof typeof columnVisibility],
);
}, [actionHandlers, columnVisibility, formatBytes, isAdvanced, isEditMode, options.enableRowSorting, t, tCommon]);
- const { effectiveColumns, storeKey } = usePersistedTableLayout({
+ const { effectiveColumns, storeKey, setMultipleColumnWidths } = usePersistedTableLayout({
columns,
columnAccessors,
columnOrder: options.columnOrder,
@@ -432,6 +432,9 @@ export default function DockerWidget({
<Box style={{ flex: 1, minHeight: 0 }}>
<HomarrDataTable
preserveColumnWidths
+ fillColumnAccessor="name"
+ defaultColumnWidths={columns}
+ onColumnWidthsChange={setMultipleColumnWidths}
isEditMode={isEditMode}
cellPadding={isAdvanced || width < 400 ? "2px 8px" : "4px 8px"}
className="docker-table"
diff --git a/packages/widgets/src/docker/layout.spec.ts b/packages/widgets/src/docker/layout.spec.ts
index 8813a5ce7..c726d0545 100644
--- a/packages/widgets/src/docker/layout.spec.ts
+++ b/packages/widgets/src/docker/layout.spec.ts
@@ -5,27 +5,19 @@ import { getDockerColumnVisibility, getDockerFooterVisibility } from "./layout";
const columns = ["name", "state", "host", "cpuUsage", "memoryUsage", "actions"] as const;
describe("getDockerColumnVisibility", () => {
- test("prioritizes identity and state in a narrow compact widget", () => {
- expect(getDockerColumnVisibility(columns, 240, false)).toEqual({
+ test("keeps every configured column in a compact widget", () => {
+ expect(getDockerColumnVisibility(columns, false)).toEqual({
name: true,
state: true,
- host: false,
- cpuUsage: false,
- memoryUsage: false,
- actions: false,
+ host: true,
+ cpuUsage: true,
+ memoryUsage: true,
+ actions: true,
});
});
- test("honors the configured compact column set at every width", () => {
- expect(getDockerColumnVisibility(["name", "memoryUsage"], 200, false)).toEqual({
- name: true,
- state: false,
- host: false,
- cpuUsage: false,
- memoryUsage: true,
- actions: false,
- });
- expect(getDockerColumnVisibility(["name", "memoryUsage"], 800, false)).toEqual({
+ test("honors the configured compact column set", () => {
+ expect(getDockerColumnVisibility(["name", "memoryUsage"], false)).toEqual({
name: true,
state: false,
host: false,
@@ -36,7 +28,7 @@ describe("getDockerColumnVisibility", () => {
});
test("uses every expert column in advanced mode", () => {
- expect(getDockerColumnVisibility([], 240, true)).toEqual({
+ expect(getDockerColumnVisibility([], true)).toEqual({
name: true,
state: true,
host: true,
diff --git a/packages/widgets/src/docker/layout.ts b/packages/widgets/src/docker/layout.ts
index 8cc45e779..474068310 100644
--- a/packages/widgets/src/docker/layout.ts
+++ b/packages/widgets/src/docker/layout.ts
@@ -2,19 +2,10 @@ export type DockerColumn = "name" | "state" | "host" | "cpuUsage" | "memoryUsage
export const getDockerColumnVisibility = (
configuredColumns: readonly DockerColumn[],
- width: number,
isAdvanced: boolean,
): Record<DockerColumn, boolean> => {
const configured = new Set(configuredColumns);
- const priority: DockerColumn[] = ["name", "state", "cpuUsage", "memoryUsage", "host", "actions"];
- let budget = priority.length;
- if (!isAdvanced) {
- if (width < 280) budget = 2;
- else if (width < 420) budget = 3;
- else if (width < 640) budget = 4;
- }
- const visible = new Set(priority.filter((column) => configured.has(column)).slice(0, budget));
- const isVisible = (column: DockerColumn) => isAdvanced || visible.has(column);
+ const isVisible = (column: DockerColumn) => isAdvanced || configured.has(column);
return {
name: isVisible("name"),