Files
youtube-generator/src/components/ui/ValidatedInput.tsx
T
poli0981andClaude Opus 5.5 a2715f9c69 feat(ui): redesign — design system, app shell, command palette, every page
A new, compact interface on Radix primitives, Motion, cmdk and sonner.

Design system
- Tokens for surfaces, text, accent and states in dark and light, used
  everywhere (no more hard-coded red/yellow/pink); Inter and JetBrains
  Mono self-hosted; compact controls that grow to 44px on touch screens.
- Primitives rebuilt on Radix: Button, IconButton (cannot render without
  a name), Field/Input/Select/Textarea (labels wired to controls; the ten
  selects that shared id="type" are gone), Switch, Checkbox, Tabs,
  SegmentedControl, Accordion (closed content is inert), Dialog,
  AlertDialog, Sheet, Popover, Tooltip, Card, Badge, Banner, EmptyState,
  PageHeader, NumberField.
- Animated icons (copy→check, sun↔moon, hover micro-motion on the nav)
  that follow the OS reduce-motion setting.

App shell
- Collapsible sidebar with grouped pages and a sliding active marker;
  top bar with the command palette, output/interface language, theme
  and donate; on phones a bottom bar with four pages plus "More".
- Ctrl/⌘+K command palette: go anywhere, copy title/description/tags,
  apply a profile/preset/template, switch theme or languages.
- Skip link, one h1 per page, <main> focusable; toasts no longer share a
  corner with three other floating elements.

Pages
- Editor: two columns with a sticky live preview (tabs, counters, copy)
  or a sheet on phones; sections reordered so video type and game come
  first; genres are a searchable picker instead of a wall of 42 chips;
  expand/collapse all; a button to the Output page.
- Output: a YouTube preview (what search results and the "…more" fold
  show), language tabs, Copy All in the header (floating on phones).
- Profiles/presets/templates share one card with Apply + Undo; History,
  Logs, Batch, Social, Playlist, Settings, About, Legal, the consent
  screen and the error pages all use the same primitives.

Fixes
- Ctrl/⌘+Shift+C ignored the YouTube limits and Strict Mode.
- Number settings clamped on every keystroke; they now save on blur.
- Hard-coded English on Playlist, Batch, Logs, Profiles and Settings.
- Flags as language icons (Windows shows letters) → native name + code.
- The consent screen could not change language or theme.
- About: dead Discord invite removed; contact addresses by purpose.
- Desktop window is resizable (1280×820, min 960×640).

Initial load 262 KB gzip (budget 280): the command palette, the genre
list and the Output page load on demand.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-30 20:42:43 +07:00

190 lines
7.3 KiB
TypeScript

import { useState, useCallback, useEffect, useRef, type ClipboardEvent } from "react";
import { useTranslation } from "react-i18next";
import type { ValidationResult } from "@utils/validation";
import { usePendingInvalidStore } from "@store/pending-invalid-store";
import clsx from "clsx";
import { Field, controlClasses } from "./Field";
interface ValidatedInputProps {
label: string;
/**
* Stable id matching the one `collectEditorIssues` uses for the same field
* (`storeLinks.steam`, `contactEmail`). Supplying it lets Strict Mode see
* text that is invalid *and therefore never committed to the editor store* —
* without it, such a field is invisible to every gate outside this page.
*/
fieldId?: string;
/** i18n key naming the field in the Strict Mode banner. */
labelKey?: string;
value: string;
onChange: (value: string) => void;
validate: (value: string) => ValidationResult;
placeholder?: string;
helpText?: string;
inputMode?: "text" | "email" | "url" | "numeric" | "tel" | "search" | "decimal";
autoComplete?: string;
enterKeyHint?: "enter" | "done" | "go" | "next" | "previous" | "search" | "send";
/**
* Hard cap on typed/pasted length — see `src/config/field-limits.ts`. This
* component does not spread props onto the input, so unlike `Input` it needs
* the prop declared explicitly.
*/
maxLength?: number;
/**
* Veto or rewrite a change before it is validated or committed. Return the
* (possibly adjusted) next value to accept it, or `null` to reject the
* keystroke outright, leaving the field exactly as it was.
*
* Used for constraints a validator cannot express without destroying data —
* the three-address email cap, where rejecting the 4th address must not
* disturb the three already typed.
*/
beforeChange?: (next: string, prev: string) => string | null;
/** Shown instead of `helpText` when `beforeChange` rejects a change. */
blockedMessage?: string;
/**
* Forwarded to the inner `<input>`. Used by StoreLinkEditor to sniff a
* pasted URL and auto-fill the Game Name field — see
* `extractGameNameFromUrl`. The handler runs alongside the normal paste
* flow; nothing here interferes with the value committed via onChange.
*/
onPaste?: (e: ClipboardEvent<HTMLInputElement>) => void;
}
export function ValidatedInput({
label,
fieldId,
labelKey,
value,
onChange,
validate,
placeholder,
helpText,
inputMode,
autoComplete,
enterKeyHint,
maxLength,
beforeChange,
blockedMessage,
onPaste,
}: ValidatedInputProps) {
const { t } = useTranslation("ui");
const [displayValue, setDisplayValue] = useState(value);
const [error, setError] = useState<string | null>(null);
const [touched, setTouched] = useState(false);
const [blocked, setBlocked] = useState(false);
// Re-sync when the value changes underneath us — applying a profile, preset
// or template, or resetting the editor. Without this the box kept showing
// whatever was typed first and silently disagreed with the store, because
// `useState(value)` only reads the prop on mount.
//
// Guarded on `displayValue` so it never fights the user's own typing: while
// they type, `value` and `displayValue` already agree (or `value` is the last
// valid one, in which case an external change is what we want to adopt).
const lastExternalValue = useRef(value);
useEffect(() => {
if (value !== lastExternalValue.current) {
lastExternalValue.current = value;
setDisplayValue(value);
setError(null);
setBlocked(false);
}
}, [value]);
const handleChange = useCallback(
(rawValue: string) => {
const next = beforeChange ? beforeChange(rawValue, displayValue) : rawValue;
if (next === null) {
// Rejected outright — leave both the display and the committed value
// untouched so nothing already entered is lost.
setBlocked(true);
setTouched(true);
return;
}
setBlocked(false);
setDisplayValue(next);
lastExternalValue.current = next;
const result = validate(next);
if (result.valid && !result.error) {
// Fully valid: commit the value to the store.
setError(null);
onChange(next);
} else if (result.valid && result.error) {
// Warning (e.g. prefix mismatch from validateUrlWithPrefix).
// Surface the warning but still commit so the user can decide.
setError(t(result.error, result.errorParams as Record<string, string>));
onChange(next);
} else {
// Invalid: surface the error and DON'T commit, so the bad input never
// reaches the generated description/tags.
//
// Before v0.35.0 this branch called `onChange("")`, which wiped the
// stored value while the box still showed the user's text — so
// backspacing one character out of a complete URL silently emptied the
// field, and autosave/profile-save then persisted the empty value with
// no visible signal. Leaving the last valid value in place keeps bad
// input out just as effectively without destroying good input.
setError(t(result.error ?? "", result.errorParams as Record<string, string>));
}
setTouched(true);
},
[validate, onChange, t, beforeChange, displayValue],
);
const message = blocked ? (blockedMessage ?? error) : error;
// Publish "this field currently holds text that will not be saved" so gates
// on other pages can see it. Keyed on the raw validation result rather than
// the rendered message, which is already translated and would re-publish on
// every UI-language change.
const liveResult = validate(displayValue);
const isInvalid = blocked || (!liveResult.valid && displayValue.trim() !== "");
const invalidKey = blocked ? "validation.emailMaxReached" : (liveResult.error ?? "");
const invalidParams = JSON.stringify(liveResult.errorParams ?? null);
useEffect(() => {
if (!fieldId) return;
const { markInvalid, clearInvalid } = usePendingInvalidStore.getState();
if (isInvalid) {
const parsed = JSON.parse(invalidParams) as Record<string, string | number> | null;
markInvalid({
id: fieldId,
labelKey: labelKey ?? label,
messageKey: invalidKey,
...(parsed ? { params: parsed } : {}),
});
} else {
clearInvalid(fieldId);
}
// Deliberately NO cleanup on unmount — see the store's doc comment. An
// entry lives until the field is valid again, so navigating to Batch does
// not silently clear the very thing Strict Mode is supposed to catch.
}, [fieldId, labelKey, label, isInvalid, invalidKey, invalidParams]);
const showError = touched && Boolean(message);
return (
<Field label={label} error={showError ? message : undefined} help={helpText}>
{(control) => (
<input
{...control}
type="text"
value={displayValue}
onChange={(e) => handleChange(e.target.value)}
onBlur={() => setTouched(true)}
onPaste={onPaste}
placeholder={placeholder}
inputMode={inputMode}
autoComplete={autoComplete}
enterKeyHint={enterKeyHint}
maxLength={maxLength}
aria-invalid={showError || undefined}
className={clsx(controlClasses(showError), "h-control text-base sm:text-sm")}
/>
)}
</Field>
);
}