feat: use timeline api

This commit is contained in:
DIYgod
2024-05-28 23:03:56 +08:00
parent 350f61d5b0
commit f9b2ceedf2
15 changed files with 174 additions and 131 deletions
@@ -1,10 +1,10 @@
import { FeedIcon } from "@renderer/components/feed-icon"
import { Image } from "@renderer/components/ui/image"
import dayjs from "@renderer/lib/dayjs"
import type { EntriesResponse } from "@renderer/lib/types"
import type { TimelineResponse } from "@renderer/lib/types"
import { cn } from "@renderer/lib/utils"
export function ArticleItem({ entry }: { entry: EntriesResponse[number] }) {
export function ArticleItem({ entry }: { entry: TimelineResponse[number] }) {
return (
<div className="my-5 flex px-2 py-3">
<FeedIcon feed={entry.feeds} />
@@ -15,7 +15,7 @@ export function ArticleItem({ entry }: { entry: EntriesResponse[number] }) {
<span>
{dayjs
.duration(
dayjs(entry.publishedAt).diff(dayjs(), "minute"),
dayjs(entry.entries.publishedAt).diff(dayjs(), "minute"),
"minute",
)
.humanize()}
@@ -28,13 +28,13 @@ export function ArticleItem({ entry }: { entry: EntriesResponse[number] }) {
entry.read ? "text-zinc-500" : "text-black dark:text-white/90",
)}
>
{entry.title}
{entry.entries.title}
</div>
<div className="text-[13px] text-zinc-500">{entry.description}</div>
<div className="text-[13px] text-zinc-500">{entry.entries.description}</div>
</div>
{entry.images?.[0] && (
{entry.entries.images?.[0] && (
<Image
src={entry.images[0]}
src={entry.entries.images[0]}
className="ml-2 size-20 shrink-0"
loading="lazy"
proxy={{
@@ -72,7 +72,7 @@ export function EntryColumn() {
>
{page.data?.map((entry) => (
<EntryItemWrapper
key={entry.id}
key={entry.entries.id}
entry={entry}
view={activeList?.view}
>
@@ -1,7 +1,7 @@
import { useEntryActions } from "@renderer/hooks/useEntryActions"
import { useUpdateEntry } from "@renderer/hooks/useUpdateEntry"
import { showNativeMenu } from "@renderer/lib/native-menu"
import type { EntriesResponse } from "@renderer/lib/types"
import type { EntryResponse, TimelineResponse } from "@renderer/lib/types"
import { cn } from "@renderer/lib/utils"
import { apiFetch } from "@renderer/queries/api-fetch"
import { feedActions, useFeedStore } from "@renderer/store"
@@ -14,7 +14,7 @@ export function EntryItemWrapper({
children,
view,
}: {
entry: EntriesResponse[number]
entry: TimelineResponse[number] | EntryResponse
children: React.ReactNode
view?: number
}) {
@@ -28,7 +28,7 @@ export function EntryItemWrapper({
)
const updateEntry = useUpdateEntry({
entryId: entry?.id,
entryId: entry?.entries.id,
})
const read = useMutation({
@@ -36,7 +36,7 @@ export function EntryItemWrapper({
apiFetch("/reads", {
method: "POST",
body: {
entryId: entry?.id,
entryId: entry?.entries.id,
},
}),
onSuccess: () => {
@@ -55,22 +55,22 @@ export function EntryItemWrapper({
target: itemRef,
})
if (!entry?.url || view === undefined) return children
if (!entry?.entries.url || view === undefined) return children
return (
<div
key={entry.id}
key={entry.entries.id}
className={cn(
"rounded-md transition-colors",
activeEntry === entry.id && "bg-native-active/50",
activeEntry === entry.entries.id && "bg-native-active/50",
entry.read && "text-foreground/80",
)}
ref={itemRef}
onClick={(e) => {
e.stopPropagation()
feedActions.setActiveEntry(entry.id)
feedActions.setActiveEntry(entry.entries.id)
}}
onDoubleClick={() => entry.url && window.open(entry.url, "_blank")}
onDoubleClick={() => entry.entries.url && window.open(entry.entries.url, "_blank")}
onContextMenu={(e) => {
e.preventDefault()
showNativeMenu(
@@ -1,11 +1,11 @@
import { FeedIcon } from "@renderer/components/feed-icon"
import dayjs from "@renderer/lib/dayjs"
import type { EntriesResponse } from "@renderer/lib/types"
import type { TimelineResponse } from "@renderer/lib/types"
export function NotificationItem({
entry,
}: {
entry: EntriesResponse[number]
entry: TimelineResponse[number]
}) {
return (
<div className="my-5 flex px-2 py-3">
@@ -17,13 +17,13 @@ export function NotificationItem({
<span>
{dayjs
.duration(
dayjs(entry.publishedAt).diff(dayjs(), "minute"),
dayjs(entry.entries.publishedAt).diff(dayjs(), "minute"),
"minute",
)
.humanize()}
</span>
</div>
<div className="my-0.5">{entry.title}</div>
<div className="my-0.5">{entry.entries.title}</div>
</div>
</div>
)
@@ -1,14 +1,14 @@
import { FeedIcon } from "@renderer/components/feed-icon"
import { Image } from "@renderer/components/ui/image"
import dayjs from "@renderer/lib/dayjs"
import type { EntriesResponse } from "@renderer/lib/types"
import type { TimelineResponse } from "@renderer/lib/types"
export function PictureItem({ entry }: { entry: EntriesResponse[number] }) {
export function PictureItem({ entry }: { entry: TimelineResponse[number] }) {
return (
<div>
<div>
<div className="flex gap-2 overflow-x-auto">
{entry.images?.slice(0, 1).map((image) => (
{entry.entries.images?.slice(0, 1).map((image) => (
<Image
key={image}
src={image}
@@ -22,7 +22,7 @@ export function PictureItem({ entry }: { entry: EntriesResponse[number] }) {
))}
</div>
<div className="line-clamp-5 flex-1 px-2 pb-3 pt-1 text-sm">
<div className="line-clamp-2 font-medium">{entry.title}</div>
<div className="line-clamp-2 font-medium">{entry.entries.title}</div>
<div className="space-x-1 text-[13px]">
<FeedIcon
className="mr-0 inline-block size-3.5 align-sub"
@@ -33,7 +33,7 @@ export function PictureItem({ entry }: { entry: EntriesResponse[number] }) {
<span className="text-zinc-500">
{dayjs
.duration(
dayjs(entry.publishedAt).diff(dayjs(), "minute"),
dayjs(entry.entries.publishedAt).diff(dayjs(), "minute"),
"minute",
)
.humanize()}
@@ -1,30 +1,30 @@
import { FeedIcon } from "@renderer/components/feed-icon"
import { Image } from "@renderer/components/ui/image"
import dayjs from "@renderer/lib/dayjs"
import type { EntriesResponse } from "@renderer/lib/types"
import type { TimelineResponse } from "@renderer/lib/types"
export function SocialMediaItem({ entry }: { entry: EntriesResponse[number] }) {
export function SocialMediaItem({ entry }: { entry: TimelineResponse[number] }) {
return (
<div className="my-5 flex px-2 py-3">
<FeedIcon feed={entry.feeds} />
<div>
<div className="-mt-0.5 line-clamp-5 flex-1 text-sm">
<div className="space-x-1">
<span className="font-medium">{entry.author}</span>
<span className="font-medium">{entry.entries.author}</span>
<span className="text-zinc-500">·</span>
<span className="text-zinc-500">
{dayjs
.duration(
dayjs(entry.publishedAt).diff(dayjs(), "minute"),
dayjs(entry.entries.publishedAt).diff(dayjs(), "minute"),
"minute",
)
.humanize()}
</span>
</div>
<div className="mt-0.5">{entry.description}</div>
<div className="mt-0.5">{entry.entries.description}</div>
</div>
<div className="mt-1 flex gap-2 overflow-x-auto">
{entry.images?.map((image) => (
{entry.entries.images?.map((image) => (
<Image
key={image}
src={image}
@@ -1,16 +1,16 @@
import { FeedIcon } from "@renderer/components/feed-icon"
import { Image } from "@renderer/components/ui/image"
import dayjs from "@renderer/lib/dayjs"
import type { EntriesResponse } from "@renderer/lib/types"
import type { TimelineResponse } from "@renderer/lib/types"
export function VideoItem({ entry }: { entry: EntriesResponse[number] }) {
export function VideoItem({ entry }: { entry: TimelineResponse[number] }) {
return (
<div className="flex">
<div className="w-full">
<div className="flex gap-2 overflow-x-auto">
<Image
key={entry.images?.[0]}
src={entry.images?.[0]}
key={entry.entries.images?.[0]}
src={entry.entries.images?.[0]}
className="aspect-video w-full shrink-0 rounded-md object-cover"
loading="lazy"
proxy={{
@@ -20,18 +20,18 @@ export function VideoItem({ entry }: { entry: EntriesResponse[number] }) {
/>
</div>
<div className="line-clamp-5 flex-1 px-2 pb-3 pt-1 text-sm">
<div className="line-clamp-2 font-medium">{entry.title}</div>
<div className="line-clamp-2 font-medium">{entry.entries.title}</div>
<div className="space-x-1 text-[13px]">
<FeedIcon
className="mr-0 inline-block size-3.5 align-sub"
feed={entry.feeds}
/>
<span>{entry.author}</span>
<span>{entry.entries.author}</span>
<span className="text-zinc-500">·</span>
<span className="text-zinc-500">
{dayjs
.duration(
dayjs(entry.publishedAt).diff(dayjs(), "minute"),
dayjs(entry.entries.publishedAt).diff(dayjs(), "minute"),
"minute",
)
.humanize()}
@@ -13,14 +13,14 @@ export function EntryContent({ entryId }: { entryId: ActiveEntry }) {
const [content, setContent] = useState<JSX.Element>()
useEffect(() => {
if (entry.data?.content) {
parseHtml(entry.data?.content).then((parsed) => {
if (entry.data?.entries.content) {
parseHtml(entry.data?.entries.content).then((parsed) => {
setContent(parsed.content)
})
} else {
setContent(undefined)
}
}, [entry.data?.content])
}, [entry.data?.entries.content])
if (!entry.data) return null
@@ -32,24 +32,24 @@ export function EntryContent({ entryId }: { entryId: ActiveEntry }) {
initial={{ opacity: 0.01, y: 100 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0.01, y: -100 }}
key={entry.data?.id}
key={entry.data?.entries.id}
>
<div>
<a
href={entry.data?.url || void 0}
href={entry.data?.entries.url || void 0}
target="_blank"
className="mx-auto block max-w-[598px] rounded-md p-6 transition-colors hover:bg-zinc-100 dark:hover:bg-neutral-800"
rel="noreferrer"
>
<div className="select-text break-words text-3xl font-bold">
{entry.data?.title}
{entry.data?.entries.title}
</div>
<div className="mt-2 text-[13px] font-medium text-zinc-500">
{entry.data?.feeds?.title}
</div>
<div className="text-[13px] text-zinc-500">
{entry.data?.publishedAt &&
new Date(entry.data?.publishedAt).toUTCString()}
{entry.data?.entries.publishedAt &&
new Date(entry.data?.entries.publishedAt).toUTCString()}
</div>
</a>
<div className="prose prose-zinc mx-auto mb-32 mt-10 max-w-[550px] cursor-auto select-text break-all text-[15px] dark:prose-invert">
@@ -6,20 +6,20 @@ import {
TooltipTrigger,
} from "@renderer/components/ui/tooltip"
import { useEntryActions } from "@renderer/hooks/useEntryActions"
import type { EntriesResponse } from "@renderer/lib/types"
import type { EntryResponse } from "@renderer/lib/types"
export function EntryShare({
view,
entry,
}: {
view: number
entry?: EntriesResponse[number]
entry?: EntryResponse
}) {
const { items } = useEntryActions({
view,
entry,
})
if (!entry?.url) return null
if (!entry?.entries.url) return null
return (
<div className="flex h-14 items-center justify-end gap-3 px-5 text-lg text-zinc-500">
+2 -2
View File
@@ -16,7 +16,7 @@ import {
} from "@renderer/components/ui/form"
import { Image } from "@renderer/components/ui/image"
import { Input } from "@renderer/components/ui/input"
import type { EntriesResponse, FeedResponse } from "@renderer/lib/types"
import type { FeedResponse } from "@renderer/lib/types"
import { apiFetch } from "@renderer/queries/api-fetch"
import { useMutation } from "@tanstack/react-query"
import { useEffect } from "react"
@@ -67,7 +67,7 @@ export function FollowForm({ type }: { type: string }) {
data: {
feed: FeedResponse
docs?: string
entries?: Partial<EntriesResponse>
entries?: Partial<unknown>
isSubscribed?: boolean
subscriptionCount?: number
}[]
+72 -15
View File
@@ -3,6 +3,60 @@ import * as hono_utils_http_status from 'hono/utils/http-status';
import * as hono from 'hono';
declare const routes: hono_hono_base.HonoBase<hono.Env, {
"/timeline": {
$post: {
input: {
json: {
feedId?: string | undefined;
view?: number | undefined;
read?: boolean | undefined;
limit?: number | undefined;
offset?: number | undefined;
feedIdList?: string[] | undefined;
};
};
output: {
code: 0;
total: number;
data?: {
entries: {
description: string | null;
title: string | null;
id: string;
url: string | null;
guid: string;
author: string | null;
changedAt: string;
publishedAt: string;
images: string[] | null;
categories: string[] | null;
};
collections: {
createdAt: string;
};
feeds: {
description: string | null;
title: string | null;
id: string;
image: string | null;
url: string;
siteUrl: string | null;
checkedAt: string;
nextCheckAt: string;
lastModifiedHeader: string | null;
etagHeader: string | null;
ttl: number | null;
errorMessage: string | null;
errorAt: string | null;
};
read: boolean | null;
}[] | undefined;
};
outputFormat: "json";
status: 200;
};
};
} & {
"/reads": {
$post: {
input: {
@@ -280,8 +334,8 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
publishedAt: string;
images: string[] | null;
categories: string[] | null;
collected: boolean;
read: boolean;
collected: boolean;
}[] | undefined;
};
outputFormat: "json";
@@ -296,10 +350,22 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
output: {
code: 0;
data?: {
description: string | null;
title: string | null;
content: string | null;
id: string;
entries: {
description: string | null;
title: string | null;
content: string | null;
id: string;
url: string | null;
guid: string;
author: string | null;
changedAt: string;
publishedAt: string;
images: string[] | null;
categories: string[] | null;
};
collections: {
createdAt: string;
};
feeds: {
description: string | null;
title: string | null;
@@ -315,16 +381,7 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
errorMessage: string | null;
errorAt: string | null;
};
url: string | null;
feedId: string;
guid: string;
author: string | null;
changedAt: string;
publishedAt: string;
images: string[] | null;
categories: string[] | null;
collected: boolean;
read: boolean;
read: boolean | null;
} | undefined;
};
outputFormat: "json";
+25 -23
View File
@@ -1,7 +1,7 @@
import { useToast } from "@renderer/components/ui/use-toast"
import { useUpdateEntry } from "@renderer/hooks/useUpdateEntry"
import { client } from "@renderer/lib/client"
import type { EntriesResponse } from "@renderer/lib/types"
import type { TimelineResponse } from "@renderer/lib/types"
import { apiFetch } from "@renderer/queries/api-fetch"
import {
useMutation,
@@ -15,11 +15,11 @@ export const useEntryActions = ({
entry,
}: {
view?: number
entry?: EntriesResponse[number]
entry?: TimelineResponse[number]
}) => {
const checkEagle = useQuery({
queryKey: ["check-eagle"],
enabled: !!entry?.url && !!view,
enabled: !!entry?.entries.url && !!view,
queryFn: async () => {
try {
await ofetch("http://localhost:41595")
@@ -31,7 +31,7 @@ export const useEntryActions = ({
})
const updateEntry = useUpdateEntry({
entryId: entry?.id,
entryId: entry?.entries.id,
})
const collect = useMutation({
@@ -39,12 +39,14 @@ export const useEntryActions = ({
apiFetch("/collections", {
method: "POST",
body: {
entryId: entry?.id,
entryId: entry?.entries.id,
},
}),
onSuccess: () => {
updateEntry({
collected: true,
collections: {
createdAt: new Date().toISOString(),
},
})
toast({
@@ -58,12 +60,12 @@ export const useEntryActions = ({
apiFetch("/collections", {
method: "DELETE",
body: {
entryId: entry?.id,
entryId: entry?.entries.id,
},
}),
onSuccess: () => {
updateEntry({
collected: false,
collections: undefined,
})
toast({
@@ -77,7 +79,7 @@ export const useEntryActions = ({
apiFetch("/reads", {
method: "POST",
body: {
entryId: entry?.id,
entryId: entry?.entries.id,
},
}),
onSuccess: () => {
@@ -91,7 +93,7 @@ export const useEntryActions = ({
apiFetch("/reads", {
method: "DELETE",
body: {
entryId: entry?.id,
entryId: entry?.entries.id,
},
}),
onSuccess: () => {
@@ -103,14 +105,14 @@ export const useEntryActions = ({
const { toast } = useToast()
if (!entry?.url || view === undefined) return { items: [] }
if (!entry?.entries.url || view === undefined) return { items: [] }
const items = [
[
{
name: "Collect",
className: "i-mingcute-star-line",
disabled: !!entry.collected,
disabled: !!entry.collections,
onClick: () => {
collect.mutate()
},
@@ -118,7 +120,7 @@ export const useEntryActions = ({
{
name: "Uncollect",
className: "i-mingcute-star-fill",
disabled: !entry.collected,
disabled: !entry.collections,
onClick: () => {
uncollect.mutate()
},
@@ -127,8 +129,8 @@ export const useEntryActions = ({
name: "Copy Link",
className: "i-mingcute-link-line",
onClick: () => {
if (!entry.url) return
navigator.clipboard.writeText(entry.url)
if (!entry.entries.url) return
navigator.clipboard.writeText(entry.entries.url)
toast({
duration: 1000,
description: "Link copied to clipboard.",
@@ -139,8 +141,8 @@ export const useEntryActions = ({
name: "Open in Browser",
className: "i-mingcute-world-2-line",
onClick: () => {
if (!entry.url) return
window.open(entry.url, "_blank")
if (!entry.entries.url) return
window.open(entry.entries.url, "_blank")
},
},
{
@@ -148,12 +150,12 @@ export const useEntryActions = ({
icon: "/eagle.svg",
disabled:
(checkEagle.isLoading ? true : !checkEagle.data) ||
!entry.images?.length,
!entry.entries.images?.length,
onClick: async () => {
if (!entry.url || !entry.images?.length) return
if (!entry.entries.url || !entry.entries.images?.length) return
const response = await client?.saveToEagle({
url: entry.url,
images: entry.images,
url: entry.entries.url,
images: entry.entries.images,
})
if (response?.status === "success") {
toast({
@@ -172,8 +174,8 @@ export const useEntryActions = ({
name: "Share",
className: "i-mingcute-share-2-line",
onClick: () => {
if (!entry.url) return
client?.showShareMenu(entry.url)
if (!entry.entries.url) return
client?.showShareMenu(entry.entries.url)
},
},
{
+4 -4
View File
@@ -1,4 +1,4 @@
import type { EntriesResponse, ListResponse } from "@renderer/lib/types"
import type { TimelineResponse, ListResponse } from "@renderer/lib/types"
import { Queries } from "@renderer/queries"
import type { InfiniteData, QueryKey } from "@tanstack/react-query"
import { useQueryClient } from "@tanstack/react-query"
@@ -7,7 +7,7 @@ import { produce } from "immer"
export const useUpdateEntry = ({ entryId }: { entryId?: string }) => {
const queryClient = useQueryClient()
const updateEntry = (changed: Partial<EntriesResponse[number]>) => {
const updateEntry = (changed: Partial<TimelineResponse[number]>) => {
const query = Queries.entries.byId(entryId)
query.setData((draft) => {
@@ -19,12 +19,12 @@ export const useUpdateEntry = ({ entryId }: { entryId?: string }) => {
queryKey: ["entries"],
})
entriesData.forEach(([key, data]: [QueryKey, unknown]) => {
const assertData = data as InfiniteData<ListResponse<EntriesResponse>>
const assertData = data as InfiniteData<ListResponse<TimelineResponse>>
const finaldata = produce(assertData, (assertData) => {
const list = assertData?.pages?.[0]?.data
if (list) {
for (const item of list) {
if (item.id === entryId) {
if (item.entries.id === entryId) {
for (const [key, value] of Object.entries(changed)) {
item[key] = value
}
+10 -27
View File
@@ -20,39 +20,22 @@ export type SubscriptionResponse = Array<
>[number] & { unread?: number }
>
// export type EntriesResponse = {
// author?: string
// category?: string[]
// changedAt: string
// content?: string
// description?: string
// enclosure?: {
// url: string
// type?: string
// length?: number
// title?: string
// }
// feedId: string
// guid: string
// id: string
// images?: string[]
// publishedAt: string
// readingTime?: number
// title?: string
// url?: string
export type EntryResponse = Exclude<
Extract<
InferResponseType<typeof apiClient.entries.$get>,
{ code: 0 }
>["data"],
undefined
>
// feeds: FeedResponse
// collected: boolean
// read: boolean
// }[]
export type EntriesResponse = Array<
export type TimelineResponse = Array<
Exclude<
Extract<
InferResponseType<typeof apiClient.entries.$get>,
InferResponseType<typeof apiClient.timeline.$post>,
{ code: 0 }
>["data"],
undefined
>
>[number]
>
export type ListResponse<T> = {
+11 -10
View File
@@ -4,8 +4,7 @@ import {
import { useBizInfiniteQuery } from "@renderer/hooks/useBizQuery"
import { levels } from "@renderer/lib/constants"
import { defineQuery } from "@renderer/lib/defineQuery"
import type { EntriesResponse, ListResponse } from "@renderer/lib/types"
import { apiClient, apiFetch } from "@renderer/queries/api-fetch"
import { apiClient } from "@renderer/queries/api-fetch"
export const entries = {
entries: ({
@@ -18,11 +17,9 @@ export const entries = {
view?: number
}) =>
defineQuery(
["entries", level, id],
["entries", level, id, view],
async ({ pageParam }) => {
const params: {
category?: string
view?: number
feedId?: string
feedIdList?: string[]
} = {}
@@ -31,14 +28,14 @@ export const entries = {
} else if (level === levels.feed) {
params.feedId = `${id}`
}
return await apiFetch<ListResponse<EntriesResponse>>("/entries", {
method: "POST",
body: {
offset: pageParam,
const res = await apiClient.timeline.$post({
json: {
offset: pageParam as number | undefined,
view,
...params,
},
})
return await res.json()
},
{
rootKey: ["entries"],
@@ -51,7 +48,11 @@ export const entries = {
if (!id) {
throw new UnprocessableEntityError("id is required")
}
const res = await apiClient.entries.$get({ query: { id } })
const res = await apiClient.entries.$get({
query: {
id,
},
})
const json = await res.json()
return json.data