| 27902ea | | | 1 | "use client"; |
| 27902ea | | | 2 | |
| 27902ea | | | 3 | import { useEffect, useState } from "react"; |
| 27902ea | | | 4 | import { useRouter } from "next/navigation"; |
| 27902ea | | | 5 | import { useAuth } from "@/lib/auth"; |
| 27902ea | | | 6 | import { useTheme } from "@/lib/theme"; |
| 27902ea | | | 7 | import { auth as authApi, type ApiToken } from "@/lib/api"; |
| 27902ea | | | 8 | import { Skeleton } from "@/app/components/skeleton"; |
| 27902ea | | | 9 | |
| 27902ea | | | 10 | export default function SettingsPage() { |
| 27902ea | | | 11 | const { user, loading: authLoading } = useAuth(); |
| 27902ea | | | 12 | const { theme, toggle: toggleTheme } = useTheme(); |
| 27902ea | | | 13 | const router = useRouter(); |
| 27902ea | | | 14 | |
| 27902ea | | | 15 | const [tokens, setTokens] = useState<ApiToken[]>([]); |
| 27902ea | | | 16 | const [tokensLoaded, setTokensLoaded] = useState(false); |
| 27902ea | | | 17 | |
| 27902ea | | | 18 | // New token form |
| 27902ea | | | 19 | const [showNewToken, setShowNewToken] = useState(false); |
| 27902ea | | | 20 | const [tokenName, setTokenName] = useState(""); |
| 27902ea | | | 21 | const [tokenExpiry, setTokenExpiry] = useState<"30d" | "90d" | "1y">("90d"); |
| 27902ea | | | 22 | const [createdToken, setCreatedToken] = useState<string | null>(null); |
| 27902ea | | | 23 | const [tokenCreating, setTokenCreating] = useState(false); |
| 27902ea | | | 24 | const [tokenError, setTokenError] = useState(""); |
| 27902ea | | | 25 | |
| 27902ea | | | 26 | useEffect(() => { |
| 27902ea | | | 27 | document.title = "Settings"; |
| 27902ea | | | 28 | }, []); |
| 27902ea | | | 29 | |
| 27902ea | | | 30 | useEffect(() => { |
| 27902ea | | | 31 | if (!authLoading && !user) { |
| 27902ea | | | 32 | router.push(`/login?redirect=${encodeURIComponent("/settings")}`); |
| 27902ea | | | 33 | } |
| 27902ea | | | 34 | }, [authLoading, user, router]); |
| 27902ea | | | 35 | |
| 27902ea | | | 36 | useEffect(() => { |
| 27902ea | | | 37 | if (!user) return; |
| 27902ea | | | 38 | authApi.listTokens().then(({ tokens }) => { |
| 27902ea | | | 39 | setTokens(tokens); |
| 27902ea | | | 40 | setTokensLoaded(true); |
| 27902ea | | | 41 | }).catch(() => setTokensLoaded(true)); |
| 27902ea | | | 42 | }, [user]); |
| 27902ea | | | 43 | |
| 27902ea | | | 44 | async function handleCreateToken(e: React.FormEvent) { |
| 27902ea | | | 45 | e.preventDefault(); |
| 27902ea | | | 46 | setTokenError(""); |
| 27902ea | | | 47 | setTokenCreating(true); |
| 27902ea | | | 48 | try { |
| 27902ea | | | 49 | const { token, api_token } = await authApi.createToken({ |
| 27902ea | | | 50 | name: tokenName, |
| 27902ea | | | 51 | expires_in: tokenExpiry, |
| 27902ea | | | 52 | }); |
| 27902ea | | | 53 | setCreatedToken(token); |
| 27902ea | | | 54 | setTokens((prev) => [api_token, ...prev]); |
| 27902ea | | | 55 | setTokenName(""); |
| 27902ea | | | 56 | setShowNewToken(false); |
| 27902ea | | | 57 | } catch (err: unknown) { |
| 27902ea | | | 58 | setTokenError(err instanceof Error ? err.message : "Failed to create token"); |
| 27902ea | | | 59 | } finally { |
| 27902ea | | | 60 | setTokenCreating(false); |
| 27902ea | | | 61 | } |
| 27902ea | | | 62 | } |
| 27902ea | | | 63 | |
| 27902ea | | | 64 | async function handleDeleteToken(id: number) { |
| 27902ea | | | 65 | try { |
| 27902ea | | | 66 | await authApi.deleteToken(id); |
| 27902ea | | | 67 | setTokens((prev) => prev.filter((t) => t.id !== id)); |
| 27902ea | | | 68 | } catch {} |
| 27902ea | | | 69 | } |
| 27902ea | | | 70 | |
| 27902ea | | | 71 | if (authLoading || !user) return null; |
| 27902ea | | | 72 | |
| 27902ea | | | 73 | return ( |
| 27902ea | | | 74 | <div style={{ maxWidth: 640, margin: "0 auto", padding: "1.5rem 1rem 3rem" }}> |
| 27902ea | | | 75 | <h1 className="text-lg font-medium" style={{ color: "var(--text-primary)", margin: "0 0 1.5rem" }}> |
| 27902ea | | | 76 | Settings |
| 27902ea | | | 77 | </h1> |
| 27902ea | | | 78 | |
| 27902ea | | | 79 | {/* Appearance */} |
| 27902ea | | | 80 | <Section title="Appearance"> |
| 27902ea | | | 81 | <div className="flex items-center justify-between py-2.5 px-1" style={{ borderBottom: "1px solid var(--divide)" }}> |
| 27902ea | | | 82 | <div> |
| 27902ea | | | 83 | <div className="text-sm" style={{ color: "var(--text-primary)" }}>Theme</div> |
| 27902ea | | | 84 | <div className="text-xs mt-0.5" style={{ color: "var(--text-faint)" }}>{theme} mode</div> |
| 27902ea | | | 85 | </div> |
| 27902ea | | | 86 | <button |
| 27902ea | | | 87 | onClick={toggleTheme} |
| 27902ea | | | 88 | className="btn-reset text-xs" |
| 27902ea | | | 89 | style={{ color: "var(--accent)", font: "inherit", fontSize: "0.75rem" }} |
| 27902ea | | | 90 | > |
| 27902ea | | | 91 | Switch to {theme === "light" ? "dark" : "light"} |
| 27902ea | | | 92 | </button> |
| 27902ea | | | 93 | </div> |
| 27902ea | | | 94 | </Section> |
| 27902ea | | | 95 | |
| 27902ea | | | 96 | {/* API Tokens */} |
| 27902ea | | | 97 | <Section |
| 27902ea | | | 98 | title="API Tokens" |
| 27902ea | | | 99 | action={ |
| 27902ea | | | 100 | <button |
| 27902ea | | | 101 | onClick={() => { setShowNewToken((v) => !v); setCreatedToken(null); }} |
| 27902ea | | | 102 | className="btn-reset text-xs" |
| 27902ea | | | 103 | style={{ color: "var(--text-muted)", font: "inherit", fontSize: "0.75rem" }} |
| 27902ea | | | 104 | > |
| 27902ea | | | 105 | {showNewToken ? "Cancel" : "New token"} |
| 27902ea | | | 106 | </button> |
| 27902ea | | | 107 | } |
| 27902ea | | | 108 | > |
| 27902ea | | | 109 | {createdToken && ( |
| 27902ea | | | 110 | <div |
| 27902ea | | | 111 | className="px-3 py-2 text-xs font-mono" |
| 27902ea | | | 112 | style={{ |
| 27902ea | | | 113 | backgroundColor: "var(--status-open-bg)", |
| 27902ea | | | 114 | border: "1px solid var(--status-open-border)", |
| 27902ea | | | 115 | color: "var(--status-open-text)", |
| 27902ea | | | 116 | wordBreak: "break-all", |
| 27902ea | | | 117 | borderBottom: "1px solid var(--divide)", |
| 27902ea | | | 118 | }} |
| 27902ea | | | 119 | > |
| 27902ea | | | 120 | <div className="text-xs" style={{ fontFamily: "inherit", marginBottom: 2 }}> |
| 27902ea | | | 121 | Copy this token now — it won't be shown again: |
| 27902ea | | | 122 | </div> |
| 27902ea | | | 123 | {createdToken} |
| 27902ea | | | 124 | </div> |
| 27902ea | | | 125 | )} |
| 27902ea | | | 126 | |
| 27902ea | | | 127 | {showNewToken && ( |
| 27902ea | | | 128 | <form onSubmit={handleCreateToken} className="px-3 py-2.5 flex flex-col gap-2" style={{ borderBottom: "1px solid var(--divide)" }}> |
| 27902ea | | | 129 | <input |
| 27902ea | | | 130 | value={tokenName} |
| 27902ea | | | 131 | onChange={(e) => setTokenName(e.target.value)} |
| 27902ea | | | 132 | placeholder="Token name (e.g. laptop)" |
| 27902ea | | | 133 | required |
| 27902ea | | | 134 | className="text-sm px-2.5 py-1.5" |
| 27902ea | | | 135 | style={{ |
| 27902ea | | | 136 | backgroundColor: "var(--bg-input)", |
| 27902ea | | | 137 | border: "1px solid var(--border-subtle)", |
| 27902ea | | | 138 | color: "var(--text-primary)", |
| 27902ea | | | 139 | font: "inherit", |
| 27902ea | | | 140 | fontSize: "0.8125rem", |
| 27902ea | | | 141 | }} |
| 27902ea | | | 142 | /> |
| 27902ea | | | 143 | <div className="flex items-center gap-2"> |
| 27902ea | | | 144 | {(["30d", "90d", "1y"] as const).map((exp) => ( |
| 27902ea | | | 145 | <button |
| 27902ea | | | 146 | key={exp} |
| 27902ea | | | 147 | type="button" |
| 27902ea | | | 148 | onClick={() => setTokenExpiry(exp)} |
| 27902ea | | | 149 | className="text-xs px-2 py-1" |
| 27902ea | | | 150 | style={{ |
| 27902ea | | | 151 | backgroundColor: tokenExpiry === exp ? "var(--accent)" : "var(--bg-inset)", |
| 27902ea | | | 152 | color: tokenExpiry === exp ? "var(--accent-text)" : "var(--text-muted)", |
| 27902ea | | | 153 | border: `1px solid ${tokenExpiry === exp ? "var(--accent)" : "var(--border-subtle)"}`, |
| 27902ea | | | 154 | cursor: "pointer", |
| 27902ea | | | 155 | font: "inherit", |
| 27902ea | | | 156 | fontSize: "0.6875rem", |
| 27902ea | | | 157 | }} |
| 27902ea | | | 158 | > |
| 27902ea | | | 159 | {exp === "30d" ? "30 days" : exp === "90d" ? "90 days" : "1 year"} |
| 27902ea | | | 160 | </button> |
| 27902ea | | | 161 | ))} |
| 27902ea | | | 162 | </div> |
| 27902ea | | | 163 | {tokenError && <div className="text-xs" style={{ color: "var(--status-closed-text)" }}>{tokenError}</div>} |
| 27902ea | | | 164 | <button |
| 27902ea | | | 165 | type="submit" |
| 27902ea | | | 166 | disabled={tokenCreating} |
| 27902ea | | | 167 | className="text-xs self-start px-3 py-1.5" |
| 27902ea | | | 168 | style={{ |
| 27902ea | | | 169 | backgroundColor: "var(--accent)", |
| 27902ea | | | 170 | color: "var(--accent-text)", |
| 27902ea | | | 171 | border: "none", |
| 27902ea | | | 172 | cursor: tokenCreating ? "wait" : "pointer", |
| 27902ea | | | 173 | font: "inherit", |
| 27902ea | | | 174 | fontSize: "0.6875rem", |
| 27902ea | | | 175 | opacity: tokenCreating ? 0.6 : 1, |
| 27902ea | | | 176 | }} |
| 27902ea | | | 177 | > |
| 27902ea | | | 178 | {tokenCreating ? "Creating..." : "Create token"} |
| 27902ea | | | 179 | </button> |
| 27902ea | | | 180 | </form> |
| 27902ea | | | 181 | )} |
| 27902ea | | | 182 | |
| 27902ea | | | 183 | {!tokensLoaded ? ( |
| 27902ea | | | 184 | <div className="p-3 space-y-1"> |
| 27902ea | | | 185 | <Skeleton width="100%" height="2rem" /> |
| 27902ea | | | 186 | <Skeleton width="100%" height="2rem" /> |
| 27902ea | | | 187 | </div> |
| 27902ea | | | 188 | ) : tokens.length === 0 ? ( |
| 27902ea | | | 189 | <div className="py-4 text-center text-xs" style={{ color: "var(--text-faint)" }}> |
| 27902ea | | | 190 | No API tokens. |
| 27902ea | | | 191 | </div> |
| 27902ea | | | 192 | ) : ( |
| 27902ea | | | 193 | tokens.map((t) => ( |
| 27902ea | | | 194 | <div |
| 27902ea | | | 195 | key={t.id} |
| 27902ea | | | 196 | className="flex items-center justify-between px-3 py-2" |
| 27902ea | | | 197 | style={{ borderBottom: "1px solid var(--divide)" }} |
| 27902ea | | | 198 | > |
| 27902ea | | | 199 | <div> |
| 27902ea | | | 200 | <div className="text-sm" style={{ color: "var(--text-primary)" }}> |
| 27902ea | | | 201 | {t.name} |
| 27902ea | | | 202 | <span className="font-mono text-xs ml-1.5" style={{ color: "var(--text-faint)" }}>#{t.id}</span> |
| 27902ea | | | 203 | </div> |
| 27902ea | | | 204 | <div className="text-xs" style={{ color: "var(--text-faint)" }}> |
| 27902ea | | | 205 | Expires {new Date(t.expires_at).toLocaleDateString()} |
| 27902ea | | | 206 | {t.last_used_at && <> · last used {new Date(t.last_used_at).toLocaleDateString()}</>} |
| 27902ea | | | 207 | </div> |
| 27902ea | | | 208 | </div> |
| 27902ea | | | 209 | <button |
| 27902ea | | | 210 | onClick={() => handleDeleteToken(t.id)} |
| 27902ea | | | 211 | className="btn-reset text-xs" |
| 27902ea | | | 212 | style={{ color: "var(--status-closed-text)", font: "inherit", fontSize: "0.6875rem" }} |
| 27902ea | | | 213 | > |
| 27902ea | | | 214 | Revoke |
| 27902ea | | | 215 | </button> |
| 27902ea | | | 216 | </div> |
| 27902ea | | | 217 | )) |
| 27902ea | | | 218 | )} |
| 27902ea | | | 219 | </Section> |
| 27902ea | | | 220 | </div> |
| 27902ea | | | 221 | ); |
| 27902ea | | | 222 | } |
| 27902ea | | | 223 | |
| 27902ea | | | 224 | function Section({ |
| 27902ea | | | 225 | title, |
| 27902ea | | | 226 | action, |
| 27902ea | | | 227 | children, |
| 27902ea | | | 228 | }: { |
| 27902ea | | | 229 | title: string; |
| 27902ea | | | 230 | action?: React.ReactNode; |
| 27902ea | | | 231 | children: React.ReactNode; |
| 27902ea | | | 232 | }) { |
| 27902ea | | | 233 | return ( |
| 27902ea | | | 234 | <div style={{ marginBottom: "1.5rem" }}> |
| 27902ea | | | 235 | <div |
| 27902ea | | | 236 | className="flex items-center justify-between pb-2" |
| 27902ea | | | 237 | style={{ borderBottom: "1px solid var(--divide)" }} |
| 27902ea | | | 238 | > |
| 27902ea | | | 239 | <span className="text-sm font-medium" style={{ color: "var(--text-primary)" }}>{title}</span> |
| 27902ea | | | 240 | {action} |
| 27902ea | | | 241 | </div> |
| 27902ea | | | 242 | {children} |
| 27902ea | | | 243 | </div> |
| 27902ea | | | 244 | ); |
| 27902ea | | | 245 | } |
| 27902ea | | | 246 | |