aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/components/dashboard/preview/EditableTitle.tsx
blob: 03b95e74c6d0fcafae80dd69e74cbce337e1005e (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
import { toast } from "@/components/ui/use-toast";

import { useUpdateBookmark } from "@hoarder/shared-react/hooks/bookmarks";
import { BookmarkTypes, ZBookmark } from "@hoarder/shared/types/bookmarks";

import { EditableText } from "../EditableText";

export function EditableTitle({ bookmark }: { bookmark: ZBookmark }) {
  const { mutate: updateBookmark, isPending } = useUpdateBookmark({
    onSuccess: () => {
      toast({
        description: "Title updated!",
      });
    },
  });

  let title: string | null = null;
  switch (bookmark.content.type) {
    case BookmarkTypes.LINK:
      title = bookmark.content.title ?? bookmark.content.url;
      break;
    case BookmarkTypes.TEXT:
      title = null;
      break;
    case BookmarkTypes.ASSET:
      title = bookmark.content.fileName ?? null;
      break;
  }

  title = bookmark.title ?? title;
  if (title == "") {
    title = null;
  }

  return (
    <EditableText
      originalText={title}
      editClassName="p-2 text-lg break-all"
      viewClassName="break-words line-clamp-2 text-lg text-ellipsis"
      untitledClassName="text-lg italic text-gray-600"
      onSave={(newTitle) => {
        updateBookmark(
          {
            bookmarkId: bookmark.id,
            title: newTitle,
          },
          {
            onError: () => {
              toast({
                description: "Something went wrong",
                variant: "destructive",
              });
            },
          },
        );
      }}
      isSaving={isPending}
    />
  );
}