From 9d9b2c5eccc11b5d6ca10be59041aef4985cc34b Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 29 Aug 2024 18:55:59 +0800 Subject: [PATCH] feat: save to instapaper --- .../src/atoms/settings/integration.ts | 3 ++ .../platform-icon/collections/instapaper.tsx | 15 +++++++++ .../src/components/ui/platform-icon/icons.ts | 1 + .../src/hooks/biz/useEntryActions.tsx | 33 ++++++++++++++++++- src/renderer/src/initialize/hydrate.ts | 2 ++ .../src/modules/settings/tabs/integration.tsx | 16 ++++++++- src/shared/src/interface/settings.ts | 3 ++ 7 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/renderer/src/components/ui/platform-icon/collections/instapaper.tsx diff --git a/src/renderer/src/atoms/settings/integration.ts b/src/renderer/src/atoms/settings/integration.ts index 7e47e5fa9..c732f39ab 100644 --- a/src/renderer/src/atoms/settings/integration.ts +++ b/src/renderer/src/atoms/settings/integration.ts @@ -6,6 +6,9 @@ export const createDefaultSettings = (): IntegrationSettings => ({ enableEagle: true, enableReadwise: true, readwiseToken: "", + enableInstapaper: true, + instapaperUsername: "", + instapaperPassword: "", }) export const { diff --git a/src/renderer/src/components/ui/platform-icon/collections/instapaper.tsx b/src/renderer/src/components/ui/platform-icon/collections/instapaper.tsx new file mode 100644 index 000000000..4e482f072 --- /dev/null +++ b/src/renderer/src/components/ui/platform-icon/collections/instapaper.tsx @@ -0,0 +1,15 @@ +import type { SVGProps } from "react" + +export function SimpleIconsInstapaper(props: SVGProps) { + return ( + + + + ) +} diff --git a/src/renderer/src/components/ui/platform-icon/icons.ts b/src/renderer/src/components/ui/platform-icon/icons.ts index 1d0fd8339..1b8a6e63c 100644 --- a/src/renderer/src/components/ui/platform-icon/icons.ts +++ b/src/renderer/src/components/ui/platform-icon/icons.ts @@ -1,3 +1,4 @@ export * from "./collections/eagle" +export * from "./collections/instapaper" export * from "./collections/readwise" export * from "./collections/rss3" diff --git a/src/renderer/src/hooks/biz/useEntryActions.tsx b/src/renderer/src/hooks/biz/useEntryActions.tsx index d17cdf36c..9943e6bbf 100644 --- a/src/renderer/src/hooks/biz/useEntryActions.tsx +++ b/src/renderer/src/hooks/biz/useEntryActions.tsx @@ -6,7 +6,7 @@ import { } from "@renderer/atoms/readability" import { useIntegrationSettingKey } from "@renderer/atoms/settings/integration" import { whoami } from "@renderer/atoms/user" -import { SimpleIconsEagle, SimpleIconsReadwise } from "@renderer/components/ui/platform-icon/icons" +import { SimpleIconsEagle, SimpleIconsInstapaper, SimpleIconsReadwise } from "@renderer/components/ui/platform-icon/icons" import { COPY_MAP } from "@renderer/constants" import { shortcuts } from "@renderer/constants/shortcuts" import { tipcClient } from "@renderer/lib/client" @@ -140,6 +140,9 @@ export const useEntryActions = ({ const enableEagle = useIntegrationSettingKey("enableEagle") const enableReadwise = useIntegrationSettingKey("enableReadwise") const readwiseToken = useIntegrationSettingKey("readwiseToken") + const enableInstapaper = useIntegrationSettingKey("enableInstapaper") + const instapaperUsername = useIntegrationSettingKey("instapaperUsername") + const instapaperPassword = useIntegrationSettingKey("instapaperPassword") const items = useMemo(() => { if (!populatedEntry || view === undefined) return [] @@ -216,6 +219,34 @@ export const useEntryActions = ({ } }, }, + { + name: "Save to Instapaper", + icon: , + key: "saveToInstapaper", + hide: !enableInstapaper || !instapaperPassword || !instapaperUsername || !populatedEntry.entries.url, + onClick: async () => { + try { + const data = await ofetch("https://www.instapaper.com/api/add", { + query: { + url: populatedEntry.entries.url, + title: populatedEntry.entries.title, + }, + method: "POST", + headers: { + Authorization: `Basic ${btoa(`${instapaperUsername}:${instapaperPassword}`)}`, + }, + parseResponse: JSON.parse, + }) + toast.success(<>Saved to Instapaper, view, { + duration: 3000, + }) + } catch { + toast.error("Failed to save to Instapaper.", { + duration: 3000, + }) + } + }, + }, { key: "tip", shortcut: shortcuts.entry.tip.key, diff --git a/src/renderer/src/initialize/hydrate.ts b/src/renderer/src/initialize/hydrate.ts index 036247691..7b8e506b0 100644 --- a/src/renderer/src/initialize/hydrate.ts +++ b/src/renderer/src/initialize/hydrate.ts @@ -1,4 +1,5 @@ import { initializeDefaultGeneralSettings } from "@renderer/atoms/settings/general" +import { initializeDefaultIntegrationSettings } from "@renderer/atoms/settings/integration" import { initializeDefaultUISettings } from "@renderer/atoms/settings/ui" import { appLog } from "@renderer/lib/log" import { sleep } from "@renderer/lib/utils" @@ -108,4 +109,5 @@ const logHydrateError = (message: string) => { export const hydrateSettings = () => { initializeDefaultUISettings() initializeDefaultGeneralSettings() + initializeDefaultIntegrationSettings() } diff --git a/src/renderer/src/modules/settings/tabs/integration.tsx b/src/renderer/src/modules/settings/tabs/integration.tsx index cc1141625..ec593fe34 100644 --- a/src/renderer/src/modules/settings/tabs/integration.tsx +++ b/src/renderer/src/modules/settings/tabs/integration.tsx @@ -8,7 +8,7 @@ import { setUISetting, useUISettingSelector, } from "@renderer/atoms/settings/ui" -import { SimpleIconsEagle, SimpleIconsReadwise } from "@renderer/components/ui/platform-icon/icons" +import { SimpleIconsEagle, SimpleIconsInstapaper, SimpleIconsReadwise } from "@renderer/components/ui/platform-icon/icons" import { Select, SelectContent, @@ -51,6 +51,20 @@ export const SettingIntegration = () => ( label: "Readwise Access Token", description: <>You can get it here: readwise.io/access_token., }), + { + type: "title", + value: Instapaper, + }, + defineSettingItem("enableInstapaper", { + label: "Enable", + description: <>Display Save to Instapaper button when available., + }), + defineSettingItem("instapaperUsername", { + label: "Instapaper Username", + }), + defineSettingItem("instapaperPassword", { + label: "Instapaper Password", + }), ]} /> diff --git a/src/shared/src/interface/settings.ts b/src/shared/src/interface/settings.ts index f4f92c387..8f2e7d99f 100644 --- a/src/shared/src/interface/settings.ts +++ b/src/shared/src/interface/settings.ts @@ -38,4 +38,7 @@ export interface IntegrationSettings { enableEagle: boolean enableReadwise: boolean readwiseToken: string + enableInstapaper: boolean + instapaperUsername: string + instapaperPassword: string }