397 行
12 KiB
TypeScript
397 行
12 KiB
TypeScript
|
|
import { invoke } from "@tauri-apps/api/core";
|
|||
|
|
import { useCallback, useEffect, useState, type FormEvent } from "react";
|
|||
|
|
|
|||
|
|
import {
|
|||
|
|
connectionProfileListSchema,
|
|||
|
|
connectionProfileSchema,
|
|||
|
|
type AuthenticationType,
|
|||
|
|
type ConnectionProfile,
|
|||
|
|
type ConnectionProfileInput,
|
|||
|
|
type ProfileRole,
|
|||
|
|
type UpdateConnectionProfileRequest,
|
|||
|
|
} from "../../types/profile";
|
|||
|
|
|
|||
|
|
interface ProfileFormState {
|
|||
|
|
displayName: string;
|
|||
|
|
role: ProfileRole;
|
|||
|
|
baseUrl: string;
|
|||
|
|
eventStreamUrl: string;
|
|||
|
|
authenticationType: AuthenticationType;
|
|||
|
|
allowInsecureHttp: boolean;
|
|||
|
|
pinnedCertificateFingerprint: string;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const emptyForm: ProfileFormState = {
|
|||
|
|
displayName: "",
|
|||
|
|
role: "general",
|
|||
|
|
baseUrl: "https://",
|
|||
|
|
eventStreamUrl: "",
|
|||
|
|
authenticationType: "device-token",
|
|||
|
|
allowInsecureHttp: false,
|
|||
|
|
pinnedCertificateFingerprint: "",
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
export function ProfileManager() {
|
|||
|
|
const [profiles, setProfiles] = useState<ConnectionProfile[]>([]);
|
|||
|
|
const [form, setForm] = useState<ProfileFormState>(emptyForm);
|
|||
|
|
const [editingId, setEditingId] = useState<string | null>(null);
|
|||
|
|
const [busy, setBusy] = useState(false);
|
|||
|
|
const [error, setError] = useState<string | null>(null);
|
|||
|
|
const [notice, setNotice] = useState<string | null>(null);
|
|||
|
|
|
|||
|
|
const loadProfiles = useCallback(async () => {
|
|||
|
|
try {
|
|||
|
|
const result = await invoke<unknown>("list_connection_profiles");
|
|||
|
|
setProfiles(connectionProfileListSchema.parse(result));
|
|||
|
|
setError(null);
|
|||
|
|
} catch (reason) {
|
|||
|
|
setError(`Profile storage unavailable: ${errorMessage(reason)}`);
|
|||
|
|
}
|
|||
|
|
}, []);
|
|||
|
|
|
|||
|
|
useEffect(() => {
|
|||
|
|
void loadProfiles();
|
|||
|
|
}, [loadProfiles]);
|
|||
|
|
|
|||
|
|
async function submitProfile(event: FormEvent<HTMLFormElement>) {
|
|||
|
|
event.preventDefault();
|
|||
|
|
setBusy(true);
|
|||
|
|
setError(null);
|
|||
|
|
setNotice(null);
|
|||
|
|
|
|||
|
|
try {
|
|||
|
|
const profileInput = toProfileInput(form);
|
|||
|
|
const result = editingId
|
|||
|
|
? await invoke<unknown>("update_connection_profile", {
|
|||
|
|
request: {
|
|||
|
|
id: editingId,
|
|||
|
|
profile: profileInput,
|
|||
|
|
} satisfies UpdateConnectionProfileRequest,
|
|||
|
|
})
|
|||
|
|
: await invoke<unknown>("create_connection_profile", {
|
|||
|
|
request: profileInput,
|
|||
|
|
});
|
|||
|
|
const saved = connectionProfileSchema.parse(result);
|
|||
|
|
setProfiles((current) =>
|
|||
|
|
sortProfiles([
|
|||
|
|
...current.filter((profile) => profile.id !== saved.id),
|
|||
|
|
saved,
|
|||
|
|
]),
|
|||
|
|
);
|
|||
|
|
setNotice(
|
|||
|
|
editingId
|
|||
|
|
? `已更新 Profile:${saved.displayName}`
|
|||
|
|
: `已创建 Profile:${saved.displayName}`,
|
|||
|
|
);
|
|||
|
|
resetForm();
|
|||
|
|
} catch (reason) {
|
|||
|
|
setError(`Profile save failed: ${errorMessage(reason)}`);
|
|||
|
|
} finally {
|
|||
|
|
setBusy(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function editProfile(profile: ConnectionProfile) {
|
|||
|
|
setEditingId(profile.id);
|
|||
|
|
setForm({
|
|||
|
|
displayName: profile.displayName,
|
|||
|
|
role: profile.role,
|
|||
|
|
baseUrl: profile.baseUrl,
|
|||
|
|
eventStreamUrl: profile.eventStreamUrl ?? "",
|
|||
|
|
authenticationType: profile.authenticationType,
|
|||
|
|
allowInsecureHttp: profile.allowInsecureHttp,
|
|||
|
|
pinnedCertificateFingerprint: profile.pinnedCertificateFingerprint ?? "",
|
|||
|
|
});
|
|||
|
|
setError(null);
|
|||
|
|
setNotice(null);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
async function deleteProfile(profile: ConnectionProfile) {
|
|||
|
|
if (
|
|||
|
|
!window.confirm(
|
|||
|
|
`删除 Profile“${profile.displayName}”?此操作不会删除凭据值,因为 P01 不接触凭据。`,
|
|||
|
|
)
|
|||
|
|
) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
setBusy(true);
|
|||
|
|
setError(null);
|
|||
|
|
setNotice(null);
|
|||
|
|
try {
|
|||
|
|
await invoke("delete_connection_profile", { id: profile.id });
|
|||
|
|
setProfiles((current) =>
|
|||
|
|
current.filter((item) => item.id !== profile.id),
|
|||
|
|
);
|
|||
|
|
if (editingId === profile.id) {
|
|||
|
|
resetForm();
|
|||
|
|
}
|
|||
|
|
setNotice(`已删除 Profile:${profile.displayName}`);
|
|||
|
|
} catch (reason) {
|
|||
|
|
setError(`Profile deletion failed: ${errorMessage(reason)}`);
|
|||
|
|
} finally {
|
|||
|
|
setBusy(false);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function resetForm() {
|
|||
|
|
setEditingId(null);
|
|||
|
|
setForm(emptyForm);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return (
|
|||
|
|
<section className="profile-manager" aria-labelledby="profile-heading">
|
|||
|
|
<div className="section-heading">
|
|||
|
|
<div>
|
|||
|
|
<p className="eyebrow">P01 · foundation</p>
|
|||
|
|
<h2 id="profile-heading">连接 Profile</h2>
|
|||
|
|
</div>
|
|||
|
|
<p>
|
|||
|
|
这里只管理非敏感连接元数据。Token、密码和 API Key 不会进入 React 或
|
|||
|
|
SQLite 普通字段。
|
|||
|
|
</p>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<div className="profile-layout">
|
|||
|
|
<form className="profile-form" onSubmit={submitProfile}>
|
|||
|
|
<h3>{editingId ? "编辑 Profile" : "新建 Profile"}</h3>
|
|||
|
|
|
|||
|
|
<label>
|
|||
|
|
<span>显示名称</span>
|
|||
|
|
<input
|
|||
|
|
name="displayName"
|
|||
|
|
required
|
|||
|
|
maxLength={80}
|
|||
|
|
value={form.displayName}
|
|||
|
|
onChange={(event) =>
|
|||
|
|
setForm((current) => ({
|
|||
|
|
...current,
|
|||
|
|
displayName: event.target.value,
|
|||
|
|
}))
|
|||
|
|
}
|
|||
|
|
/>
|
|||
|
|
</label>
|
|||
|
|
|
|||
|
|
<label>
|
|||
|
|
<span>角色</span>
|
|||
|
|
<select
|
|||
|
|
name="role"
|
|||
|
|
value={form.role}
|
|||
|
|
onChange={(event) =>
|
|||
|
|
setForm((current) => ({
|
|||
|
|
...current,
|
|||
|
|
role: event.target.value as ProfileRole,
|
|||
|
|
}))
|
|||
|
|
}
|
|||
|
|
>
|
|||
|
|
<option value="general">通用</option>
|
|||
|
|
<option value="companion">陪伴</option>
|
|||
|
|
<option value="operations">运维</option>
|
|||
|
|
</select>
|
|||
|
|
</label>
|
|||
|
|
|
|||
|
|
<label>
|
|||
|
|
<span>REST 基础 URL</span>
|
|||
|
|
<input
|
|||
|
|
name="baseUrl"
|
|||
|
|
type="url"
|
|||
|
|
required
|
|||
|
|
value={form.baseUrl}
|
|||
|
|
onChange={(event) =>
|
|||
|
|
setForm((current) => ({
|
|||
|
|
...current,
|
|||
|
|
baseUrl: event.target.value,
|
|||
|
|
}))
|
|||
|
|
}
|
|||
|
|
/>
|
|||
|
|
</label>
|
|||
|
|
|
|||
|
|
<label>
|
|||
|
|
<span>SSE URL(可选)</span>
|
|||
|
|
<input
|
|||
|
|
name="eventStreamUrl"
|
|||
|
|
type="url"
|
|||
|
|
value={form.eventStreamUrl}
|
|||
|
|
onChange={(event) =>
|
|||
|
|
setForm((current) => ({
|
|||
|
|
...current,
|
|||
|
|
eventStreamUrl: event.target.value,
|
|||
|
|
}))
|
|||
|
|
}
|
|||
|
|
/>
|
|||
|
|
</label>
|
|||
|
|
|
|||
|
|
<label>
|
|||
|
|
<span>认证类型</span>
|
|||
|
|
<select
|
|||
|
|
name="authenticationType"
|
|||
|
|
value={form.authenticationType}
|
|||
|
|
onChange={(event) =>
|
|||
|
|
setForm((current) => ({
|
|||
|
|
...current,
|
|||
|
|
authenticationType: event.target.value as AuthenticationType,
|
|||
|
|
}))
|
|||
|
|
}
|
|||
|
|
>
|
|||
|
|
<option value="device-token">设备 Token</option>
|
|||
|
|
<option value="api-key">API Key</option>
|
|||
|
|
</select>
|
|||
|
|
</label>
|
|||
|
|
|
|||
|
|
<label>
|
|||
|
|
<span>证书 SHA-256 指纹(可选)</span>
|
|||
|
|
<input
|
|||
|
|
name="pinnedCertificateFingerprint"
|
|||
|
|
inputMode="text"
|
|||
|
|
placeholder="64 位十六进制"
|
|||
|
|
value={form.pinnedCertificateFingerprint}
|
|||
|
|
onChange={(event) =>
|
|||
|
|
setForm((current) => ({
|
|||
|
|
...current,
|
|||
|
|
pinnedCertificateFingerprint: event.target.value,
|
|||
|
|
}))
|
|||
|
|
}
|
|||
|
|
/>
|
|||
|
|
</label>
|
|||
|
|
|
|||
|
|
<label className="checkbox-field">
|
|||
|
|
<input
|
|||
|
|
name="allowInsecureHttp"
|
|||
|
|
type="checkbox"
|
|||
|
|
checked={form.allowInsecureHttp}
|
|||
|
|
onChange={(event) =>
|
|||
|
|
setForm((current) => ({
|
|||
|
|
...current,
|
|||
|
|
allowInsecureHttp: event.target.checked,
|
|||
|
|
}))
|
|||
|
|
}
|
|||
|
|
/>
|
|||
|
|
<span>明确允许不安全 HTTP(仅限受信任网络)</span>
|
|||
|
|
</label>
|
|||
|
|
|
|||
|
|
<p className="form-note">
|
|||
|
|
槽位分配、自动连接和连接切换保护将在 P02 实现。
|
|||
|
|
</p>
|
|||
|
|
|
|||
|
|
<div className="button-row">
|
|||
|
|
<button type="submit" disabled={busy}>
|
|||
|
|
{editingId ? "更新 Profile" : "保存 Profile"}
|
|||
|
|
</button>
|
|||
|
|
{editingId ? (
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="button-secondary"
|
|||
|
|
disabled={busy}
|
|||
|
|
onClick={resetForm}
|
|||
|
|
>
|
|||
|
|
取消编辑
|
|||
|
|
</button>
|
|||
|
|
) : null}
|
|||
|
|
</div>
|
|||
|
|
</form>
|
|||
|
|
|
|||
|
|
<div className="profile-list" aria-label="已保存 Profile">
|
|||
|
|
<h3>已保存 ({profiles.length})</h3>
|
|||
|
|
{profiles.length === 0 ? (
|
|||
|
|
<p className="empty-state">尚未保存 Profile。</p>
|
|||
|
|
) : (
|
|||
|
|
profiles.map((profile) => (
|
|||
|
|
<article className="profile-card" key={profile.id}>
|
|||
|
|
<div>
|
|||
|
|
<strong>{profile.displayName}</strong>
|
|||
|
|
<span>{roleLabel(profile.role)}</span>
|
|||
|
|
</div>
|
|||
|
|
<code>{profile.baseUrl}</code>
|
|||
|
|
<p>
|
|||
|
|
{profile.authenticationType === "api-key"
|
|||
|
|
? "API Key 引用"
|
|||
|
|
: "设备 Token 引用"}
|
|||
|
|
{" · "}
|
|||
|
|
{profile.credentialReference ? "已登记" : "待登记"}
|
|||
|
|
</p>
|
|||
|
|
<div className="button-row">
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="button-secondary"
|
|||
|
|
disabled={busy}
|
|||
|
|
aria-label={`编辑 ${profile.displayName}`}
|
|||
|
|
onClick={() => editProfile(profile)}
|
|||
|
|
>
|
|||
|
|
编辑
|
|||
|
|
</button>
|
|||
|
|
<button
|
|||
|
|
type="button"
|
|||
|
|
className="button-danger"
|
|||
|
|
disabled={busy}
|
|||
|
|
aria-label={`删除 ${profile.displayName}`}
|
|||
|
|
onClick={() => void deleteProfile(profile)}
|
|||
|
|
>
|
|||
|
|
删除
|
|||
|
|
</button>
|
|||
|
|
</div>
|
|||
|
|
</article>
|
|||
|
|
))
|
|||
|
|
)}
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
{notice ? (
|
|||
|
|
<p className="notice-banner" aria-live="polite">
|
|||
|
|
{notice}
|
|||
|
|
</p>
|
|||
|
|
) : null}
|
|||
|
|
{error ? (
|
|||
|
|
<p className="error-banner" role="alert">
|
|||
|
|
{error}
|
|||
|
|
</p>
|
|||
|
|
) : null}
|
|||
|
|
</section>
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function toProfileInput(form: ProfileFormState): ConnectionProfileInput {
|
|||
|
|
const eventStreamUrl = form.eventStreamUrl.trim();
|
|||
|
|
const fingerprint = form.pinnedCertificateFingerprint.trim();
|
|||
|
|
return {
|
|||
|
|
displayName: form.displayName.trim(),
|
|||
|
|
role: form.role,
|
|||
|
|
baseUrl: form.baseUrl.trim(),
|
|||
|
|
eventStreamUrl: eventStreamUrl || undefined,
|
|||
|
|
authenticationType: form.authenticationType,
|
|||
|
|
allowInsecureHttp: form.allowInsecureHttp,
|
|||
|
|
pinnedCertificateFingerprint: fingerprint || undefined,
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function sortProfiles(profiles: ConnectionProfile[]) {
|
|||
|
|
return [...profiles].sort((left, right) =>
|
|||
|
|
left.displayName.localeCompare(right.displayName, "zh-CN"),
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function roleLabel(role: ProfileRole) {
|
|||
|
|
switch (role) {
|
|||
|
|
case "companion":
|
|||
|
|
return "陪伴";
|
|||
|
|
case "operations":
|
|||
|
|
return "运维";
|
|||
|
|
case "general":
|
|||
|
|
return "通用";
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function errorMessage(reason: unknown) {
|
|||
|
|
if (reason instanceof Error) {
|
|||
|
|
return reason.message;
|
|||
|
|
}
|
|||
|
|
if (
|
|||
|
|
typeof reason === "object" &&
|
|||
|
|
reason !== null &&
|
|||
|
|
"message" in reason &&
|
|||
|
|
typeof reason.message === "string"
|
|||
|
|
) {
|
|||
|
|
return reason.message;
|
|||
|
|
}
|
|||
|
|
return String(reason);
|
|||
|
|
}
|