1import { Client } from "@atcute/client";
2import { Did } from "@atcute/lexicons";
3import { getSession, OAuthUserAgent } from "@atcute/oauth-browser-client";
4import { remove } from "@mary/exif-rm";
5import { useNavigate, useParams } from "@solidjs/router";
6import { createEffect, createSignal, For, onCleanup, Show } from "solid-js";
7import { Editor, editorView } from "../components/editor.jsx";
8import { agent } from "../components/login.jsx";
9import { sessions } from "./account.jsx";
10import { Button } from "./button.jsx";
11import { Modal } from "./modal.jsx";
12import { addNotification, removeNotification } from "./notification.jsx";
13import { TextInput } from "./text-input.jsx";
14import Tooltip from "./tooltip.jsx";
15
16export const [placeholder, setPlaceholder] = createSignal<any>();
17
18export const RecordEditor = (props: { create: boolean; record?: any; refetch?: any }) => {
19 const navigate = useNavigate();
20 const params = useParams();
21 const [openDialog, setOpenDialog] = createSignal(false);
22 const [notice, setNotice] = createSignal("");
23 const [openUpload, setOpenUpload] = createSignal(false);
24 const [openInsertMenu, setOpenInsertMenu] = createSignal(false);
25 const [validate, setValidate] = createSignal<boolean | undefined>(undefined);
26 const [isMaximized, setIsMaximized] = createSignal(false);
27 const [isMinimized, setIsMinimized] = createSignal(false);
28 let blobInput!: HTMLInputElement;
29 let formRef!: HTMLFormElement;
30 let insertMenuRef!: HTMLDivElement;
31
32 createEffect(() => {
33 if (openInsertMenu()) {
34 const handleClickOutside = (e: MouseEvent) => {
35 if (insertMenuRef && !insertMenuRef.contains(e.target as Node)) {
36 setOpenInsertMenu(false);
37 }
38 };
39 document.addEventListener("mousedown", handleClickOutside);
40 onCleanup(() => document.removeEventListener("mousedown", handleClickOutside));
41 }
42 });
43
44 const defaultPlaceholder = () => {
45 return {
46 $type: "app.bsky.feed.post",
47 text: "This post was sent from PDSls",
48 embed: {
49 $type: "app.bsky.embed.external",
50 external: {
51 uri: "https://pdsls.dev",
52 title: "PDSls",
53 description: "Browse the public data on atproto",
54 },
55 },
56 langs: ["en"],
57 createdAt: new Date().toISOString(),
58 };
59 };
60
61 const getValidateIcon = () => {
62 return (
63 validate() === true ? "lucide--circle-check"
64 : validate() === false ? "lucide--circle-x"
65 : "lucide--circle"
66 );
67 };
68
69 const getValidateLabel = () => {
70 return (
71 validate() === true ? "True"
72 : validate() === false ? "False"
73 : "Unset"
74 );
75 };
76
77 createEffect(() => {
78 if (openDialog()) {
79 setValidate(undefined);
80 }
81 });
82
83 const createRecord = async (formData: FormData) => {
84 const repo = formData.get("repo")?.toString();
85 if (!repo) return;
86 const rpc = new Client({ handler: new OAuthUserAgent(await getSession(repo as Did)) });
87 const collection = formData.get("collection");
88 const rkey = formData.get("rkey");
89 let record: any;
90 try {
91 record = JSON.parse(editorView.state.doc.toString());
92 } catch (e: any) {
93 setNotice(e.message);
94 return;
95 }
96 const res = await rpc.post("com.atproto.repo.createRecord", {
97 input: {
98 repo: repo as Did,
99 collection: collection ? collection.toString() : record.$type,
100 rkey: rkey?.toString().length ? rkey?.toString() : undefined,
101 record: record,
102 validate: validate(),
103 },
104 });
105 if (!res.ok) {
106 setNotice(`${res.data.error}: ${res.data.message}`);
107 return;
108 }
109 setOpenDialog(false);
110 const id = addNotification({
111 message: "Record created",
112 type: "success",
113 });
114 setTimeout(() => removeNotification(id), 3000);
115 navigate(`/${res.data.uri}`);
116 };
117
118 const editRecord = async (recreate?: boolean) => {
119 const record = editorView.state.doc.toString();
120 if (!record) return;
121 const rpc = new Client({ handler: agent()! });
122 try {
123 const editedRecord = JSON.parse(record);
124 if (recreate) {
125 const res = await rpc.post("com.atproto.repo.applyWrites", {
126 input: {
127 repo: agent()!.sub,
128 validate: validate(),
129 writes: [
130 {
131 collection: params.collection as `${string}.${string}.${string}`,
132 rkey: params.rkey!,
133 $type: "com.atproto.repo.applyWrites#delete",
134 },
135 {
136 collection: params.collection as `${string}.${string}.${string}`,
137 rkey: params.rkey,
138 $type: "com.atproto.repo.applyWrites#create",
139 value: editedRecord,
140 },
141 ],
142 },
143 });
144 if (!res.ok) {
145 setNotice(`${res.data.error}: ${res.data.message}`);
146 return;
147 }
148 } else {
149 const res = await rpc.post("com.atproto.repo.putRecord", {
150 input: {
151 repo: agent()!.sub,
152 collection: params.collection as `${string}.${string}.${string}`,
153 rkey: params.rkey!,
154 record: editedRecord,
155 validate: validate(),
156 },
157 });
158 if (!res.ok) {
159 setNotice(`${res.data.error}: ${res.data.message}`);
160 return;
161 }
162 }
163 setOpenDialog(false);
164 const id = addNotification({
165 message: "Record edited",
166 type: "success",
167 });
168 setTimeout(() => removeNotification(id), 3000);
169 props.refetch();
170 } catch (err: any) {
171 setNotice(err.message);
172 }
173 };
174
175 const insertTimestamp = () => {
176 const timestamp = new Date().toISOString();
177 editorView.dispatch({
178 changes: {
179 from: editorView.state.selection.main.head,
180 insert: `"${timestamp}"`,
181 },
182 });
183 setOpenInsertMenu(false);
184 };
185
186 const MenuItem = (props: { icon: string; label: string; onClick: () => void }) => {
187 return (
188 <button
189 type="button"
190 class="flex items-center gap-2 rounded-lg p-2 text-left text-xs hover:bg-neutral-100 active:bg-neutral-200 dark:hover:bg-neutral-700 dark:active:bg-neutral-600"
191 onClick={props.onClick}
192 >
193 <span class={`iconify ${props.icon}`}></span>
194 <span>{props.label}</span>
195 </button>
196 );
197 };
198
199 const FileUpload = (props: { file: File }) => {
200 const [uploading, setUploading] = createSignal(false);
201 const [error, setError] = createSignal("");
202
203 onCleanup(() => (blobInput.value = ""));
204
205 const formatFileSize = (bytes: number) => {
206 if (bytes === 0) return "0 Bytes";
207 const k = 1024;
208 const sizes = ["Bytes", "KB", "MB", "GB"];
209 const i = Math.floor(Math.log(bytes) / Math.log(k));
210 return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + " " + sizes[i];
211 };
212
213 const uploadBlob = async () => {
214 let blob: Blob;
215
216 const mimetype = (document.getElementById("mimetype") as HTMLInputElement)?.value;
217 (document.getElementById("mimetype") as HTMLInputElement).value = "";
218 if (mimetype) blob = new Blob([props.file], { type: mimetype });
219 else blob = props.file;
220
221 if ((document.getElementById("exif-rm") as HTMLInputElement).checked) {
222 const exifRemoved = remove(new Uint8Array(await blob.arrayBuffer()));
223 if (exifRemoved !== null) blob = new Blob([exifRemoved], { type: blob.type });
224 }
225
226 const rpc = new Client({ handler: agent()! });
227 setUploading(true);
228 const res = await rpc.post("com.atproto.repo.uploadBlob", {
229 input: blob,
230 });
231 setUploading(false);
232 if (!res.ok) {
233 setError(res.data.error);
234 return;
235 }
236 editorView.dispatch({
237 changes: {
238 from: editorView.state.selection.main.head,
239 insert: JSON.stringify(res.data.blob, null, 2),
240 },
241 });
242 setOpenUpload(false);
243 };
244
245 return (
246 <div class="dark:bg-dark-300 dark:shadow-dark-700 absolute top-70 left-[50%] w-[20rem] -translate-x-1/2 rounded-lg border-[0.5px] border-neutral-300 bg-neutral-50 p-4 shadow-md transition-opacity duration-200 dark:border-neutral-700 starting:opacity-0">
247 <h2 class="mb-2 font-semibold">Upload blob</h2>
248 <div class="flex flex-col gap-2 text-sm">
249 <div class="flex flex-col gap-1">
250 <p class="flex gap-1">
251 <span class="truncate">{props.file.name}</span>
252 <span class="shrink-0 text-neutral-600 dark:text-neutral-400">
253 ({formatFileSize(props.file.size)})
254 </span>
255 </p>
256 </div>
257 <div class="flex items-center gap-x-2">
258 <label for="mimetype" class="shrink-0 select-none">
259 MIME type
260 </label>
261 <TextInput id="mimetype" placeholder={props.file.type} />
262 </div>
263 <div class="flex items-center gap-1">
264 <input id="exif-rm" type="checkbox" checked />
265 <label for="exif-rm" class="select-none">
266 Remove EXIF data
267 </label>
268 </div>
269 <p class="text-xs text-neutral-600 dark:text-neutral-400">
270 Metadata will be pasted after the cursor
271 </p>
272 <Show when={error()}>
273 <span class="text-red-500 dark:text-red-400">Error: {error()}</span>
274 </Show>
275 <div class="flex justify-between gap-2">
276 <Button onClick={() => setOpenUpload(false)}>Cancel</Button>
277 <Show when={uploading()}>
278 <div class="flex items-center gap-1">
279 <span class="iconify lucide--loader-circle animate-spin"></span>
280 <span>Uploading</span>
281 </div>
282 </Show>
283 <Show when={!uploading()}>
284 <Button
285 onClick={uploadBlob}
286 class="dark:shadow-dark-700 flex items-center gap-1 rounded-lg bg-blue-500 px-2 py-1.5 text-xs text-white shadow-xs select-none hover:bg-blue-600 active:bg-blue-700 dark:bg-blue-600 dark:hover:bg-blue-500 dark:active:bg-blue-400"
287 >
288 Upload
289 </Button>
290 </Show>
291 </div>
292 </div>
293 </div>
294 );
295 };
296
297 return (
298 <>
299 <Modal
300 open={openDialog()}
301 onClose={() => setOpenDialog(false)}
302 closeOnClick={false}
303 nonBlocking={isMinimized()}
304 >
305 <div
306 style="transform: translateX(-50%) translateZ(0);"
307 classList={{
308 "dark:bg-dark-300 dark:shadow-dark-700 pointer-events-auto absolute top-18 left-1/2 flex flex-col rounded-lg border-[0.5px] border-neutral-300 bg-neutral-50 p-4 shadow-md transition-all duration-200 dark:border-neutral-700 starting:opacity-0": true,
309 "w-[calc(100%-1rem)] max-w-3xl h-[65vh]": !isMaximized(),
310 "w-[calc(100%-1rem)] max-w-7xl h-[85vh]": isMaximized(),
311 hidden: isMinimized(),
312 }}
313 >
314 <div class="mb-2 flex w-full justify-between text-base">
315 <div class="flex items-center gap-2">
316 <span class="font-semibold select-none">
317 {props.create ? "Creating" : "Editing"} record
318 </span>
319 </div>
320 <div class="flex items-center gap-1">
321 <button
322 type="button"
323 onclick={() => setIsMinimized(true)}
324 class="flex items-center rounded-lg p-1.5 hover:bg-neutral-200 active:bg-neutral-300 dark:hover:bg-neutral-700 dark:active:bg-neutral-600"
325 >
326 <span class="iconify lucide--minus"></span>
327 </button>
328 <button
329 type="button"
330 onclick={() => setIsMaximized(!isMaximized())}
331 class="flex items-center rounded-lg p-1.5 hover:bg-neutral-200 active:bg-neutral-300 dark:hover:bg-neutral-700 dark:active:bg-neutral-600"
332 >
333 <span
334 class={`iconify ${isMaximized() ? "lucide--minimize-2" : "lucide--maximize-2"}`}
335 ></span>
336 </button>
337 <button
338 id="close"
339 onclick={() => setOpenDialog(false)}
340 class="flex items-center rounded-lg p-1.5 hover:bg-neutral-200 active:bg-neutral-300 dark:hover:bg-neutral-700 dark:active:bg-neutral-600"
341 >
342 <span class="iconify lucide--x"></span>
343 </button>
344 </div>
345 </div>
346 <form ref={formRef} class="flex min-h-0 flex-1 flex-col gap-y-2">
347 <Show when={props.create}>
348 <div class="flex flex-wrap items-center gap-1 text-sm">
349 <span>at://</span>
350 <select
351 class="dark:bg-dark-100 max-w-40 truncate rounded-lg border-[0.5px] border-neutral-300 bg-white px-1 py-1 select-none focus:outline-[1px] focus:outline-neutral-600 dark:border-neutral-600 dark:focus:outline-neutral-400"
352 name="repo"
353 id="repo"
354 >
355 <For each={Object.keys(sessions)}>
356 {(session) => (
357 <option value={session} selected={session === agent()?.sub}>
358 {sessions[session].handle ?? session}
359 </option>
360 )}
361 </For>
362 </select>
363 <span>/</span>
364 <TextInput
365 id="collection"
366 name="collection"
367 placeholder="Collection (default: $type)"
368 class="w-40 placeholder:text-xs lg:w-52"
369 />
370 <span>/</span>
371 <TextInput
372 id="rkey"
373 name="rkey"
374 placeholder="Record key (default: TID)"
375 class="w-40 placeholder:text-xs lg:w-52"
376 />
377 </div>
378 </Show>
379 <div class="min-h-0 flex-1">
380 <Editor
381 content={JSON.stringify(
382 !props.create ? props.record
383 : params.rkey ? placeholder()
384 : defaultPlaceholder(),
385 null,
386 2,
387 )}
388 />
389 </div>
390 <div class="flex flex-col gap-2">
391 <Show when={notice()}>
392 <div class="text-sm text-red-500 dark:text-red-400">{notice()}</div>
393 </Show>
394 <div class="flex justify-between gap-2">
395 <div class="relative" ref={insertMenuRef}>
396 <button
397 type="button"
398 class="dark:hover:bg-dark-200 dark:shadow-dark-700 dark:active:bg-dark-100 flex w-fit rounded-lg border-[0.5px] border-neutral-300 bg-neutral-50 p-1.5 text-base shadow-xs hover:bg-neutral-100 active:bg-neutral-200 dark:border-neutral-700 dark:bg-neutral-800"
399 onClick={() => setOpenInsertMenu(!openInsertMenu())}
400 >
401 <span class="iconify lucide--plus select-none"></span>
402 </button>
403 <Show when={openInsertMenu()}>
404 <div class="dark:bg-dark-300 dark:shadow-dark-700 absolute bottom-full left-0 z-10 mb-1 flex w-40 flex-col rounded-lg border-[0.5px] border-neutral-300 bg-neutral-50 p-1.5 shadow-md dark:border-neutral-700">
405 <MenuItem
406 icon="lucide--upload"
407 label="Upload blob"
408 onClick={() => {
409 setOpenInsertMenu(false);
410 blobInput.click();
411 }}
412 />
413 <MenuItem
414 icon="lucide--clock"
415 label="Insert timestamp"
416 onClick={insertTimestamp}
417 />
418 </div>
419 </Show>
420 <input
421 type="file"
422 id="blob"
423 class="sr-only"
424 ref={blobInput}
425 onChange={(e) => {
426 if (e.target.files !== null) setOpenUpload(true);
427 }}
428 />
429 </div>
430 <Modal
431 open={openUpload()}
432 onClose={() => setOpenUpload(false)}
433 closeOnClick={false}
434 >
435 <FileUpload file={blobInput.files![0]} />
436 </Modal>
437 <div class="flex items-center justify-end gap-2">
438 <button
439 type="button"
440 class="flex items-center gap-1 rounded-sm p-1.5 text-xs hover:bg-neutral-200 active:bg-neutral-300 dark:hover:bg-neutral-700 dark:active:bg-neutral-600"
441 onClick={() =>
442 setValidate(
443 validate() === true ? false
444 : validate() === false ? undefined
445 : true,
446 )
447 }
448 >
449 <Tooltip text={getValidateLabel()}>
450 <span class={`iconify ${getValidateIcon()}`}></span>
451 </Tooltip>
452 <span>Validate</span>
453 </button>
454 <Show when={!props.create}>
455 <Button onClick={() => editRecord(true)}>Recreate</Button>
456 </Show>
457 <Button
458 onClick={() =>
459 props.create ? createRecord(new FormData(formRef)) : editRecord()
460 }
461 >
462 {props.create ? "Create" : "Edit"}
463 </Button>
464 </div>
465 </div>
466 </div>
467 </form>
468 </div>
469 </Modal>
470 <Show when={isMinimized() && openDialog()}>
471 <button
472 class="dark:bg-dark-300 dark:hover:bg-dark-200 dark:active:bg-dark-100 fixed right-4 bottom-4 z-30 flex items-center gap-2 rounded-lg border-[0.5px] border-neutral-300 bg-neutral-50 px-3 py-2 shadow-md hover:bg-neutral-100 active:bg-neutral-200 dark:border-neutral-700"
473 onclick={() => setIsMinimized(false)}
474 >
475 <span class="iconify lucide--square-pen text-lg"></span>
476 <span class="text-sm font-medium">{props.create ? "Creating" : "Editing"} record</span>
477 </button>
478 </Show>
479 <Tooltip text={`${props.create ? "Create" : "Edit"} record`}>
480 <button
481 class={`flex items-center p-1.5 hover:bg-neutral-200 active:bg-neutral-300 dark:hover:bg-neutral-700 dark:active:bg-neutral-600 ${props.create ? "rounded-lg" : "rounded-sm"}`}
482 onclick={() => {
483 setNotice("");
484 setOpenDialog(true);
485 setIsMinimized(false);
486 }}
487 >
488 <div
489 class={props.create ? "iconify lucide--square-pen text-lg" : "iconify lucide--pencil"}
490 />
491 </button>
492 </Tooltip>
493 </>
494 );
495};