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';
|
2026-01-10 19:45:02 +01:00
|
|
|
import { useLingui } from '@lingui/react/macro';
|
|
|
|
|
import { plural } from '@lingui/core/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 { 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';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { CustomButton } from '@/base/components/buttons/CustomButton.tsx';
|
2024-07-08 18:02:50 +02:00
|
|
|
import { 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';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { MangaTrackRecordInfo } from '@/features/manga/Manga.types.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { AppRoutes } from '@/base/AppRoute.constants.ts';
|
2026-01-18 19:16:41 +01:00
|
|
|
import { MediaQuery } from '@/base/utils/MediaQuery.tsx';
|
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'> }) => {
|
2026-01-10 19:45:02 +01:00
|
|
|
const { t } = useLingui();
|
2024-03-10 17:36:22 +01:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
2024-07-04 22:00:39 +02:00
|
|
|
const trackerList = requestManager.useGetTrackerList<GetTrackersSettingsQuery>(GET_TRACKERS_SETTINGS);
|
2024-07-08 18:02:50 +02:00
|
|
|
const trackers = trackerList.data?.trackers.nodes ?? [];
|
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) {
|
2026-01-10 19:45:02 +01:00
|
|
|
makeToast(t`Could not load track info`, 'error', trackerList.error?.toString());
|
2024-03-10 17:36:22 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!loggedInTrackers.length) {
|
2025-10-11 18:14:01 +02:00
|
|
|
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) => (
|
|
|
|
|
<>
|
2025-02-08 16:03:37 +01:00
|
|
|
<CustomButton
|
2024-03-10 17:36:22 +01:00
|
|
|
{...bindTrigger(popupState)}
|
2026-01-18 19:16:41 +01:00
|
|
|
size={MediaQuery.useIsMobileWidth() ? '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
|
2026-01-10 19:45:02 +01:00
|
|
|
? plural(trackersInUse.length, {
|
|
|
|
|
one: '# Tracker',
|
|
|
|
|
other: '# Tracker',
|
|
|
|
|
})
|
|
|
|
|
: t`Tracking`}
|
2025-02-08 16:03:37 +01:00
|
|
|
</CustomButton>
|
2025-01-12 20:27:10 +01:00
|
|
|
{popupState.isOpen && (
|
|
|
|
|
<Dialog {...bindDialog(popupState)} maxWidth="md" fullWidth scroll="paper">
|
|
|
|
|
<TrackManga manga={manga} />
|
|
|
|
|
</Dialog>
|
|
|
|
|
)}
|
2024-03-10 17:36:22 +01:00
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</PopupState>
|
|
|
|
|
);
|
|
|
|
|
};
|