|
1 | | -import React from "react"; |
| 1 | +"use client"; |
2 | 2 |
|
3 | | -const ReadmeForm = () => { |
4 | | - return <div>ReadmeForm</div>; |
| 3 | +import { User } from "@/backend/models/domain-models"; |
| 4 | +import { UserActionInput } from "@/backend/services/inputs/user.input"; |
| 5 | +import * as userActions from "@/backend/services/user.action"; |
| 6 | +import EditorCommandButton from "@/components/Editor/EditorCommandButton"; |
| 7 | +import { useMarkdownEditor } from "@/components/Editor/useMarkdownEditor"; |
| 8 | +import { Button } from "@/components/ui/button"; |
| 9 | +import { useAutosizeTextArea } from "@/hooks/use-auto-resize-textarea"; |
| 10 | +import { useTranslation } from "@/i18n/use-translation"; |
| 11 | +import { markdocParser } from "@/utils/markdoc-parser"; |
| 12 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 13 | +import { |
| 14 | + FontBoldIcon, |
| 15 | + FontItalicIcon, |
| 16 | + HeadingIcon, |
| 17 | + ImageIcon, |
| 18 | +} from "@radix-ui/react-icons"; |
| 19 | +import { useMutation } from "@tanstack/react-query"; |
| 20 | +import { Loader } from "lucide-react"; |
| 21 | +import React, { useCallback, useState } from "react"; |
| 22 | +import { useForm } from "react-hook-form"; |
| 23 | +import { toast } from "sonner"; |
| 24 | +import z from "zod"; |
| 25 | + |
| 26 | +interface Props { |
| 27 | + user: User; |
| 28 | +} |
| 29 | + |
| 30 | +const ReadmeForm: React.FC<Props> = ({ user }) => { |
| 31 | + const { _t } = useTranslation(); |
| 32 | + const [editorMode, setEditorMode] = useState<"write" | "preview">("write"); |
| 33 | + const editorRef = React.useRef<HTMLTextAreaElement>(null); |
| 34 | + |
| 35 | + const mutation = useMutation({ |
| 36 | + mutationFn: ( |
| 37 | + payload: z.infer<typeof UserActionInput.updateMyProfileInput> |
| 38 | + ) => userActions.updateMyProfile(payload), |
| 39 | + onSuccess: () => { |
| 40 | + toast.success(_t("Profile readme updated successfully")); |
| 41 | + }, |
| 42 | + onError: (error) => { |
| 43 | + toast.error(_t("Failed to update profile readme")); |
| 44 | + console.error("Error updating profile readme:", error); |
| 45 | + }, |
| 46 | + }); |
| 47 | + |
| 48 | + const form = useForm({ |
| 49 | + mode: "all", |
| 50 | + defaultValues: { |
| 51 | + profile_readme: user?.profile_readme || "", |
| 52 | + }, |
| 53 | + resolver: zodResolver(UserActionInput.updateMyProfileInput), |
| 54 | + }); |
| 55 | + |
| 56 | + useAutosizeTextArea(editorRef, form.watch("profile_readme") ?? ""); |
| 57 | + |
| 58 | + const editor = useMarkdownEditor({ |
| 59 | + ref: editorRef, |
| 60 | + onChange: (value) => form.setValue("profile_readme", value), |
| 61 | + }); |
| 62 | + |
| 63 | + const renderEditorToolbar = useCallback( |
| 64 | + () => ( |
| 65 | + <div className="flex w-full gap-6 p-2 my-2 bg-muted"> |
| 66 | + <EditorCommandButton |
| 67 | + onClick={() => editor?.executeCommand("heading")} |
| 68 | + Icon={<HeadingIcon />} |
| 69 | + /> |
| 70 | + <EditorCommandButton |
| 71 | + onClick={() => editor?.executeCommand("bold")} |
| 72 | + Icon={<FontBoldIcon />} |
| 73 | + /> |
| 74 | + <EditorCommandButton |
| 75 | + onClick={() => editor?.executeCommand("italic")} |
| 76 | + Icon={<FontItalicIcon />} |
| 77 | + /> |
| 78 | + {/* <EditorCommandButton |
| 79 | + onClick={() => editor?.executeCommand("image")} |
| 80 | + Icon={<ImageIcon />} |
| 81 | + /> */} |
| 82 | + </div> |
| 83 | + ), |
| 84 | + [editor] |
| 85 | + ); |
| 86 | + |
| 87 | + return ( |
| 88 | + <form onSubmit={form.handleSubmit((data) => mutation.mutate(data))}> |
| 89 | + <div className="flex items-center justify-between"> |
| 90 | + {renderEditorToolbar()} |
| 91 | + <div className="flex items-center gap-4"> |
| 92 | + {editorMode === "write" && ( |
| 93 | + <Button |
| 94 | + variant={"link"} |
| 95 | + className="!px-2" |
| 96 | + type="button" |
| 97 | + onClick={() => setEditorMode("preview")} |
| 98 | + > |
| 99 | + {_t("Preview Mode")} |
| 100 | + </Button> |
| 101 | + )} |
| 102 | + {editorMode === "preview" && ( |
| 103 | + <Button |
| 104 | + variant={"link"} |
| 105 | + className="!px-2" |
| 106 | + type="button" |
| 107 | + onClick={() => setEditorMode("write")} |
| 108 | + > |
| 109 | + {_t("Write Mode")} |
| 110 | + </Button> |
| 111 | + )} |
| 112 | + </div> |
| 113 | + </div> |
| 114 | + <div className="w-full"> |
| 115 | + {editorMode === "write" ? ( |
| 116 | + <textarea |
| 117 | + disabled={mutation.isPending} |
| 118 | + tabIndex={2} |
| 119 | + className="focus:outline-none p-2 border bg-background w-full resize-none" |
| 120 | + placeholder={_t("Write something stunning...")} |
| 121 | + ref={editorRef} |
| 122 | + value={form.watch("profile_readme")} |
| 123 | + onChange={(e) => form.setValue("profile_readme", e.target.value)} |
| 124 | + /> |
| 125 | + ) : ( |
| 126 | + <div className="content-typography"> |
| 127 | + {markdocParser(form.watch("profile_readme") ?? "")} |
| 128 | + </div> |
| 129 | + )} |
| 130 | + </div> |
| 131 | + |
| 132 | + <Button type="submit" className="mt-2" disabled={mutation.isPending}> |
| 133 | + {mutation.isPending && <Loader className="animate-spin" />} |
| 134 | + {_t("Save")} |
| 135 | + </Button> |
| 136 | + </form> |
| 137 | + ); |
5 | 138 | }; |
6 | 139 |
|
7 | 140 | export default ReadmeForm; |
0 commit comments