Skip to content

Commit

Permalink
feat: entry preview api
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed May 30, 2024
1 parent 3c6103f commit 28b6cce
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 27 deletions.
4 changes: 2 additions & 2 deletions src/renderer/src/components/entry-column/article-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { useEntry } from "@renderer/store/entry"
import { ReactVirtuosoItemPlaceholder } from "../ui/placeholder"
import type { UniversalItemProps } from "./types"

export function ArticleItem({ entryId }: UniversalItemProps) {
const entry = useEntry(entryId)
export function ArticleItem({ entryId, entryPreview }: UniversalItemProps) {
const entry = useEntry(entryId) || entryPreview

// NOTE: prevent 0 height element, react virtuoso will not stop render any more
if (!entry) return <ReactVirtuosoItemPlaceholder />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { useEntry } from "@renderer/store/entry"
import { ReactVirtuosoItemPlaceholder } from "../ui/placeholder"
import type { UniversalItemProps } from "./types"

export function NotificationItem({ entryId }: UniversalItemProps) {
const entry = useEntry(entryId)
export function NotificationItem({ entryId, entryPreview }: UniversalItemProps) {
const entry = useEntry(entryId) || entryPreview
if (!entry) return <ReactVirtuosoItemPlaceholder />
return (
<div className="mb-5 flex px-2 py-3">
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/components/entry-column/picture-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useEntry } from "@renderer/store/entry"
import { ReactVirtuosoItemPlaceholder } from "../ui/placeholder"
import type { UniversalItemProps } from "./types"

export function PictureItem({ entryId }: UniversalItemProps) {
const entry = useEntry(entryId)
export function PictureItem({ entryId, entryPreview }: UniversalItemProps) {
const entry = useEntry(entryId) || entryPreview
if (!entry) return <ReactVirtuosoItemPlaceholder />
return (
<div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useEntry } from "@renderer/store/entry"
import { ReactVirtuosoItemPlaceholder } from "../ui/placeholder"
import type { UniversalItemProps } from "./types"

export function SocialMediaItem({ entryId }: UniversalItemProps) {
const entry = useEntry(entryId)
export function SocialMediaItem({ entryId, entryPreview }: UniversalItemProps) {
const entry = useEntry(entryId) || entryPreview

// NOTE: prevent 0 height element, react virtuoso will not stop render any more
if (!entry) return <ReactVirtuosoItemPlaceholder />
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/src/components/entry-column/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export interface UniversalItemProps {
import type { EntryModel } from "@renderer/lib/types"

export type UniversalItemProps = {
entryId: string
entryPreview?: EntryModel
}

export type FilterTab = "all" | "unread"
4 changes: 2 additions & 2 deletions src/renderer/src/components/entry-column/video-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { useEntry } from "@renderer/store/entry"
import { ReactVirtuosoItemPlaceholder } from "../ui/placeholder"
import type { UniversalItemProps } from "./types"

export function VideoItem({ entryId }: UniversalItemProps) {
const entry = useEntry(entryId)
export function VideoItem({ entryId, entryPreview }: UniversalItemProps) {
const entry = useEntry(entryId) || entryPreview
if (!entry) return <ReactVirtuosoItemPlaceholder />
return (
<div className="flex">
Expand Down
34 changes: 31 additions & 3 deletions src/renderer/src/hono.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,6 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
images: string[] | null;
categories: string[] | null;
};
collections: {
createdAt: string;
};
feeds: {
description: string | null;
title: string | null;
Expand All @@ -312,6 +309,9 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
errorAt: string | null;
};
read: boolean | null;
collections?: {
createdAt: string;
} | undefined;
}[] | undefined;
};
outputFormat: "json";
Expand Down Expand Up @@ -364,6 +364,34 @@ declare const routes: hono_hono_base.HonoBase<hono.Env, {
status: 200;
};
};
"/entries/preview": {
$get: {
input: {
query: {
id: string;
};
};
output: {
code: 0;
data: {
description: string | null;
title: string | null;
content: string | null;
id: string;
url: string | null;
feedId: string;
guid: string;
author: string | null;
changedAt: string;
publishedAt: string;
images: string[] | null;
categories: string[] | null;
}[];
};
outputFormat: "json";
status: 200;
};
};
} & {
"/subscriptions": {
$get: {
Expand Down
35 changes: 24 additions & 11 deletions src/renderer/src/pages/(external)/feed/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { SocialMediaItem } from "@renderer/components/entry-column/social-media-
import type { UniversalItemProps } from "@renderer/components/entry-column/types"
import { VideoItem } from "@renderer/components/entry-column/video-item"
import { FeedIcon } from "@renderer/components/feed-icon"
import { levels } from "@renderer/lib/constants"
import { useEntries } from "@renderer/queries/entries"
import { useEntriesPreview } from "@renderer/queries/entries"
import { useFeed } from "@renderer/queries/feed"
import type { FC } from "react"
import { useParams } from "react-router-dom"
Expand All @@ -18,9 +17,8 @@ export function Component() {
const feed = useFeed({
id,
})
const entries = useEntries({
const entries = useEntriesPreview({
id,
level: levels.feed,
})

let Item: FC<UniversalItemProps>
Expand Down Expand Up @@ -61,14 +59,29 @@ export function Component() {
/>
<h1>{feed.data.feed.title}</h1>
</div>
<div className="mb-10 text-sm text-zinc-500">{feed.data.feed.description}</div>
<div className="mb-2 text-sm text-zinc-500">{feed.data.feed.description}</div>
<div className="mb-10 text-sm">
<strong>{feed.data.readCount}</strong>
{" "}
reads and
{" "}
<strong>{feed.data.subscriptionCount}</strong>
{" "}
subscriptions on Follow
</div>
<div className="w-full">
{entries.data?.pages.map((page) =>
page.data?.map((entry) => (
<a href={entry.entries.url || void 0} target="_blank" key={entry.entries.id}>
<Item entryId={entry.entries.id} />
</a>
)),
{entries.data?.map((entry) => (
<a href={entry.url || void 0} target="_blank" key={entry.id}>
<Item
entryId=""
entryPreview={{
entries: entry,
feeds: feed.data.feed,
read: false,
}}
/>
</a>
),
)}
</div>
</div>
Expand Down
29 changes: 27 additions & 2 deletions src/renderer/src/queries/entries.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UnprocessableEntityError } from "@renderer/biz/error"
import { useBizInfiniteQuery } from "@renderer/hooks/useBizQuery"
import { useBizInfiniteQuery, useBizQuery } from "@renderer/hooks/useBizQuery"
import { defineQuery } from "@renderer/lib/defineQuery"
import { apiClient } from "@renderer/queries/api-fetch"
import { entryActions } from "@renderer/store/entry"
Expand All @@ -19,7 +19,6 @@ export const entries = {
defineQuery(
["entries", level, id, view, read],
async ({ pageParam }) =>

entryActions.fetchEntries({
level,
id,
Expand Down Expand Up @@ -51,6 +50,23 @@ export const entries = {
rootKey: ["entries"],
},
),
preview: (id: string) =>
defineQuery(
["entries-preview", id],
async () => {
const res = await apiClient.entries.preview.$get({
query: {
id,
},
})
const json = await res.json()

return json.data
},
{
rootKey: ["entries-preview"],
},
),
}

export const useEntries = ({
Expand All @@ -74,3 +90,12 @@ export const useEntries = ({
},
initialPageParam: undefined,
})

export const useEntriesPreview = ({
id,
}: {
id?: string
}) =>
useBizQuery(entries.preview(id!), {
enabled: !!id,
})

0 comments on commit 28b6cce

Please sign in to comment.