mirror of https://github.com/usememos/memos.git
fix: add load-more button and pagination to attachments page
This commit is contained in:
parent
4de8712cb0
commit
c9d44c0526
|
|
@ -8,6 +8,7 @@ import Empty from "@/components/Empty";
|
|||
import MobileHeader from "@/components/MobileHeader";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { attachmentServiceClient } from "@/grpcweb";
|
||||
import useLoading from "@/hooks/useLoading";
|
||||
import useResponsiveWidth from "@/hooks/useResponsiveWidth";
|
||||
|
|
@ -42,18 +43,51 @@ const Attachments = observer(() => {
|
|||
searchQuery: "",
|
||||
});
|
||||
const [attachments, setAttachments] = useState<Attachment[]>([]);
|
||||
const [nextPageToken, setNextPageToken] = useState("");
|
||||
const [isLoadingMore, setIsLoadingMore] = useState(false);
|
||||
const filteredAttachments = attachments.filter((attachment) => includes(attachment.filename, state.searchQuery));
|
||||
const groupedAttachments = groupAttachmentsByDate(filteredAttachments.filter((attachment) => attachment.memo));
|
||||
const unusedAttachments = filteredAttachments.filter((attachment) => !attachment.memo);
|
||||
|
||||
useEffect(() => {
|
||||
attachmentServiceClient.listAttachments({}).then(({ attachments }) => {
|
||||
setAttachments(attachments);
|
||||
loadingState.setFinish();
|
||||
Promise.all(attachments.map((attachment) => (attachment.memo ? memoStore.getOrFetchMemoByName(attachment.memo) : null)));
|
||||
const fetchInitialAttachments = async () => {
|
||||
try {
|
||||
const { attachments: fetchedAttachments, nextPageToken } = await attachmentServiceClient.listAttachments({
|
||||
pageSize: 50,
|
||||
});
|
||||
setAttachments(fetchedAttachments);
|
||||
setNextPageToken(nextPageToken ?? "");
|
||||
await Promise.all(
|
||||
fetchedAttachments.map((attachment) => (attachment.memo ? memoStore.getOrFetchMemoByName(attachment.memo) : null)),
|
||||
);
|
||||
} finally {
|
||||
loadingState.setFinish();
|
||||
}
|
||||
};
|
||||
|
||||
fetchInitialAttachments();
|
||||
}, []);
|
||||
|
||||
const handleLoadMore = async () => {
|
||||
if (!nextPageToken) {
|
||||
return;
|
||||
}
|
||||
setIsLoadingMore(true);
|
||||
try {
|
||||
const { attachments: fetchedAttachments, nextPageToken: newPageToken } = await attachmentServiceClient.listAttachments({
|
||||
pageSize: 50,
|
||||
pageToken: nextPageToken,
|
||||
});
|
||||
setAttachments((prevAttachments) => [...prevAttachments, ...fetchedAttachments]);
|
||||
setNextPageToken(newPageToken ?? "");
|
||||
await Promise.all(
|
||||
fetchedAttachments.map((attachment) => (attachment.memo ? memoStore.getOrFetchMemoByName(attachment.memo) : null)),
|
||||
);
|
||||
} finally {
|
||||
setIsLoadingMore(false);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<section className="@container w-full max-w-5xl min-h-full flex flex-col justify-start items-center sm:pt-3 md:pt-6 pb-8">
|
||||
{!md && <MobileHeader />}
|
||||
|
|
@ -89,6 +123,7 @@ const Attachments = observer(() => {
|
|||
<p className="mt-4 text-muted-foreground">{t("message.no-data")}</p>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<div className={"w-full h-auto px-2 flex flex-col justify-start items-start gap-y-8"}>
|
||||
{Array.from(groupedAttachments.entries()).map(([monthStr, attachments]) => {
|
||||
return (
|
||||
|
|
@ -144,6 +179,14 @@ const Attachments = observer(() => {
|
|||
</>
|
||||
)}
|
||||
</div>
|
||||
{nextPageToken && (
|
||||
<div className="w-full flex flex-row justify-center items-center mt-4">
|
||||
<Button variant="outline" size="sm" onClick={handleLoadMore} disabled={isLoadingMore}>
|
||||
{isLoadingMore ? t("resource.fetching-data") : t("memo.load-more")}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
|
|
|||
Loading…
Reference in New Issue