Elffuss BraidHub

kikocis/utopia public

dev · 6.4 KiBRawPermalink
1import { useState } from "react";
2import { useMutation } from "@tanstack/react-query";
3import { Link, useNavigate } from "@tanstack/react-router";
4
5/* lucide 已移除品牌图标,GitHub mark 内联(官方 mark 路径,fill=currentColor) */
6function GithubMark({ size = 16 }: { size?: number }) {
7 return (
8 <svg
9 width={size}
10 height={size}
11 viewBox="0 0 24 24"
12 fill="currentColor"
13 aria-hidden
14 >
15 <path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12" />
16 </svg>
17 );
18}
19import { api, ApiError } from "../api";
20import { S } from "../i18n";
21import { Wordmark } from "../ui";
22import { LoginScene } from "./LoginScene";
23import { usePageTitle } from "../useTitle";
24
25export function Login() {
26 usePageTitle(S.app.name, S.login.signIn);
27 const navigate = useNavigate();
28 const [mode, setMode] = useState<"login" | "register">("login");
29 const [email, setEmail] = useState("");
30 const [password, setPassword] = useState("");
31 const [displayName, setDisplayName] = useState("");
32 const [leaving, setLeaving] = useState(false);
33
34 const mutation = useMutation({
35 mutationFn: async () => {
36 if (mode === "login") return api.login(email, password);
37 return api.register(email, password, displayName);
38 },
39 onSuccess: () => {
40 // 谢幕:卡片上浮淡出、巨构放大穿越,再进入图谱首页
41 setLeaving(true);
42 window.setTimeout(() => navigate({ to: "/" }), 650);
43 },
44 });
45
46 const error =
47 mutation.error instanceof ApiError
48 ? mutation.error.message
49 : mutation.error
50 ? S.login.networkError
51 : null;
52
53 const input = "input-dark w-full px-3 py-2 text-sm";
54
55 return (
56 <div className="min-h-screen flex items-center justify-center px-4">
57 {/* 巨构变换背景:星球 → 环形都市 → 城市平原 → 波动巨碑 */}
58 <LoginScene leaving={leaving} />
59 <div
60 className={`relative z-10 w-full max-w-sm ${leaving ? "u-depart" : ""}`}
61 >
62 <div className="mb-8 text-center u-rise">
63 <h1 className="text-5xl font-normal">
64 <Wordmark />
65 </h1>
66 <p className="mt-2 text-sm text-neutral-400">
67 {S.app.tagline}
68 <span className="ml-2 text-[11px]">{S.app.taglineSource}</span>
69 </p>
70 </div>
71
72 <div
73 className="u-card-opaque rounded-2xl p-6 u-rise"
74 style={{ animationDelay: "90ms" }}
75 >
76 <div className="flex gap-1 mb-5 bg-white/5 rounded-lg p-1">
77 {(["login", "register"] as const).map((m) => (
78 <button
79 key={m}
80 type="button"
81 onClick={() => setMode(m)}
82 className={`flex-1 rounded-md py-1.5 text-sm font-medium transition-colors ${
83 mode === m
84 ? "bg-white/10 text-neutral-100"
85 : "text-neutral-500 hover:text-neutral-300"
86 }`}
87 >
88 {m === "login" ? S.login.signIn : S.login.signUp}
89 </button>
90 ))}
91 </div>
92
93 <form
94 className="space-y-3"
95 onSubmit={(e) => {
96 e.preventDefault();
97 mutation.mutate();
98 }}
99 >
100 {mode === "register" && (
101 <input
102 className={input}
103 placeholder={S.login.displayName}
104 value={displayName}
105 onChange={(e) => setDisplayName(e.target.value)}
106 required
107 />
108 )}
109 <input
110 type="email"
111 className={input}
112 placeholder={S.login.email}
113 value={email}
114 onChange={(e) => setEmail(e.target.value)}
115 required
116 />
117 <input
118 type="password"
119 className={input}
120 placeholder={S.login.password}
121 value={password}
122 onChange={(e) => setPassword(e.target.value)}
123 required
124 minLength={8}
125 />
126 {error && <p className="text-sm text-rose-400">{error}</p>}
127 <button
128 type="submit"
129 disabled={mutation.isPending || leaving}
130 className="btn-primary w-full py-2 text-sm"
131 >
132 {mutation.isPending || leaving
133 ? S.login.submitting
134 : mode === "login"
135 ? S.login.signIn
136 : S.login.createAccount}
137 </button>
138 </form>
139 </div>
140
141 {/* 页脚:惯用同意句式内嵌条款/隐私链接 + GitHub 入口 */}
142 <div
143 className="mt-6 text-center u-rise"
144 style={{ animationDelay: "180ms" }}
145 >
146 <p className="u-balance text-[11px] leading-relaxed text-neutral-600">
147 {S.login.agreePrefix}
148 <Link
149 to="/terms"
150 className="whitespace-nowrap text-neutral-500 underline decoration-white/20 underline-offset-2 hover:text-neutral-300 transition-colors"
151 >
152 {S.legal.termsTitle}
153 </Link>
154 {S.login.agreeAnd}
155 <Link
156 to="/privacy"
157 className="whitespace-nowrap text-neutral-500 underline decoration-white/20 underline-offset-2 hover:text-neutral-300 transition-colors"
158 >
159 {S.legal.privacyTitle}
160 </Link>
161 {S.login.agreeSuffix}
162 </p>
163 <a
164 href={S.login.githubUrl}
165 target="_blank"
166 rel="noreferrer"
167 title="GitHub"
168 className="mt-3 inline-flex text-neutral-600 hover:text-neutral-300 transition-colors"
169 >
170 <GithubMark size={16} />
171 </a>
172 </div>
173 </div>
174 </div>
175 );
176}