aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/(canvas)/canvas/thinkPad.tsx
blob: fff30f26df3b57863bb7676fcfbb1ecd0c66d52e (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import { getCanvasData } from "@/app/actions/fetchers";
import { AnimatePresence, motion } from "framer-motion";
import Link from "next/link";
import {
  EllipsisHorizontalCircleIcon,
  TrashIcon,
  PencilSquareIcon,
} from "@heroicons/react/24/outline";
import { toast } from "sonner";
import { Label } from "@repo/ui/shadcn/label";

const childVariants = {
  hidden: { opacity: 0, y: 10, filter: "blur(2px)" },
  visible: { opacity: 1, y: 0, filter: "blur(0px)" },
};

export default function ThinkPad({
  title,
  description,
  image,
  id,
}: {
  title: string;
  description: string;
  image: string;
  id: string;
}) {
  const [deleted, setDeleted] = useState(false);
  const [info, setInfo] = useState({ title, description });
  return (
    <AnimatePresence mode="sync">
      {!deleted && (
        <motion.div
          layout
          exit={{ opacity: 0, scaleY: 0 }}
          variants={childVariants}
          className="flex h-48 origin-top relative gap-4 rounded-2xl bg-[#1F2428] p-2"
        >
          <Link
            className="h-full select-none min-w-[40%] bg-[#363f46] rounded-xl overflow-hidden"
            href={`/canvas/${id}`}
          >
            <Suspense
              fallback={
                <div className=" h-full w-full flex  justify-center items-center">
                  Loading...
                </div>
              }
            >
              <ImageComponent id={id} />
            </Suspense>
          </Link>
          <div className="flex flex-col gap-2">
            <motion.h2
              initial={{ opacity: 0, filter: "blur(3px)" }}
              animate={{ opacity: 1, filter: "blur(0px)" }}
              key={info.title}
            >
              {info.title}
            </motion.h2>
            <motion.h3
              key={info.description}
              initial={{ opacity: 0, filter: "blur(3px)" }}
              animate={{ opacity: 1, filter: "blur(0px)" }}
              className="overflow-hidden text-ellipsis text-[#B8C4C6]"
            >
              {info.description}
            </motion.h3>
          </div>
          <Menu
            info={info}
            id={id}
            setDeleted={() => setDeleted(true)}
            setInfo={(e) => setInfo(e)}
          />
        </motion.div>
      )}
    </AnimatePresence>
  );
}

import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@repo/ui/shadcn/popover";

function Menu({
  info,
  id,
  setDeleted,
  setInfo,
}: {
  info: { title: string; description: string };
  id: string;
  setDeleted: () => void;
  setInfo: ({
    title,
    description,
  }: {
    title: string;
    description: string;
  }) => void;
}) {
  return (
    <Popover>
      <PopoverTrigger className="absolute z-20 top-0 right-0" asChild>
        <Button variant="secondary">
          <EllipsisHorizontalCircleIcon className="size-5 stroke-2 stroke-[#B8C4C6]" />
        </Button>
      </PopoverTrigger>
      <PopoverContent
        align="start"
        className="w-32 px-2 py-2 bg-[#161f2a]/30 text-[#B8C4C6] border-border flex flex-col gap-3"
      >
        <EditToolbar info={info} id={id} setInfo={setInfo} />
        <Button
          onClick={async () => {
            const res = await deleteCanvas(id);
            if (res.success) {
              toast.success("Thinkpad removed.", {
                style: { backgroundColor: "rgb(22 31 42 / 0.3)" },
              });
              setDeleted();
            } else {
              toast.warning("Something went wrong.", {
                style: { backgroundColor: "rgb(22 31 42 / 0.3)" },
              });
            }
          }}
          className="flex gap-2 border-border"
          variant="outline"
        >
          <TrashIcon className="size-8 stroke-1" /> Delete
        </Button>
      </PopoverContent>
    </Popover>
  );
}

function EditToolbar({
  id,
  setInfo,
  info,
}: {
  id: string;
  setInfo: ({
    title,
    description,
  }: {
    title: string;
    description: string;
  }) => void;
  info: {
    title: string;
    description: string;
  };
}) {
  const [open, setOpen] = useState(false);
  return (
    <Dialog open={open} onOpenChange={setOpen}>
      <DialogTrigger asChild>
        <Button className="flex gap-2 border-border" variant="outline">
          <PencilSquareIcon className="size-8 stroke-1" /> Edit
        </Button>
      </DialogTrigger>
      <DialogContent className="sm:max-w-[425px] bg-[#161f2a]/30 border-0">
        <form
          action={async (FormData) => {
            const data = {
              title: FormData.get("title") as string,
              description: FormData.get("description") as string,
            };
            const res = await AddCanvasInfo({ id, ...data });
            if (res.success) {
              setOpen(false);
              setInfo(data);
            } else {
              setOpen(false);
              toast.error("Something went wrong.", {
                style: { backgroundColor: "rgb(22 31 42 / 0.3)" },
              });
            }
          }}
        >
          <DialogHeader>
            <DialogTitle>Edit Canvas</DialogTitle>
            <DialogDescription>
              Add Description to your canvas. Pro tip: Let AI do the job, as you
              add your content into canvas, we will autogenerate your
              description.
            </DialogDescription>
          </DialogHeader>
          <div className="grid gap-4 py-4">
            <div className="grid grid-cols-4 items-center gap-4">
              <Label htmlFor="title" className="text-right">
                Title
              </Label>
              <Input
                defaultValue={info.title}
                name="title"
                id="title"
                placeholder="life planning..."
                className="col-span-3 border-0"
              />
            </div>
            <div className="grid grid-cols-4 items-center gap-4">
              <Label htmlFor="description" className="text-right">
                Description
              </Label>
              <Textarea
                defaultValue={info.description}
                rows={6}
                id="description"
                name="description"
                placeholder="contains information about..."
                className="col-span-3 border-0 resize-none"
              />
            </div>
          </div>
          <DialogFooter>
            <Button type="submit">Save changes</Button>
          </DialogFooter>
        </form>
      </DialogContent>
    </Dialog>
  );
}

import { Suspense, memo, use, useState } from "react";
import { Box, TldrawImage } from "tldraw";
import { Button } from "@repo/ui/shadcn/button";
import { AddCanvasInfo, deleteCanvas } from "@/app/actions/doers";
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from "@repo/ui/shadcn/dialog";
import { Input } from "@repo/ui/shadcn/input";
import { Textarea } from "@repo/ui/shadcn/textarea";
import { textCardUtil } from "@/components/canvas/textCard";
import { twitterCardUtil } from "@/components/canvas/twitterCard";

const ImageComponent = memo(({ id }: { id: string }) => {
  const snapshot = use(getCanvasData(id));
  if (snapshot.bounds) {
    const pageBounds = new Box(
      snapshot.bounds.x,
      snapshot.bounds.y,
      snapshot.bounds.w,
      snapshot.bounds.h,
    );

    return (
      <TldrawImage
        shapeUtils={[twitterCardUtil, textCardUtil]}
        snapshot={snapshot.snapshot}
        background={false}
        darkMode={true}
        bounds={pageBounds}
        padding={0}
        scale={1}
        format="png"
      />
    );
  }
  return (
    <div className=" h-full w-full flex justify-center items-center">
      Drew things to seee here
    </div>
  );
});