|
1 | | -import { CloudFogIcon } from "lucide-react"; |
| 1 | +import { useDebounce } from "@/hooks/use-debounce"; |
| 2 | +import { useToast } from "@/hooks/use-toast"; |
| 3 | +import { useStatus } from "@liveblocks/react"; |
| 4 | +import { useMutation } from "convex/react"; |
| 5 | +import { CloudAlertIcon, CloudIcon, CloudUploadIcon } from "lucide-react"; |
| 6 | +import React, { useRef, useState } from "react"; |
| 7 | +import { api } from "../../../../../convex/_generated/api"; |
| 8 | +import { Id } from "../../../../../convex/_generated/dataModel"; |
| 9 | + |
| 10 | +type Props = { |
| 11 | + id: Id<"documents">; |
| 12 | + title: string; |
| 13 | +}; |
| 14 | + |
| 15 | +export const DocumentInput: React.FC<Props> = ({ id, title }) => { |
| 16 | + const [value, setValue] = useState<string>(title); |
| 17 | + const [isPending, setIsPending] = useState<boolean>(false); |
| 18 | + const [isEditing, setIsEditing] = useState<boolean>(false); |
| 19 | + const inputRef = useRef<HTMLInputElement>(null); |
| 20 | + const mutate = useMutation(api.document.update); |
| 21 | + const status = useStatus(); |
| 22 | + |
| 23 | + const { toast } = useToast(); |
| 24 | + |
| 25 | + const update = (value: string) => { |
| 26 | + setIsPending(true); |
| 27 | + mutate({ |
| 28 | + documentId: id, |
| 29 | + title: value, |
| 30 | + }) |
| 31 | + .then(() => { |
| 32 | + toast({ |
| 33 | + title: "Document updated", |
| 34 | + description: "Your document has been updated.", |
| 35 | + }); |
| 36 | + }) |
| 37 | + .catch(() => { |
| 38 | + toast({ |
| 39 | + title: "Document update failed", |
| 40 | + description: "Your document could not be updated", |
| 41 | + variant: "destructive", |
| 42 | + }); |
| 43 | + }) |
| 44 | + .finally(() => { |
| 45 | + setIsPending(false); |
| 46 | + }); |
| 47 | + }; |
| 48 | + |
| 49 | + const debounce = useDebounce((value: string) => { |
| 50 | + if (value === title) return; |
| 51 | + update(value); |
| 52 | + }, 1000); |
| 53 | + |
| 54 | + const submit = (e: React.FormEvent<HTMLFormElement>) => { |
| 55 | + e.preventDefault(); |
| 56 | + update(value); |
| 57 | + }; |
| 58 | + |
| 59 | + const onChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 60 | + setValue(e.target.value); |
| 61 | + debounce(e.target.value); |
| 62 | + }; |
| 63 | + |
| 64 | + const isCloudLoading = |
| 65 | + isPending || status === "connecting" || status === "reconnecting"; |
| 66 | + const isCloudError = status === "disconnected"; |
2 | 67 |
|
3 | | -export const DocumentInput: React.FC = () => { |
4 | 68 | return ( |
5 | 69 | <div className="flex items-center gap-2"> |
6 | | - <span className="text-lg px-1.5 cursor-pointer truncate"> |
7 | | - Untitled Document |
8 | | - </span> |
9 | | - <CloudFogIcon /> |
| 70 | + {isEditing ? ( |
| 71 | + <> |
| 72 | + <form className="relative w-fit max-w-[50ch]" onSubmit={submit}> |
| 73 | + <span className="invisible whitespace-pre px-1.5 text-lg"> |
| 74 | + {value || " "} |
| 75 | + </span> |
| 76 | + <input |
| 77 | + ref={inputRef} |
| 78 | + value={value} |
| 79 | + onChange={onChange} |
| 80 | + onBlur={() => setIsEditing(false)} |
| 81 | + className="absolute inset-0 text-lg text-black px-1.5 bg-transparent truncate " |
| 82 | + /> |
| 83 | + </form> |
| 84 | + </> |
| 85 | + ) : ( |
| 86 | + <> |
| 87 | + <span |
| 88 | + className="text-lg px-1.5 cursor-pointer truncate" |
| 89 | + onClick={() => { |
| 90 | + setIsEditing(true); |
| 91 | + setTimeout(() => { |
| 92 | + inputRef.current?.focus(); |
| 93 | + }, 0); |
| 94 | + }} |
| 95 | + > |
| 96 | + {title} |
| 97 | + </span> |
| 98 | + </> |
| 99 | + )} |
| 100 | + {!isCloudError && !isCloudLoading && <CloudIcon />} |
| 101 | + {isCloudLoading && <CloudUploadIcon />} |
| 102 | + {isCloudError && <CloudAlertIcon />} |
10 | 103 | </div> |
11 | 104 | ); |
12 | 105 | }; |
0 commit comments