Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

6.7 KB
Raw Blame
import { createEffect, createSignal, on, onCleanup, onMount, splitProps, type JSX } from 'solid-js';

import { TableCell, TableRow, type TableRowProps } from './Table';

export const INLINE_DETAIL_TABLE_CELL_CLASS = 'p-0 border-b border-border bg-surface-alt';
// overflow-x-clip keeps over-wide content from painting outside the row's
// border below the lg breakpoint (issue #1622) without creating a scroll
// container the way overflow-hidden would. Below lg the sticky content is as
// wide as the table's visible area: 100cqi resolves against the table scroll
// shell (a size container), so a guess at the page chrome cannot leave an
// empty strip beside the drawer or push it past the visible edge. Every
// drawer shares this wrapper (no per-table override): a table that replaced
// it silently lost the phone width cap. Layout belongs inside the children.
export const INLINE_DETAIL_TABLE_CONTENT_CLASS =
  'sticky left-0 min-w-0 max-w-[100cqi] whitespace-normal overflow-x-clip px-2 py-3 sm:px-4 sm:py-4 lg:static lg:max-w-none lg:overflow-x-visible';

export interface InlineDetailTableRowProps extends TableRowProps {
  cellId?: string;
  colSpan?: number;
  colspan?: number;
  containClicks?: boolean;
  children?: JSX.Element;
}

export function InlineDetailTableRow(props: InlineDetailTableRowProps) {
  const [local, rest] = splitProps(props, [
    'cellId',
    'ref',
    'children',
    'class',
    'colSpan',
    'colspan',
    'containClicks',
  ]);
  const containClicks = () => local.containClicks ?? true;
  const requestedColspan = () => local.colspan ?? local.colSpan ?? 1;
  const [effectiveColspan, setEffectiveColspan] = createSignal(requestedColspan());
  let detailRow: HTMLTableRowElement | undefined;

  onCleanup(() => {
    const activeElement = document.activeElement;
    const cellId = local.cellId;
    if (!cellId || !activeElement || !detailRow?.contains(activeElement)) return;

    // A drawer close commonly disposes the button that currently owns focus.
    // Restore it to the disclosure controlling this detail row once Solid has
    // removed the row, so keyboard users stay anchored to the same resource.
    queueMicrotask(() => {
      const disclosure = Array.from(
        document.querySelectorAll<HTMLButtonElement>('button[aria-controls]'),
      ).find((button) => button.getAttribute('aria-controls') === cellId);
      // Closing or replacing a detail row can happen while the surrounding
      // live table is well below the top of the page. Restore keyboard focus
      // without letting the browser scroll the disclosure into view: the
      // caller owns any deliberate reveal, while polling must not move the
      // operator's viewport.
      disclosure?.focus({ preventScroll: true });
    });
  });

  const syncColspanToVisibleSummaryCells = () => {
    const summaryRow = detailRow?.previousElementSibling;
    if (!(summaryRow instanceof HTMLTableRowElement)) {
      setEffectiveColspan(requestedColspan());
      return;
    }

    const visibleColspan = Array.from(
      summaryRow.querySelectorAll<HTMLTableCellElement>(':scope > th, :scope > td'),
    ).reduce(
      (total, cell) =>
        window.getComputedStyle(cell).display === 'none' ? total : total + cell.colSpan,
      0,
    );
    setEffectiveColspan(visibleColspan || requestedColspan());
  };

  onMount(() => {
    syncColspanToVisibleSummaryCells();
    window.addEventListener('resize', syncColspanToVisibleSummaryCells);

    const tableShell = detailRow?.closest('.table-scroll-shell');
    const observer =
      tableShell && typeof ResizeObserver === 'function'
        ? new ResizeObserver(syncColspanToVisibleSummaryCells)
        : undefined;
    if (tableShell) observer?.observe(tableShell);

    // A column picker, or a column that depends on the data (one vCenter
    // versus several), can add, remove or hide summary cells while this row
    // stays open, without resizing the table shell. Watch the summary row's
    // own cells so the detail cell never keeps a stale span. Changes deeper
    // inside a cell are ignored: live metric updates must not re-measure.
    const summaryRow = detailRow?.previousElementSibling;
    let summaryCellsObserver: MutationObserver | undefined;
    if (summaryRow instanceof HTMLTableRowElement && typeof MutationObserver === 'function') {
      summaryCellsObserver = new MutationObserver((records) => {
        const summaryCellsChanged = records.some(
          (record) =>
            record.target === summaryRow ||
            (record.type === 'attributes' && record.target.parentNode === summaryRow),
        );
        if (summaryCellsChanged) syncColspanToVisibleSummaryCells();
      });
      summaryCellsObserver.observe(summaryRow, {
        childList: true,
        subtree: true,
        attributes: true,
        attributeFilter: ['class', 'style', 'hidden', 'colspan'],
      });
    }

    onCleanup(() => {
      window.removeEventListener('resize', syncColspanToVisibleSummaryCells);
      observer?.disconnect();
      summaryCellsObserver?.disconnect();
    });
  });

  // Callers that change the span they ask for (and rows rendered without a
  // summary row, which fall back to that span) re-sync on the change.
  createEffect(on(requestedColspan, syncColspanToVisibleSummaryCells, { defer: true }));

  // Chromium keeps a fixed-layout table's column widths when a spanning
  // cell's colspan grows over a column added since the table last laid out,
  // so the detail cell would stop short of the new column. A change to the
  // cell's own width makes it recompute them; restore it after one layout.
  // The effect is not deferred so its first run records the mounted span.
  createEffect(
    on(effectiveColspan, (colspan, previousColspan) => {
      const detailCell = detailRow?.cells[0];
      if (!detailCell || previousColspan === undefined || colspan <= previousColspan) return;
      const width = detailCell.style.width;
      detailCell.style.width = '0px';
      void detailCell.offsetWidth;
      detailCell.style.width = width;
    }),
  );

  return (
    <TableRow
      ref={(row) => {
        detailRow = row;
        // A consumer's cleanup ref must not replace our measuring/focus owner.
        if (typeof local.ref === 'function') local.ref(row);
      }}
      class={local.class}
      {...rest}
      data-inline-detail-row
    >
      <TableCell
        id={local.cellId}
        colspan={effectiveColspan()}
        class={INLINE_DETAIL_TABLE_CELL_CLASS}
      >
        <div
          class={INLINE_DETAIL_TABLE_CONTENT_CLASS}
          onClick={(event) => {
            if (containClicks()) {
              event.stopPropagation();
            }
          }}
        >
          {local.children}
        </div>
      </TableCell>
    </TableRow>
  );
}

export default InlineDetailTableRow;