2021-09-28 00:41:12 +03:30
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-09-28 00:41:12 +03:30
|
|
|
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2025-01-21 20:34:17 +01:00
|
|
|
import React, { useCallback, useLayoutEffect, useMemo, useRef, useState } from 'react';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { useLingui } from '@lingui/react/macro';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { LoadingPlaceholder } from '@/base/components/feedback/LoadingPlaceholder.tsx';
|
|
|
|
|
import { EmptyViewAbsoluteCentered } from '@/base/components/feedback/EmptyViewAbsoluteCentered.tsx';
|
2025-08-15 22:07:33 +02:00
|
|
|
import { UpdateChecker } from '@/features/updates/components/UpdateChecker.tsx';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { StyledGroupedVirtuoso } from '@/base/components/virtuoso/StyledGroupedVirtuoso.tsx';
|
|
|
|
|
import { StyledGroupHeader } from '@/base/components/virtuoso/StyledGroupHeader.tsx';
|
|
|
|
|
import { StyledGroupItemWrapper } from '@/base/components/virtuoso/StyledGroupItemWrapper.tsx';
|
|
|
|
|
import { dateTimeFormatter } from '@/base/utils/DateHelper.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-10-12 18:41:42 +02:00
|
|
|
import { VirtuosoUtil } from '@/lib/virtuoso/Virtuoso.util.tsx';
|
2024-12-20 17:24:40 +01:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { ChapterUpdateCard } from '@/features/updates/components/ChapterUpdateCard.tsx';
|
2025-08-16 02:14:49 +02:00
|
|
|
import { useNavBarContext } from '@/features/navigation-bar/NavbarContext.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { Chapters } from '@/features/chapter/services/Chapters.ts';
|
|
|
|
|
import { useAppTitleAndAction } from '@/features/navigation-bar/hooks/useAppTitleAndAction.ts';
|
2025-07-23 01:37:47 +02:00
|
|
|
import { GROUPED_VIRTUOSO_Z_INDEX } from '@/lib/virtuoso/Virtuoso.constants.ts';
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export const Updates: React.FC = () => {
|
2026-01-10 19:45:02 +01:00
|
|
|
const { t } = useLingui();
|
2025-04-30 02:40:05 +02:00
|
|
|
const { appBarHeight } = useNavBarContext();
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-05-18 13:48:01 +02:00
|
|
|
const {
|
2023-09-30 16:55:35 +02:00
|
|
|
data: chapterUpdateData,
|
|
|
|
|
loading: isLoading,
|
2024-05-05 00:50:15 +02:00
|
|
|
error,
|
2023-09-30 16:55:35 +02:00
|
|
|
fetchMore,
|
2024-05-05 00:50:15 +02:00
|
|
|
refetch,
|
2023-09-30 16:55:35 +02:00
|
|
|
} = requestManager.useGetRecentlyUpdatedChapters(undefined, {
|
|
|
|
|
fetchPolicy: 'cache-and-network',
|
|
|
|
|
notifyOnNetworkStatusChange: true,
|
|
|
|
|
});
|
|
|
|
|
const hasNextPage = !!chapterUpdateData?.chapters.pageInfo.hasNextPage;
|
|
|
|
|
const endCursor = chapterUpdateData?.chapters.pageInfo.endCursor;
|
2023-10-15 16:03:08 +02:00
|
|
|
const updateEntries = chapterUpdateData?.chapters.nodes ?? [];
|
2025-03-10 00:26:31 +01:00
|
|
|
const groupedUpdates = useMemo(
|
2025-03-10 01:10:00 +01:00
|
|
|
() => Object.entries(Chapters.groupByDate(updateEntries, 'fetchedAt')),
|
2025-03-10 00:26:31 +01:00
|
|
|
[updateEntries],
|
|
|
|
|
);
|
2025-03-10 01:10:00 +01:00
|
|
|
const groupCounts: number[] = useMemo(
|
|
|
|
|
() => groupedUpdates.map((group) => group[VirtuosoUtil.ITEMS].length),
|
|
|
|
|
[groupedUpdates],
|
|
|
|
|
);
|
2025-01-12 14:09:10 +01:00
|
|
|
|
2024-10-12 18:41:42 +02:00
|
|
|
const computeItemKey = VirtuosoUtil.useCreateGroupedComputeItemKey(
|
|
|
|
|
groupCounts,
|
2025-03-10 01:10:00 +01:00
|
|
|
useCallback((index) => groupedUpdates[index][VirtuosoUtil.GROUP], [groupedUpdates]),
|
2024-10-12 18:41:42 +02:00
|
|
|
useCallback((index) => updateEntries[index].id, [updateEntries]),
|
|
|
|
|
);
|
|
|
|
|
|
2023-11-25 15:39:39 +01:00
|
|
|
const lastUpdateTimestampCompRef = useRef<HTMLElement>(null);
|
|
|
|
|
const [lastUpdateTimestampCompHeight, setLastUpdateTimestampCompHeight] = useState(0);
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
|
setLastUpdateTimestampCompHeight(lastUpdateTimestampCompRef.current?.clientHeight ?? 0);
|
|
|
|
|
}, [lastUpdateTimestampCompRef.current]);
|
|
|
|
|
|
2023-11-25 15:35:36 +01:00
|
|
|
const { data: lastUpdateTimestampData } = requestManager.useGetLastGlobalUpdateTimestamp({
|
|
|
|
|
/**
|
|
|
|
|
* The {@link UpdateChecker} is responsible for updating the timestamp
|
|
|
|
|
*/
|
|
|
|
|
fetchPolicy: 'cache-only',
|
|
|
|
|
});
|
2023-11-19 22:43:29 +01:00
|
|
|
const lastUpdateTimestamp = lastUpdateTimestampData?.lastUpdateTimestamp.timestamp;
|
2026-01-10 19:45:02 +01:00
|
|
|
const date = lastUpdateTimestamp ? dateTimeFormatter.format(+lastUpdateTimestamp) : '-';
|
2023-11-19 22:43:29 +01:00
|
|
|
|
2026-01-10 19:45:02 +01:00
|
|
|
useAppTitleAndAction(t`Updates`, <UpdateChecker />);
|
2021-09-28 00:41:12 +03:30
|
|
|
|
2023-05-18 13:48:01 +02:00
|
|
|
const loadMore = useCallback(() => {
|
|
|
|
|
if (!hasNextPage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-30 16:55:35 +02:00
|
|
|
fetchMore({ variables: { offset: updateEntries.length } });
|
|
|
|
|
}, [hasNextPage, endCursor]);
|
2023-05-18 13:48:01 +02:00
|
|
|
|
2024-05-05 00:50:15 +02:00
|
|
|
if (error) {
|
|
|
|
|
return (
|
|
|
|
|
<EmptyViewAbsoluteCentered
|
2026-01-10 19:45:02 +01:00
|
|
|
message={t`Unable to load data`}
|
2024-12-21 23:27:03 +01:00
|
|
|
messageExtra={getErrorMessage(error)}
|
2024-05-05 00:50:15 +02:00
|
|
|
retry={() => refetch().catch(defaultPromiseErrorHandler('Updates::refetch'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 03:40:41 +08:00
|
|
|
if (!isLoading && updateEntries.length === 0) {
|
2026-01-10 19:45:02 +01:00
|
|
|
return <EmptyViewAbsoluteCentered message={t`You don't have any updates yet.`} />;
|
2023-06-23 03:40:41 +08:00
|
|
|
}
|
|
|
|
|
|
2021-09-28 00:41:12 +03:30
|
|
|
return (
|
2023-11-25 15:39:39 +01:00
|
|
|
<>
|
|
|
|
|
<Typography
|
|
|
|
|
ref={lastUpdateTimestampCompRef}
|
|
|
|
|
sx={{
|
2025-04-30 02:40:05 +02:00
|
|
|
position: 'sticky',
|
|
|
|
|
top: appBarHeight,
|
|
|
|
|
zIndex: GROUPED_VIRTUOSO_Z_INDEX,
|
|
|
|
|
backgroundColor: 'background.default',
|
2023-11-25 15:39:39 +01:00
|
|
|
marginLeft: '10px',
|
|
|
|
|
paddingTop: (theme) => ({ [theme.breakpoints.up('sm')]: { paddingTop: '6px' } }),
|
|
|
|
|
}}
|
|
|
|
|
>
|
2026-01-10 19:45:02 +01:00
|
|
|
{t`Last update: ${date}`}
|
2023-11-25 15:39:39 +01:00
|
|
|
</Typography>
|
|
|
|
|
<StyledGroupedVirtuoso
|
2025-05-25 15:36:25 +02:00
|
|
|
persistKey="updates"
|
2023-11-25 15:39:39 +01:00
|
|
|
heightToSubtract={lastUpdateTimestampCompHeight}
|
|
|
|
|
components={{
|
|
|
|
|
Footer: () => (isLoading ? <LoadingPlaceholder usePadding /> : null),
|
|
|
|
|
}}
|
|
|
|
|
overscan={window.innerHeight * 0.5}
|
|
|
|
|
endReached={loadMore}
|
|
|
|
|
groupCounts={groupCounts}
|
|
|
|
|
groupContent={(index) => (
|
2024-12-26 02:29:58 +01:00
|
|
|
<StyledGroupHeader isFirstItem={index === 0}>
|
2024-11-04 01:26:58 +01:00
|
|
|
<Typography variant="h5" component="h2">
|
2025-03-10 01:10:00 +01:00
|
|
|
{groupedUpdates[index][VirtuosoUtil.GROUP]}
|
2024-11-04 01:26:58 +01:00
|
|
|
</Typography>
|
2023-11-25 15:39:39 +01:00
|
|
|
</StyledGroupHeader>
|
|
|
|
|
)}
|
2024-10-12 18:41:42 +02:00
|
|
|
computeItemKey={computeItemKey}
|
2025-01-12 15:02:33 +01:00
|
|
|
itemContent={(index) => (
|
|
|
|
|
<StyledGroupItemWrapper>
|
|
|
|
|
<ChapterUpdateCard chapter={updateEntries[index]} />
|
|
|
|
|
</StyledGroupItemWrapper>
|
|
|
|
|
)}
|
2023-11-25 15:39:39 +01:00
|
|
|
/>
|
|
|
|
|
</>
|
2021-09-28 00:41:12 +03:30
|
|
|
);
|
2022-11-26 13:51:56 +01:00
|
|
|
};
|