Files
simba-rmic-2026/client/src/components/Gate.tsx
T
sloray 0a8f7bcf90
构建客户端 / 检查(类型 + 单元测试) (push) Canceled after 0s
构建客户端 / Linux x64(deb + AppImage) (push) Canceled after 0s
构建客户端 / Windows x64(NSIS 安装包) (push) Canceled after 0s
构建客户端 / macOS 通用(.app + .dmg) (push) Canceled after 0s
fix(client): 修复双实例镜像同步并调整底部工具布局
2026-11-01 23:58:27 +08:00

256 lines
8.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 首次启动的两道门:先选身份,再设密码。
*
* 身份必须问一次,因为 `role` 的默认值(裁判端)既可能是人选了裁判端,也可能只是还没人
* 碰过这个文件——后者让用户端的人第一次打开会直接进裁判端,然后以为这个程序只有那一种
* 界面。所以后端单独记了一个 `roleChosen`。
*
* # 为什么是"装一个模块"而不是"选一个选项"
*
* 这两条路后面接的是两套完全不同的界面与权限,是这台机器**要变成什么**,不是这次会话里
* 的一个偏好。所以它长得像在装模块:一张编号的板子、一句"它会用到什么"、一个明确的
* 确认动作。选完就锁进去,下次启动直接进这一端。
*
* 密码那一道同理:它不是"注册账号",是给这台机器配一把现场用的钥匙,所以文案讲的是
* "它挡住什么、绕不过什么",而不是"请设置一个强密码"。
*/
import { useEffect, useRef, useState } from "react";
import { api } from "../lib/ipc";
import { pushNote } from "../lib/store";
import type { Role } from "../lib/types";
import { Icon } from "./Icon";
const MODULES: {
role: Role;
index: string;
cn: string;
en: string;
summary: string;
needs: string[];
}[] = [
{
role: "team",
index: "01",
cn: "用户端",
en: "OPERATOR",
summary: "供操作手使用:本队画面、双方血条、瞄准器和命中反馈。",
needs: ["绑定一块图传板", "绑定一块装甲板", "设置现场密码"],
},
{
role: "referee",
index: "02",
cn: "裁判端",
en: "REFEREE",
summary: "供场边裁判使用:双路画面、双方血量、装甲板列表、截图和录制。",
needs: ["无需绑定设备", "无需密码", "自动分配左右槽位"],
},
];
export function RolePicker({ onOpenMaintenance }: { onOpenMaintenance?: () => void }) {
const [busy, setBusy] = useState<Role | null>(null);
const [error, setError] = useState<string | null>(null);
const choose = async (role: Role) => {
setBusy(role);
setError(null);
try {
await api.setRole(role);
await api.refresh();
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
} finally {
setBusy(null);
}
};
return (
<div className="gate">
<div className="gate__grid" aria-hidden="true" />
<header className="gate__head">
<span className="gate__brand">
RMIC<span className="gate__slash">::</span>2026
</span>
<span className="gate__phase label">FIRST RUN / 选择运行模式</span>
</header>
<div className="gate__lede">
<h1 className="gate__title">选择运行模式</h1>
<p className="gate__hint">
下次启动会沿用所选模式。也可在设置中切换;从用户端切换模式需要密码。
</p>
</div>
<div className="gate__choices">
{MODULES.map((module) => (
<button
key={module.role}
type="button"
className={`module plate${busy === module.role ? " is-busy" : ""}`}
disabled={busy !== null}
onClick={() => void choose(module.role)}
>
<span className="module__index num">{module.index}</span>
<span className="module__name">
<Icon name={module.role === "team" ? "gamepad" : "sports"} />
{module.cn}
<span className="module__en label">{module.en}</span>
</span>
<span className="module__summary">{module.summary}</span>
<span className="module__needs">
{module.needs.map((need) => (
<span key={need} className="module__need">
{need}
</span>
))}
</span>
<span className="module__cta">
{busy === module.role ? "正在切换…" : "使用此模式"}
<Icon name="next" className="module__arrow" />
</span>
<span className="module__corners" aria-hidden="true" />
</button>
))}
</div>
{error && <p className="gate__error">{error}</p>}
{onOpenMaintenance && (
<button type="button" className="gate__maintenance" onClick={onOpenMaintenance} disabled={busy !== null}>
<Icon name="firmware" />打开固件维护
</button>
)}
</div>
);
}
export function SetupPassword({ onOpenMaintenance }: { onOpenMaintenance?: () => void }) {
const [first, setFirst] = useState("");
const [again, setAgain] = useState("");
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const input = useRef<HTMLInputElement | null>(null);
useEffect(() => {
input.current?.focus();
}, []);
const submit = async () => {
if (busy) return;
if (first !== again) {
setError("两次输入的密码不一致");
setAgain("");
return;
}
setBusy(true);
setError(null);
try {
await api.setPassword(first);
await api.refresh();
pushNote("密码已设置。修改图传板或装甲板绑定、切换队伍号、退出程序时需要输入密码。");
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
} finally {
setBusy(false);
}
};
const strength = Math.min(4, Math.floor(first.length / 2));
return (
<div className="gate gate--narrow">
<div className="gate__grid" aria-hidden="true" />
<header className="gate__head">
<span className="gate__brand">
RMIC<span className="gate__slash">::</span>2026
</span>
<span className="gate__phase label">OPERATOR / 现场密码</span>
</header>
<div className="gate__lede">
<h1 className="gate__title">设置现场密码</h1>
<p className="gate__hint">
修改用户端的设备绑定、切换队伍号或退出程序时需要密码。密码至少 4 位;
本机保存 Argon2id 哈希,不保存明文。
</p>
</div>
<div className="keypad">
<label className="keypad__field">
<span className="keypad__label label">PASSWORD</span>
<input
ref={input}
className="input input--lg"
type="password"
placeholder="密码"
autoComplete="new-password"
value={first}
onChange={(event) => setFirst(event.target.value)}
/>
</label>
<label className="keypad__field">
<span className="keypad__label label">REPEAT</span>
<input
className="input input--lg"
type="password"
placeholder="再次输入密码"
autoComplete="new-password"
value={again}
onChange={(event) => setAgain(event.target.value)}
onKeyDown={(event) => {
if (event.key === "Enter") void submit();
}}
/>
</label>
<div className="keypad__meter" aria-hidden="true">
{[0, 1, 2, 3].map((step) => (
<span key={step} className={`keypad__cell${strength > step ? " is-on" : ""}`} />
))}
<span className="keypad__meter-note">
{first.length === 0
? "请输入密码"
: first.length < 4
? `还需要 ${4 - first.length} 位`
: "长度符合要求"}
</span>
</div>
{error && <p className="gate__error">{error}</p>}
<button
type="button"
className="btn btn--primary btn--lg keypad__submit"
onClick={() => void submit()}
disabled={busy || first.length < 4}
>
<Icon name="key" />{busy ? "保存中…" : "设置密码并继续"}
<span className="btn__key">⏎</span>
</button>
</div>
<p className="gate__note">
忘记密码时,可删除配置文件中的 password 一行,或删除整个配置文件。旧客户端
(Slint 版)的配置会在首次启动时自动导入,并备份为 .bak。
</p>
{onOpenMaintenance && (
<button type="button" className="gate__maintenance" onClick={onOpenMaintenance} disabled={busy}>
<Icon name="firmware" />打开固件维护
</button>
)}
</div>
);
}