import { useRequest } from "ahooks"; import { Empty, Spin } from "antd"; import { useCallback, useEffect, useRef } from "react"; import { useTranslation } from "react-i18next"; import { GroupedVirtuoso, GroupedVirtuosoHandle } from "react-virtuoso"; import { useContactStore } from "@/store"; import { formatContactsByWorker } from "@/utils/contactsFormat"; import { emit } from "@/utils/events"; import AlphabetIndex from "./AlphabetIndex"; import FriendListItem from "./FriendListItem"; export const MyFriends = () => { const { t } = useTranslation(); const friendList = useContactStore((state) => state.friendList); const virtuoso = useRef(null); const alphabetRef = useRef<{ updateCurrentLetter: (letter: string) => void }>(null); const { data: sectionData, cancel } = useRequest( () => formatContactsByWorker(friendList), { refreshDeps: [friendList], }, ); useEffect(() => { return () => { cancel(); }; }, []); const scrollToLetter = useCallback( (idx: number) => { const prevNum = sectionData?.groupCounts.slice(0, idx).reduce((a, b) => a + b, 0); console.log(prevNum); virtuoso.current?.scrollToIndex({ index: prevNum ?? 0, // behavior: "smooth", }); }, [sectionData?.groupCounts], ); const showUserCard = useCallback((userID: string) => { emit("OPEN_USER_CARD", { userID, }); }, []); const determineCurrentGroup = (startIndex: number) => { if (!sectionData) return; let currentItemIndex = 0; for ( let groupIndex = 0; groupIndex < sectionData.groupCounts.length; groupIndex++ ) { const groupItemCount = sectionData.groupCounts[groupIndex]; if (startIndex < currentItemIndex + groupItemCount) { alphabetRef.current?.updateCurrentLetter(sectionData.indexList[groupIndex]); break; } currentItemIndex += groupItemCount; } }; return (
{t("placeholder.myFriend")}
{!sectionData ? ( ) : !sectionData.groupCounts.length ? ( ) : (
(
{sectionData.indexList[index]}
)} itemContent={(index) => { return ( ); }} rangeChanged={({ startIndex }) => determineCurrentGroup(startIndex)} className="no-scrollbar h-full overflow-x-hidden" />
)}
); };