import React from 'react';
import { useTranslation } from 'next-i18next';
import Link from 'next/link';
import useSWRInfinite, { SWRInfiniteResponse } from 'swr/infinite';
import { apiv3Get } from '~/client/util/apiv3-client';
import { IPageHasId } from '~/interfaces/page';
import { useCurrentPagePath } from '~/stores/page';
import { useTimelineOptions } from '~/stores/renderer';
import InfiniteScroll from './InfiniteScroll';
import { RevisionLoader } from './Page/RevisionLoader';
import styles from './PageTimeline.module.scss';
type TimelineCardProps = {
page: IPageHasId,
}
const TimelineCard = ({ page }: TimelineCardProps): JSX.Element => {
const { data: rendererOptions } = useTimelineOptions(page.path);
return (
{page.path}
{ rendererOptions != null && (
) }
);
};
type PageTimelineResult = {
pages: IPageHasId[],
totalCount: number,
offset: number,
}
const useSWRINFxPageTimeline = (path: string | undefined, limit: number) : SWRInfiniteResponse => {
return useSWRInfinite(
(pageIndex, previousPageData) => {
if (previousPageData != null && previousPageData.pages.length === 0) return null;
if (path === undefined) return null;
return ['/pages/list', path, pageIndex + 1, limit];
},
([endpoint, path, page, limit]) => apiv3Get(endpoint, { path, page, limit }).then(response => response.data),
{
revalidateFirstPage: false,
revalidateAll: false,
},
);
};
export const PageTimeline = (): JSX.Element => {
const PER_PAGE = 3;
const { t } = useTranslation();
const { data: currentPagePath } = useCurrentPagePath();
const swrInfinitexPageTimeline = useSWRINFxPageTimeline(currentPagePath, PER_PAGE);
const { data } = swrInfinitexPageTimeline;
const isEmpty = data?.[0]?.pages.length === 0;
const isReachingEnd = isEmpty || (data != null && data[data.length - 1]?.pages.length < PER_PAGE);
if (data == null || isEmpty) {
return (
{/* eslint-disable-next-line react/no-danger */}
{t('custom_navigation.no_page_list')}
);
}
return (
{ data != null && data.flatMap(apiResult => apiResult.pages)
.map(page => (
))
}
);
};