feat: comments pagination loading

This commit is contained in:
DIYgod
2022-09-16 00:55:33 +01:00
parent 03913ffcaf
commit ec09ed7342
2 changed files with 34 additions and 15 deletions
+3 -3
View File
@@ -81,8 +81,8 @@ export const Comment: React.FC<{
<div className={clsx("xlog-comment", "comment", className)}>
<div className="xlog-comment-count border-b pb-2 mb-6">
<span>
{comments.data?.length || "0"} Comment
{comments.data?.length && comments.data.length > 1 ? "s" : ""}
{comments.data?.count || "0"} Comment
{comments.data?.count && comments.data.count > 1 ? "s" : ""}
</span>
</div>
<div className="xlog-comment-input flex">
@@ -145,7 +145,7 @@ export const Comment: React.FC<{
</form>
</div>
<div className="xlog-comment-list">
{comments.data?.map((comment) => (
{comments.data?.list?.map((comment) => (
<div
key={comment.transactionHash}
className="flex border-b border-dashed py-6"
+31 -12
View File
@@ -3,7 +3,7 @@ import { notFound } from "~/lib/server-side-props"
import { PageVisibilityEnum, Notes, Note } from "~/lib/types"
import unidata from "~/lib/unidata"
import { indexer, getContract } from "~/lib/crossbell"
import { NoteEntity, CharacterEntity } from "crossbell.js"
import { NoteEntity, CharacterEntity, ListResponse } from "crossbell.js"
import { getStorage, getKeys } from "~/lib/storage"
import axios from "axios"
import { toGateway } from "~/lib/ipfs-parser"
@@ -537,22 +537,41 @@ export async function commentPage({
}
export async function getComments({ pageId }: { pageId: string }) {
const list: (NoteEntity & {
character?: CharacterEntity | null
})[] = (
await indexer.getNotes({
toCharacterId: pageId.split("-")[0],
toNoteId: pageId.split("-")[1],
sources: ["xlog"],
})
).list
const options = {
toCharacterId: pageId.split("-")[0],
toNoteId: pageId.split("-")[1],
sources: ["xlog"],
cursor: "",
}
let pages: ListResponse<
NoteEntity & {
character?: CharacterEntity | null
}
> = {
count: 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)
await Promise.all(
list.map(async (item) => {
pages.list.map(async (item) => {
const characterId = item.characterId
item.character = await indexer.getCharacter(characterId)
}),
)
return list
return pages
}