From 53e62cee803ad05546cbf655728abcaa7051f543 Mon Sep 17 00:00:00 2001 From: reya Date: Wed, 17 Apr 2024 07:30:52 +0700 Subject: [PATCH] fix: prevent app crash in some cases --- .../ark/src/components/note/buttons/pin.tsx | 65 +++++++------ .../ark/src/components/note/preview/image.tsx | 95 ++++++++++--------- .../src/components/note/primitives/thread.tsx | 74 ++++++++------- 3 files changed, 121 insertions(+), 113 deletions(-) diff --git a/packages/ark/src/components/note/buttons/pin.tsx b/packages/ark/src/components/note/buttons/pin.tsx index 6125f4e31..b439b19f6 100644 --- a/packages/ark/src/components/note/buttons/pin.tsx +++ b/packages/ark/src/components/note/buttons/pin.tsx @@ -4,39 +4,42 @@ import * as Tooltip from "@radix-ui/react-tooltip"; import { useTranslation } from "react-i18next"; import { useColumnContext } from "../../column/provider"; import { useNoteContext } from "../provider"; +import { toast } from "sonner"; export function NotePin() { - const event = useNoteContext(); + const { t } = useTranslation(); + const { addColumn } = useColumnContext(); + const event = useNoteContext(); - const { t } = useTranslation(); - const { addColumn } = useColumnContext(); + const pin = async () => { + if (!event) toast.error("Something is wrong!"); + await addColumn({ + kind: COL_TYPES.thread, + title: "Thread", + content: event?.id, + }); + }; - return ( - - - - - - - - {t("note.buttons.pinTooltip")} - - - - - - ); + return ( + + + + + + + + {t("note.buttons.pinTooltip")} + + + + + + ); } diff --git a/packages/ark/src/components/note/preview/image.tsx b/packages/ark/src/components/note/preview/image.tsx index 883ac33c7..5f850553e 100644 --- a/packages/ark/src/components/note/preview/image.tsx +++ b/packages/ark/src/components/note/preview/image.tsx @@ -3,59 +3,60 @@ import { downloadDir } from "@tauri-apps/api/path"; import { Window } from "@tauri-apps/api/window"; import { download } from "@tauri-apps/plugin-upload"; import { SyntheticEvent, useState } from "react"; +import { useNoteContext } from "../provider"; export function ImagePreview({ url }: { url: string }) { - const [downloaded, setDownloaded] = useState(false); + const event = useNoteContext(); + const [downloaded, setDownloaded] = useState(false); - const downloadImage = async (e: { stopPropagation: () => void }) => { - try { - e.stopPropagation(); + const downloadImage = async (e: { stopPropagation: () => void }) => { + try { + e.stopPropagation(); - const downloadDirPath = await downloadDir(); - const filename = url.substring(url.lastIndexOf("/") + 1); - await download(url, `${downloadDirPath}/${filename}`); + const downloadDirPath = await downloadDir(); + const filename = url.substring(url.lastIndexOf("/") + 1); + await download(url, `${downloadDirPath}/${filename}`); - setDownloaded(true); - } catch (e) { - console.error(e); - } - }; + setDownloaded(true); + } catch (e) { + console.error(e); + } + }; - const open = async () => { - const name = new URL(url).pathname.split("/").pop(); - return new Window("image-viewer", { - url, - title: name, - }); - }; + const open = async () => { + return new Window(`image-viewer-${event.id}`, { + url, + title: "Image Viewer", + }); + }; - const fallback = (event: SyntheticEvent) => { - event.currentTarget.src = "/fallback-image.jpg"; - }; + const fallback = (event: SyntheticEvent) => { + event.currentTarget.src = "/fallback-image.jpg"; + }; - return ( - // biome-ignore lint/a11y/useKeyWithClickEvents: -
- {url} - -
- ); + return ( + // biome-ignore lint/a11y/useKeyWithClickEvents: +
+ {url} + +
+ ); } diff --git a/packages/ark/src/components/note/primitives/thread.tsx b/packages/ark/src/components/note/primitives/thread.tsx index 236d23b90..d95904111 100644 --- a/packages/ark/src/components/note/primitives/thread.tsx +++ b/packages/ark/src/components/note/primitives/thread.tsx @@ -3,41 +3,45 @@ import { useEvent } from "../../../hooks/useEvent"; import { User } from "../../user"; export function ThreadNote({ eventId }: { eventId: string }) { - const { isLoading, data } = useEvent(eventId); + const { isLoading, isError, data } = useEvent(eventId); - if (isLoading) { - return
Loading...
; - } + if (isLoading || !data) { + return
Loading...
; + } - return ( - - -
- - - -
- -
- - · - -
-
-
-
- -
- - -
- -
- - -
-
-
-
- ); + if (isError) { + return
Error
; + } + + return ( + + +
+ + + +
+ +
+ + · + +
+
+
+
+ +
+ + +
+ +
+ + +
+
+
+
+ ); }