Open History to see the last commit
1import { eq, useLiveQuery } from "@tanstack/react-db";
2import { CheckIcon, Edit2Icon, RotateCwIcon } from "lucide-react";
3import { useEffect, useState } from "react";
4import {
5 ProjectsNotFoundFromAPIError,
6 projectsCollection,
7} from "@/collections/projects";
8import { Button } from "@/components/ui/button";
9import {
10 Popover,
11 PopoverContent,
12 PopoverTrigger,
13} from "@/components/ui/popover";
14import { useAppForm } from "@/hooks/app.form";
15import { HighlightWrapper } from "@/utils/highlight-collection-related-info";
16import { Skeleton } from "./ui/skeleton";
17
18function EditProjectNamePopover({ name, id }: { name: string; id: string }) {
19 const [isOpen, setIsOpen] = useState(false);
20
21 const form = useAppForm({
22 defaultValues: {
23 name: name,
24 },
25 onSubmit: ({ value }) => {
26 projectsCollection.update(id, (project) => {
27 project.name = value.name;
28 });
29 setIsOpen(false);
30 },
31 });
32
33 return (
34 <HighlightWrapper highlightId="editProject">
35 <Popover open={isOpen} onOpenChange={setIsOpen}>
36 <PopoverTrigger asChild>
37 <Button variant="ghost" size="icon">
38 <Edit2Icon />
39 </Button>
40 </PopoverTrigger>
41 <PopoverContent className="w-80">
42 <form
43 onSubmit={(e) => {
44 e.preventDefault();
45 form.handleSubmit(e);
46 }}
47 className="flex items-center gap-2"
48 >
49 <form.AppField
50 name="name"
51 validators={{
52 onSubmit: ({ value }) => {
53 if (value.trim().length === 0) {
54 return "Project name cannot be empty";
55 }
56 },
57 }}
58 children={(field) => <field.TextField label="New name" />}
59 />
60 <Button className="self-end ml-auto" type="submit" size="icon">
61 <CheckIcon />
62 </Button>
63 </form>
64 </PopoverContent>
65 </Popover>
66 </HighlightWrapper>
67 );
68}
69
70function ProjectsNotFoundFromAPIErrorMessage() {
71 return (
72 <div className="flex flex-col gap-10">
73 <p className="text-destructive-foreground">Error: API returned a 404.</p>
74 <div className="flex gap-4 flex-col text-muted-foreground">
75 <p>
76 We use a service worker to simulate the backend APi. Sometimes it's
77 not working properly, typically after a hard refresh of the page.
78 </p>
79 <p>Please try refreshing the page by clicking the button below!</p>
80 </div>
81 <Button
82 className="grow-0 shrink w-fit mx-auto"
83 onClick={() => {
84 window.location.reload();
85 }}
86 >
87 <RotateCwIcon /> Refresh page
88 </Button>
89 </div>
90 );
91}
92
93export function EditableProjectDetails({ projectId }: { projectId: string }) {
94 const {
95 data: [project],
96 isLoading,
97 isReady,
98 } = useLiveQuery(
99 (q) =>
100 q
101 .from({ project: projectsCollection })
102 .where(({ project }) => eq(project.id, projectId)),
103 [projectId],
104 );
105
106 const [notFoundErrorMessageVisible, setNotFoundErrorMessageVisible] =
107 useState(false);
108
109 useEffect(() => {
110 if (!project && isReady) {
111 /*
112 NOTE:
113 Sometimes typically after a hard refresh), the service worker
114 is not able to serve the API requests, and they all result in
115 404 errors. We need to tell the user that they should try
116 a normal refresh in these cases.
117
118 By default, query collection sync errors will not throw,
119 so we'll have to manually check if there's an error, when
120 the `isReady` flag is true.
121
122 https://tanstack.com/db/latest/docs/guides/error-handling#query-collection-sync-errors
123 */
124 const lastError = projectsCollection.utils.lastError;
125 if (lastError instanceof ProjectsNotFoundFromAPIError) {
126 setNotFoundErrorMessageVisible(true);
127 }
128 }
129 }, [project, isReady]);
130
131 return notFoundErrorMessageVisible ? (
132 /*
133 The API returned a 404 error when trying to sync the
134 projects collection.
135 */
136 <ProjectsNotFoundFromAPIErrorMessage />
137 ) : isReady && !project ? (
138 // The collection is ready, but no project was found
139 // with this particular live query
140 <div className="text-muted-foreground">
141 <p>No project was found with this id.</p>
142 <p>Please select a project from the sidebar!</p>
143 </div>
144 ) : (
145 <HighlightWrapper highlightId="project_projectPage">
146 <div className="flex flex-col mb-6 gap-1">
147 <div className="flex items-center gap-2">
148 {isLoading ? (
149 <div className="flex items-center h-9">
150 <Skeleton className="h-8 w-40" />
151 </div>
152 ) : (
153 <>
154 <h1 className="scroll-m-20 text-2xl font-bold">{project.name}</h1>
155 <EditProjectNamePopover name={project.name} id={project.id} />
156 </>
157 )}
158 </div>
159 {isLoading ? (
160 <Skeleton className="h-6 block w-80" />
161 ) : (
162 <p className="text-muted-foreground">{project.description}</p>
163 )}
164 </div>
165 </HighlightWrapper>
166 );
167}