refactor: 템플릿에서 기본적으로 제공하고 있는 워크 플로우에 디자인과 라우팅 흡수
This commit is contained in:
@@ -158,7 +158,7 @@ export function useApplicationQuery<Value>(
|
||||
}
|
||||
},
|
||||
});
|
||||
const hasData = query.data !== undefined && query.data !== null;
|
||||
const hasData = query.data !== undefined;
|
||||
|
||||
useEffect(() => {
|
||||
if (query.isError && hasData) {
|
||||
@@ -293,6 +293,8 @@ export function useApplicationMutation<Input, Value>(
|
||||
"requiresIdempotencyKey" in options
|
||||
? options.requiresIdempotencyKey
|
||||
: false;
|
||||
const mutationIdentity =
|
||||
"scope" in options ? options.identity : undefined;
|
||||
const unknownEffectChannelKey = scope
|
||||
? `bound:${scope.generation}:${scope.fingerprint}:${definitionId}:${definitionVersion}`
|
||||
: `legacy:${definitionId}:${
|
||||
@@ -410,7 +412,11 @@ export function useApplicationMutation<Input, Value>(
|
||||
),
|
||||
});
|
||||
}
|
||||
identityLease = scope.identities.intern(input);
|
||||
const identityInput =
|
||||
mutationIdentity
|
||||
? mutationIdentity(input)
|
||||
: input;
|
||||
identityLease = scope.identities.intern(identityInput);
|
||||
identityLease.acquire();
|
||||
identity = `${scope.fingerprint}:${definitionId}:${identityLease.token}`;
|
||||
} else {
|
||||
@@ -446,9 +452,16 @@ export function useApplicationMutation<Input, Value>(
|
||||
),
|
||||
});
|
||||
}
|
||||
// A user can submit during the same event turn in which this controller
|
||||
// mounted. The subscription effect may not have published the channel yet;
|
||||
// channel admission is therefore also available lazily from the event path.
|
||||
// A conflicting scope owner or a saturated registry still fails closed.
|
||||
const submissionChannel =
|
||||
unknownEffectChannel ??
|
||||
acquireUnknownEffectChannel(queryClient, unknownEffectChannelKey, scope);
|
||||
if (
|
||||
!unknownEffectChannel ||
|
||||
unknownEffectChannel.registry.activeAdmissions >=
|
||||
!submissionChannel ||
|
||||
submissionChannel.registry.activeAdmissions >=
|
||||
MUTATION_COORDINATOR_BOUNDS.activeIntentsTotal
|
||||
) {
|
||||
identityLease?.release();
|
||||
@@ -466,7 +479,7 @@ export function useApplicationMutation<Input, Value>(
|
||||
});
|
||||
}
|
||||
const admission: MutationAdmission = {
|
||||
sequence: unknownEffectChannel.nextSequence++,
|
||||
sequence: submissionChannel.nextSequence++,
|
||||
state: "ACTIVE",
|
||||
intent: null,
|
||||
scope: scope ?? null,
|
||||
@@ -475,9 +488,9 @@ export function useApplicationMutation<Input, Value>(
|
||||
invalidate,
|
||||
invalidationCoordinator: invalidationCoordinator ?? null,
|
||||
};
|
||||
unknownEffectChannel.admissions.push(admission);
|
||||
unknownEffectChannel.registry.activeAdmissions += 1;
|
||||
notifyUnknownEffectChannel(unknownEffectChannel);
|
||||
submissionChannel.admissions.push(admission);
|
||||
submissionChannel.registry.activeAdmissions += 1;
|
||||
notifyUnknownEffectChannel(submissionChannel);
|
||||
setConflict(null);
|
||||
mutation.reset();
|
||||
|
||||
@@ -533,7 +546,7 @@ export function useApplicationMutation<Input, Value>(
|
||||
case "NOT_APPLIED":
|
||||
optimisticLayer?.rollback();
|
||||
if (failure.kind === "CONFLICT") setConflict(failure);
|
||||
settleMutationAdmission(unknownEffectChannel, admission);
|
||||
settleMutationAdmission(submissionChannel, admission);
|
||||
break;
|
||||
case "APPLIED_CONFIRMED":
|
||||
optimisticLayer?.commit();
|
||||
@@ -544,15 +557,15 @@ export function useApplicationMutation<Input, Value>(
|
||||
// A confirmed command remains committed if refresh fails.
|
||||
}
|
||||
}
|
||||
settleMutationAdmission(unknownEffectChannel, admission);
|
||||
settleMutationAdmission(submissionChannel, admission);
|
||||
break;
|
||||
case "MAYBE_APPLIED":
|
||||
optimisticLayer?.markUncertain();
|
||||
if (!scope || scope.isCurrent()) {
|
||||
admission.state = "UNKNOWN";
|
||||
notifyUnknownEffectChannel(unknownEffectChannel);
|
||||
notifyUnknownEffectChannel(submissionChannel);
|
||||
} else {
|
||||
settleMutationAdmission(unknownEffectChannel, admission);
|
||||
settleMutationAdmission(submissionChannel, admission);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -565,7 +578,7 @@ export function useApplicationMutation<Input, Value>(
|
||||
} catch {
|
||||
// Cache refresh remains best effort after the server has committed.
|
||||
}
|
||||
settleMutationAdmission(unknownEffectChannel, admission);
|
||||
settleMutationAdmission(submissionChannel, admission);
|
||||
return { ok: true, value };
|
||||
} finally {
|
||||
try {
|
||||
@@ -582,7 +595,7 @@ export function useApplicationMutation<Input, Value>(
|
||||
}),
|
||||
"NOT_STARTED",
|
||||
);
|
||||
settleMutationAdmission(unknownEffectChannel, admission);
|
||||
settleMutationAdmission(submissionChannel, admission);
|
||||
if (failure.kind === "CONFLICT") setConflict(failure);
|
||||
return { ok: false as const, error: failure };
|
||||
})
|
||||
@@ -605,11 +618,13 @@ export function useApplicationMutation<Input, Value>(
|
||||
queryClient,
|
||||
definitionId,
|
||||
duplicatePolicy,
|
||||
mutationIdentity,
|
||||
mutationIntentFactory,
|
||||
mutationOperationId,
|
||||
requiresIdempotencyKey,
|
||||
scope,
|
||||
unknownEffectChannel,
|
||||
unknownEffectChannelKey,
|
||||
],
|
||||
);
|
||||
|
||||
|
||||
@@ -28,11 +28,14 @@ export type {
|
||||
} from "./primitives/core.tsx";
|
||||
export {
|
||||
Checkbox,
|
||||
InputControl,
|
||||
RadioGroup,
|
||||
SearchField,
|
||||
SelectControl,
|
||||
Select,
|
||||
Switch,
|
||||
TextArea,
|
||||
TextAreaControl,
|
||||
} from "./primitives/forms.tsx";
|
||||
export type {
|
||||
CheckboxProps,
|
||||
|
||||
@@ -62,6 +62,7 @@ export function SearchFilterToolbar({
|
||||
resetLabel,
|
||||
onReset,
|
||||
resultCount,
|
||||
className = "",
|
||||
}: Readonly<{
|
||||
label: string;
|
||||
search: React.ReactNode;
|
||||
@@ -69,9 +70,13 @@ export function SearchFilterToolbar({
|
||||
resetLabel: string;
|
||||
onReset(): void;
|
||||
resultCount: React.ReactNode;
|
||||
className?: string;
|
||||
}>) {
|
||||
return (
|
||||
<section aria-label={label} className="ui-search-filter-toolbar">
|
||||
<section
|
||||
aria-label={label}
|
||||
className={"ui-search-filter-toolbar " + className}
|
||||
>
|
||||
<div>{search}</div>
|
||||
{filters ? <div>{filters}</div> : null}
|
||||
<Button onClick={onReset} variant="ghost">
|
||||
|
||||
@@ -19,6 +19,7 @@ export type ButtonProps = React.ButtonHTMLAttributes<HTMLButtonElement> &
|
||||
size?: ButtonSize;
|
||||
pending?: boolean;
|
||||
pendingLabel?: string;
|
||||
visualPolicy?: "platform" | "inherit";
|
||||
}>;
|
||||
|
||||
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
@@ -28,6 +29,7 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
size = "default",
|
||||
pending = false,
|
||||
pendingLabel,
|
||||
visualPolicy = "platform",
|
||||
className = "",
|
||||
type = "button",
|
||||
children,
|
||||
@@ -36,15 +38,18 @@ export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
},
|
||||
ref,
|
||||
) {
|
||||
const classes = [
|
||||
"ui-button",
|
||||
`ui-button--${variant}`,
|
||||
size === "compact" ? "ui-button--compact" : "",
|
||||
pending ? "ui-button--pending" : "",
|
||||
className,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
const classes =
|
||||
visualPolicy === "inherit"
|
||||
? className
|
||||
: [
|
||||
"ui-button",
|
||||
`ui-button--${variant}`,
|
||||
size === "compact" ? "ui-button--compact" : "",
|
||||
pending ? "ui-button--pending" : "",
|
||||
className,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ");
|
||||
|
||||
return (
|
||||
<button
|
||||
@@ -317,11 +322,15 @@ export type DialogProps = Readonly<{
|
||||
open: boolean;
|
||||
onClose(): void;
|
||||
title: string;
|
||||
accessibleTitle?: string;
|
||||
description?: string;
|
||||
eyebrow?: React.ReactNode;
|
||||
closeLabel?: string;
|
||||
hideCloseButton?: boolean;
|
||||
children?: React.ReactNode;
|
||||
actions?: React.ReactNode;
|
||||
className?: string;
|
||||
initialFocusRef?: React.RefObject<HTMLElement | null>;
|
||||
returnFocusRef?: React.RefObject<HTMLElement | null>;
|
||||
}>;
|
||||
|
||||
@@ -331,11 +340,15 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
open,
|
||||
onClose,
|
||||
title,
|
||||
accessibleTitle,
|
||||
description,
|
||||
eyebrow,
|
||||
closeLabel,
|
||||
hideCloseButton = false,
|
||||
children,
|
||||
actions,
|
||||
className = "",
|
||||
initialFocusRef,
|
||||
returnFocusRef,
|
||||
},
|
||||
forwardedRef,
|
||||
@@ -367,7 +380,7 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
const firstFocusable = dialog.querySelector<HTMLElement>(
|
||||
"[autofocus], button, [href], input, select, textarea, [tabindex]:not([tabindex='-1'])",
|
||||
);
|
||||
firstFocusable?.focus();
|
||||
(initialFocusRef?.current ?? firstFocusable)?.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -377,24 +390,19 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
}
|
||||
const previousFocus = previousFocusRef.current;
|
||||
previousFocusRef.current = null;
|
||||
const restoreFocus = () => {
|
||||
if (
|
||||
focusRestoreGenerationRef.current === generation &&
|
||||
previousFocus?.isConnected
|
||||
) {
|
||||
previousFocus.focus();
|
||||
}
|
||||
};
|
||||
const timer = globalThis.setTimeout(restoreFocus, 0);
|
||||
return () => {
|
||||
globalThis.clearTimeout(timer);
|
||||
};
|
||||
}, [open, returnFocusRef]);
|
||||
if (
|
||||
focusRestoreGenerationRef.current === generation &&
|
||||
previousFocus?.isConnected
|
||||
) {
|
||||
previousFocus.focus();
|
||||
}
|
||||
}, [initialFocusRef, open, returnFocusRef]);
|
||||
|
||||
return (
|
||||
<dialog
|
||||
aria-describedby={description ? descriptionId : undefined}
|
||||
aria-labelledby={titleId}
|
||||
aria-label={accessibleTitle}
|
||||
aria-labelledby={accessibleTitle ? undefined : titleId}
|
||||
className={`ui-dialog ${className}`.trim()}
|
||||
onCancel={(event) => {
|
||||
event.preventDefault();
|
||||
@@ -407,6 +415,27 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
onClose();
|
||||
return;
|
||||
}
|
||||
if (event.key !== "Tab") return;
|
||||
const focusable = Array.from(
|
||||
event.currentTarget.querySelectorAll<HTMLElement>(
|
||||
"button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex='-1'])",
|
||||
),
|
||||
).filter((element) => !element.hasAttribute("hidden"));
|
||||
if (focusable.length === 0) {
|
||||
event.preventDefault();
|
||||
event.currentTarget.focus();
|
||||
return;
|
||||
}
|
||||
const first = focusable[0]!;
|
||||
const last = focusable[focusable.length - 1]!;
|
||||
if (event.shiftKey && document.activeElement === first) {
|
||||
event.preventDefault();
|
||||
last.focus();
|
||||
} else if (!event.shiftKey && document.activeElement === last) {
|
||||
event.preventDefault();
|
||||
first.focus();
|
||||
}
|
||||
}}
|
||||
ref={dialogRef}
|
||||
@@ -414,19 +443,22 @@ export const Dialog = forwardRef<HTMLDialogElement, DialogProps>(
|
||||
<div className="ui-dialog__surface">
|
||||
<header className="ui-dialog__header">
|
||||
<div>
|
||||
{eyebrow}
|
||||
<h2 id={titleId}>{title}</h2>
|
||||
{description ? <p id={descriptionId}>{description}</p> : null}
|
||||
</div>
|
||||
<IconButton
|
||||
accessibleName={
|
||||
closeLabel ?? message("action.closeNamed", { title })
|
||||
}
|
||||
className="ui-dialog__close"
|
||||
onClick={onClose}
|
||||
variant="ghost"
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
{hideCloseButton ? null : (
|
||||
<IconButton
|
||||
accessibleName={
|
||||
closeLabel ?? message("action.closeNamed", { title })
|
||||
}
|
||||
className="ui-dialog__close"
|
||||
onClick={onClose}
|
||||
variant="ghost"
|
||||
>
|
||||
<CloseIcon />
|
||||
</IconButton>
|
||||
)}
|
||||
</header>
|
||||
{children ? (
|
||||
<div className="ui-dialog__content">{children}</div>
|
||||
|
||||
@@ -11,6 +11,29 @@ import { Field, IconButton } from "./core.tsx";
|
||||
import type { TextFieldProps } from "./core.tsx";
|
||||
import { useLocale } from "../../i18n/index.ts";
|
||||
|
||||
|
||||
|
||||
export const InputControl = forwardRef<
|
||||
HTMLInputElement,
|
||||
React.InputHTMLAttributes<HTMLInputElement>
|
||||
>(function InputControl(props, ref) {
|
||||
return <input {...props} ref={ref} />;
|
||||
});
|
||||
|
||||
export const SelectControl = forwardRef<
|
||||
HTMLSelectElement,
|
||||
React.SelectHTMLAttributes<HTMLSelectElement>
|
||||
>(function SelectControl(props, ref) {
|
||||
return <select {...props} ref={ref} />;
|
||||
});
|
||||
|
||||
export const TextAreaControl = forwardRef<
|
||||
HTMLTextAreaElement,
|
||||
React.TextareaHTMLAttributes<HTMLTextAreaElement>
|
||||
>(function TextAreaControl(props, ref) {
|
||||
return <textarea {...props} ref={ref} />;
|
||||
});
|
||||
|
||||
type FieldCopy = Readonly<{
|
||||
id?: string;
|
||||
label: string;
|
||||
@@ -23,7 +46,10 @@ export type TextAreaProps = Omit<
|
||||
"id"
|
||||
> &
|
||||
FieldCopy &
|
||||
Readonly<{ maxLengthMessage?: (remaining: number) => string }>;
|
||||
Readonly<{
|
||||
maxLengthMessage?: (remaining: number) => string;
|
||||
controlClassName?: string;
|
||||
}>;
|
||||
|
||||
export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
|
||||
function TextArea(
|
||||
@@ -34,6 +60,7 @@ export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
|
||||
error,
|
||||
required,
|
||||
className = "",
|
||||
controlClassName = "",
|
||||
maxLength,
|
||||
maxLengthMessage,
|
||||
value,
|
||||
@@ -66,7 +93,7 @@ export const TextArea = forwardRef<HTMLTextAreaElement, TextAreaProps>(
|
||||
{...props}
|
||||
aria-describedby={describedBy}
|
||||
aria-invalid={invalid || undefined}
|
||||
className="ui-field__input ui-field__textarea"
|
||||
className={"ui-field__input ui-field__textarea " + controlClassName}
|
||||
defaultValue={value === undefined ? defaultValue : undefined}
|
||||
id={controlId}
|
||||
maxLength={maxLength}
|
||||
@@ -172,11 +199,20 @@ export type CheckboxProps = Omit<
|
||||
description?: string;
|
||||
error?: string;
|
||||
indeterminate?: boolean;
|
||||
fieldClassName?: string;
|
||||
}>;
|
||||
|
||||
export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
|
||||
function Checkbox(
|
||||
{ label, description, error, indeterminate = false, id, ...props },
|
||||
{
|
||||
label,
|
||||
description,
|
||||
error,
|
||||
indeterminate = false,
|
||||
fieldClassName = "",
|
||||
id,
|
||||
...props
|
||||
},
|
||||
forwardedRef,
|
||||
) {
|
||||
const generatedId = useId();
|
||||
@@ -189,7 +225,7 @@ export const Checkbox = forwardRef<HTMLInputElement, CheckboxProps>(
|
||||
}, [indeterminate]);
|
||||
|
||||
return (
|
||||
<div className="ui-choice-field">
|
||||
<div className={"ui-choice-field " + fieldClassName}>
|
||||
<label className="ui-choice-field__control" htmlFor={controlId}>
|
||||
<input
|
||||
{...props}
|
||||
|
||||
@@ -2,7 +2,7 @@ import { useId, useRef, useState } from "react";
|
||||
|
||||
import { useLocale } from "../../i18n/index.ts";
|
||||
import { NextIcon, PreviousIcon } from "../icons/semantic-icons.tsx";
|
||||
import { IconButton, LinkButton } from "./core.tsx";
|
||||
import { Button, IconButton, LinkButton } from "./core.tsx";
|
||||
|
||||
export type BreadcrumbItem = Readonly<{
|
||||
label: string;
|
||||
@@ -140,6 +140,38 @@ export function Tabs({
|
||||
);
|
||||
}
|
||||
|
||||
type PaginationEntry = number | "gap";
|
||||
|
||||
function paginationEntries(
|
||||
current: number,
|
||||
total: number,
|
||||
around: number,
|
||||
): readonly PaginationEntry[] {
|
||||
if (total <= 0) return [];
|
||||
if (total <= around * 2 + 5) {
|
||||
return Array.from({ length: total }, (_, index) => index + 1);
|
||||
}
|
||||
|
||||
const shown = new Set<number>([1, total]);
|
||||
for (
|
||||
let candidate = current - around;
|
||||
candidate <= current + around;
|
||||
candidate += 1
|
||||
) {
|
||||
if (candidate >= 1 && candidate <= total) shown.add(candidate);
|
||||
}
|
||||
|
||||
const sorted = [...shown].sort((left, right) => left - right);
|
||||
const entries: PaginationEntry[] = [];
|
||||
let previous = 0;
|
||||
for (const candidate of sorted) {
|
||||
if (previous && candidate - previous > 1) entries.push("gap");
|
||||
entries.push(candidate);
|
||||
previous = candidate;
|
||||
}
|
||||
return entries;
|
||||
}
|
||||
|
||||
export function Pagination({
|
||||
label,
|
||||
page,
|
||||
@@ -147,7 +179,11 @@ export function Pagination({
|
||||
previousLabel,
|
||||
nextLabel,
|
||||
pageLabel,
|
||||
pageAccessibleLabel,
|
||||
pageControl = "link",
|
||||
onChange,
|
||||
className = "",
|
||||
around = 2,
|
||||
}: Readonly<{
|
||||
label: string;
|
||||
page: number;
|
||||
@@ -155,15 +191,23 @@ export function Pagination({
|
||||
previousLabel: string;
|
||||
nextLabel: string;
|
||||
pageLabel(page: number): string;
|
||||
pageAccessibleLabel?: (page: number) => string;
|
||||
pageControl?: "link" | "button";
|
||||
onChange(page: number): void;
|
||||
className?: string;
|
||||
around?: number;
|
||||
}>) {
|
||||
const pages = Array.from({ length: pageCount }, (_, index) => index + 1);
|
||||
const pages = paginationEntries(page, pageCount, around);
|
||||
const { direction } = useLocale();
|
||||
const PreviousDirectionalIcon =
|
||||
direction === "rtl" ? NextIcon : PreviousIcon;
|
||||
const NextDirectionalIcon = direction === "rtl" ? PreviousIcon : NextIcon;
|
||||
|
||||
return (
|
||||
<nav aria-label={label} className="ui-pagination">
|
||||
<nav
|
||||
aria-label={label}
|
||||
className={"ui-pagination " + className}
|
||||
>
|
||||
<IconButton
|
||||
accessibleName={previousLabel}
|
||||
disabled={page <= 1}
|
||||
@@ -172,20 +216,39 @@ export function Pagination({
|
||||
>
|
||||
<PreviousDirectionalIcon />
|
||||
</IconButton>
|
||||
{pages.map((candidate) => (
|
||||
<LinkButton
|
||||
aria-current={candidate === page ? "page" : undefined}
|
||||
href={`?page=${candidate}`}
|
||||
key={candidate}
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
onChange(candidate);
|
||||
}}
|
||||
variant={candidate === page ? "primary" : "ghost"}
|
||||
>
|
||||
{pageLabel(candidate)}
|
||||
</LinkButton>
|
||||
))}
|
||||
<ol className="ui-pagination__pages">
|
||||
{pages.map((candidate, index) => (
|
||||
<li key={candidate === "gap" ? "gap-" + index : candidate}>
|
||||
{candidate === "gap" ? (
|
||||
<span aria-hidden="true" className="ui-pagination__gap">
|
||||
…
|
||||
</span>
|
||||
) : pageControl === "button" ? (
|
||||
<Button
|
||||
aria-current={candidate === page ? "page" : undefined}
|
||||
aria-label={pageAccessibleLabel?.(candidate)}
|
||||
onClick={() => onChange(candidate)}
|
||||
variant={candidate === page ? "primary" : "ghost"}
|
||||
>
|
||||
{pageLabel(candidate)}
|
||||
</Button>
|
||||
) : (
|
||||
<LinkButton
|
||||
aria-current={candidate === page ? "page" : undefined}
|
||||
aria-label={pageAccessibleLabel?.(candidate)}
|
||||
href={"?page=" + candidate}
|
||||
onClick={(event) => {
|
||||
event.preventDefault();
|
||||
onChange(candidate);
|
||||
}}
|
||||
variant={candidate === page ? "primary" : "ghost"}
|
||||
>
|
||||
{pageLabel(candidate)}
|
||||
</LinkButton>
|
||||
)}
|
||||
</li>
|
||||
))}
|
||||
</ol>
|
||||
<IconButton
|
||||
accessibleName={nextLabel}
|
||||
disabled={page >= pageCount}
|
||||
|
||||
@@ -114,7 +114,7 @@ export function useAppForm<
|
||||
id: fieldId(name),
|
||||
name,
|
||||
value: String(values[name] ?? ""),
|
||||
onChange(event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) {
|
||||
onChange(event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement>) {
|
||||
setValue(name, event.currentTarget.value as Values[FieldName<Values>]);
|
||||
},
|
||||
onBlur() {
|
||||
|
||||
@@ -1,12 +1,49 @@
|
||||
import { useCallback } from "react";
|
||||
import {
|
||||
useCallback,
|
||||
useRef,
|
||||
useState,
|
||||
type MutableRefObject,
|
||||
} from "react";
|
||||
import { useBeforeUnload, useBlocker } from "react-router-dom";
|
||||
|
||||
import { Button } from "../components/ui/button.ts";
|
||||
import { Dialog } from "../components/ui/dialog.ts";
|
||||
import { useLocale } from "../i18n/index.ts";
|
||||
|
||||
export function useDirtyNavigationGuard(when: boolean) {
|
||||
const blocker = useBlocker(when);
|
||||
type PendingNavigation = Readonly<{
|
||||
proceed(): void;
|
||||
}>;
|
||||
|
||||
export type DirtyNavigationProtection = Readonly<{
|
||||
blocked: boolean;
|
||||
request(proceed: () => void): boolean;
|
||||
stay(): void;
|
||||
leave(): void;
|
||||
}>;
|
||||
|
||||
function clearPending(
|
||||
pendingRef: MutableRefObject<PendingNavigation | null>,
|
||||
setBlocked: (blocked: boolean) => void,
|
||||
): PendingNavigation | null {
|
||||
const pending = pendingRef.current;
|
||||
pendingRef.current = null;
|
||||
setBlocked(false);
|
||||
return pending;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared dirty-state protection for feature-owned navigation.
|
||||
*
|
||||
* Router navigation uses useDirtyNavigationGuard below. Features that own
|
||||
* an imperative navigation adapter use this base hook so beforeunload,
|
||||
* pending-transition ownership, and stay/leave semantics remain platform
|
||||
* behavior rather than being reimplemented per feature.
|
||||
*/
|
||||
export function useDirtyNavigationProtection(
|
||||
when: boolean,
|
||||
): DirtyNavigationProtection {
|
||||
const pendingRef = useRef<PendingNavigation | null>(null);
|
||||
const [blocked, setBlocked] = useState(false);
|
||||
|
||||
useBeforeUnload(
|
||||
useCallback(
|
||||
@@ -21,12 +58,39 @@ export function useDirtyNavigationGuard(when: boolean) {
|
||||
);
|
||||
|
||||
return Object.freeze({
|
||||
blocked: blocker.state === "blocked",
|
||||
blocked,
|
||||
request(proceed: () => void) {
|
||||
if (!when) {
|
||||
proceed();
|
||||
return false;
|
||||
}
|
||||
pendingRef.current = Object.freeze({ proceed });
|
||||
setBlocked(true);
|
||||
return true;
|
||||
},
|
||||
stay() {
|
||||
blocker.reset?.();
|
||||
clearPending(pendingRef, setBlocked);
|
||||
},
|
||||
leave() {
|
||||
blocker.proceed?.();
|
||||
clearPending(pendingRef, setBlocked)?.proceed();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useDirtyNavigationGuard(when: boolean) {
|
||||
const blocker = useBlocker(when);
|
||||
const manual = useDirtyNavigationProtection(when);
|
||||
|
||||
return Object.freeze({
|
||||
blocked: blocker.state === "blocked" || manual.blocked,
|
||||
request: manual.request,
|
||||
stay() {
|
||||
blocker.reset?.();
|
||||
manual.stay();
|
||||
},
|
||||
leave() {
|
||||
if (blocker.state === "blocked") blocker.proceed?.();
|
||||
manual.leave();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,6 +23,17 @@ export type MessageParameters = Readonly<{
|
||||
"template.supportReference": { reference: string };
|
||||
"form.remaining": { count: number };
|
||||
"boot.supportReference": { reference: string };
|
||||
"techLog.notice.releaseDraftCreated": { title: string };
|
||||
"techLog.notice.releaseDeleted": { version: string };
|
||||
"techLog.notice.topicSavedVersion": { version: string | number };
|
||||
"techLog.notice.itemAdded": { title: string };
|
||||
"techLog.notice.itemSaved": { title: string };
|
||||
"techLog.notice.itemDeleted": { title: string };
|
||||
"techLog.notice.topicCreated": { name: string };
|
||||
"techLog.notice.projectCreated": { title: string };
|
||||
"techLog.notice.topicDeleted": { name: string };
|
||||
"techLog.notice.projectDeleted": { name: string };
|
||||
"techLog.notice.savedVersion": { version: string | number };
|
||||
}>;
|
||||
export type ParameterlessMessageKey = Exclude<
|
||||
MessageKey,
|
||||
|
||||
@@ -178,15 +178,10 @@ function PrimaryNavigation({ id }: Readonly<{ id: string }>) {
|
||||
});
|
||||
|
||||
/*
|
||||
`aria-current="page"` 와 시각적 강조는 서로 다른 것을 말한다.
|
||||
|
||||
`end` 없이 `NavLink` 를 쓰면 `/studio/documents` 링크가 `/studio/documents/:id/edit` 에서도
|
||||
활성으로 잡히고, 그때 `aria-current="page"` 가 붙는다. 그 화면은 그 페이지가 아니다 —
|
||||
화면 읽기 프로그램에게는 잘못된 말이다.
|
||||
|
||||
그래서 `end` 로 정확히 일치할 때만 `aria-current` 가 붙게 하고, 「지금 이 구역에 있다」는
|
||||
표시는 여기서 따로 계산한다. 가장 긴 접두사 하나만 고른다 — 그러지 않으면 `/studio` 가
|
||||
모든 Studio 화면에서 함께 켜진다.
|
||||
aria-current and the visual section highlight communicate different things.
|
||||
A parent navigation route must not be announced as the current page on a child editor route.
|
||||
Exact NavLink matching owns aria-current; the longest matching route prefix owns only the
|
||||
visual section highlight.
|
||||
*/
|
||||
const sectionPath = routes
|
||||
.map((definition) => definition.path)
|
||||
|
||||
@@ -4,26 +4,24 @@ import {
|
||||
} from "../../features/installed-feature-contracts.ts";
|
||||
import { ROUTE_CODECS } from "../../features/installed-feature-runtimes.tsx";
|
||||
import type { RouteDefinition } from "../../contracts/routes.ts";
|
||||
import type { RouteRuntimeDefinition } from "../../contracts/route-runtime-contract.ts";
|
||||
import type {
|
||||
ParsedRouteInput,
|
||||
RouteId,
|
||||
RouteInputResult,
|
||||
} from "./route-contract.ts";
|
||||
import {
|
||||
buildRouteUrlFromContract,
|
||||
buildRouteUrlFromDefinition,
|
||||
type RouteCodecRegistry,
|
||||
} from "./route-url-builder.ts";
|
||||
|
||||
export type { ParsedRouteInput, RouteId, RouteInputResult };
|
||||
export {
|
||||
buildRouteUrlFromContract,
|
||||
buildRouteUrlFromDefinition,
|
||||
};
|
||||
export type { RouteCodecRegistry };
|
||||
|
||||
type RouteCodec = Readonly<{
|
||||
parse(value: unknown): Readonly<Record<string, unknown>>;
|
||||
safeParse(value: unknown):
|
||||
| Readonly<{
|
||||
success: true;
|
||||
data: Readonly<Record<string, unknown>>;
|
||||
}>
|
||||
| Readonly<{ success: false }>;
|
||||
}>;
|
||||
|
||||
export type RouteCodecRegistry = Readonly<Record<string, RouteCodec>>;
|
||||
export type ContractRouteInputResult<
|
||||
RouteIdValue extends string = string,
|
||||
> = RouteInputResult<RouteIdValue>;
|
||||
@@ -100,91 +98,6 @@ export function buildRouteUrl(
|
||||
);
|
||||
}
|
||||
|
||||
export function buildRouteUrlFromContract(
|
||||
definition: Pick<RouteDefinition, "path">,
|
||||
runtime: Pick<RouteRuntimeDefinition, "paramsCodec" | "searchCodec">,
|
||||
codecs: RouteCodecRegistry,
|
||||
input: Readonly<{
|
||||
params?: Readonly<Record<string, unknown>>;
|
||||
search?: Readonly<Record<string, unknown>>;
|
||||
}> = {},
|
||||
): string {
|
||||
return buildRouteUrlWithCodecs(
|
||||
definition.path,
|
||||
runtime.paramsCodec,
|
||||
runtime.searchCodec,
|
||||
codecs,
|
||||
input,
|
||||
);
|
||||
}
|
||||
|
||||
export function buildRouteUrlFromDefinition(
|
||||
definition: Pick<
|
||||
RouteDefinition,
|
||||
"path" | "paramsSchema" | "searchSchema"
|
||||
>,
|
||||
codecs: RouteCodecRegistry,
|
||||
input: Readonly<{
|
||||
params?: Readonly<Record<string, unknown>>;
|
||||
search?: Readonly<Record<string, unknown>>;
|
||||
}> = {},
|
||||
): string {
|
||||
return buildRouteUrlWithCodecs(
|
||||
definition.path,
|
||||
definition.paramsSchema ?? "none",
|
||||
definition.searchSchema ?? "none",
|
||||
codecs,
|
||||
input,
|
||||
);
|
||||
}
|
||||
|
||||
function buildRouteUrlWithCodecs(
|
||||
pathTemplate: string,
|
||||
paramsCodec: string,
|
||||
searchCodec: string,
|
||||
codecs: RouteCodecRegistry,
|
||||
input: Readonly<{
|
||||
params?: Readonly<Record<string, unknown>>;
|
||||
search?: Readonly<Record<string, unknown>>;
|
||||
}>,
|
||||
): string {
|
||||
if (pathTemplate.includes("*")) {
|
||||
throw new TypeError("A wildcard route cannot build a canonical URL");
|
||||
}
|
||||
const params = codecById(paramsCodec, codecs).parse(input.params ?? {});
|
||||
const search = codecById(searchCodec, codecs).parse(input.search ?? {});
|
||||
const parsedParams: Record<string, unknown> = { ...params };
|
||||
const parsedSearch: Record<string, unknown> = { ...search };
|
||||
let path = pathTemplate;
|
||||
path = path.replace(
|
||||
/:([A-Za-z][A-Za-z0-9_]*)|\{([A-Za-z][A-Za-z0-9_]*)\}/g,
|
||||
(
|
||||
_token: string,
|
||||
colonName: string | undefined,
|
||||
braceName: string | undefined,
|
||||
) => {
|
||||
const name = colonName ?? braceName ?? "";
|
||||
const value = parsedParams[name];
|
||||
if (typeof value !== "string" && typeof value !== "number") {
|
||||
throw new TypeError(`Missing route path parameter: ${name}`);
|
||||
}
|
||||
return encodeURIComponent(String(value));
|
||||
},
|
||||
);
|
||||
const query = new URLSearchParams();
|
||||
for (const key of Object.keys(parsedSearch).sort((left, right) =>
|
||||
left.localeCompare(right),
|
||||
)) {
|
||||
const value = parsedSearch[key];
|
||||
if (value === undefined || value === null) continue;
|
||||
for (const item of Array.isArray(value) ? value : [value]) {
|
||||
query.append(key, String(item));
|
||||
}
|
||||
}
|
||||
const serialized = query.toString();
|
||||
return serialized ? `${path}?${serialized}` : path;
|
||||
}
|
||||
|
||||
function searchRecord(
|
||||
search: URLSearchParams,
|
||||
): Readonly<Record<string, string | readonly string[]>> {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import type { RouteRuntimeDefinition } from "../../contracts/route-runtime-contract.ts";
|
||||
import type { RouteDefinition } from "../../contracts/routes.ts";
|
||||
|
||||
type RouteCodec = Readonly<{
|
||||
parse(value: unknown): Readonly<Record<string, unknown>>;
|
||||
safeParse(value: unknown):
|
||||
| Readonly<{ success: true; data: Readonly<Record<string, unknown>> }>
|
||||
| Readonly<{ success: false }>;
|
||||
}>;
|
||||
|
||||
export type RouteCodecRegistry = Readonly<Record<string, RouteCodec>>;
|
||||
|
||||
function codecById(codecId: string, codecs: RouteCodecRegistry) {
|
||||
const codec = codecs[codecId];
|
||||
if (!codec) throw new TypeError(`Unregistered route codec: ${codecId}`);
|
||||
return codec;
|
||||
}
|
||||
|
||||
export function buildRouteUrlFromContract(
|
||||
definition: Pick<RouteDefinition, "path">,
|
||||
runtime: Pick<RouteRuntimeDefinition, "paramsCodec" | "searchCodec">,
|
||||
codecs: RouteCodecRegistry,
|
||||
input: Readonly<{ params?: Readonly<Record<string, unknown>>; search?: Readonly<Record<string, unknown>> }> = {},
|
||||
): string {
|
||||
return buildRouteUrlWithCodecs(definition.path, runtime.paramsCodec, runtime.searchCodec, codecs, input);
|
||||
}
|
||||
|
||||
export function buildRouteUrlFromDefinition(
|
||||
definition: Pick<RouteDefinition, "path" | "paramsSchema" | "searchSchema">,
|
||||
codecs: RouteCodecRegistry,
|
||||
input: Readonly<{ params?: Readonly<Record<string, unknown>>; search?: Readonly<Record<string, unknown>> }> = {},
|
||||
): string {
|
||||
return buildRouteUrlWithCodecs(definition.path, definition.paramsSchema ?? "none", definition.searchSchema ?? "none", codecs, input);
|
||||
}
|
||||
|
||||
function buildRouteUrlWithCodecs(
|
||||
pathTemplate: string,
|
||||
paramsCodec: string,
|
||||
searchCodec: string,
|
||||
codecs: RouteCodecRegistry,
|
||||
input: Readonly<{ params?: Readonly<Record<string, unknown>>; search?: Readonly<Record<string, unknown>> }>,
|
||||
): string {
|
||||
if (pathTemplate.includes("*")) throw new TypeError("A wildcard route cannot build a canonical URL");
|
||||
const params = codecById(paramsCodec, codecs).parse(input.params ?? {});
|
||||
const search = codecById(searchCodec, codecs).parse(input.search ?? {});
|
||||
const parsedParams: Record<string, unknown> = { ...params };
|
||||
const parsedSearch: Record<string, unknown> = { ...search };
|
||||
let path = pathTemplate;
|
||||
path = path.replace(/:([A-Za-z][A-Za-z0-9_]*)|\\{([A-Za-z][A-Za-z0-9_]*)\\}/g, (_token, colonName: string | undefined, braceName: string | undefined) => {
|
||||
const name = colonName ?? braceName ?? "";
|
||||
const value = parsedParams[name];
|
||||
if (typeof value !== "string" && typeof value !== "number") throw new TypeError(`Missing route path parameter: ${name}`);
|
||||
return encodeURIComponent(String(value));
|
||||
});
|
||||
const query = new URLSearchParams();
|
||||
for (const key of Object.keys(parsedSearch).sort((left, right) => left.localeCompare(right))) {
|
||||
const value = parsedSearch[key];
|
||||
if (value === undefined || value === null) continue;
|
||||
for (const item of Array.isArray(value) ? value : [value]) query.append(key, String(item));
|
||||
}
|
||||
const serialized = query.toString();
|
||||
return serialized ? `${path}?${serialized}` : path;
|
||||
}
|
||||
@@ -207,15 +207,28 @@ export function StatusPage(
|
||||
primaryAction?: PageActionDefinition;
|
||||
secondaryAction?: PageActionDefinition;
|
||||
supportReference?: string;
|
||||
className?: string;
|
||||
children?: ReactNode;
|
||||
headingNode?: ReactNode;
|
||||
visualPolicy?: "platform" | "inherit";
|
||||
}>,
|
||||
) {
|
||||
const { message } = useLocale();
|
||||
const platformVisuals = props.visualPolicy !== "inherit";
|
||||
return (
|
||||
<section
|
||||
className={`ui-page page-template status-page status-page--${props.variant}`}
|
||||
className={[
|
||||
platformVisuals ? "ui-page" : null,
|
||||
platformVisuals ? "page-template" : null,
|
||||
platformVisuals ? "status-page" : null,
|
||||
platformVisuals ? `status-page--${props.variant}` : null,
|
||||
props.className,
|
||||
]
|
||||
.filter(Boolean)
|
||||
.join(" ")}
|
||||
data-status-variant={props.variant}
|
||||
>
|
||||
<PageHeader {...props.heading} />
|
||||
{props.headingNode ?? <PageHeader {...props.heading} />}
|
||||
{props.primaryAction || props.secondaryAction ? (
|
||||
<PageActionBar
|
||||
className="status-page__actions"
|
||||
@@ -233,6 +246,7 @@ export function StatusPage(
|
||||
})}
|
||||
</p>
|
||||
) : null}
|
||||
{props.children}
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user