summaryrefslogtreecommitdiff
path: root/apps/web/src/components/editor/rename-dialog.tsx
blob: ad9833c05b5cf0b8cac83e42f2eb6c23fc9692ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { Button } from "@/components/ui/button";
import {
	Dialog,
	DialogBody,
	DialogContent,
	DialogFooter,
	DialogHeader,
	DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { useState } from "react";
import { Label } from "@/components/ui/label";

export function RenameDialog({
	isOpen,
	onOpenChange,
	onConfirm,
	currentName,
	title,
}: {
	isOpen: boolean;
	onOpenChange: (open: boolean) => void;
	onConfirm: (newName: string) => void;
	currentName: string;
	title: string;
}) {
	const [name, setName] = useState(currentName);

	const handleOpenChange = (open: boolean) => {
		if (open) {
			setName(currentName);
		}
		onOpenChange(open);
	};

	const trimmedName = name.trim();
	const isConfirmDisabled =
		trimmedName.length === 0 || trimmedName === currentName;

	const handleConfirm = () => {
		if (isConfirmDisabled) {
			return;
		}
		onConfirm(trimmedName);
	};

	return (
		<Dialog open={isOpen} onOpenChange={handleOpenChange}>
			<DialogContent>
				<DialogHeader>
					<DialogTitle>{title}</DialogTitle>
				</DialogHeader>

				<DialogBody className="gap-3">
					<Label>New name</Label>
					<Input
						value={name}
						onChange={(e) => setName(e.target.value)}
						onKeyDown={(e) => {
							if (e.key === "Enter") {
								e.preventDefault();
								handleConfirm();
							}
						}}
						onFocus={(e) => e.target.select()}
						placeholder="Enter a new name"
					/>
				</DialogBody>

				<DialogFooter>
					<Button
						variant="outline"
						onClick={(e) => {
							e.preventDefault();
							e.stopPropagation();
							onOpenChange(false);
						}}
					>
						Cancel
					</Button>
					<Button disabled={isConfirmDisabled} onClick={handleConfirm}>
						Rename
					</Button>
				</DialogFooter>
			</DialogContent>
		</Dialog>
	);
}