wrolpi/app/src/components/ui/Feedback.tsx
Roland eba9bbf7de
review: close the mixed-unit gaps the scale left behind
Seven review comments, all valid.  The common thread is the one the guard was supposed to
catch and did not: geometry converted to rem beside geometry that stayed px.

The guard was narrower than its name.  It missed:

- `transform`, `flex` and `flex-basis`, so `translateX(26px)` and `flex: 1 1 200px` were
  invisible on properties nobody thought to list;
- every NEGATIVE length, because the lookbehind treated the minus sign as part of the
  preceding token -- `left: -7px` and `margin-top: -2.5px` position the eyelet on a tag whose
  diamond HAD been converted, and `right: -3px` / `bottom: -3px` place the corner badge on a
  converted icon.  Each is a piece of geometry that stopped tracking the piece it belongs to.

With both fixed it found nine values, including one no comment mentioned (`flex: 1 1 200px`).
All nine converted, except the three in a dead `.toggle` / `.slider` block: nothing has
referenced those classes since Toggle became a Mantine Switch, and it was ALREADY broken --
track and knob in rem, travel still `translateX(26px)`, so the thumb stopped ~4px short of a
54px track.  Dead CSS that is also wrong is a trap for whoever revives it, so it is deleted.

The tag eyelet is centred with `translateY(-50%)` rather than a negative margin of half its
own height, so the two pieces cannot drift again whatever the scale.

Also from the review:

- A runtime check, which is what the source guards were missing.  Three cases in
  ui-layout.cy.js measure the real thing: the root font-size is the declared multiple of 16,
  our text AND a Mantine control both grow by the same factor when the scale changes, and a
  hairline does not.  The second is the one that catches `theme.scale` being set and silently
  cancelling the root.
- `max-height: max(..., 56.25cqw)` now follows a plain rem declaration.  An engine without
  container query units throws out the whole declaration, which would have left the poster
  with NO cap -- a portrait cover standing as tall as the card is wide.  Older engines clamp;
  newer ones take the second.
- `pxToRem` in components/ui/scale.ts, replacing the `/ 16` written out in CardGroup and
  Placeholder.
- The map overlays: 20 values across the layer panel, add-pin dialog, search panel and
  context menu, whose type had grown 10% while their padding had not.  Left in px on purpose:
  the context menu's `left`/`top` (a real click coordinate), maplibre's own `ScaleControl`
  maxWidth, and the `calc(100% - 100px)` reserve for maplibre's px-sized controls.  The
  `100vh - 120px` reserve DID become rem, since the chrome it reserves for is ours.
- The keyboard-shortcuts spec no longer deletes `.mantine-Notifications-root` from the DOM.
  The reviewer was right that it is brittle; the endpoints are mocked instead, now that the
  probe named them: `/api/tag`, `/api/events/feed`, `/api/files/search` and
  `/api/files/worker_status`.  Two of those were already "mocked" as `/api/tags` and
  `/api/events`, which nothing calls -- the wrong paths were the reason a toast appeared at
  all.  Verified under CI's condition (`forceNetworkError` on everything unmocked): 12
  passing with no DOM surgery.

What is NOT done, deliberately: about 176 numeric geometry values remain in 23 feature files.
The map overlays were the visible ones.  Sweeping the rest is a change to 23 files nobody has
reviewed, so instead the guard's name now says "in our stylesheets" rather than implying the
whole app, and a new ratchet keeps numeric geometry out of the component library so the
eleventh inline `gap: 10` cannot be added there.

Both new guards planted against.  Verified: 1142 jest / 65 suites, 251 ui-layout + 10
modal-stack component tests, all 50 e2e specs, clean build, map overlays measured to track the
scale (panel minWidth 200 -> 220, search 320 -> 352) and screenshotted.
2026-08-08 18:05:51 -06:00

237 lines
8.7 KiB
TypeScript

import React from 'react';
import {Loader as MLoader, Skeleton} from '@mantine/core';
import {Icon} from './Icon';
import {pxToRem} from './scale';
import {RoleName, SemanticColorName} from '../../themes/mantine';
/*
* Feedback: messages, labels, progress, status text, loaders.
*
* These carry the design rules that change form (not just color) between
* themes, so their markup is ours and their values come from tokens.
*/
// ------------------------------------------------------------------ Messages
/*
* `error` is the original spelling and stays: 26 call sites use it, and renaming those
* is app-level churn for no behaviour change. `danger` is the same thing under the
* name the token uses, and is what new code should write.
*/
export type MessageKind = 'info' | 'success' | 'warning' | 'error' | 'danger';
/** The role each kind means. Never a hue -- see the note on roles in tokens.css. */
const messageRoles: Record<MessageKind, RoleName> = {
info: 'info',
success: 'success',
warning: 'warning',
error: 'danger',
danger: 'danger',
};
export interface MessageProps {
kind?: MessageKind;
title?: React.ReactNode;
children?: React.ReactNode;
/** Semantic icon name or a Tabler component, shown to the left of the text. */
icon?: string | React.ComponentType<any>;
/** Renders a dismiss button. Omit for messages the user cannot clear. */
onDismiss?: () => void;
className?: string;
}
export function Message({kind = 'info', title, children, icon, onDismiss, className}: MessageProps) {
return <div
// Errors and warnings interrupt; info and success are announced politely.
role={messageRoles[kind] === 'danger' ? 'alert' : 'status'}
className={['wrolpi-message', messageRoles[kind] === 'danger' ? 'wrolpi-message-error' : '', className]
.filter(Boolean).join(' ')}
style={{['--message-color' as string]: `var(--${messageRoles[kind]})`}}
>
{icon && <div className='wrolpi-message-icon'>
{typeof icon === 'string' ? <Icon name={icon} size={20}/> : <Icon component={icon} size={20}/>}
</div>}
<div className='wrolpi-message-content'>
{title && <div className='wrolpi-message-title'>{title}</div>}
{children && <div className='wrolpi-message-body'>{children}</div>}
</div>
{onDismiss && <button
type='button'
className='wrolpi-message-dismiss'
aria-label='Dismiss'
onClick={onDismiss}
>
<Icon name='close' size={16}/>
</button>}
</div>
}
// -------------------------------------------------------------------- Labels
export interface LabelProps {
color?: SemanticColorName | RoleName | 'black' | 'white';
icon?: string | React.ComponentType<any>;
/**
* Draw it as a physical tag — pointed left edge and an eyelet — rather than a plain
* chip. Reserved for things that actually are tags: the same component also carries
* the count badges in the Tags table, and a point on a number reads as an arrow.
*/
tag?: boolean;
children?: React.ReactNode;
onClick?: React.MouseEventHandler<HTMLSpanElement>;
className?: string;
}
/** A chip. Filled in light, dark and amber; an outline in night, where a filled
* patch would be a bright surface. */
export function Label({color = 'grey', icon, tag, children, onClick, className}: LabelProps) {
return <span
className={['wrolpi-label', `wrolpi-label-${color}`, tag ? 'wrolpi-tag' : '', className]
.filter(Boolean).join(' ')}
style={{['--label-color' as string]: `var(--${color})`, cursor: onClick ? 'pointer' : undefined}}
onClick={onClick}
>
{icon && (typeof icon === 'string' ? <Icon name={icon} size={14}/> : <Icon component={icon} size={14}/>)}
{children}
</span>
}
// ------------------------------------------------------------------ Progress
export interface ProgressProps {
/** 0-100. Values outside the range are clamped. */
percent?: number;
/**
* Work is happening but its size is unknown — an upload that has not reported yet.
* Animates instead of sitting at 0%, which otherwise reads as stalled.
*/
indeterminate?: boolean;
/** Show the percentage inside the bar. */
showPercent?: boolean;
color?: SemanticColorName | RoleName;
/** Replaces the percentage with arbitrary text (e.g. "2.1 GB / 5.3 GB"). */
label?: React.ReactNode;
className?: string;
}
export function Progress({
percent = 0, showPercent = true, color = 'blue', label, indeterminate, className,
}: ProgressProps) {
const clamped = Math.min(100, Math.max(0, Number.isFinite(percent) ? percent : 0));
return <div
className={['wrolpi-progress', indeterminate ? 'wrolpi-progress-indeterminate' : '', className]
.filter(Boolean).join(' ')}
role='progressbar'
// An indeterminate bar reports no value, which is what tells assistive tech the
// amount is unknown rather than zero.
aria-valuenow={indeterminate ? undefined : Math.round(clamped)}
aria-valuemin={0}
aria-valuemax={100}
>
<div
className='wrolpi-progress-fill'
style={{
width: indeterminate ? undefined : `${clamped}%`,
['--progress-color' as string]: `var(--${color})`,
}}
/>
{(showPercent || label) && <span className='wrolpi-progress-text'>
{label ?? `${Math.round(clamped)}%`}
</span>}
</div>
}
// -------------------------------------------------------------------- Status
export type StatusKind = 'complete' | 'active' | 'pending' | 'failed';
/*
* Status was the one component already doing this correctly: it had per-theme overrides
* in ui.css because night has no hues to distinguish four states with. Those are gone --
* the roles carry the brightness ramp now, so this table is the whole of it and every
* theme is served by the same rule.
*/
const statusRoles: Record<StatusKind, RoleName> = {
complete: 'success',
active: 'info',
pending: 'neutral',
failed: 'danger',
};
const statusIcons: Record<StatusKind, string> = {
complete: 'check',
active: 'circle notch',
pending: 'circle',
failed: 'x',
};
export interface StatusProps {
kind: StatusKind;
children?: React.ReactNode;
/** Hide the leading icon. */
plain?: boolean;
}
/**
* Status text. Encoded by hue in light and dark; night mode has no hue to
* spend, so brightness carries it (see ui.css). Callers name the state, never
* the color.
*/
export function Status({kind, children, plain}: StatusProps) {
return <span
className={`wrolpi-status wrolpi-status-${kind}`}
style={{['--status-color' as string]: `var(--${statusRoles[kind]})`}}
>
{!plain && <Icon name={statusIcons[kind]} size={14} loading={kind === 'active'}/>}
{children}
</span>
}
// ------------------------------------------------------------------- Loaders
export interface LoaderProps {
size?: number | 'xs' | 'sm' | 'md' | 'lg' | 'xl';
label?: string;
}
export function Loader({size = 'sm', label}: LoaderProps) {
return <MLoader size={size} color='var(--blue)' aria-label={label ?? 'Loading'}/>
}
export interface LoadingProps {
/** What is being waited on: "Loading backups…". Also the accessible name. */
children?: React.ReactNode;
size?: LoaderProps['size'];
/** Vertical padding around the loader. */
padding?: number | string;
}
/**
* A centered loader with a caption, for a region that has nothing to show yet.
* Replaces Semantic's `<Loader active inline='centered'>`, which 19 call sites
* used with their own wrapper div.
*/
export function Loading({children, size = 'sm', padding = '2em'}: LoadingProps) {
// `padding` is the caller's, so it stays inline; the rest is in ui.css so it scales.
return <div className='wrolpi-loading' style={{padding}}>
<Loader size={size} label={typeof children === 'string' ? children : undefined}/>
{children && <div className='wrolpi-loading-caption'>{children}</div>}
</div>
}
/** Placeholder for content that has not arrived. Replaces Semantic's Placeholder. */
export function Placeholder({lines = 3, height = 12}: {lines?: number; height?: number}) {
return <div className='wrolpi-placeholder'>
{Array.from({length: lines}, (_, index) => <Skeleton
key={index}
// In rem so a placeholder line matches the height of the text it stands in for.
height={pxToRem(height)}
// A ragged last line reads as text rather than a block.
width={index === lines - 1 ? '60%' : '100%'}
radius={0}
animate
/>)}
</div>
}
export {Skeleton};