mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-01-02 22:32:27 +08:00
* fix: clear button cant clear error msg * new: add setting dialog and add accesscode * fix: address review feedback - dark mode, types, formatting * feat: only show Settings button when access code is required * refactor: rename ACCESS_CODES to ACCESS_CODE_LIST --------- Co-authored-by: dayuan.jiang <jdy.toh@gmail.com>
84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect } from "react";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Input } from "@/components/ui/input";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogFooter,
|
|
DialogDescription,
|
|
} from "@/components/ui/dialog";
|
|
|
|
interface SettingsDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
}
|
|
|
|
export const STORAGE_ACCESS_CODE_KEY = "next-ai-draw-io-access-code";
|
|
|
|
export function SettingsDialog({
|
|
open,
|
|
onOpenChange,
|
|
}: SettingsDialogProps) {
|
|
const [accessCode, setAccessCode] = useState("");
|
|
|
|
useEffect(() => {
|
|
if (open) {
|
|
const storedCode = localStorage.getItem(STORAGE_ACCESS_CODE_KEY) || "";
|
|
setAccessCode(storedCode);
|
|
}
|
|
}, [open]);
|
|
|
|
const handleSave = () => {
|
|
localStorage.setItem(STORAGE_ACCESS_CODE_KEY, accessCode.trim());
|
|
onOpenChange(false);
|
|
};
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
if (e.key === "Enter") {
|
|
e.preventDefault();
|
|
handleSave();
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-md">
|
|
<DialogHeader>
|
|
<DialogTitle>Settings</DialogTitle>
|
|
<DialogDescription>
|
|
Configure your access settings.
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
<div className="space-y-4 py-2">
|
|
<div className="space-y-2">
|
|
<label className="text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70">
|
|
Access Code
|
|
</label>
|
|
<Input
|
|
type="password"
|
|
value={accessCode}
|
|
onChange={(e) => setAccessCode(e.target.value)}
|
|
onKeyDown={handleKeyDown}
|
|
placeholder="Enter access code"
|
|
autoComplete="off"
|
|
/>
|
|
<p className="text-[0.8rem] text-muted-foreground">
|
|
Required if the server has enabled access control.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<DialogFooter>
|
|
<Button variant="outline" onClick={() => onOpenChange(false)}>
|
|
Cancel
|
|
</Button>
|
|
<Button onClick={handleSave}>Save</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|