Files
suwayomi-material-you-webui/src/features/manga/components/TrackMangaButton.tsx

83 lines
3.6 KiB
TypeScript
Raw Normal View History

2024-03-10 17:36:22 +01:00
/*
* 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
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { useNavigate } from 'react-router-dom';
import SyncIcon from '@mui/icons-material/Sync';
import PopupState, { bindDialog, bindTrigger } from 'material-ui-popup-state';
import Dialog from '@mui/material/Dialog';
import CheckIcon from '@mui/icons-material/Check';
import { useLingui } from '@lingui/react/macro';
import { plural } from '@lingui/core/macro';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { makeToast } from '@/base/utils/Toast.ts';
2025-08-15 22:02:58 +02:00
import { TrackManga } from '@/features/tracker/components/TrackManga.tsx';
import { Trackers } from '@/features/tracker/services/Trackers.ts';
import { CustomButton } from '@/base/components/buttons/CustomButton.tsx';
2026-03-11 00:11:29 +01:00
import type { GetTrackersSettingsQuery, MangaType } from '@/lib/graphql/generated/graphql.ts';
2025-12-22 14:00:44 +01:00
import { GET_TRACKERS_SETTINGS } from '@/lib/graphql/tracker/TrackerQuery.ts';
2026-03-11 00:11:29 +01:00
import type { MangaTrackRecordInfo } from '@/features/manga/Manga.types.ts';
import { AppRoutes } from '@/base/AppRoute.constants.ts';
import { MediaQuery } from '@/base/utils/MediaQuery.tsx';
2026-03-20 03:29:45 +01:00
import { STABLE_EMPTY_ARRAY } from '@/base/Base.constants.ts';
2024-03-10 17:36:22 +01:00
2024-07-08 18:02:50 +02:00
export const TrackMangaButton = ({ manga }: { manga: MangaTrackRecordInfo & Pick<MangaType, 'title'> }) => {
const { t } = useLingui();
2024-03-10 17:36:22 +01:00
const navigate = useNavigate();
2026-03-15 01:42:37 +01:00
const isMobileWidth = MediaQuery.useIsMobileWidth();
2024-03-10 17:36:22 +01:00
const trackerList = requestManager.useGetTrackerList<GetTrackersSettingsQuery>(GET_TRACKERS_SETTINGS);
2026-03-20 03:29:45 +01:00
const trackers = trackerList.data?.trackers.nodes ?? STABLE_EMPTY_ARRAY;
2024-03-10 17:36:22 +01:00
const mangaTrackers = manga.trackRecords.nodes;
2024-07-08 18:02:50 +02:00
const loggedInTrackers = Trackers.getLoggedIn(trackers);
const trackersInUse = Trackers.getLoggedIn(Trackers.getTrackers(mangaTrackers, trackers));
2024-03-10 17:36:22 +01:00
const handleClick = (openPopup: () => void) => {
if (trackerList.error) {
makeToast(t`Could not load track info`, 'error', trackerList.error?.toString());
2024-03-10 17:36:22 +01:00
return;
}
if (!loggedInTrackers.length) {
navigate(AppRoutes.settings.childRoutes.tracking.path);
2024-03-10 17:36:22 +01:00
return;
}
openPopup();
};
return (
<PopupState variant="dialog" popupId="manga-track-modal">
{(popupState) => (
<>
<CustomButton
2024-03-10 17:36:22 +01:00
{...bindTrigger(popupState)}
2026-03-15 01:42:37 +01:00
size={isMobileWidth ? 'small' : 'medium'}
2024-03-10 17:36:22 +01:00
disabled={trackerList.loading || !!trackerList.error}
onClick={() => handleClick(popupState.open)}
2024-08-30 04:34:28 +02:00
variant={trackersInUse.length ? 'contained' : 'outlined'}
2024-03-10 17:36:22 +01:00
>
{trackersInUse.length ? <CheckIcon /> : <SyncIcon />}
{trackersInUse.length
? plural(trackersInUse.length, {
one: '# Tracker',
other: '# Tracker',
})
: t`Tracking`}
</CustomButton>
{popupState.isOpen && (
<Dialog {...bindDialog(popupState)} maxWidth="md" fullWidth scroll="paper">
<TrackManga manga={manga} />
</Dialog>
)}
2024-03-10 17:36:22 +01:00
</>
)}
</PopupState>
);
};