aboutsummaryrefslogtreecommitdiffstats
path: root/packages/web/app/dashboard/bookmarks/components/AddBookmark.tsx
blob: d12fc663e161853771d68382930ad4a0c536ac21 (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
"use client";

import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
import { Input } from "@/components/ui/input";
import { Pencil, Plus } from "lucide-react";
import { useForm, SubmitErrorHandler } from "react-hook-form";
import { z } from "zod";
import { zodResolver } from "@hookform/resolvers/zod";
import { toast } from "@/components/ui/use-toast";
import { api } from "@/lib/trpc";
import { ActionButton } from "@/components/ui/action-button";
import { Button } from "@/components/ui/button";
import { BookmarkedTextEditor } from "./BookmarkedTextEditor";
import { useState } from "react";

function AddText() {
  const [isEditorOpen, setEditorOpen] = useState(false);

  return (
    <div className="flex">
      <BookmarkedTextEditor open={isEditorOpen} setOpen={setEditorOpen} />
      <Button className="m-auto" onClick={() => setEditorOpen(true)}>
        <Pencil />
      </Button>
    </div>
  );
}

function AddLink() {
  const formSchema = z.object({
    url: z.string().url({ message: "The link must be a valid URL" }),
  });
  const form = useForm<z.infer<typeof formSchema>>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      url: "",
    },
  });

  const invalidateBookmarksCache = api.useUtils().bookmarks.invalidate;
  const createBookmarkMutator = api.bookmarks.createBookmark.useMutation({
    onSuccess: () => {
      invalidateBookmarksCache();
      form.reset();
    },
    onError: () => {
      toast({ description: "Something went wrong", variant: "destructive" });
    },
  });

  const onError: SubmitErrorHandler<z.infer<typeof formSchema>> = (errors) => {
    toast({
      description: Object.values(errors)
        .map((v) => v.message)
        .join("\n"),
      variant: "destructive",
    });
  };

  return (
    <Form {...form}>
      <form
        className="flex-grow"
        onSubmit={form.handleSubmit(
          (value) =>
            createBookmarkMutator.mutate({ url: value.url, type: "link" }),
          onError,
        )}
      >
        <div className="flex w-full items-center space-x-2 py-4">
          <FormField
            control={form.control}
            name="url"
            render={({ field }) => {
              return (
                <FormItem className="flex-1">
                  <FormControl>
                    <Input type="text" placeholder="Link" {...field} />
                  </FormControl>
                </FormItem>
              );
            }}
          />
          <ActionButton type="submit" loading={createBookmarkMutator.isPending}>
            <Plus />
          </ActionButton>
        </div>
      </form>
    </Form>
  );
}

export default function AddBookmark() {
  return (
    <div className="container flex gap-2">
      <AddLink />
      <AddText />
    </div>
  );
}