2025-12-06 12:46:40 +09:00
|
|
|
"use client"
|
2025-12-05 21:09:34 +08:00
|
|
|
|
2025-12-10 08:21:15 +08:00
|
|
|
import { Moon, Sun } from "lucide-react"
|
2025-12-22 21:54:25 +08:00
|
|
|
import { usePathname, useRouter, useSearchParams } from "next/navigation"
|
|
|
|
|
import { Suspense, useEffect, useState } from "react"
|
2025-12-06 12:46:40 +09:00
|
|
|
import { Button } from "@/components/ui/button"
|
2025-12-05 21:09:34 +08:00
|
|
|
import {
|
|
|
|
|
Dialog,
|
|
|
|
|
DialogContent,
|
2025-12-06 12:46:40 +09:00
|
|
|
DialogDescription,
|
2025-12-05 21:09:34 +08:00
|
|
|
DialogHeader,
|
|
|
|
|
DialogTitle,
|
2025-12-06 12:46:40 +09:00
|
|
|
} from "@/components/ui/dialog"
|
|
|
|
|
import { Input } from "@/components/ui/input"
|
2025-12-06 21:42:28 +09:00
|
|
|
import { Label } from "@/components/ui/label"
|
2025-12-22 21:54:25 +08:00
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@/components/ui/select"
|
2025-12-06 21:42:28 +09:00
|
|
|
import { Switch } from "@/components/ui/switch"
|
2025-12-20 20:18:54 +05:30
|
|
|
import { useDictionary } from "@/hooks/use-dictionary"
|
2025-12-22 19:58:55 +05:30
|
|
|
import { getApiEndpoint } from "@/lib/base-path"
|
2025-12-22 21:54:25 +08:00
|
|
|
import { i18n, type Locale } from "@/lib/i18n/config"
|
2025-12-23 21:52:04 +09:00
|
|
|
import { cn } from "@/lib/utils"
|
|
|
|
|
|
|
|
|
|
// Reusable setting item component for consistent layout
|
|
|
|
|
function SettingItem({
|
|
|
|
|
label,
|
|
|
|
|
description,
|
|
|
|
|
children,
|
|
|
|
|
}: {
|
|
|
|
|
label: string
|
|
|
|
|
description?: string
|
|
|
|
|
children: React.ReactNode
|
|
|
|
|
}) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="flex items-center justify-between py-4 first:pt-0 last:pb-0">
|
|
|
|
|
<div className="space-y-0.5 pr-4">
|
|
|
|
|
<Label className="text-sm font-medium">{label}</Label>
|
|
|
|
|
{description && (
|
|
|
|
|
<p className="text-xs text-muted-foreground max-w-[260px]">
|
|
|
|
|
{description}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="shrink-0">{children}</div>
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
2025-12-22 21:54:25 +08:00
|
|
|
|
|
|
|
|
const LANGUAGE_LABELS: Record<Locale, string> = {
|
|
|
|
|
en: "English",
|
|
|
|
|
zh: "中文",
|
|
|
|
|
ja: "日本語",
|
|
|
|
|
}
|
2025-12-05 21:09:34 +08:00
|
|
|
|
|
|
|
|
interface SettingsDialogProps {
|
2025-12-06 12:46:40 +09:00
|
|
|
open: boolean
|
|
|
|
|
onOpenChange: (open: boolean) => void
|
2025-12-06 21:42:28 +09:00
|
|
|
onCloseProtectionChange?: (enabled: boolean) => void
|
2025-12-10 08:21:15 +08:00
|
|
|
drawioUi: "min" | "sketch"
|
|
|
|
|
onToggleDrawioUi: () => void
|
|
|
|
|
darkMode: boolean
|
|
|
|
|
onToggleDarkMode: () => void
|
2025-12-05 21:09:34 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-06 12:46:40 +09:00
|
|
|
export const STORAGE_ACCESS_CODE_KEY = "next-ai-draw-io-access-code"
|
2025-12-06 21:42:28 +09:00
|
|
|
export const STORAGE_CLOSE_PROTECTION_KEY = "next-ai-draw-io-close-protection"
|
2025-12-08 11:00:14 +09:00
|
|
|
const STORAGE_ACCESS_CODE_REQUIRED_KEY = "next-ai-draw-io-access-code-required"
|
|
|
|
|
|
|
|
|
|
function getStoredAccessCodeRequired(): boolean | null {
|
|
|
|
|
if (typeof window === "undefined") return null
|
|
|
|
|
const stored = localStorage.getItem(STORAGE_ACCESS_CODE_REQUIRED_KEY)
|
|
|
|
|
if (stored === null) return null
|
|
|
|
|
return stored === "true"
|
|
|
|
|
}
|
2025-12-05 21:09:34 +08:00
|
|
|
|
2025-12-22 21:54:25 +08:00
|
|
|
function SettingsContent({
|
2025-12-06 21:42:28 +09:00
|
|
|
open,
|
|
|
|
|
onOpenChange,
|
|
|
|
|
onCloseProtectionChange,
|
2025-12-10 08:21:15 +08:00
|
|
|
drawioUi,
|
|
|
|
|
onToggleDrawioUi,
|
|
|
|
|
darkMode,
|
|
|
|
|
onToggleDarkMode,
|
2025-12-06 21:42:28 +09:00
|
|
|
}: SettingsDialogProps) {
|
2025-12-20 20:18:54 +05:30
|
|
|
const dict = useDictionary()
|
2025-12-22 21:54:25 +08:00
|
|
|
const router = useRouter()
|
|
|
|
|
const pathname = usePathname() || "/"
|
|
|
|
|
const search = useSearchParams()
|
2025-12-06 12:46:40 +09:00
|
|
|
const [accessCode, setAccessCode] = useState("")
|
2025-12-06 21:42:28 +09:00
|
|
|
const [closeProtection, setCloseProtection] = useState(true)
|
2025-12-07 00:21:59 +09:00
|
|
|
const [isVerifying, setIsVerifying] = useState(false)
|
|
|
|
|
const [error, setError] = useState("")
|
2025-12-08 11:00:14 +09:00
|
|
|
const [accessCodeRequired, setAccessCodeRequired] = useState(
|
|
|
|
|
() => getStoredAccessCodeRequired() ?? false,
|
|
|
|
|
)
|
2025-12-22 21:54:25 +08:00
|
|
|
const [currentLang, setCurrentLang] = useState("en")
|
2025-12-08 11:00:14 +09:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
// Only fetch if not cached in localStorage
|
|
|
|
|
if (getStoredAccessCodeRequired() !== null) return
|
|
|
|
|
|
2025-12-22 19:58:55 +05:30
|
|
|
fetch(getApiEndpoint("/api/config"))
|
2025-12-08 11:00:14 +09:00
|
|
|
.then((res) => {
|
|
|
|
|
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
|
|
|
|
return res.json()
|
|
|
|
|
})
|
|
|
|
|
.then((data) => {
|
|
|
|
|
const required = data?.accessCodeRequired === true
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
STORAGE_ACCESS_CODE_REQUIRED_KEY,
|
|
|
|
|
String(required),
|
|
|
|
|
)
|
|
|
|
|
setAccessCodeRequired(required)
|
|
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
// Don't cache on error - allow retry on next mount
|
|
|
|
|
setAccessCodeRequired(false)
|
|
|
|
|
})
|
|
|
|
|
}, [])
|
2025-12-05 21:09:34 +08:00
|
|
|
|
2025-12-22 21:54:25 +08:00
|
|
|
// Detect current language from pathname
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const seg = pathname.split("/").filter(Boolean)
|
|
|
|
|
const first = seg[0]
|
|
|
|
|
if (first && i18n.locales.includes(first as Locale)) {
|
|
|
|
|
setCurrentLang(first)
|
|
|
|
|
} else {
|
|
|
|
|
setCurrentLang(i18n.defaultLocale)
|
|
|
|
|
}
|
|
|
|
|
}, [pathname])
|
|
|
|
|
|
2025-12-05 21:09:34 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (open) {
|
2025-12-06 12:46:40 +09:00
|
|
|
const storedCode =
|
|
|
|
|
localStorage.getItem(STORAGE_ACCESS_CODE_KEY) || ""
|
|
|
|
|
setAccessCode(storedCode)
|
2025-12-06 21:42:28 +09:00
|
|
|
|
|
|
|
|
const storedCloseProtection = localStorage.getItem(
|
|
|
|
|
STORAGE_CLOSE_PROTECTION_KEY,
|
|
|
|
|
)
|
|
|
|
|
// Default to true if not set
|
|
|
|
|
setCloseProtection(storedCloseProtection !== "false")
|
feat: add bring-your-own-API-key support (#186)
- Add AI provider settings to config panel (provider, model, API key, base URL)
- Support 7 providers: OpenAI, Anthropic, Google, Azure, OpenRouter, DeepSeek, SiliconFlow
- Client API keys stored in localStorage, never stored on server
- Client settings override server env vars when provided
- Skip server credential validation when client provides API key
- Bypass usage limits (request/token/TPM) when using own API key
- Add /api/config endpoint for fetching usage limits
- Add privacy notices to settings dialog, about pages, and quota toast
- Add clear settings button to reset saved API keys
- Update README files (EN/CN/JA) with BYOK documentation
Co-authored-by: dayuan.jiang <jiangdy@amazon.co.jp>
2025-12-09 17:50:07 +09:00
|
|
|
|
2025-12-07 00:21:59 +09:00
|
|
|
setError("")
|
2025-12-05 21:09:34 +08:00
|
|
|
}
|
2025-12-06 12:46:40 +09:00
|
|
|
}, [open])
|
2025-12-05 21:09:34 +08:00
|
|
|
|
2025-12-22 21:54:25 +08:00
|
|
|
const changeLanguage = (lang: string) => {
|
2025-12-25 22:20:59 +09:00
|
|
|
// Save locale to localStorage for persistence across restarts
|
|
|
|
|
localStorage.setItem("next-ai-draw-io-locale", lang)
|
|
|
|
|
|
2025-12-22 21:54:25 +08:00
|
|
|
const parts = pathname.split("/")
|
|
|
|
|
if (parts.length > 1 && i18n.locales.includes(parts[1] as Locale)) {
|
|
|
|
|
parts[1] = lang
|
|
|
|
|
} else {
|
|
|
|
|
parts.splice(1, 0, lang)
|
|
|
|
|
}
|
|
|
|
|
const newPath = parts.join("/") || "/"
|
|
|
|
|
const searchStr = search?.toString() ? `?${search.toString()}` : ""
|
|
|
|
|
router.push(newPath + searchStr)
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-07 00:21:59 +09:00
|
|
|
const handleSave = async () => {
|
2025-12-08 11:00:14 +09:00
|
|
|
if (!accessCodeRequired) return
|
|
|
|
|
|
2025-12-07 00:21:59 +09:00
|
|
|
setError("")
|
|
|
|
|
setIsVerifying(true)
|
|
|
|
|
|
|
|
|
|
try {
|
2025-12-22 19:58:55 +05:30
|
|
|
const response = await fetch(
|
|
|
|
|
getApiEndpoint("/api/verify-access-code"),
|
|
|
|
|
{
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"x-access-code": accessCode.trim(),
|
|
|
|
|
},
|
2025-12-07 00:21:59 +09:00
|
|
|
},
|
2025-12-22 19:58:55 +05:30
|
|
|
)
|
2025-12-07 00:21:59 +09:00
|
|
|
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
|
|
|
|
|
if (!data.valid) {
|
2025-12-20 20:18:54 +05:30
|
|
|
setError(data.message || dict.errors.invalidAccessCode)
|
2025-12-07 00:21:59 +09:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
localStorage.setItem(STORAGE_ACCESS_CODE_KEY, accessCode.trim())
|
|
|
|
|
onOpenChange(false)
|
|
|
|
|
} catch {
|
2025-12-20 20:18:54 +05:30
|
|
|
setError(dict.errors.networkError)
|
2025-12-07 00:21:59 +09:00
|
|
|
} finally {
|
|
|
|
|
setIsVerifying(false)
|
|
|
|
|
}
|
2025-12-06 12:46:40 +09:00
|
|
|
}
|
2025-12-05 21:09:34 +08:00
|
|
|
|
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
|
|
|
if (e.key === "Enter") {
|
2025-12-06 12:46:40 +09:00
|
|
|
e.preventDefault()
|
|
|
|
|
handleSave()
|
2025-12-05 21:09:34 +08:00
|
|
|
}
|
2025-12-06 12:46:40 +09:00
|
|
|
}
|
2025-12-05 21:09:34 +08:00
|
|
|
|
|
|
|
|
return (
|
2025-12-23 21:52:04 +09:00
|
|
|
<DialogContent className="sm:max-w-lg p-0 gap-0">
|
|
|
|
|
{/* Header */}
|
|
|
|
|
<DialogHeader className="px-6 pt-6 pb-4">
|
2025-12-22 21:54:25 +08:00
|
|
|
<DialogTitle>{dict.settings.title}</DialogTitle>
|
2025-12-23 21:52:04 +09:00
|
|
|
<DialogDescription className="mt-1">
|
2025-12-22 21:54:25 +08:00
|
|
|
{dict.settings.description}
|
|
|
|
|
</DialogDescription>
|
|
|
|
|
</DialogHeader>
|
2025-12-23 21:52:04 +09:00
|
|
|
|
|
|
|
|
{/* Content */}
|
|
|
|
|
<div className="px-6 pb-6">
|
|
|
|
|
<div className="divide-y divide-border-subtle">
|
|
|
|
|
{/* Access Code (conditional) */}
|
|
|
|
|
{accessCodeRequired && (
|
|
|
|
|
<div className="py-4 first:pt-0 space-y-3">
|
|
|
|
|
<div className="space-y-0.5">
|
|
|
|
|
<Label
|
|
|
|
|
htmlFor="access-code"
|
|
|
|
|
className="text-sm font-medium"
|
|
|
|
|
>
|
|
|
|
|
{dict.settings.accessCode}
|
|
|
|
|
</Label>
|
|
|
|
|
<p className="text-xs text-muted-foreground">
|
|
|
|
|
{dict.settings.accessCodeDescription}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex gap-2">
|
|
|
|
|
<Input
|
|
|
|
|
id="access-code"
|
|
|
|
|
type="password"
|
|
|
|
|
value={accessCode}
|
|
|
|
|
onChange={(e) =>
|
|
|
|
|
setAccessCode(e.target.value)
|
|
|
|
|
}
|
|
|
|
|
onKeyDown={handleKeyDown}
|
|
|
|
|
placeholder={
|
|
|
|
|
dict.settings.accessCodePlaceholder
|
|
|
|
|
}
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
className="h-9"
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={handleSave}
|
|
|
|
|
disabled={isVerifying || !accessCode.trim()}
|
|
|
|
|
className="h-9 px-4 rounded-xl"
|
|
|
|
|
>
|
|
|
|
|
{isVerifying ? "..." : dict.common.save}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
{error && (
|
|
|
|
|
<p className="text-xs text-destructive">
|
|
|
|
|
{error}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2025-12-08 11:00:14 +09:00
|
|
|
</div>
|
2025-12-23 21:52:04 +09:00
|
|
|
)}
|
2025-12-10 08:21:15 +08:00
|
|
|
|
2025-12-23 21:52:04 +09:00
|
|
|
{/* Language */}
|
|
|
|
|
<SettingItem
|
|
|
|
|
label={dict.settings.language}
|
|
|
|
|
description={dict.settings.languageDescription}
|
|
|
|
|
>
|
|
|
|
|
<Select
|
|
|
|
|
value={currentLang}
|
|
|
|
|
onValueChange={changeLanguage}
|
|
|
|
|
>
|
|
|
|
|
<SelectTrigger
|
|
|
|
|
id="language-select"
|
|
|
|
|
className="w-[120px] h-9 rounded-xl"
|
|
|
|
|
>
|
|
|
|
|
<SelectValue />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
{i18n.locales.map((locale) => (
|
|
|
|
|
<SelectItem key={locale} value={locale}>
|
|
|
|
|
{LANGUAGE_LABELS[locale]}
|
|
|
|
|
</SelectItem>
|
|
|
|
|
))}
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</SettingItem>
|
2025-12-10 08:21:15 +08:00
|
|
|
|
2025-12-23 21:52:04 +09:00
|
|
|
{/* Theme */}
|
|
|
|
|
<SettingItem
|
|
|
|
|
label={dict.settings.theme}
|
|
|
|
|
description={dict.settings.themeDescription}
|
2025-12-22 21:54:25 +08:00
|
|
|
>
|
2025-12-23 21:52:04 +09:00
|
|
|
<Button
|
|
|
|
|
id="theme-toggle"
|
|
|
|
|
variant="outline"
|
|
|
|
|
size="icon"
|
|
|
|
|
onClick={onToggleDarkMode}
|
|
|
|
|
className="h-9 w-9 rounded-xl border-border-subtle hover:bg-interactive-hover"
|
|
|
|
|
>
|
|
|
|
|
{darkMode ? (
|
|
|
|
|
<Sun className="h-4 w-4" />
|
|
|
|
|
) : (
|
|
|
|
|
<Moon className="h-4 w-4" />
|
|
|
|
|
)}
|
|
|
|
|
</Button>
|
|
|
|
|
</SettingItem>
|
2025-12-22 21:54:25 +08:00
|
|
|
|
2025-12-23 21:52:04 +09:00
|
|
|
{/* Draw.io Style */}
|
|
|
|
|
<SettingItem
|
|
|
|
|
label={dict.settings.drawioStyle}
|
|
|
|
|
description={`${dict.settings.drawioStyleDescription} ${
|
|
|
|
|
drawioUi === "min"
|
2025-12-22 21:54:25 +08:00
|
|
|
? dict.settings.minimal
|
2025-12-23 21:52:04 +09:00
|
|
|
: dict.settings.sketch
|
|
|
|
|
}`}
|
2025-12-22 21:54:25 +08:00
|
|
|
>
|
2025-12-23 21:52:04 +09:00
|
|
|
<Button
|
|
|
|
|
id="drawio-ui"
|
|
|
|
|
variant="outline"
|
|
|
|
|
onClick={onToggleDrawioUi}
|
|
|
|
|
className="h-9 w-[120px] rounded-xl border-border-subtle hover:bg-interactive-hover font-normal"
|
|
|
|
|
>
|
|
|
|
|
{dict.settings.switchTo}{" "}
|
|
|
|
|
{drawioUi === "min"
|
|
|
|
|
? dict.settings.sketch
|
|
|
|
|
: dict.settings.minimal}
|
|
|
|
|
</Button>
|
|
|
|
|
</SettingItem>
|
2025-12-22 21:54:25 +08:00
|
|
|
|
2025-12-23 21:52:04 +09:00
|
|
|
{/* Close Protection */}
|
|
|
|
|
<SettingItem
|
|
|
|
|
label={dict.settings.closeProtection}
|
|
|
|
|
description={dict.settings.closeProtectionDescription}
|
|
|
|
|
>
|
|
|
|
|
<Switch
|
|
|
|
|
id="close-protection"
|
|
|
|
|
checked={closeProtection}
|
|
|
|
|
onCheckedChange={(checked) => {
|
|
|
|
|
setCloseProtection(checked)
|
|
|
|
|
localStorage.setItem(
|
|
|
|
|
STORAGE_CLOSE_PROTECTION_KEY,
|
|
|
|
|
checked.toString(),
|
|
|
|
|
)
|
|
|
|
|
onCloseProtectionChange?.(checked)
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
</SettingItem>
|
2025-12-21 00:55:54 +09:00
|
|
|
</div>
|
2025-12-22 21:54:25 +08:00
|
|
|
</div>
|
2025-12-23 21:52:04 +09:00
|
|
|
|
|
|
|
|
{/* Footer */}
|
|
|
|
|
<div className="px-6 py-4 border-t border-border-subtle bg-surface-1/50 rounded-b-2xl">
|
|
|
|
|
<p className="text-xs text-muted-foreground text-center">
|
2025-12-22 21:54:25 +08:00
|
|
|
Version {process.env.APP_VERSION}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function SettingsDialog(props: SettingsDialogProps) {
|
|
|
|
|
return (
|
|
|
|
|
<Dialog open={props.open} onOpenChange={props.onOpenChange}>
|
|
|
|
|
<Suspense
|
|
|
|
|
fallback={
|
2025-12-23 21:52:04 +09:00
|
|
|
<DialogContent className="sm:max-w-lg p-0">
|
|
|
|
|
<div className="h-80 flex items-center justify-center">
|
|
|
|
|
<div className="animate-spin h-6 w-6 border-2 border-primary border-t-transparent rounded-full" />
|
2025-12-22 21:54:25 +08:00
|
|
|
</div>
|
|
|
|
|
</DialogContent>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<SettingsContent {...props} />
|
|
|
|
|
</Suspense>
|
2025-12-05 21:09:34 +08:00
|
|
|
</Dialog>
|
2025-12-06 12:46:40 +09:00
|
|
|
)
|
2025-12-05 21:09:34 +08:00
|
|
|
}
|