Skip to content

Commit

Permalink
feat: save to readwise
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Aug 29, 2024
1 parent f9f1938 commit 086ddfd
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ export function SimpleIconsReadwise(props: SVGProps<SVGSVGElement>) {
width="1em"
height="1em"
viewBox="0 0 50 50"
className={cn(props.className, "rounded-sm")}
{...props}
className={cn(props.className, "rounded-[2px]")}
>
<g>
<path d="M50 0H0V50H50V0Z" fill="currentColor" />
Expand Down
98 changes: 67 additions & 31 deletions src/renderer/src/hooks/biz/useEntryActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import {
setReadabilityContent,
setReadabilityStatus,
} from "@renderer/atoms/readability"
import { useIntegrationSettingKey } from "@renderer/atoms/settings/integration"
import { whoami } from "@renderer/atoms/user"
import { SimpleIconsEagle } from "@renderer/components/ui/platform-icon/icons"
import { SimpleIconsEagle, 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"
Expand Down Expand Up @@ -136,6 +137,9 @@ export const useEntryActions = ({
const uncollect = useUnCollect(populatedEntry)
const read = useRead()
const unread = useUnread()
const enableEagle = useIntegrationSettingKey("enableEagle")
const enableReadwise = useIntegrationSettingKey("enableReadwise")
const readwiseToken = useIntegrationSettingKey("readwiseToken")

const items = useMemo(() => {
if (!populatedEntry || view === undefined) return []
Expand All @@ -150,6 +154,68 @@ export const useEntryActions = ({
disabled?: boolean
onClick: () => void
}[] = [
{
name: "Save media to Eagle",
icon: <SimpleIconsEagle />,
key: "saveToEagle",
hide:
!enableEagle || (checkEagle.isLoading ? true : !checkEagle.data) ||
!populatedEntry.entries.media?.length,
onClick: async () => {
if (
!populatedEntry.entries.url ||
!populatedEntry.entries.media?.length
) {
return
}
const response = await tipcClient?.saveToEagle({
url: populatedEntry.entries.url,
mediaUrls: populatedEntry.entries.media.map((m) => m.url),
})
if (response?.status === "success") {
toast.success("Saved to Eagle.", {
duration: 3000,
})
} else {
toast.error("Failed to save to Eagle.", {
duration: 3000,
})
}
},
},
{
name: "Save to Readwise",
icon: <SimpleIconsReadwise />,
key: "saveToReadwise",
hide: !enableReadwise || !readwiseToken || !populatedEntry.entries.url,
onClick: async () => {
try {
const data = await ofetch("https://readwise.io/api/v3/save/", {
method: "POST",
headers: {
Authorization: `Token ${readwiseToken}`,
},
body: {
url: populatedEntry.entries.url,
html: populatedEntry.entries.content || undefined,
title: populatedEntry.entries.title || undefined,
author: populatedEntry.entries.author || undefined,
summary: populatedEntry.entries.description || undefined,
published_date: populatedEntry.entries.publishedAt || undefined,
image_url: populatedEntry.entries.media?.[0]?.url || undefined,
saved_using: "Follow",
},
})
toast.success(<>Saved to Readwise, <a target="_blank" className="underline" href={data.url}>view</a></>, {
duration: 3000,
})
} catch {
toast.error("Failed to save to Readwise.", {
duration: 3000,
})
}
},
},
{
key: "tip",
shortcut: shortcuts.entry.tip.key,
Expand Down Expand Up @@ -205,36 +271,6 @@ export const useEntryActions = ({
window.open(populatedEntry.entries.url, "_blank")
},
},

{
name: "Save media to Eagle",
icon: <SimpleIconsEagle />,
key: "saveToEagle",
hide:
(checkEagle.isLoading ? true : !checkEagle.data) ||
!populatedEntry.entries.media?.length,
onClick: async () => {
if (
!populatedEntry.entries.url ||
!populatedEntry.entries.media?.length
) {
return
}
const response = await tipcClient?.saveToEagle({
url: populatedEntry.entries.url,
mediaUrls: populatedEntry.entries.media.map((m) => m.url),
})
if (response?.status === "success") {
toast("Saved to Eagle.", {
duration: 3000,
})
} else {
toast("Failed to save to Eagle.", {
duration: 3000,
})
}
},
},
{
name: "Share",
key: "share",
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/modules/settings/tabs/integration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ export const SettingIntegration = () => (
settings={[
{
type: "title",
value: <span className="flex items-center gap-1"><SimpleIconsEagle />Eagle</span>,
value: <span className="flex items-center gap-2"><SimpleIconsEagle />Eagle</span>,
},
defineSettingItem("enableEagle", {
label: "Enable",
description: <>Display <i>Save media to Eagle</i> button when available.</>,
}),
{
type: "title",
value: <span className="flex items-center gap-1"><SimpleIconsReadwise />Readwise</span>,
value: <span className="flex items-center gap-2"><SimpleIconsReadwise />Readwise</span>,
},
defineSettingItem("enableReadwise", {
label: "Enable",
Expand Down

0 comments on commit 086ddfd

Please sign in to comment.