"use client"; import { useState } from "react"; import { useTranslations } from "next-intl"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Separator } from "@/components/ui/separator"; import { KeySchemaBuilder } from "@/components/KeySchemaBuilder"; import { encrypt, validateSelections, type KeySelection, type Language, type KeyType, } from "@/lib/crypto"; // Initial: name + custom_question in random order function initSelections(): KeySelection[] { const base: KeySelection[] = [ { type: "name", value: "" }, { type: "custom_question", question: "", value: "" }, ]; return Math.random() > 0.5 ? base : [base[1], base[0]]; } // ─── Asset Entry ────────────────────────────────────────── export interface AssetEntry { id: string; category: string; content: string; } // ─── VaultPayload (sent to server — no values / plaintext) ─ export interface KeySchemaItemSafe { type: KeyType; question?: string; } export interface VaultPayload { ciphertext: string; salt: string; iv: string; keySchema: KeySchemaItemSafe[]; keyLanguage: Language; } // ─── Asset Editor ───────────────────────────────────────── interface AssetEditorProps { entries: AssetEntry[]; onChange: (entries: AssetEntry[]) => void; } function AssetEditor({ entries, onChange }: AssetEditorProps) { const t = useTranslations("VaultForm"); const PRESET_CATEGORIES = [ t("categories.privateKey"), t("categories.crypto"), t("categories.bank"), t("categories.hardware"), t("categories.passwords"), t("categories.property"), t("categories.stocks"), t("categories.other"), ]; function addEntry() { const id = Date.now().toString(); onChange([...entries, { id, category: PRESET_CATEGORIES[0], content: "" }]); } function removeEntry(id: string) { onChange(entries.filter((e) => e.id !== id)); } function moveUp(index: number) { if (index === 0) return; const next = [...entries]; [next[index - 1], next[index]] = [next[index], next[index - 1]]; onChange(next); } function moveDown(index: number) { if (index === entries.length - 1) return; const next = [...entries]; [next[index], next[index + 1]] = [next[index + 1], next[index]]; onChange(next); } function updateField(id: string, field: keyof AssetEntry, value: string) { onChange(entries.map((e) => (e.id === id ? { ...e, [field]: value } : e))); } function getPlaceholder(category: string): string { if (category.includes("Private Key") || category.includes("Mnemonic") || category.includes("私钥") || category.includes("助记词")) return t("placeholders.privateKey"); if (category.includes("Crypto") || category.includes("加密货币")) return t("placeholders.crypto"); if (category.includes("Bank") || category.includes("银行")) return t("placeholders.bank"); if (category.includes("Hardware") || category.includes("硬件")) return t("placeholders.hardware"); if (category.includes("Password") || category.includes("Login") || category.includes("账号")) return t("placeholders.passwords"); if (category.includes("Property") || category.includes("Safe") || category.includes("房产")) return t("placeholders.property"); if (category.includes("Stock") || category.includes("Fund") || category.includes("股票")) return t("placeholders.stocks"); return t("placeholders.default"); } return (