|
|
@ -1,6 +1,7 @@
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { useEffect, useRef } from "react";
|
|
|
|
import { toast } from "react-hot-toast";
|
|
|
|
import { toast } from "react-hot-toast";
|
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
|
|
|
|
import MemoFilter from "@/components/MemoFilter";
|
|
|
|
import { DEFAULT_MEMO_LIMIT } from "@/helpers/consts";
|
|
|
|
import { DEFAULT_MEMO_LIMIT } from "@/helpers/consts";
|
|
|
|
import { getTimeStampByDate } from "@/helpers/datetime";
|
|
|
|
import { getTimeStampByDate } from "@/helpers/datetime";
|
|
|
|
import useCurrentUser from "@/hooks/useCurrentUser";
|
|
|
|
import useCurrentUser from "@/hooks/useCurrentUser";
|
|
|
@ -16,14 +17,14 @@ const MemoList: React.FC = () => {
|
|
|
|
const memoStore = useMemoStore();
|
|
|
|
const memoStore = useMemoStore();
|
|
|
|
const filterStore = useFilterStore();
|
|
|
|
const filterStore = useFilterStore();
|
|
|
|
const filter = filterStore.state;
|
|
|
|
const filter = filterStore.state;
|
|
|
|
const { memos } = memoStore.state;
|
|
|
|
const { loadingStatus, memos } = memoStore.state;
|
|
|
|
const [isFetching, setIsFetching] = useState<boolean>(true);
|
|
|
|
|
|
|
|
const [isComplete, setIsComplete] = useState<boolean>(false);
|
|
|
|
|
|
|
|
const user = useCurrentUser();
|
|
|
|
const user = useCurrentUser();
|
|
|
|
const { tag: tagQuery, duration, text: textQuery, visibility } = filter;
|
|
|
|
const { tag: tagQuery, duration, text: textQuery, visibility } = filter;
|
|
|
|
const showMemoFilter = Boolean(tagQuery || (duration && duration.from < duration.to) || textQuery || visibility);
|
|
|
|
const showMemoFilter = Boolean(tagQuery || (duration && duration.from < duration.to) || textQuery || visibility);
|
|
|
|
const username = params.username || user?.username || "";
|
|
|
|
const username = params.username || user?.username || "";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const fetchMoreRef = useRef<HTMLSpanElement>(null);
|
|
|
|
|
|
|
|
|
|
|
|
const shownMemos = (
|
|
|
|
const shownMemos = (
|
|
|
|
showMemoFilter
|
|
|
|
showMemoFilter
|
|
|
|
? memos.filter((memo) => {
|
|
|
|
? memos.filter((memo) => {
|
|
|
@ -74,74 +75,48 @@ const MemoList: React.FC = () => {
|
|
|
|
const sortedMemos = pinnedMemos.concat(unpinnedMemos).filter((m) => m.rowStatus === "NORMAL");
|
|
|
|
const sortedMemos = pinnedMemos.concat(unpinnedMemos).filter((m) => m.rowStatus === "NORMAL");
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
memoStore
|
|
|
|
const root = document.body.querySelector("#root");
|
|
|
|
.fetchMemos(username)
|
|
|
|
if (root) {
|
|
|
|
.then((fetchedMemos) => {
|
|
|
|
root.scrollTo(0, 0);
|
|
|
|
if (fetchedMemos.length < DEFAULT_MEMO_LIMIT) {
|
|
|
|
|
|
|
|
setIsComplete(true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
setIsComplete(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsFetching(false);
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.catch((error) => {
|
|
|
|
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
toast.error(error.response.data.message);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}, [user?.username]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
|
|
const pageWrapper = document.body.querySelector(".page-wrapper");
|
|
|
|
|
|
|
|
if (pageWrapper) {
|
|
|
|
|
|
|
|
pageWrapper.scrollTo(0, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [filter]);
|
|
|
|
}, [filter]);
|
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
useEffect(() => {
|
|
|
|
if (isFetching || isComplete) {
|
|
|
|
if (!fetchMoreRef.current) return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sortedMemos.length < DEFAULT_MEMO_LIMIT) {
|
|
|
|
|
|
|
|
handleFetchMoreClick();
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const observer = new IntersectionObserver(([entry]) => {
|
|
|
|
const observer = new IntersectionObserver(([entry]) => {
|
|
|
|
if (entry.isIntersecting) {
|
|
|
|
if (!entry.isIntersecting) return;
|
|
|
|
|
|
|
|
observer.disconnect();
|
|
|
|
handleFetchMoreClick();
|
|
|
|
handleFetchMoreClick();
|
|
|
|
observer.unobserve(entry.target);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}, [isFetching, isComplete, filter, sortedMemos.length]);
|
|
|
|
observer.observe(fetchMoreRef.current);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return () => observer.disconnect();
|
|
|
|
|
|
|
|
}, [loadingStatus]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleFetchMoreClick = async () => {
|
|
|
|
const handleFetchMoreClick = async () => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
setIsFetching(true);
|
|
|
|
await memoStore.fetchMemos(username, DEFAULT_MEMO_LIMIT, memos.length);
|
|
|
|
const fetchedMemos = await memoStore.fetchMemos(username, DEFAULT_MEMO_LIMIT, memos.length);
|
|
|
|
|
|
|
|
if (fetchedMemos.length < DEFAULT_MEMO_LIMIT) {
|
|
|
|
|
|
|
|
setIsComplete(true);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
setIsComplete(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
setIsFetching(false);
|
|
|
|
|
|
|
|
} catch (error: any) {
|
|
|
|
} catch (error: any) {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
toast.error(error.response.data.message);
|
|
|
|
toast.error(error.response.data.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
return (
|
|
|
|
<div className="flex flex-col justify-start items-start w-full max-w-full overflow-y-scroll pb-28 hide-scrollbar">
|
|
|
|
<div className="flex flex-col justify-start items-start w-full max-w-full overflow-y-scroll pb-28 hide-scrollbar">
|
|
|
|
|
|
|
|
<MemoFilter />
|
|
|
|
{sortedMemos.map((memo) => (
|
|
|
|
{sortedMemos.map((memo) => (
|
|
|
|
<Memo key={`${memo.id}-${memo.displayTs}`} memo={memo} lazyRendering showVisibility showPinnedStyle />
|
|
|
|
<Memo key={memo.id} memo={memo} lazyRendering showVisibility showPinnedStyle />
|
|
|
|
))}
|
|
|
|
))}
|
|
|
|
{isFetching ? (
|
|
|
|
|
|
|
|
|
|
|
|
{loadingStatus === "fetching" ? (
|
|
|
|
<div className="flex flex-col justify-start items-center w-full mt-2 mb-1">
|
|
|
|
<div className="flex flex-col justify-start items-center w-full mt-2 mb-1">
|
|
|
|
<p className="text-sm text-gray-400 italic">{t("memo.fetching-data")}</p>
|
|
|
|
<p className="text-sm text-gray-400 italic">{t("memo.fetching-data")}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
) : (
|
|
|
|
<div className="flex flex-col justify-start items-center w-full my-6">
|
|
|
|
<div className="flex flex-col justify-start items-center w-full my-6">
|
|
|
|
<div className="text-sm text-gray-400 italic">
|
|
|
|
<div className="text-sm text-gray-400 italic">
|
|
|
|
{isComplete ? (
|
|
|
|
{loadingStatus === "complete" ? (
|
|
|
|
sortedMemos.length === 0 && (
|
|
|
|
sortedMemos.length === 0 && (
|
|
|
|
<div className="w-full mt-12 mb-8 flex flex-col justify-center items-center italic">
|
|
|
|
<div className="w-full mt-12 mb-8 flex flex-col justify-center items-center italic">
|
|
|
|
<Empty />
|
|
|
|
<Empty />
|
|
|
@ -149,11 +124,9 @@ const MemoList: React.FC = () => {
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) : (
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<span ref={fetchMoreRef} className="cursor-pointer hover:text-green-600" onClick={handleFetchMoreClick}>
|
|
|
|
<span className="cursor-pointer hover:text-green-600" onClick={handleFetchMoreClick}>
|
|
|
|
|
|
|
|
{t("memo.fetch-more")}
|
|
|
|
{t("memo.fetch-more")}
|
|
|
|
</span>
|
|
|
|
</span>
|
|
|
|
</>
|
|
|
|
|
|
|
|
)}
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|