feat(web): React 19 + Vite + Tailwind v4 + shadcn foundation
- design tokens ported verbatim from the product design, data-theme light/dark - app shell: topbar, sidebar/rail/tabbar responsive nav, lang + theme menus - ui primitives (button/card/badge/dot/chip/tabs/dialog/sheet/toast/kv/seg/progress) - api client, reconnecting ws client, zustand stores, route guard to /connect - full zh/en dictionaries (key parity test), 32 vitest specs, dist committed
This commit is contained in:
1 parent
4e98ef8658
commit
5b55e622ea
63 files changed
+8466
No files matched your search
@@ -0,0 +1,62 @@
|
||||
import { useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Modal } from "@/components/ui/dialog";
|
||||
|
||||
export interface ConfirmDialogProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
title: string;
|
||||
description?: string;
|
||||
confirmLabel?: string;
|
||||
cancelLabel?: string;
|
||||
danger?: boolean;
|
||||
onConfirm: () => void | Promise<void>;
|
||||
}
|
||||
|
||||
/** Destructive-action confirmation (base.css modal + two actions). */
|
||||
export function ConfirmDialog({
|
||||
open,
|
||||
onOpenChange,
|
||||
title,
|
||||
description,
|
||||
confirmLabel,
|
||||
cancelLabel,
|
||||
danger = true,
|
||||
onConfirm,
|
||||
}: ConfirmDialogProps) {
|
||||
const { t } = useTranslation();
|
||||
const [busy, setBusy] = useState(false);
|
||||
|
||||
async function confirm() {
|
||||
if (busy) return;
|
||||
setBusy(true);
|
||||
try {
|
||||
await onConfirm();
|
||||
onOpenChange(false);
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Modal
|
||||
open={open}
|
||||
onOpenChange={onOpenChange}
|
||||
title={title}
|
||||
footer={
|
||||
<>
|
||||
<Button variant="ghost" onClick={() => onOpenChange(false)} disabled={busy}>
|
||||
{cancelLabel ?? t("common.cancel")}
|
||||
</Button>
|
||||
<Button variant={danger ? "danger" : "primary"} onClick={() => void confirm()} disabled={busy}>
|
||||
{confirmLabel ?? t("common.confirm")}
|
||||
</Button>
|
||||
</>
|
||||
}
|
||||
>
|
||||
{description ? <p className="muted">{description}</p> : null}
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { Empty as EmptyState, type EmptyProps } from "@/components/ui/empty";
|
||||
@@ -0,0 +1,72 @@
|
||||
import * as DropdownMenu from "@radix-ui/react-dropdown-menu";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { Icon } from "@/components/icons";
|
||||
import { cn } from "@/lib/utils";
|
||||
import {
|
||||
LANGUAGES,
|
||||
isLanguage,
|
||||
languageLabel,
|
||||
languageShort,
|
||||
setLanguage,
|
||||
type Language,
|
||||
} from "@/lib/i18n";
|
||||
|
||||
/** Topbar language menu (globe + current language + caret), persisted to `pi2-lang`. */
|
||||
export function LangMenu() {
|
||||
const { t, i18n } = useTranslation();
|
||||
const current: Language = isLanguage(i18n.language) ? i18n.language : "zh";
|
||||
|
||||
return (
|
||||
<DropdownMenu.Root>
|
||||
<DropdownMenu.Trigger asChild>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn--sm btn--ghost lang-switch"
|
||||
title={t("shell.language")}
|
||||
aria-label={`${t("shell.language")}: ${languageLabel(current)}`}
|
||||
>
|
||||
<Icon name="globe" />
|
||||
<span className="lang-switch__full">{languageLabel(current)}</span>
|
||||
<span className="lang-switch__short">{languageShort(current)}</span>
|
||||
<span className="lang-switch__caret">
|
||||
<Icon name="chevron-down" />
|
||||
</span>
|
||||
</button>
|
||||
</DropdownMenu.Trigger>
|
||||
<DropdownMenu.Portal>
|
||||
<DropdownMenu.Content
|
||||
className="menu-popover"
|
||||
align="end"
|
||||
sideOffset={4}
|
||||
aria-label={t("shell.language")}
|
||||
>
|
||||
<DropdownMenu.RadioGroup
|
||||
value={current}
|
||||
onValueChange={(value) => {
|
||||
if (isLanguage(value)) void setLanguage(value);
|
||||
}}
|
||||
>
|
||||
{LANGUAGES.map((language) => {
|
||||
const active = language === current;
|
||||
return (
|
||||
<DropdownMenu.RadioItem
|
||||
key={language}
|
||||
value={language}
|
||||
className={cn("menu__item", active && "is-active")}
|
||||
>
|
||||
<span className="grow">{languageLabel(language)}</span>
|
||||
{active ? (
|
||||
<span className="menu__check">
|
||||
<Icon name="check" />
|
||||
</span>
|
||||
) : null}
|
||||
</DropdownMenu.RadioItem>
|
||||
);
|
||||
})}
|
||||
</DropdownMenu.RadioGroup>
|
||||
</DropdownMenu.Content>
|
||||
</DropdownMenu.Portal>
|
||||
</DropdownMenu.Root>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import * as React from "react";
|
||||
import { useEffect } from "react";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
export interface PageHeaderProps {
|
||||
title: React.ReactNode;
|
||||
subtitle?: React.ReactNode;
|
||||
/** Right-hand actions (buttons, links). */
|
||||
actions?: React.ReactNode;
|
||||
/** Optional extra row under the subtitle. */
|
||||
children?: React.ReactNode;
|
||||
size?: "default" | "lg";
|
||||
/** Title used for `document.title` when `title` is not a plain string. */
|
||||
docTitle?: string;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared page header from workspaces.html/status.html:
|
||||
* h1 + muted subtitle, actions on the right (wrapping under <600px).
|
||||
*/
|
||||
export function PageHeader({ title, subtitle, actions, children, size = "default", docTitle, className }: PageHeaderProps) {
|
||||
useEffect(() => {
|
||||
if (typeof document === "undefined") return;
|
||||
const label = docTitle ?? (typeof title === "string" ? title : "");
|
||||
document.title = label ? `${label} — pi2.nvim` : "pi2.nvim";
|
||||
}, [docTitle, title]);
|
||||
|
||||
return (
|
||||
<header className={cn("page-head", size === "lg" && "page-head--lg", className)}>
|
||||
<div className="page-head__text">
|
||||
<h1 className="page-head__title">{title}</h1>
|
||||
{subtitle ? <p className="page-head__sub">{subtitle}</p> : null}
|
||||
{children}
|
||||
</div>
|
||||
{actions ? <div className="page-head__actions">{actions}</div> : null}
|
||||
</header>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import { useEffect, useReducer } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { currentLanguage, localeFor } from "@/lib/i18n";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
type Translator = (key: string, params?: Record<string, unknown>) => string;
|
||||
|
||||
/** Relative time exactly as `App.fmt.rel` (just now / N minutes / N hours / N days). */
|
||||
export function formatRelative(ts: number, t: Translator, now = Date.now()): string {
|
||||
const seconds = Math.max(0, (now - ts) / 1000);
|
||||
if (seconds < 60) return t("time.justNow");
|
||||
if (seconds < 3600) return t("time.minutesAgo", { n: Math.floor(seconds / 60) });
|
||||
if (seconds < 86400) return t("time.hoursAgo", { n: Math.floor(seconds / 3600) });
|
||||
return t("time.daysAgo", { n: Math.floor(seconds / 86400) });
|
||||
}
|
||||
|
||||
export interface RelativeTimeProps {
|
||||
ts: number | null | undefined;
|
||||
className?: string;
|
||||
/** Refresh once a minute (default true). */
|
||||
live?: boolean;
|
||||
}
|
||||
|
||||
export function RelativeTime({ ts, className, live = true }: RelativeTimeProps) {
|
||||
const { t } = useTranslation();
|
||||
const [, tick] = useReducer((n: number) => n + 1, 0);
|
||||
|
||||
useEffect(() => {
|
||||
if (!live) return;
|
||||
const timer = setInterval(tick, 30_000);
|
||||
return () => clearInterval(timer);
|
||||
}, [live]);
|
||||
|
||||
if (ts == null || !Number.isFinite(ts)) return <span className={cn("muted", className)}>—</span>;
|
||||
|
||||
return (
|
||||
<time
|
||||
className={className}
|
||||
dateTime={new Date(ts).toISOString()}
|
||||
title={new Date(ts).toLocaleString(localeFor(currentLanguage()))}
|
||||
>
|
||||
{formatRelative(ts, t)}
|
||||
</time>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { Link } from "@tanstack/react-router";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { StatusDot, serverDotClass } from "@/components/ui/status-dot";
|
||||
import type { InfoResponse } from "@/lib/api";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { useConnectionStore } from "@/stores/connection";
|
||||
|
||||
export interface ServerChipProps {
|
||||
server?: InfoResponse | null;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Single-server identity chip in the topbar: status dot + server name + hostname,
|
||||
* linking to /status. Never a server picker (single-server invariant).
|
||||
*/
|
||||
export function ServerChip({ server: override, className }: ServerChipProps) {
|
||||
const { t } = useTranslation();
|
||||
const stored = useConnectionStore((state) => state.server);
|
||||
const server = override ?? stored;
|
||||
|
||||
const name = server?.name ?? "pi2.nvim";
|
||||
const hostname =
|
||||
server?.hostname ?? (typeof window !== "undefined" ? window.location.hostname || "localhost" : "localhost");
|
||||
const status = server?.status ?? "offline";
|
||||
const dot = serverDotClass(status);
|
||||
const dotStatus = dot.replace("dot--", "") as "ok" | "attention" | "exited" | "idle";
|
||||
|
||||
return (
|
||||
<Link
|
||||
to="/status"
|
||||
className={cn("server-chip", className)}
|
||||
title={t("shell.serverChip", { name })}
|
||||
>
|
||||
<StatusDot status={dotStatus} title={t(`state.${status}`)} />
|
||||
<span className="server-chip__name">{name}</span>
|
||||
<span className="server-chip__host mono">{hostname}</span>
|
||||
</Link>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { useTheme } from "next-themes";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { Seg, type SegOption } from "@/components/ui/seg";
|
||||
|
||||
type ThemeValue = "light" | "dark" | "system";
|
||||
|
||||
const THEMES: ThemeValue[] = ["light", "dark", "system"];
|
||||
|
||||
function isThemeValue(value: string | undefined): value is ThemeValue {
|
||||
return value === "light" || value === "dark" || value === "system";
|
||||
}
|
||||
|
||||
/** Topbar light/dark/system segment; next-themes persists it to `pi2-theme`. */
|
||||
export function ThemeMenu() {
|
||||
const { t } = useTranslation();
|
||||
const { theme, setTheme } = useTheme();
|
||||
const [mounted, setMounted] = useState(false);
|
||||
|
||||
useEffect(() => setMounted(true), []);
|
||||
|
||||
const value: ThemeValue = mounted && isThemeValue(theme) ? theme : "system";
|
||||
const options: SegOption<ThemeValue>[] = THEMES.map((item) => ({
|
||||
value: item,
|
||||
label: t(`shell.${item}`),
|
||||
}));
|
||||
|
||||
return (
|
||||
<Seg
|
||||
value={value}
|
||||
options={options}
|
||||
onChange={setTheme}
|
||||
ariaLabel={t("shell.theme")}
|
||||
/>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user