import { FC, memo, useMemo, useRef, useState } from 'react' import { useAnalyticsData } from '@app/data/external/analytics/analytics' import { InnerWrapper } from '../../list-wrapper' import { Button } from '../../../buttons' import { AnalyticsList } from './analytics-list' import type { Analytic } from '@app/types' import { GrFormNextLink, GrFormPreviousLink } from 'react-icons/gr' type AnalyticsPagingProps = { pageUrl?: string liveData?: Analytic[] open?: boolean } // paging issues for website dashboard cell const RenderInnerAnalyticsWrapper: FC = ({ liveData, pageUrl, open: defaultOpen, }) => { const [issueIndex, setIndex] = useState(0) const { data, loading, onLoadMore } = useAnalyticsData(pageUrl, false) // todo: use onComplete callback for next page const offsetTracking = useRef<{ last: number; next: number }>({ last: 0, next: 0, }) const { issueSource, liveDataExist } = useMemo(() => { const liveDataExist = liveData?.length return { issueSource: (liveDataExist ? liveData : (data as Analytic[])) || [], liveDataExist, } }, [liveData, data]) const [issueList, stats] = useMemo(() => { let base = (issueIndex + 1) * 10 if (liveDataExist && issueIndex) { // set the last offset value offsetTracking.current.last = offsetTracking.current.next base += offsetTracking.current.next } const items: Analytic[] = new Array(Math.max(issueSource.length, 10)) let errorCount = 0 let warningCount = 0 // iterator let j = 0 let i = base - 10 // offset position let offsetTracker = 0 // loop until ten items filled for (; j < 10; i++) { const item = issueSource[i] if (!item || j === 10) { break } if (item.errorCount || item.warningCount) { items[j] = item errorCount += item.errorCount warningCount += item.warningCount j++ } else { offsetTracker += 1 } } offsetTracking.current.next = offsetTracker items.length = j return [items, { errorCount, warningCount }] }, [issueIndex, issueSource, liveDataExist, offsetTracking]) const onPrevSelect = () => { if (issueIndex) { if (liveDataExist) { offsetTracking.current.next = offsetTracking.current.last } setIndex((x: number) => x - 1) } } const idx = (issueIndex + 1) * 10 const onLoadEvent = async () => { // get the next set of data if (idx === issueSource.length) { onLoadMore && (await onLoadMore()) } setIndex((x: number) => x + 1) } return ( <>
    {issueList.map((page) => ( ))}
1 ? '' : 'hidden' } text-right flex place-items-center place-content-end p-2 gap-x-2`} >
) } export const RenderInnerAnalyticsPaging = memo(RenderInnerAnalyticsWrapper)