aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/EditableText.tsx
blob: e5027b9300341c3de1669b39c193339f64c0137b (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { useEffect, useRef, useState } from "react";
import { ActionButtonWithTooltip } from "@/components/ui/action-button";
import { ButtonWithTooltip } from "@/components/ui/button";
import {
  Tooltip,
  TooltipContent,
  TooltipPortal,
  TooltipTrigger,
} from "@/components/ui/tooltip";
import { useTranslation } from "@/lib/i18n/client";
import { Check, Pencil, X } from "lucide-react";

interface Props {
  viewClassName?: string;
  untitledClassName?: string;
  editClassName?: string;
  onSave: (title: string | null) => void;
  isSaving: boolean;
  originalText: string | null;
  setEditable: (editable: boolean) => void;
}

function EditMode({
  onSave: onSaveCB,
  editClassName: className,
  isSaving,
  originalText,
  setEditable,
}: Props) {
  const { t } = useTranslation();
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    if (ref.current) {
      ref.current.focus();
      ref.current.textContent = originalText;
    }
  }, [ref]);

  const onSave = () => {
    let toSave: string | null = ref.current?.textContent ?? null;
    if (originalText == toSave) {
      // Nothing to do here
      return;
    }
    if (toSave == "") {
      toSave = null;
    }
    onSaveCB(toSave);
    setEditable(false);
  };

  return (
    <div className="flex gap-3">
      <div
        ref={ref}
        role="presentation"
        className={className}
        contentEditable={true}
        onKeyDown={(e) => {
          if (e.key === "Enter") {
            e.preventDefault();
            onSave();
          }
        }}
      />
      <ActionButtonWithTooltip
        tooltip={t("actions.save")}
        delayDuration={500}
        size="none"
        variant="ghost"
        className="align-middle text-gray-400"
        loading={isSaving}
        onClick={() => onSave()}
      >
        <Check className="size-4" />
      </ActionButtonWithTooltip>
      <ButtonWithTooltip
        tooltip={t("actions.cancel")}
        delayDuration={500}
        size="none"
        variant="ghost"
        className="align-middle text-gray-400"
        onClick={() => {
          setEditable(false);
        }}
      >
        <X className="size-4" />
      </ButtonWithTooltip>
    </div>
  );
}

function ViewMode({
  originalText,
  setEditable,
  viewClassName,
  untitledClassName,
}: Props) {
  const { t } = useTranslation();
  return (
    <Tooltip delayDuration={500}>
      <div className="flex max-w-full items-center gap-3">
        <TooltipTrigger asChild>
          {originalText ? (
            <p className={viewClassName}>{originalText}</p>
          ) : (
            <p className={untitledClassName}>Untitled</p>
          )}
        </TooltipTrigger>
        <ButtonWithTooltip
          delayDuration={500}
          tooltip={t("actions.edit_title")}
          size="none"
          variant="ghost"
          className="align-middle text-gray-400"
          onClick={() => {
            setEditable(true);
          }}
        >
          <Pencil className="size-4" />
        </ButtonWithTooltip>
      </div>
      <TooltipPortal>
        {originalText && (
          <TooltipContent side="bottom" className="max-w-[40ch] break-words">
            {originalText}
          </TooltipContent>
        )}
      </TooltipPortal>
    </Tooltip>
  );
}

export function EditableText(props: {
  viewClassName?: string;
  untitledClassName?: string;
  editClassName?: string;
  originalText: string | null;
  onSave: (title: string | null) => void;
  isSaving: boolean;
}) {
  const [editable, setEditable] = useState(false);

  return editable ? (
    <EditMode setEditable={setEditable} {...props} />
  ) : (
    <ViewMode setEditable={setEditable} {...props} />
  );
}