Files
suwayomi-material-you-webui/src/screens/SourceMangas.tsx

100 lines
3.4 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-01-26 23:32:12 +03:30
* 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/. */
2021-01-22 17:00:33 +03:30
import React, { useContext, useEffect, useState } from 'react';
import { useParams, useHistory } from 'react-router-dom';
import IconButton from '@mui/material/IconButton';
2021-10-30 16:10:34 +03:30
import MangaGrid from 'components/MangaGrid';
import NavbarContext from 'components/context/NavbarContext';
import client from 'util/client';
import SettingsIcon from '@mui/icons-material/Settings';
2021-01-19 14:47:07 +03:30
2021-02-20 01:23:52 +03:30
export default function SourceMangas(props: { popular: boolean }) {
2021-03-09 16:44:09 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
const history = useHistory();
2021-03-09 16:44:09 +03:30
const { sourceId } = useParams<{ sourceId: string }>();
const [isConfigurable, setIsConfigurable] = useState<boolean>(false);
const [mangas, setMangas] = useState<IMangaCard[]>([]);
2021-01-22 21:11:00 +03:30
const [hasNextPage, setHasNextPage] = useState<boolean>(false);
const [lastPageNum, setLastPageNum] = useState<number>(1);
2021-09-18 00:05:00 +04:30
const [fetched, setFetched] = useState<boolean>(false);
2021-01-19 14:47:07 +03:30
useEffect(() => {
2021-09-18 00:05:00 +04:30
setTitle('Source'); // title is later set after a fetch but we set it here once
2021-09-13 19:18:21 +04:30
}, []);
useEffect(() => {
setAction(
<>
{isConfigurable && (
<IconButton
onClick={() => history.push(`/sources/${sourceId}/configure/`)}
aria-label="display more actions"
edge="end"
color="inherit"
size="large"
>
<SettingsIcon />
</IconButton>
)}
</>,
);
}, [isConfigurable]);
2021-01-22 17:00:33 +03:30
useEffect(() => {
client.get(`/api/v1/source/${sourceId}`)
.then((response) => response.data)
.then((data: ISource) => {
2021-09-18 00:05:00 +04:30
setTitle(data.displayName);
setIsConfigurable(data.isConfigurable);
});
2021-01-22 17:00:33 +03:30
}, []);
2021-01-19 14:47:07 +03:30
useEffect(() => {
const sourceType = props.popular ? 'popular' : 'latest';
client.get(`/api/v1/source/${sourceId}/${sourceType}/${lastPageNum}`)
.then((response) => response.data)
2021-01-22 21:11:00 +03:30
.then((data: { mangaList: IManga[], hasNextPage: boolean }) => {
setMangas([
...mangas,
...data.mangaList.map((it) => ({
title: it.title, thumbnailUrl: it.thumbnailUrl, id: it.id,
}))]);
setHasNextPage(data.hasNextPage);
2021-09-18 00:05:00 +04:30
setFetched(true);
2021-01-22 21:11:00 +03:30
});
}, [lastPageNum]);
2021-01-19 14:47:07 +03:30
2021-09-18 00:05:00 +04:30
let message;
2021-09-19 17:30:34 +04:30
let messageExtra;
if (fetched) {
message = 'No manga was found!';
if (sourceId === '0') {
messageExtra = (
<>
<span>Check out </span>
<a href="https://github.com/Suwayomi/Tachidesk-Server/wiki/Local-Source">Local source guide</a>
</>
);
}
}
2021-09-18 00:05:00 +04:30
2021-01-22 21:11:00 +03:30
return (
<MangaGrid
mangas={mangas}
hasNextPage={hasNextPage}
lastPageNum={lastPageNum}
setLastPageNum={setLastPageNum}
2021-09-18 00:05:00 +04:30
message={message}
2021-09-19 17:30:34 +04:30
messageExtra={messageExtra}
isLoading={!fetched}
2021-01-22 21:11:00 +03:30
/>
);
2021-01-19 14:47:07 +03:30
}