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';
|
2026-07-05 18:55:09 +02:00
|
|
|
import React, { useCallback, useEffect, 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';
|
2026-07-05 18:55:09 +02:00
|
|
|
import { dateTimeFormatter, epochToDate, getDateString } 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';
|
2026-03-20 03:02:52 +01:00
|
|
|
import { STABLE_EMPTY_ARRAY } from '@/base/Base.constants.ts';
|
2026-07-05 18:55:09 +02:00
|
|
|
import mapValues from 'lodash/fp/mapValues';
|
|
|
|
|
import difference from 'lodash/fp/difference';
|
|
|
|
|
import uniqBy from 'lodash/fp/uniqBy';
|
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
|
|
|
|
2026-07-05 18:55:09 +02:00
|
|
|
useAppTitleAndAction(t`Updates`, <UpdateChecker />);
|
|
|
|
|
|
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',
|
|
|
|
|
});
|
|
|
|
|
const hasNextPage = !!chapterUpdateData?.chapters.pageInfo.hasNextPage;
|
2026-07-05 18:55:09 +02:00
|
|
|
const allUpdateEntries = chapterUpdateData?.chapters.nodes ?? STABLE_EMPTY_ARRAY;
|
|
|
|
|
|
|
|
|
|
const [prevUpdateEntriesCount, setPrevUpdateEntriesCount] = useState(0);
|
|
|
|
|
|
|
|
|
|
const [firstUnreadUpdatesByGroup, otherUpdatesByMangaByGroup] = useMemo(() => {
|
|
|
|
|
const groupedEntries = Chapters.groupByDate(allUpdateEntries, 'fetchedAt');
|
|
|
|
|
|
|
|
|
|
const mangaIdByGroup = mapValues(
|
|
|
|
|
(groupEntries) => uniqBy('mangaId', groupEntries).map((entry) => entry.mangaId),
|
|
|
|
|
groupedEntries,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const entriesByMangaByGroup = mapValues(
|
|
|
|
|
(entries) => Object.groupBy(entries!, (entry) => entry.mangaId),
|
|
|
|
|
groupedEntries,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const firstUnreadEntryByMangaByGroup = mapValues(
|
|
|
|
|
(entriesByManga) =>
|
|
|
|
|
mapValues(
|
|
|
|
|
(mangaEntries) => [mangaEntries!.findLast((entry) => !entry.isRead) ?? mangaEntries![0]],
|
|
|
|
|
entriesByManga,
|
|
|
|
|
),
|
|
|
|
|
entriesByMangaByGroup,
|
|
|
|
|
);
|
|
|
|
|
const firstUnreadEntryByGroup = mapValues(
|
|
|
|
|
(firstUnreadEntryByManga) =>
|
|
|
|
|
Object.values(firstUnreadEntryByManga)
|
|
|
|
|
.flat()
|
|
|
|
|
.toSorted((a, b) => {
|
|
|
|
|
const groupMangaIds = mangaIdByGroup[getDateString(epochToDate(Number(a.fetchedAt)))];
|
|
|
|
|
|
|
|
|
|
return groupMangaIds.indexOf(a.mangaId) - groupMangaIds.indexOf(b.mangaId);
|
|
|
|
|
}),
|
|
|
|
|
firstUnreadEntryByMangaByGroup,
|
|
|
|
|
);
|
|
|
|
|
const remainingEntriesByMangaByGroup = mapValues(
|
|
|
|
|
(entriesByManga) =>
|
|
|
|
|
mapValues(
|
|
|
|
|
(mangaEntries) =>
|
|
|
|
|
difference(
|
|
|
|
|
mangaEntries!,
|
|
|
|
|
firstUnreadEntryByMangaByGroup[
|
|
|
|
|
getDateString(epochToDate(Number(mangaEntries![0].fetchedAt)))
|
|
|
|
|
]![mangaEntries![0].mangaId],
|
|
|
|
|
),
|
|
|
|
|
entriesByManga,
|
|
|
|
|
),
|
|
|
|
|
entriesByMangaByGroup,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return [Object.entries(firstUnreadEntryByGroup), remainingEntriesByMangaByGroup];
|
|
|
|
|
}, [allUpdateEntries]);
|
|
|
|
|
|
|
|
|
|
const firstUnreadUpdatesGroupCounts = useMemo(
|
|
|
|
|
() => firstUnreadUpdatesByGroup.map((updatesByGroup) => updatesByGroup[VirtuosoUtil.ITEMS].length),
|
|
|
|
|
[firstUnreadUpdatesByGroup],
|
2025-03-10 00:26:31 +01:00
|
|
|
);
|
2026-07-05 18:55:09 +02:00
|
|
|
const firstUnreadUpdatesEntries = useMemo(
|
|
|
|
|
() => firstUnreadUpdatesByGroup.flatMap((updatesByGroup) => updatesByGroup[VirtuosoUtil.ITEMS]),
|
|
|
|
|
[firstUnreadUpdatesByGroup],
|
2025-03-10 01:10:00 +01:00
|
|
|
);
|
2025-01-12 14:09:10 +01:00
|
|
|
|
2026-07-05 18:55:09 +02:00
|
|
|
const computeFirstUnreadUpdateItemKey = VirtuosoUtil.useCreateGroupedComputeItemKey(
|
|
|
|
|
firstUnreadUpdatesGroupCounts,
|
|
|
|
|
useCallback((index) => firstUnreadUpdatesByGroup[index][VirtuosoUtil.GROUP], [firstUnreadUpdatesByGroup]),
|
|
|
|
|
useCallback((index) => firstUnreadUpdatesEntries[index].id, [firstUnreadUpdatesEntries]),
|
2024-10-12 18:41:42 +02:00
|
|
|
);
|
|
|
|
|
|
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
|
|
|
|
2023-05-18 13:48:01 +02:00
|
|
|
const loadMore = useCallback(() => {
|
|
|
|
|
if (!hasNextPage) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 18:55:09 +02:00
|
|
|
fetchMore({ variables: { offset: allUpdateEntries.length } }).then(() =>
|
|
|
|
|
setPrevUpdateEntriesCount(firstUnreadUpdatesEntries.length),
|
|
|
|
|
);
|
|
|
|
|
}, [hasNextPage, allUpdateEntries.length, firstUnreadUpdatesEntries.length]);
|
|
|
|
|
|
|
|
|
|
const filteredOutAllItemsOfFetchedPage =
|
|
|
|
|
allUpdateEntries.length > 0 && prevUpdateEntriesCount === firstUnreadUpdatesEntries.length;
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (filteredOutAllItemsOfFetchedPage && hasNextPage && !isLoading) {
|
|
|
|
|
loadMore();
|
|
|
|
|
}
|
|
|
|
|
}, [isLoading, hasNextPage, filteredOutAllItemsOfFetchedPage, loadMore]);
|
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'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-05 18:55:09 +02:00
|
|
|
if (!isLoading && firstUnreadUpdatesEntries.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}
|
2026-07-05 18:55:09 +02:00
|
|
|
groupCounts={firstUnreadUpdatesGroupCounts}
|
2023-11-25 15:39:39 +01:00
|
|
|
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">
|
2026-07-05 18:55:09 +02:00
|
|
|
{firstUnreadUpdatesByGroup[index][VirtuosoUtil.GROUP]}
|
2024-11-04 01:26:58 +01:00
|
|
|
</Typography>
|
2023-11-25 15:39:39 +01:00
|
|
|
</StyledGroupHeader>
|
|
|
|
|
)}
|
2026-07-05 18:55:09 +02:00
|
|
|
computeItemKey={computeFirstUnreadUpdateItemKey}
|
2025-01-12 15:02:33 +01:00
|
|
|
itemContent={(index) => (
|
|
|
|
|
<StyledGroupItemWrapper>
|
2026-07-05 18:55:09 +02:00
|
|
|
<ChapterUpdateCard
|
|
|
|
|
chapter={firstUnreadUpdatesEntries[index]}
|
|
|
|
|
otherChapters={
|
|
|
|
|
otherUpdatesByMangaByGroup[
|
|
|
|
|
getDateString(epochToDate(Number(firstUnreadUpdatesEntries[index].fetchedAt)))
|
|
|
|
|
][firstUnreadUpdatesEntries[index].mangaId]
|
|
|
|
|
}
|
|
|
|
|
/>
|
2025-01-12 15:02:33 +01:00
|
|
|
</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
|
|
|
};
|