feat: load comments by page

This commit is contained in:
DIYgod
2023-04-06 00:22:01 +01:00
parent efbead54cf
commit 653cf39d24
6 changed files with 99 additions and 123 deletions
+1 -1
View File
@@ -10,5 +10,5 @@
43625, 42787, 43258, 33462, 47109, 46939, 45701, 33233, 37346, 40671, 33870,
33421, 51552, 51585, 48937, 51379, 51460, 51084, 51509, 51478, 51639, 48910,
51139, 39329, 51683, 51077, 51374, 49120, 47356, 17096, 50708, 51430, 51935,
51756, 6558, 49392, 52186
51756, 6558, 49392, 52186, 52055, 52282, 52270, 5825, 51351, 52233
]
+14 -3
View File
@@ -48,7 +48,11 @@
"43000-23",
"50701-28",
"52186-11",
"52186-6"
"52186-6",
"52270-127",
"52258-1",
"52319-1",
"52299-1"
]
},
{
@@ -120,7 +124,9 @@
"77-115",
"51935-11",
"50622-16",
"50622-15"
"50622-15",
"33492-5",
"52242-1"
]
},
{
@@ -243,7 +249,12 @@
"48952-54",
"48952-53",
"48952-57",
"48952-48"
"48952-48",
"50351-30",
"52055-24",
"52055-21",
"51903-2",
"51683-125"
]
},
{
+38 -12
View File
@@ -4,6 +4,7 @@ import { useGetComments } from "~/queries/page"
import { CommentItem } from "~/components/common/CommentItem"
import { CommentInput } from "~/components/common/CommentInput"
import { useTranslation } from "next-i18next"
import InfiniteScroll from "react-infinite-scroller"
export const Comment: React.FC<{
page?: Note | null
@@ -18,25 +19,50 @@ export const Comment: React.FC<{
<div className={cn("xlog-comment", "comment", className)} id="comments">
<div className="xlog-comment-count border-b pb-2 mb-6">
<span>
{comments.data?.count || "0"}{" "}
{comments.isLoading
? t("Loading")
: comments.data?.pages?.[0]?.count || "0"}{" "}
{t(
`Comment${
comments.data?.count && comments.data.count > 1 ? "s" : ""
comments.data?.pages?.[0]?.count &&
comments.data.pages[0].count > 1
? "s"
: ""
}`,
)}
</span>
</div>
<CommentInput pageId={page?.id} />
<div className="xlog-comment-list space-y-6 pt-6">
{comments.data?.list?.map((comment) => (
<CommentItem
originalId={page?.id}
comment={comment}
key={comment.transactionHash}
depth={0}
/>
))}
</div>
<InfiniteScroll
className="xlog-comment-list space-y-6 pt-6"
loadMore={comments.fetchNextPage as any}
hasMore={comments.hasNextPage}
loader={
<div
className="relative mt-4 w-full text-sm text-center py-4"
key={"loading"}
>
{t("Loading")}...
</div>
}
>
{comments.isLoading ? (
<div className="relative mt-4 w-full text-sm text-center py-4">
{t("Loading")}...
</div>
) : (
comments.data?.pages?.map((p) =>
p?.list?.map((comment) => (
<CommentItem
originalId={page?.id}
comment={comment}
key={comment.transactionHash}
depth={0}
/>
)),
)
)}
</InfiniteScroll>
</div>
)
}
+18 -66
View File
@@ -458,30 +458,9 @@ export async function getLikes({
},
)
if (includeCharacter) {
await Promise.all(
res.list?.map(async (item) => {
if (
!item.fromCharacter?.metadata?.content &&
item.fromCharacter?.metadata?.uri
) {
try {
item.fromCharacter.metadata.content = (
await axios.get(toGateway(item.fromCharacter?.metadata?.uri), {
...(typeof window === "undefined" && {
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
},
}),
})
).data
} catch (error) {
console.warn(error)
}
}
;(<any>item).character = item.fromCharacter
}),
)
res.list?.forEach((item) => {
;(<any>item).character = item.fromCharacter
})
}
return res
@@ -532,32 +511,15 @@ export async function getMints({
pageId.split("-")[1],
{
cursor,
limit: 5,
},
)
if (includeCharacter) {
await Promise.all(
data.list.map(async (item: any) => {
const owner = item.owner
item.character = await indexer.getPrimaryCharacter(owner)
if (
!item.character?.metadata?.content &&
item.character?.metadata?.uri
) {
try {
item.character.metadata.content = (
await axios.get(toGateway(item.character?.metadata?.uri), {
...(typeof window === "undefined" && {
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
},
}),
})
).data
} catch (error) {
console.warn(error)
}
if (!item.character) {
item.character = await indexer.getPrimaryCharacter(item.owner)
}
}),
)
@@ -579,40 +541,30 @@ export async function checkMint({
})
}
export async function getComments({ pageId }: { pageId: string }) {
export async function getComments({
pageId,
cursor,
}: {
pageId: string
cursor?: string
}) {
const options = {
toCharacterId: pageId.split("-")[0],
toNoteId: pageId.split("-")[1],
cursor: "",
cursor,
includeCharacter: true,
includeNestedNotes: true,
nestedNotesDepth: 3 as 3,
nestedNotesLimit: 20,
limit: 5,
}
let pages: ListResponse<
NoteEntity & {
character?: CharacterEntity | null
}
> = {
count: 0,
const res = (await indexer.getNotes(options)) || {
total: 0,
list: [],
cursor: "",
}
let cursor = ""
do {
options.cursor = cursor
const res = (await indexer.getNotes(options)) || {
total: 0,
list: [],
}
pages.count = res.count
pages.list = pages.list.concat(res.list)
cursor = res.cursor || ""
} while (cursor)
return pages
return res
}
export function parsePageId(pageId: string) {
+16 -34
View File
@@ -158,43 +158,25 @@ export const getSites = async (input: number[]) => {
)
.toPromise()
await Promise.all(
result.data?.characters?.map(async (site: any) => {
if (!site?.metadata?.content && site?.metadata?.uri) {
try {
site.metadata.content = (
await axios.get(toGateway(site?.metadata?.uri), {
...(typeof window === "undefined" && {
headers: {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36",
},
}),
})
).data
} catch (error) {
console.warn(error)
}
}
if (site.metadata.content) {
site.metadata.content.name = site.metadata?.content?.name || site.handle
} else {
site.metadata.content = {
name: site.handle,
}
result.data?.characters?.forEach((site: any) => {
if (site.metadata.content) {
site.metadata.content.name = site.metadata?.content?.name || site.handle
} else {
site.metadata.content = {
name: site.handle,
}
}
site.custom_domain =
site.metadata?.content?.attributes?.find(
(a: any) => a.trait_type === "xlog_custom_domain",
)?.value || ""
site.custom_domain =
site.metadata?.content?.attributes?.find(
(a: any) => a.trait_type === "xlog_custom_domain",
)?.value || ""
site.updatedAt = [...site.notes, site]
.map((i) => i.updatedAt)
.sort()
.pop()
}),
)
site.updatedAt = [...site.notes, site]
.map((i) => i.updatedAt)
.sort()
.pop()
})
result.data?.characters?.sort((a: any, b: any) => {
return b.updatedAt > a.updatedAt ? 1 : -1
+12 -7
View File
@@ -234,13 +234,18 @@ export function useCommentPage() {
}
export function useGetComments(input: { pageId?: string }) {
return useQuery(["getComments", input.pageId], async () => {
if (!input.pageId) {
return
}
return pageModel.getComments({
pageId: input.pageId,
})
return useInfiniteQuery({
queryKey: ["getComments", input.pageId],
queryFn: async ({ pageParam }) => {
if (!input.pageId) {
return
}
return pageModel.getComments({
pageId: input.pageId,
cursor: pageParam,
})
},
getNextPageParam: (lastPage) => lastPage?.cursor || undefined,
})
}