summaryrefslogtreecommitdiff
path: root/apps/web/src/components/editor/project-name-editor.tsx
blob: aa0141ae137e809b81d1f526da5e1ed19d1a812b (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"use client";

import { useState, useRef, useEffect } from "react";
import { Input } from "../ui/input";
import { useProjectStore } from "@/stores/project-store";
import { Edit2, Check, X } from "lucide-react";
import { Button } from "../ui/button";

interface ProjectNameEditorProps {
  className?: string;
}

export function ProjectNameEditor({ className }: ProjectNameEditorProps) {
  const { activeProject, updateProjectName } = useProjectStore();
  const [isEditing, setIsEditing] = useState(false);
  const [editValue, setEditValue] = useState("");
  const inputRef = useRef<HTMLInputElement>(null);

  useEffect(() => {
    if (activeProject) {
      setEditValue(activeProject.name);
    }
  }, [activeProject]);

  useEffect(() => {
    if (isEditing && inputRef.current) {
      inputRef.current.focus();
      inputRef.current.select();
    }
  }, [isEditing]);

  const handleStartEdit = () => {
    if (activeProject) {
      setEditValue(activeProject.name);
      setIsEditing(true);
    }
  };

  const handleSave = () => {
    if (editValue.trim()) {
      updateProjectName(editValue.trim());
      setIsEditing(false);
    }
  };

  const handleCancel = () => {
    if (activeProject) {
      setEditValue(activeProject.name);
    }
    setIsEditing(false);
  };

  const handleKeyDown = (e: React.KeyboardEvent) => {
    if (e.key === "Enter") {
      handleSave();
    } else if (e.key === "Escape") {
      handleCancel();
    }
  };

  if (!activeProject) {
    return <span className="text-sm text-muted-foreground">Loading...</span>;
  }

  if (isEditing) {
    return (
      <div className="flex items-center gap-1">
        <Input
          ref={inputRef}
          value={editValue}
          onChange={(e) => setEditValue(e.target.value)}
          onKeyDown={handleKeyDown}
          className="h-7 text-sm px-3 py-1 min-w-[200px]"
          size={1}
        />
        <Button
          size="sm"
          variant="text"
          onClick={handleSave}
          className="h-7 w-7 p-0"
          disabled={!editValue.trim()}
        >
          <Check className="h-3 w-3" />
        </Button>
        <Button
          size="sm"
          variant="text"
          onClick={handleCancel}
          className="h-7 w-7 p-0"
        >
          <X className="h-3 w-3" />
        </Button>
      </div>
    );
  }

  return (
    <div className="flex items-center gap-1 group">
      <span className="text-sm font-medium">{activeProject.name}</span>
      <Button
        size="sm"
        variant="text"
        onClick={handleStartEdit}
        className="h-6 w-6 p-0 opacity-0 group-hover:opacity-100 transition-opacity"
      >
        <Edit2 className="h-3 w-3" />
      </Button>
    </div>
  );
}