Skip to content

Commit

Permalink
feat: polling entry reading history
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jul 28, 2024
1 parent 5ff0d3d commit 3681810
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/renderer/src/lib/api-fetch.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from "@env"
import { getCsrfToken } from "@hono/auth-js/react"
import PKG from "@pkg"
import { NetworkStatus, setApiStatus } from "@renderer/atoms/network"
Expand All @@ -8,7 +9,7 @@ import type { AppType } from "src/hono"

let csrfTokenPromise: Promise<string> | null = null
export const apiFetch = ofetch.create({
baseURL: import.meta.env.VITE_API_URL,
baseURL: env.VITE_API_URL,
credentials: "include",
retry: false,
onRequest: async ({ options }) => {
Expand Down Expand Up @@ -70,7 +71,7 @@ export const apiFetch = ofetch.create({
},
})

export const apiClient = hc<AppType>("", {
export const apiClient = hc<AppType>(env.VITE_API_URL, {
fetch: async (input, options = {}) => apiFetch(input.toString(), options).catch(
(err) => {
if (err instanceof FetchError && !err.response) {
Expand Down
8 changes: 7 additions & 1 deletion src/renderer/src/modules/entry-content/read-history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
TooltipContent,
TooltipTrigger,
} from "@renderer/components/ui/tooltip"
import { useAuthQuery } from "@renderer/hooks/common"
import { Queries } from "@renderer/queries"
import { useEntryReadHistory } from "@renderer/store/entry"
import { useUserById } from "@renderer/store/user"
import { Fragment } from "react"
Expand All @@ -21,6 +23,10 @@ export const EntryReadHistory: Component<{ entryId: string }> = ({
const me = useMe()
const entryHistory = useEntryReadHistory(entryId)

useAuthQuery(Queries.entries.entryReadingHistory(entryId), {
refetchInterval: 1000 * 60,
})

if (!entryHistory) return null
if (!me) return null
if (!entryHistory.userIds) return null
Expand Down Expand Up @@ -80,7 +86,7 @@ const EntryUser: Component<{
}}
>
<button
className="no-drag-region"
className="no-drag-region cursor-pointer"
type="button"
onClick={() => {
presentUserProfile(userId)
Expand Down
10 changes: 10 additions & 0 deletions src/renderer/src/queries/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { apiClient } from "@renderer/lib/api-fetch"
import { defineQuery } from "@renderer/lib/defineQuery"
import { getEntriesParams } from "@renderer/lib/utils"
import { entryActions } from "@renderer/store/entry"
import { entryHistoryActions } from "@renderer/store/entry-history/action"

export const entries = {
entries: ({
Expand Down Expand Up @@ -88,6 +89,15 @@ export const entries = {
rootKey: ["entry-checkNew", id],
},
),

entryReadingHistory: (entryId: string) =>
defineQuery(
["entry-reading-history", entryId],
async () => entryHistoryActions.fetchEntryHistory(entryId),
{
rootKey: ["entry-reading-history", entryId],
},
),
}

export const useEntries = ({
Expand Down
27 changes: 27 additions & 0 deletions src/renderer/src/store/entry-history/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { apiClient } from "@renderer/lib/api-fetch"
import type { UserModel } from "@renderer/models"

import { entryActions } from "../entry/store"
import { userActions } from "../user"

class EntryHistoryActions {
async fetchEntryHistory(entryId: string) {
const res = await apiClient.entries["read-histories"][entryId].$get()

const data = res.data as {
users: Record<string, UserModel>
entryReadHistories: {
entryId: string
userIds: string[]
readCount: number
}
}

userActions.upsert(data.users)
entryActions.updateReadHistory(entryId, data.entryReadHistories)

return data
}
}

export const entryHistoryActions = new EntryHistoryActions()

0 comments on commit 3681810

Please sign in to comment.