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 Button from '@mui/material/Button';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-04-14 15:50:12 +02:00
|
|
|
import List from '@mui/material/List';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
2024-03-10 17:36:22 +01:00
|
|
|
import { useEffect, useMemo, useState } from 'react';
|
|
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import ArrowBack from '@mui/icons-material/ArrowBack';
|
2024-04-26 12:53:31 +02:00
|
|
|
import ArrowForwardIcon from '@mui/icons-material/ArrowForward';
|
2024-03-10 17:36:22 +01:00
|
|
|
import DialogContent from '@mui/material/DialogContent';
|
|
|
|
|
import DialogTitle from '@mui/material/DialogTitle';
|
2024-05-07 14:42:02 +02:00
|
|
|
import InputAdornment from '@mui/material/InputAdornment';
|
|
|
|
|
import InfoIcon from '@mui/icons-material/Info';
|
|
|
|
|
import PopupState, { bindPopover, bindTrigger } from 'material-ui-popup-state';
|
|
|
|
|
import Popover from '@mui/material/Popover';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2025-07-19 18:33:33 +02:00
|
|
|
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2025-05-03 12:14:05 +02:00
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/feedback/EmptyViewAbsoluteCentered.tsx';
|
|
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/feedback/LoadingPlaceholder.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { SearchTextField } from '@/modules/core/components/inputs/SearchTextField.tsx';
|
2024-10-05 23:22:42 +02:00
|
|
|
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
2024-10-05 21:32:31 +02:00
|
|
|
import { TrackerMangaCard } from '@/modules/tracker/components/cards/TrackerMangaCard.tsx';
|
|
|
|
|
import { DIALOG_PADDING } from '@/modules/tracker/Tracker.constants.ts';
|
2024-12-11 13:59:50 +01:00
|
|
|
import { useGetOptionForDirection } from '@/modules/theme/services/ThemeCreator.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-07-08 18:02:50 +02:00
|
|
|
import { MangaType } from '@/lib/graphql/generated/graphql.ts';
|
2024-10-07 13:35:07 +02:00
|
|
|
|
|
|
|
|
import { MangaIdInfo } from '@/modules/manga/Manga.types.ts';
|
2024-12-20 17:24:40 +01:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2024-12-23 00:59:13 +01:00
|
|
|
import { applyStyles } from '@/modules/core/utils/ApplyStyles.ts';
|
2025-07-19 18:33:33 +02:00
|
|
|
import { Tracker, TrackerIdInfo, TTrackerBind } from '@/modules/tracker/Tracker.types.ts';
|
|
|
|
|
import { CustomButtonIcon } from '@/modules/core/components/buttons/CustomButtonIcon.tsx';
|
|
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2025-07-19 16:53:02 +02:00
|
|
|
|
|
|
|
|
const TrackButton = ({
|
|
|
|
|
mangaId,
|
|
|
|
|
selectedTrackerRemoteId,
|
|
|
|
|
trackerId,
|
|
|
|
|
closeSearchMode,
|
2025-07-19 18:33:33 +02:00
|
|
|
supportsPrivateTracking,
|
2025-07-19 16:53:02 +02:00
|
|
|
}: {
|
|
|
|
|
trackerId: TrackerIdInfo['id'];
|
|
|
|
|
mangaId: MangaIdInfo['id'];
|
|
|
|
|
selectedTrackerRemoteId: string | undefined;
|
|
|
|
|
closeSearchMode: () => void;
|
2025-07-19 18:33:33 +02:00
|
|
|
supportsPrivateTracking: boolean;
|
2025-07-19 16:53:02 +02:00
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const [bindTracker, bindTrackerMutation] = requestManager.useBindTracker();
|
|
|
|
|
|
2025-07-19 18:33:33 +02:00
|
|
|
const trackManga = (asPrivate: boolean) => {
|
2025-07-19 16:53:02 +02:00
|
|
|
if (selectedTrackerRemoteId === undefined) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-19 18:33:33 +02:00
|
|
|
bindTracker({
|
|
|
|
|
variables: { input: { mangaId, remoteId: selectedTrackerRemoteId, trackerId, private: asPrivate } },
|
|
|
|
|
})
|
2025-07-19 16:53:02 +02:00
|
|
|
.then(() => {
|
|
|
|
|
makeToast(t('manga.action.track.add.label.success'), 'success');
|
|
|
|
|
closeSearchMode();
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => makeToast(t('manga.action.track.add.label.error'), 'error', getErrorMessage(e)));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
sx={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
left: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
paddingBottom: DIALOG_PADDING,
|
2025-07-19 18:33:33 +02:00
|
|
|
gap: 2,
|
|
|
|
|
px: DIALOG_PADDING,
|
2025-07-19 16:53:02 +02:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Button
|
|
|
|
|
disabled={bindTrackerMutation.loading}
|
|
|
|
|
size="large"
|
|
|
|
|
variant="contained"
|
2025-07-19 18:33:33 +02:00
|
|
|
onClick={() => trackManga(false)}
|
|
|
|
|
sx={{ flexBasis: '65%' }}
|
2025-07-19 16:53:02 +02:00
|
|
|
>
|
|
|
|
|
{t('manga.action.track.add.label.action')}
|
|
|
|
|
</Button>
|
2025-07-19 18:33:33 +02:00
|
|
|
{supportsPrivateTracking && (
|
|
|
|
|
<CustomTooltip
|
|
|
|
|
title={t('tracking.action.button.track_privately')}
|
|
|
|
|
disabled={bindTrackerMutation.loading}
|
|
|
|
|
>
|
|
|
|
|
<CustomButtonIcon
|
|
|
|
|
disabled={bindTrackerMutation.loading}
|
|
|
|
|
sx={{ flexBasis: '10%', maxWidth: '100px' }}
|
|
|
|
|
variant="contained"
|
|
|
|
|
onClick={() => trackManga(true)}
|
|
|
|
|
>
|
|
|
|
|
<VisibilityOffIcon />
|
|
|
|
|
</CustomButtonIcon>
|
|
|
|
|
</CustomTooltip>
|
|
|
|
|
)}
|
2025-07-19 16:53:02 +02:00
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
};
|
2024-03-10 17:36:22 +01:00
|
|
|
|
|
|
|
|
export const TrackerSearch = ({
|
2024-07-08 18:02:50 +02:00
|
|
|
manga,
|
2024-03-10 17:36:22 +01:00
|
|
|
tracker,
|
|
|
|
|
closeSearchMode,
|
|
|
|
|
trackedId,
|
|
|
|
|
}: {
|
2024-07-08 18:02:50 +02:00
|
|
|
manga: MangaIdInfo & Pick<MangaType, 'title'>;
|
2025-07-19 18:33:33 +02:00
|
|
|
tracker: TTrackerBind;
|
2024-03-10 17:36:22 +01:00
|
|
|
closeSearchMode: () => void;
|
|
|
|
|
trackedId?: string;
|
|
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
2024-09-30 02:27:10 +02:00
|
|
|
const getOptionForDirection = useGetOptionForDirection();
|
2024-03-10 17:36:22 +01:00
|
|
|
|
2024-07-08 18:02:50 +02:00
|
|
|
const [searchString, setSearchString] = useState<string>(manga.title);
|
2024-03-10 17:36:22 +01:00
|
|
|
const [tmpSearchString, setTmpSearchString] = useState(searchString);
|
|
|
|
|
|
|
|
|
|
const [selectedTrackerRemoteId, setSelectedTrackerRemoteId] = useState<string | undefined>(trackedId);
|
|
|
|
|
|
2024-10-06 16:20:50 +02:00
|
|
|
const trackerSearch = requestManager.useTrackerSearch(tracker.id, searchString, {
|
|
|
|
|
notifyOnNetworkStatusChange: true,
|
|
|
|
|
});
|
2024-03-10 17:36:22 +01:00
|
|
|
const searchResults = trackerSearch.data?.searchTracker.trackSearches ?? [];
|
|
|
|
|
|
|
|
|
|
const hasResults = !!searchResults.length;
|
2024-12-23 00:59:13 +01:00
|
|
|
const hasNoResults = !trackerSearch.loading && !trackerSearch.error && !hasResults;
|
|
|
|
|
const hasError = !!trackerSearch.error && !trackerSearch.loading;
|
2024-03-10 17:36:22 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setSelectedTrackerRemoteId(trackedId);
|
|
|
|
|
|
|
|
|
|
return () =>
|
2024-07-08 18:02:50 +02:00
|
|
|
trackerSearch.abortRequest(new Error(`MangaTrackerSearchCard(${tracker.id}, ${manga.id}): search changed`));
|
2024-03-10 17:36:22 +01:00
|
|
|
}, [searchString]);
|
|
|
|
|
|
|
|
|
|
const showTrackButton =
|
|
|
|
|
useMemo(
|
|
|
|
|
() =>
|
|
|
|
|
!!selectedTrackerRemoteId &&
|
|
|
|
|
!!searchResults.find((searchResult) => searchResult.remoteId === selectedTrackerRemoteId),
|
|
|
|
|
[selectedTrackerRemoteId, searchResults],
|
2024-12-23 00:59:13 +01:00
|
|
|
) && !hasError;
|
2024-03-10 17:36:22 +01:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2024-03-29 16:54:53 +01:00
|
|
|
<DialogTitle sx={{ padding: DIALOG_PADDING }}>
|
2024-09-03 17:15:47 +02:00
|
|
|
<Stack
|
|
|
|
|
direction="row"
|
|
|
|
|
sx={{
|
|
|
|
|
gap: '10px',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-03-10 17:36:22 +01:00
|
|
|
<IconButton onClick={closeSearchMode}>
|
2024-04-26 12:53:31 +02:00
|
|
|
{getOptionForDirection(<ArrowBack />, <ArrowForwardIcon />)}
|
2024-03-10 17:36:22 +01:00
|
|
|
</IconButton>
|
|
|
|
|
<SearchTextField
|
|
|
|
|
sx={{ width: '100%' }}
|
|
|
|
|
variant="standard"
|
|
|
|
|
value={tmpSearchString}
|
|
|
|
|
onChange={(e) => setTmpSearchString(e.target.value)}
|
|
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
|
|
|
|
setSearchString(tmpSearchString);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onCancel={() => setTmpSearchString('')}
|
2024-05-07 14:42:02 +02:00
|
|
|
InputProps={{
|
|
|
|
|
startAdornment: tracker.id === Tracker.MYANIMELIST && (
|
|
|
|
|
<InputAdornment position="start">
|
|
|
|
|
<PopupState variant="popover" popupId="tracker-search-info">
|
|
|
|
|
{(popupState) => (
|
|
|
|
|
<>
|
2024-09-06 16:59:27 +02:00
|
|
|
<IconButton {...bindTrigger(popupState)} color="inherit">
|
2024-05-07 14:42:02 +02:00
|
|
|
<InfoIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
<Popover
|
|
|
|
|
{...bindPopover(popupState)}
|
|
|
|
|
anchorOrigin={{
|
|
|
|
|
vertical: 'bottom',
|
|
|
|
|
horizontal: 'left',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography sx={{ padding: 1, whiteSpace: 'pre-line' }}>
|
|
|
|
|
{t('tracking.my_anime_list.search.label.hint')}
|
|
|
|
|
</Typography>
|
|
|
|
|
</Popover>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</PopupState>
|
|
|
|
|
</InputAdornment>
|
|
|
|
|
),
|
|
|
|
|
}}
|
2024-03-10 17:36:22 +01:00
|
|
|
/>
|
|
|
|
|
</Stack>
|
|
|
|
|
</DialogTitle>
|
2024-12-23 00:59:13 +01:00
|
|
|
<DialogContent
|
|
|
|
|
dividers
|
|
|
|
|
sx={{
|
|
|
|
|
padding: DIALOG_PADDING,
|
|
|
|
|
height: '100vh',
|
|
|
|
|
...applyStyles(hasNoResults || hasError, { position: 'relative' }),
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{hasNoResults && <EmptyViewAbsoluteCentered message={t('manga.error.label.no_mangas_found')} />}
|
2024-03-10 17:36:22 +01:00
|
|
|
{trackerSearch.loading && <LoadingPlaceholder />}
|
2024-12-23 00:59:13 +01:00
|
|
|
{hasError && (
|
2024-04-29 15:29:33 +02:00
|
|
|
<EmptyViewAbsoluteCentered
|
2024-03-10 17:36:22 +01:00
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
2024-12-21 23:27:03 +01:00
|
|
|
messageExtra={getErrorMessage(trackerSearch.error)}
|
2024-04-27 22:14:42 +02:00
|
|
|
retry={() =>
|
|
|
|
|
trackerSearch.refetch().catch(defaultPromiseErrorHandler('TrackerSearch::refetch'))
|
|
|
|
|
}
|
2024-03-10 17:36:22 +01:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<List sx={{ padding: 0 }}>
|
|
|
|
|
{hasResults &&
|
|
|
|
|
searchResults.map((trackerManga) => (
|
|
|
|
|
<TrackerMangaCard
|
|
|
|
|
key={trackerManga.id}
|
|
|
|
|
manga={trackerManga}
|
|
|
|
|
selected={trackerManga.remoteId === selectedTrackerRemoteId}
|
|
|
|
|
onSelect={() => setSelectedTrackerRemoteId(trackerManga.remoteId)}
|
|
|
|
|
/>
|
|
|
|
|
))}
|
|
|
|
|
</List>
|
|
|
|
|
{showTrackButton && (
|
2025-07-19 16:53:02 +02:00
|
|
|
<TrackButton
|
|
|
|
|
mangaId={manga.id}
|
|
|
|
|
trackerId={tracker.id}
|
|
|
|
|
closeSearchMode={closeSearchMode}
|
|
|
|
|
selectedTrackerRemoteId={selectedTrackerRemoteId}
|
2025-07-19 18:33:33 +02:00
|
|
|
supportsPrivateTracking={tracker.supportsPrivateTracking}
|
2025-07-19 16:53:02 +02:00
|
|
|
/>
|
2024-03-10 17:36:22 +01:00
|
|
|
)}
|
|
|
|
|
</DialogContent>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|