feat: useReportStats

This commit is contained in:
DIYgod
2023-06-13 17:12:51 +01:00
parent 59c8c7a5fa
commit f888c4e4af
4 changed files with 89 additions and 7 deletions
+6
View File
@@ -9,6 +9,7 @@ import { useHash } from "~/hooks/useHash"
import { ExpandedCharacter, ExpandedNote } from "~/lib/types"
import { cn, scrollTo } from "~/lib/utils"
import { renderPageContent } from "~/markdown"
import { useReportStats } from "~/queries/page"
export const PageContent = ({
className,
@@ -53,6 +54,11 @@ export const PageContent = ({
scrollTo(hash)
}, [hash])
useReportStats({
characterId: page?.characterId,
noteId: page?.noteId,
})
return (
<div
className={cn("relative", className)}
+12 -6
View File
@@ -72,12 +72,18 @@ export const expandCrossbellNote = async ({
)
if (useStat) {
const stat = await (
await fetch(
`https://indexer.crossbell.io/v1/stat/notes/${expandedNote.characterId}/${expandedNote.noteId}`,
)
).json()
expandedNote.metadata.content.views = stat.viewDetailCount
if ((note as any).stat?.viewDetailCount) {
expandedNote.metadata.content.views = (
note as any
).stat?.viewDetailCount
} else {
const stat = await (
await fetch(
`https://indexer.crossbell.io/v1/stat/notes/${expandedNote.characterId}/${expandedNote.noteId}`,
)
).json()
expandedNote.metadata.content.views = stat.viewDetailCount
}
}
if (useScore) {
+54 -1
View File
@@ -465,6 +465,13 @@ export async function getPage<TRender extends boolean = false>(input: {
blockNumber
updatedTransactionHash
updatedBlockNumber
${
input.useStat
? `stat {
viewDetailCount
}`
: ""
}
}
}`,
{},
@@ -472,7 +479,46 @@ export async function getPage<TRender extends boolean = false>(input: {
.toPromise()
page = result.data.notes[0]
} else {
page = await indexer.note.get(input.characterId, input.noteId)
const result = await client
.query(
`
query getNote {
note(
where: {
note_characterId_noteId_unique: {
characterId: ${input.characterId},
noteId: ${input.noteId},
},
},
) {
characterId
noteId
uri
metadata {
uri
content
}
owner
createdAt
updatedAt
publishedAt
transactionHash
blockNumber
updatedTransactionHash
updatedBlockNumber
${
input.useStat
? `stat {
viewDetailCount
}`
: ""
}
}
}`,
{},
)
.toPromise()
page = result.data.note
}
}
@@ -522,6 +568,13 @@ export async function getPage<TRender extends boolean = false>(input: {
return expandedNote
}
export async function reportStats(input: {
characterId: number
noteId: number
}) {
return await indexer.note.get(input.characterId, input.noteId)
}
export async function deletePage(
{ site, id }: { site: string; id: string },
customUnidata?: Unidata,
+17
View File
@@ -434,3 +434,20 @@ export function useGetImageInfo(src?: string) {
}
})
}
export const useReportStats = (
input: Partial<Parameters<typeof pageModel.reportStats>[0]>,
) => {
return useQuery(
["reportStats", input.characterId, input.noteId],
async () => {
if (!input.characterId || !input.noteId) {
return null
}
return pageModel.reportStats({
characterId: input.characterId,
noteId: input.noteId,
})
},
)
}