import { useEffect, useState } from 'react'; import { AllTransactionListProps, ListItemProps } from '../model/types'; import { ListDateGroup } from './list-date-group'; import { GetListHeight, IMAGE_ROOT, ListScrollOn } from '@/shared/constants/common'; import { useTranslation } from 'react-i18next'; import { useGroupDateOnStore, useGroupDateStore } from '@/shared/model/store'; export const AllTransactionList = ({ transactionCategory, listItems, setDetailData, onClickToOpenFilter, onClickToOpenDownloadBottomSheet }: AllTransactionListProps) => { const { t, i18n } = useTranslation(); const { groupDate, setGroupDate } = useGroupDateStore(); const { groupDateOn, setGroupDateOn } = useGroupDateOnStore(); const [listHeight, setListHeight] = useState(0); const getListDateGroup = () => { let rs = []; let date = ''; let list: Array = []; for(let i=0;i 0){ rs.push( ); } date = transactionDate; list = []; } list.push(item); } } } if(list.length > 0){ rs.push( ); } return rs; }; const getMax = (data: Array>) => { let maxItem = null; if(data.length > 0){ let numberArr = data.map(( value: Record, index: number ) => { return value.top; }); let max = Math.max(...numberArr); maxItem = data.filter(( value: Record, index: number ) => { return value.top === max; }); } return maxItem? maxItem[0]: null; }; const setScrollAction = (e: Event) => { let dateHeader = document.querySelectorAll('.date-header'); let posData: Array> = []; dateHeader.forEach((value, index) => { let date: string = value.innerHTML; let top: number = value.getBoundingClientRect().top; if(top < 10){ posData.push({ date: date, top: top }); } }); let maxItem = getMax(posData); if(maxItem){ setGroupDateOn(true); setGroupDate(maxItem.date); } else{ setGroupDateOn(false); setGroupDate(''); } }; useEffect(() => { ListScrollOn(true); let heightList = GetListHeight(); setListHeight(heightList.listHeight); let tabContent = document.querySelector('.tab-content'); tabContent?.addEventListener('scroll', setScrollAction); return () => { ListScrollOn(false); tabContent?.removeEventListener('scroll', setScrollAction); }; }, []); return ( <> { groupDateOn &&
{ groupDate }
}
0)? listHeight + 'px': 'unset' }} > { getListDateGroup() }
); };