From 46451ac343cd9649b9cc356769c616656c593ac3 Mon Sep 17 00:00:00 2001 From: sevichecc <91365763+Sevichecc@users.noreply.github.com> Date: Sun, 16 Apr 2023 20:35:16 +0800 Subject: [PATCH] refactor: bookmark command --- src/bookmark.tsx | 22 ++++++++++++---------- src/oauth.ts | 2 +- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/bookmark.tsx b/src/bookmark.tsx index a19b720..2f85cdd 100644 --- a/src/bookmark.tsx +++ b/src/bookmark.tsx @@ -3,8 +3,16 @@ import { Action, ActionPanel, List, Toast, showToast, Cache } from "@raycast/api import { BookmarkedStatus, AkkomaError } from "./types"; import { NodeHtmlMarkdown } from "node-html-markdown"; import apiServer from "./api"; +import { authorize } from "./oauth"; const cache = new Cache(); +const nhm = new NodeHtmlMarkdown(); +const dateTimeFormatter = new Intl.DateTimeFormat("default", { + hour: "numeric", + minute: "numeric", + day: "numeric", + month: "long", +}); export default function BookmarkCommand() { const cached = cache.get("latest_bookmarks"); @@ -14,7 +22,8 @@ export default function BookmarkCommand() { useEffect(() => { const getBookmark = async () => { try { - showToast(Toast.Style.Animated, "Loaing bookmarks..."); + await authorize(); + showToast(Toast.Style.Animated, "Loading bookmarks..."); const newBookmarks = await apiServer.fetchBookmarks(); setBookmarks(newBookmarks); cache.set("latest_bookmarks", JSON.stringify(newBookmarks)); @@ -30,20 +39,13 @@ export default function BookmarkCommand() { }, []); const parseStatus = ({ content, media_attachments, account, created_at }: BookmarkedStatus) => { - const nhm = new NodeHtmlMarkdown(); - const images = media_attachments.filter((attachment) => attachment.type === "image"); const parsedImages = images.reduce((link, image) => link + `![${image.description}](${image.remote_url})`, ""); const date = new Date(created_at); - const parseTime = new Intl.DateTimeFormat("default", { - hour: "numeric", - minute: "numeric", - day: "numeric", - month: "long", - }).format(date); + const parsedTime = dateTimeFormatter.format(date); - return ` _@${account.acct} (${parseTime})_ ` + nhm.translate("
" + content) + parsedImages; + return ` _@${account.acct} (${parsedTime})_ ` + nhm.translate("
" + content) + parsedImages; }; return ( diff --git a/src/oauth.ts b/src/oauth.ts index 327db5f..efbe10d 100644 --- a/src/oauth.ts +++ b/src/oauth.ts @@ -1,4 +1,4 @@ -import { LocalStorage, OAuth, getPreferenceValues,Cache } from "@raycast/api"; +import { LocalStorage, OAuth, getPreferenceValues } from "@raycast/api"; import { Preference } from "./types"; import apiServer from "./api";