From f6ca0b0c4fb3c1e6d93a00fcce7cb0aee12930c0 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sat, 16 Mar 2024 00:34:04 +0000 Subject: ui(web): Add an editor card inline in the bookmark grid and remove the top nav buttons --- .../components/dashboard/bookmarks/EditorCard.tsx | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 apps/web/components/dashboard/bookmarks/EditorCard.tsx (limited to 'apps/web/components/dashboard/bookmarks/EditorCard.tsx') diff --git a/apps/web/components/dashboard/bookmarks/EditorCard.tsx b/apps/web/components/dashboard/bookmarks/EditorCard.tsx new file mode 100644 index 00000000..28e8f41f --- /dev/null +++ b/apps/web/components/dashboard/bookmarks/EditorCard.tsx @@ -0,0 +1,91 @@ +import type { SubmitErrorHandler, SubmitHandler } from "react-hook-form"; +import { ActionButton } from "@/components/ui/action-button"; +import { Form, FormControl, FormField, FormItem } from "@/components/ui/form"; +import { Textarea } from "@/components/ui/textarea"; +import { toast } from "@/components/ui/use-toast"; +import { api } from "@/lib/trpc"; +import { cn } from "@/lib/utils"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; +import { z } from "zod"; + +export default function EditorCard({ className }: { className?: string }) { + const formSchema = z.object({ + text: z.string(), + }); + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues: { + text: "", + }, + }); + + const invalidateBookmarksCache = api.useUtils().bookmarks.invalidate; + const { mutate, isPending } = api.bookmarks.createBookmark.useMutation({ + onSuccess: () => { + invalidateBookmarksCache(); + form.reset(); + }, + onError: () => { + toast({ description: "Something went wrong", variant: "destructive" }); + }, + }); + + const onSubmit: SubmitHandler> = (data) => { + const text = data.text.trim(); + try { + new URL(text); + mutate({ type: "link", url: text }); + } catch (e) { + // Not a URL + mutate({ type: "text", text }); + } + }; + const onError: SubmitErrorHandler> = (errors) => { + toast({ + description: Object.values(errors) + .map((v) => v.message) + .join("\n"), + variant: "destructive", + }); + }; + + return ( +
+ + { + return ( + + +