Web UI on Ant Design, scrollable factory columns, fix empty page after deleting the open chat
Buttons, fields, drop-downs, check boxes, tabs, dialogs and notices are Ant Design components; their tokens come from the theme variables (web/src/ui). Every factory board column has its own vertical scrollbar. ChatView rendered fewer hooks once its chat was deleted. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
90b9ab6670
commit
6b1a1742b2
79 files changed
+2628
-1393
No files matched your search
@@ -0,0 +1,90 @@
|
||||
// Ant Design setup: its tokens come from the app's theme variables (src/themes/*.css), so every theme applies to
|
||||
// the components too; the language follows the app's language.
|
||||
import { App as AntApp, ConfigProvider, theme as antTheme } from "antd";
|
||||
import deDE from "antd/locale/de_DE";
|
||||
import enUS from "antd/locale/en_US";
|
||||
import { useMemo, useSyncExternalStore, type ReactNode } from "react";
|
||||
import { onThemeChange } from "../theme";
|
||||
import { getLang, onLangChange } from "../i18n";
|
||||
|
||||
/** Any CSS colour (hex, color-mix(), …) as the hex form Ant Design can calculate with. */
|
||||
function resolver() {
|
||||
const probe = document.createElement("span");
|
||||
probe.style.display = "none";
|
||||
document.body.appendChild(probe);
|
||||
const ctx = document.createElement("canvas").getContext("2d", { willReadFrequently: true })!;
|
||||
return {
|
||||
color(name: string, fallback: string) {
|
||||
probe.style.color = fallback;
|
||||
probe.style.color = `var(${name}, ${fallback})`;
|
||||
ctx.clearRect(0, 0, 1, 1);
|
||||
ctx.fillStyle = getComputedStyle(probe).color;
|
||||
ctx.fillRect(0, 0, 1, 1);
|
||||
const [r, g, b] = ctx.getImageData(0, 0, 1, 1).data;
|
||||
return `#${[r, g, b].map((v) => v.toString(16).padStart(2, "0")).join("")}`;
|
||||
},
|
||||
done: () => probe.remove(),
|
||||
};
|
||||
}
|
||||
|
||||
function themeConfig() {
|
||||
const root = document.documentElement;
|
||||
const dark = root.dataset.mode !== "light";
|
||||
const r = resolver();
|
||||
const c = (name: string, fallback = "#888888") => r.color(name, fallback);
|
||||
const [bg0, bg1, bg2, bg3] = [c("--bg0"), c("--bg1"), c("--bg2"), c("--bg3")];
|
||||
const [fg0, fg1, fg2, fg3, muted] = [c("--fg0"), c("--fg1"), c("--fg2"), c("--fg3"), c("--muted")];
|
||||
const [accent, onAccent, red, green, blue, warn] = [c("--accent"), c("--on-accent"), c("--red"), c("--green"), c("--blue"), c("--accent2")];
|
||||
r.done();
|
||||
const css = getComputedStyle(root);
|
||||
return {
|
||||
algorithm: dark ? antTheme.darkAlgorithm : antTheme.defaultAlgorithm,
|
||||
token: {
|
||||
colorPrimary: accent,
|
||||
colorError: red,
|
||||
colorSuccess: green,
|
||||
colorWarning: warn,
|
||||
colorInfo: blue,
|
||||
colorLink: blue,
|
||||
colorBgBase: bg0,
|
||||
colorTextBase: fg1,
|
||||
colorBgLayout: bg0,
|
||||
colorBgContainer: bg1,
|
||||
colorBgElevated: bg1,
|
||||
colorBorder: bg3,
|
||||
colorBorderSecondary: bg2,
|
||||
colorText: fg1,
|
||||
colorTextSecondary: fg2,
|
||||
colorTextTertiary: fg3,
|
||||
colorTextQuaternary: muted,
|
||||
colorTextPlaceholder: muted,
|
||||
colorTextHeading: fg0,
|
||||
borderRadius: 6,
|
||||
// Below the full-screen image viewer (200), above the app's own panels and menus.
|
||||
zIndexPopupBase: 100,
|
||||
fontSize: 14,
|
||||
fontFamily: css.getPropertyValue("--font").trim() || "system-ui, sans-serif",
|
||||
fontFamilyCode: css.getPropertyValue("--mono").trim() || "monospace",
|
||||
},
|
||||
components: {
|
||||
Button: { primaryColor: onAccent, dangerColor: onAccent, defaultBg: bg2, defaultHoverBg: bg2, fontWeight: 500, primaryShadow: "none", defaultShadow: "none", dangerShadow: "none" },
|
||||
Select: { optionSelectedBg: bg3 },
|
||||
Modal: { contentBg: bg1, headerBg: bg1 },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
let version = 0;
|
||||
const subscribeTheme = (fn: () => void) => onThemeChange(() => (version++, fn()));
|
||||
|
||||
export function AntdProvider({ children }: { children: ReactNode }) {
|
||||
const themeVersion = useSyncExternalStore(subscribeTheme, () => version);
|
||||
const lang = useSyncExternalStore(onLangChange, getLang);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
const theme = useMemo(themeConfig, [themeVersion]);
|
||||
return (
|
||||
<ConfigProvider theme={theme} locale={lang === "de" ? deDE : enUS} button={{ autoInsertSpace: false }}>
|
||||
<AntApp component={false}>{children}</AntApp>
|
||||
</ConfigProvider>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// The app's dialog window: an Ant Design modal around the dialog's own content (heading, fields, action row).
|
||||
import { Modal as AntModal } from "antd";
|
||||
import type { HTMLAttributes, ReactNode } from "react";
|
||||
|
||||
interface Props extends Omit<HTMLAttributes<HTMLDivElement>, "onClick"> {
|
||||
/** Escape and a click outside close the dialog; without it the dialog stays until its own buttons close it. */
|
||||
onClose?: () => void;
|
||||
/** Extra classes of the content ("wide" makes the window wider). */
|
||||
className?: string;
|
||||
children?: ReactNode;
|
||||
}
|
||||
|
||||
export function Modal({ onClose, className = "", children, ...rest }: Props) {
|
||||
const wide = /\bwide\b/.test(className);
|
||||
return (
|
||||
<AntModal
|
||||
open
|
||||
centered
|
||||
footer={null}
|
||||
closable={false}
|
||||
width={wide ? 960 : 560}
|
||||
keyboard={!!onClose}
|
||||
mask={{ closable: !!onClose }}
|
||||
onCancel={() => onClose?.()}
|
||||
destroyOnHidden
|
||||
>
|
||||
<div className={`modal ${className}`.trim()} {...rest}>
|
||||
{children}
|
||||
</div>
|
||||
</AntModal>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// A drop-down with the interface of a native <select> (value, onChange with `target.value`, <option> and
|
||||
// <optgroup> children) drawn by Ant Design.
|
||||
import { Select as AntSelect } from "antd";
|
||||
import { Children, Fragment, isValidElement, useState, type CSSProperties, type ReactNode } from "react";
|
||||
|
||||
export interface SelectChange {
|
||||
target: { value: string };
|
||||
currentTarget: { value: string };
|
||||
}
|
||||
|
||||
interface Props {
|
||||
value?: string | number;
|
||||
defaultValue?: string | number;
|
||||
onChange?: (e: SelectChange) => void;
|
||||
children?: ReactNode;
|
||||
className?: string;
|
||||
style?: CSSProperties;
|
||||
title?: string;
|
||||
id?: string;
|
||||
disabled?: boolean;
|
||||
autoFocus?: boolean;
|
||||
required?: boolean;
|
||||
size?: "small" | "middle" | "large";
|
||||
"aria-label"?: string;
|
||||
onClick?: (e: React.MouseEvent) => void;
|
||||
}
|
||||
|
||||
type Option = { value: string; label: ReactNode; text: string; disabled?: boolean; title?: string };
|
||||
type Entry = Option | { label: ReactNode; title?: string; options: Option[] };
|
||||
|
||||
const textOf = (node: ReactNode): string => {
|
||||
if (typeof node === "string" || typeof node === "number") return String(node);
|
||||
if (Array.isArray(node)) return node.map(textOf).join("");
|
||||
return isValidElement<{ children?: ReactNode }>(node) ? textOf(node.props.children) : "";
|
||||
};
|
||||
|
||||
function entries(children: ReactNode): Entry[] {
|
||||
const out: Entry[] = [];
|
||||
Children.forEach(children, (child) => {
|
||||
if (!isValidElement<any>(child)) return;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
const p = child.props as any;
|
||||
if (child.type === Fragment) out.push(...entries(p.children));
|
||||
else if (child.type === "optgroup") out.push({ label: p.label, options: entries(p.children).flatMap((e) => ("options" in e ? e.options : [e])) });
|
||||
else if (child.type === "option") {
|
||||
const text = textOf(p.children);
|
||||
out.push({ value: String(p.value ?? text), label: p.children, text, disabled: p.disabled, title: p.title ?? text });
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
|
||||
const SEARCH_FROM = 12;
|
||||
|
||||
export function Select({ value, defaultValue, onChange, children, className, style, size, ...rest }: Props) {
|
||||
const options = entries(children);
|
||||
const flat = options.flatMap((e) => ("options" in e ? e.options : [e]));
|
||||
// Like a native select, an uncontrolled one starts with its first option.
|
||||
const [own, setOwn] = useState(() => String(defaultValue ?? flat[0]?.value ?? ""));
|
||||
const current = value === undefined ? own : String(value);
|
||||
return (
|
||||
<AntSelect<string, Entry>
|
||||
{...rest}
|
||||
size={size}
|
||||
className={className}
|
||||
style={style}
|
||||
value={flat.some((o) => o.value === current) ? current : undefined}
|
||||
options={options}
|
||||
popupMatchSelectWidth={false}
|
||||
showSearch={flat.length >= SEARCH_FROM ? { filterOption: (q, o) => String((o as Option | undefined)?.text ?? "").toLowerCase().includes(q.toLowerCase()) } : false}
|
||||
onChange={(v) => {
|
||||
setOwn(v);
|
||||
onChange?.({ target: { value: v }, currentTarget: { value: v } });
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user