import {useOutdatedZims, useSearchZim, useWROLMode} from "../hooks/customHooks";
import {
Accordion,
Button,
Divider,
Header,
Icon,
Loader,
Modal,
Placeholder,
Segment
} from "./Theme";
import {HeadlineText} from "./Headline";
import {TextPlaceholder} from "./Placeholder";
import {
AccordionContent,
AccordionTitle,
Button as SButton,
Dropdown,
Icon as SIcon,
Label,
Message,
PlaceholderHeader,
PlaceholderLine,
TableCell,
TableRow
} from "semantic-ui-react";
import React, {useContext, useState} from "react";
import {
APIButton,
encodeMediaPath,
ErrorMessage,
HandPointMessage,
humanFileSize,
IframeViewer,
InfoMessage,
normalizeEstimate,
PageContainer,
Paginator,
TabLinks,
TagIcon,
Toggle,
useTitle,
WarningMessage
} from "./Common";
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
import {TagsSelector} from "../Tags";
import {AddToPlaylistButton} from "./AddToPlaylist";
import {
deleteOutdatedZims,
fetchZims,
fetchZimSubscriptions,
refreshFiles,
saveSettings,
setZimAutoSearch,
tagZimEntry,
untagZimEntry,
zimSubscribe,
zimUnsubscribe
} from "../api";
import {useSearch} from "./Search";
import {TagsQuerySelector} from "./Files";
import {ThemeContext} from "../contexts/contexts";
import {Link, Route, Routes} from "react-router";
import {SortableTable} from "./SortableTable";
import {toast} from "react-semantic-toasts-2";
import _ from "lodash";
import {ZIM_VIEWER_URI} from "./Vars";
export const OutdatedZimsMessage = ({onClick}) => {
const [open, setOpen] = React.useState(false);
const {outdated, current} = useOutdatedZims();
const onOpen = () => setOpen(true);
const onClose = () => setOpen(false);
const handleDelete = async () => {
try {
const success = await deleteOutdatedZims();
if (success) {
toast({
type: 'info',
title: 'Zims deleted',
description: 'Outdated Zims have been deleted.',
time: 5000,
});
} else {
toast({
type: 'error',
title: 'Failed to delete Zims',
description: 'Outdated Zims have NOT been deleted. See server logs.',
time: 5000,
});
}
} finally {
onClose();
}
}
const handleIgnore = async () => {
const config = {ignore_outdated_zims: true};
await saveSettings(config);
if (onClick) {
await onClick();
}
}
let modalContent =
;
if (outdated && outdated.length > 0) {
modalContent = <>
{outdated.map(i =>
{i})}
{current.map(i => {i})}
>
}
return
Outdated Zim Files
New Zim files have been downloaded. Outdated Zim files can be removed.
setOpen(true)}>Delete
Delete
{modalContent}
Delete
Delete Manually
Ignore Forever
}
const ZimSearchEntry = ({zimId, onTag, onUntag, entry}) => {
const {path, title, headline, tag_names} = entry;
const [open, setOpen] = React.useState(false);
const url = `/api/zim/${zimId}/entry/${encodeMediaPath(path)}`;
const handleClick = async (e) => {
if (e) {
e.preventDefault();
}
setOpen(true);
}
const localAddTag = (name) => {
onTag(zimId, path, name);
}
const localUntag = (name) => {
onUntag(zimId, path, name);
}
const tagIcon = tag_names && tag_names.length > 0 ? : null;
return
'} closeTag={''}/>
{tagIcon}
setOpen(false)}>
]*>/g, '')}}
content={null} title='Add to Playlist'/>
}
const ZimAccordion = ({data, index, activeIndex, onClick, searchStr, activeTags}) => {
const {id, estimate, metadata} = data;
const {title, date} = metadata;
const {zim, fetchSearch, pages, loading} = useSearchZim(searchStr, id, index === activeIndex, activeTags);
const {t} = useContext(ThemeContext);
const localAddTag = async (zimId, path, name) => {
await tagZimEntry(zimId, path, name);
await fetchSearch();
}
const localUntag = async (zimId, path, name) => {
await untagZimEntry(zimId, path, name);
await fetchSearch();
}
let body = ;
if (zim && !loading) {
const {search} = zim;
if (search && search.length > 0) {
body = search.map(i =>
);
} else {
body = No results
;
}
}
const paginator =
;
const label = ;
return
onClick(index, activeIndex)}
>
{body}
{paginator}
}
const ZimsRefreshWarning = () => {
return
No Zims have been indexed.
Click here to refresh all your files.
;
}
export const ZimSearchView = ({suggestions, loading}) => {
const [activeIndex, setActiveIndex] = React.useState(null);
const {searchStr, activeTags, setTags} = useSearch();
const {zimsEstimates} = suggestions;
const handleClick = (index, activeIndex_) => {
setActiveIndex(index === activeIndex_ ? -1 : index);
}
let body;
if (!_.isEmpty(zimsEstimates)) {
body = zimsEstimates.map((i, index) => );
} else if (loading) {
body =
;
} else if (_.isEmpty(zimsEstimates)) {
body = ;
}
return <>
setTags(i)}/>
{body}
>
}
const DownloadMessage = () => {
return
More Zim files are available from the full Kiwix library
https://download.kiwix.org/
}
const ViewerMessage = () => {
return
You can view your Zim files using the Kiwix app, or at {ZIM_VIEWER_URI}
}
const ZimCatalogItemRow = ({item, subscriptions, iso_639_codes, fetchSubscriptions}) => {
const {name, languages, size} = item;
const subscription = name in subscriptions ? subscriptions[name] : null;
const subscriptionLanguage = subscription ? subscription['language'] : 'en';
const [language, setLanguage] = useState(subscriptionLanguage);
const [pending, setPending] = useState(false);
const languageChange = subscription ? language !== subscription['language'] : false;
const wrolModeEnabled = useWROLMode();
const handleButton = async () => {
let success = false;
try {
setPending(true);
if (subscription && !languageChange) {
success = await zimUnsubscribe(subscription['id']);
} else {
success = await zimSubscribe(name, language);
}
} catch (e) {
console.error(e);
}
setPending(false);
if (!success) {
toast({
type: 'error',
title: 'Error!',
description: subscription ? 'Failed to unsubscribe' : 'Failed to subscribe',
})
}
if (fetchSubscriptions) {
await fetchSubscriptions();
}
}
const handleLanguageChange = (e, {value}) => {
if (e) {
e.preventDefault();
}
setLanguage(value);
}
const languageOptions = languages.map(i => {
return {key: i, value: i, text: iso_639_codes[i]}
});
const languageDropdown = ;
const subscribeButton =
{subscription && !languageChange ? 'Unsubscribe' : 'Subscribe'}
return
{name}
{languageDropdown}
{subscribeButton}
{humanFileSize(size)}
}
class ManageZim extends React.Component {
constructor(props) {
super(props);
this.state = {
catalog: null,
iso_639_codes: null,
subscriptions: null,
zims: null,
}
}
async componentDidMount() {
await this.fetchZims();
await this.fetchSubscriptions();
}
async fetchZims() {
try {
const {zims} = await fetchZims();
this.setState({zims});
} catch (e) {
console.error(e);
this.setState({zims: undefined}); // Display error.
}
}
async fetchSubscriptions() {
try {
const {subscriptions, catalog, iso_639_codes} = await fetchZimSubscriptions();
this.setState({subscriptions, catalog, iso_639_codes});
} catch (e) {
console.error(e);
this.setState({subscriptions: undefined, catalog: undefined}); // Display error.
}
}
zimFileTableRow = (zim, sortData, localFetchZims) => {
const {id, path, size, auto_search} = zim;
const toggleZimAutoSearch = async () => {
try {
await setZimAutoSearch(id, !auto_search);
} catch (e) {
throw e;
} finally {
await localFetchZims();
}
}
const toggle =
return
{path}
{humanFileSize(size)}
{toggle}
}
render() {
const {zims, catalog, iso_639_codes, subscriptions} = this.state;
const zimFilesHeaders = [
{key: 'path', text: 'Path', sortBy: 'path', width: 14},
{key: 'size', text: 'Size', sortBy: 'size', width: 2},
{key: 'search', text: 'Search', sortBy: 'auto_search', width: 2},
];
let zimFilesBody =
;
if (zims && zims.length >= 1) {
zimFilesBody = this.zimFileTableRow(i, sortData, this.fetchZims.bind(this))}
rowKey='path'
tableHeaders={zimFilesHeaders}
/>;
} else if (zims && zims.length === 0) {
zimFilesBody =
You have not subscribed to any Kiwix projects, or your files have not been refreshed.
;
} else if (zims === undefined) {
zimFilesBody = Could not fetch Zim files;
}
const kiwixCatalogHeaders = [
{
key: 'name', text: 'Name', 'sortBy': [i => {
return i['name'].toLowerCase()
}], width: 8
},
{key: 'language', text: 'Language', 'sortBy': null, width: 4},
{key: 'subscription', text: 'Subscription', 'sortBy': null, width: 2},
{key: 'size', text: 'Maximum Size', sortBy: 'size', width: 2},
];
let kiwixCatalog =
;
if (catalog && catalog.length > 0) {
kiwixCatalog = await this.fetchSubscriptions()}
/>}
rowKey='name'
tableHeaders={kiwixCatalogHeaders}
/>
} else if (catalog === undefined) {
kiwixCatalog = Could not fetch catalog;
}
return
{zimFilesBody}
{kiwixCatalog}
}
}
function ZimViewer({src = ZIM_VIEWER_URI, style = null}) {
const fallback =
Failed to fetch Zim service.
You may need to give permission to access the page: {src}
If the above does not work, try starting the service:
sudo systemctl start wrolpi-kiwix
Check the logs
journalctl -u wrolpi-kiwix
;
return
}
export function ZimRoute() {
useTitle('Zim');
const links = [
{text: 'Viewer', to: '/zim', key: 'zim', end: true},
{text: 'Manage', to: '/zim/manage', key: 'manage'},
]
return
}/>
}/>
}