import React, {ChangeEventHandler, KeyboardEventHandler, MouseEventHandler, useEffect, useState} from 'react'; import IFrontendSettings from "./interfaces/IFrontendSettings"; import '../styles/settings.css'; import IBackendSettings from "./interfaces/IBackendSettings"; import {getData, postData} from "../App"; import LibraryConnector, {Kavita, Komga} from "./LibraryConnector"; import NotificationConnector, {Gotify, Lunasea, Ntfy} from "./NotificationConnector"; import ILibraryConnector from "./interfaces/ILibraryConnector"; import INotificationConnector from "./interfaces/INotificationConnector"; import Toggle from 'react-toggle'; import '../styles/react-toggle.css'; export default function Settings({backendConnected, apiUri, settings, changeSettings} : {backendConnected: boolean, apiUri: string, settings: IFrontendSettings, changeSettings: (settings: IFrontendSettings) => void}) { const [frontendSettings, setFrontendSettings] = useState(settings); const [backendSettings, setBackendSettings] = useState(); const [showSettings, setShowSettings] = useState(false); const [libraryConnectors, setLibraryConnectors] = useState(); const [notificationConnectors, setNotificationConnectors] = useState(); const [komgaSettings, setKomgaSettings] = useState<{url: string, username: string, password: string}>({url: "", username: "", password: ""}); const [kavitaSettings, setKavitaSettings] = useState<{url: string, username: string, password: string}>({url: "", username: "", password: ""}); const [gotifySettings, setGotifySettings] = useState<{url: string, appToken: string}>({url: "", appToken: ""}); const [lunaseaSettings, setLunaseaSettings] = useState<{webhook: string}>({webhook: ""}); const [ntfySettings, setNtfySettings] = useState<{url: string, username: string, password: string, topic: string | undefined}>({url: "", username: "", password: "", topic: undefined}); useEffect(() => { console.debug(`${showSettings ? "Showing" : "Not showing"} settings.`); if(!showSettings || !backendConnected) return; UpdateBackendSettings(); LibraryConnector.GetLibraryConnectors(apiUri).then(setLibraryConnectors).catch(console.error); NotificationConnector.GetNotificationConnectors(apiUri).then(setNotificationConnectors).catch(console.error); }, [showSettings]); useEffect(() => { changeSettings(frontendSettings); }, [frontendSettings]); async function GetSettings(apiUri: string) : Promise { //console.info("Getting Settings"); return getData(`${apiUri}/v2/Settings`) .then((json) => { //console.info("Got Settings"); const ret = json as IBackendSettings; //console.debug(ret); return (ret); }) .catch(Promise.reject); } function UpdateBackendSettings() { GetSettings(apiUri).then(setBackendSettings).catch(console.error); } const GetKomga = () : ILibraryConnector | undefined => libraryConnectors?.find(con => con.libraryType == 0); const KomgaConnected = () : boolean => GetKomga() != undefined; const GetKavita = () : ILibraryConnector | undefined => libraryConnectors?.find(con => con.libraryType == 1); const KavitaConnected = () : boolean => GetKavita() != undefined; const GetGotify = () : INotificationConnector | undefined => notificationConnectors?.find(con => con.notificationConnectorType == 0); const GotifyConnected = () : boolean => GetGotify() != undefined; const GetLunasea = () : INotificationConnector | undefined => notificationConnectors?.find(con => con.notificationConnectorType == 1); const LunaseaConnected = () : boolean => GetLunasea() != undefined; const GetNtfy = () : INotificationConnector | undefined => notificationConnectors?.find(con => con.notificationConnectorType == 2); const NtfyConnected = () : boolean => GetNtfy() != undefined; const SubmitApiUri : KeyboardEventHandler = (e) => { if(e.currentTarget.value.length < 1) return; if(e.key == "Enter"){ setFrontendSettings({...frontendSettings, apiUri: e.currentTarget.value}); RefreshInputs(); } } const SubmitUserAgent: KeyboardEventHandler = (e) => { if(e.currentTarget.value.length < 1 || backendSettings === undefined) return; if(e.key == "Enter"){ //console.info(`Updating Useragent ${e.currentTarget.value}`); postData(`${apiUri}/v2/Settings/UserAgent`, {value: e.currentTarget.value}) .then((json) => { //console.info(`Successfully updated Useragent ${e.currentTarget.value}`); UpdateBackendSettings(); RefreshInputs(); }) .catch(() => alert("Failed to update Useragent.")); } } const ResetUserAgent: MouseEventHandler = () => { //console.info(`Resetting Useragent`); postData(`${apiUri}/v2/Settings/UserAgent`, {value: undefined}) .then((json) => { //console.info(`Successfully reset Useragent`); UpdateBackendSettings(); RefreshInputs(); }) .catch(() => alert("Failed to update Useragent.")); } const SetAprilFoolsMode : ChangeEventHandler = (e) => { //console.info(`Updating AprilFoolsMode ${e.target.checked}`); postData(`${apiUri}/v2/Settings/AprilFoolsMode`, {value: e.target.checked}) .then((json) => { //console.info(`Successfully updated AprilFoolsMode ${e.currentTarget.value}`); UpdateBackendSettings(); }) } const SetCompressImages : MouseEventHandler = (e) => { //console.info(`Updating ImageCompression ${e.currentTarget.value}`); postData(`${apiUri}/v2/Settings/CompressImages`, {value: e.currentTarget.value}) .then((json) => { //console.info(`Successfully updated ImageCompression ${e.currentTarget.value}`); UpdateBackendSettings(); }) } const SetBWImages : ChangeEventHandler = (e) => { //console.info(`Updating B/W Images ${e.target.checked}`); postData(`${apiUri}/v2/Settings/BWImages`, {value: e.target.checked}) .then((json) => { //console.info(`Successfully updated B/W Images ${e.target.checked}`); UpdateBackendSettings(); }) } function RefreshInputs(){ alert("Saved."); setShowSettings(false); } return (
cogwheel setShowSettings(true)}/> {showSettings ?

Settings

Close Settings setShowSettings(false)}/>
TRANGA
API Settings Reset
Rate Limits
Appearance
Sources
Mangadex Logo MangaDex
LIBRARY CONNECTORS
Komga Logo Komga setKomgaSettings(s => ({...s, url: e.target.value}))} /> setKomgaSettings(s => ({...s, username: e.target.value}))} /> setKomgaSettings(s => ({...s, password: e.target.value}))} />
new Komga(komgaSettings).Test(apiUri).then(()=>alert("Test successful"))}>Test new Komga(komgaSettings).Reset(apiUri).then(RefreshInputs)}>Reset new Komga(komgaSettings).Create(apiUri).then(RefreshInputs)}>Apply
Kavita Logo Kavita setKavitaSettings(s => ({...s, url: e.target.value}))} /> setKavitaSettings(s => ({...s, username: e.target.value}))} /> setKavitaSettings(s => ({...s, password: e.target.value}))} />
new Kavita(kavitaSettings).Test(apiUri).then(()=>alert("Test successful"))}>Test new Kavita(kavitaSettings).Reset(apiUri).then(RefreshInputs)}>Reset new Kavita(kavitaSettings).Create(apiUri).then(RefreshInputs)}>Apply
NOTIFICATION CONNECTORS
Gotify Logo Gotify setGotifySettings(s => ({...s, url: e.target.value}))} /> setGotifySettings(s => ({...s, appToken: e.target.value}))} />
new Gotify(gotifySettings).Test(apiUri).then(()=>alert("Test successful"))}>Test new Gotify(gotifySettings).Reset(apiUri).then(RefreshInputs)}>Reset new Gotify(gotifySettings).Create(apiUri).then(RefreshInputs)}>Apply
Lunasea Logo LunaSea setLunaseaSettings(s => ({...s, webhook: e.target.value}))} />
new Lunasea(lunaseaSettings).Test(apiUri).then(()=>alert("Test successful"))}>Test new Lunasea(lunaseaSettings).Reset(apiUri).then(RefreshInputs)}>Reset new Lunasea(lunaseaSettings).Create(apiUri).then(RefreshInputs)}>Apply
ntfy Logo Ntfy setNtfySettings(s => ({...s, url: e.target.value}))} /> setNtfySettings(s => ({...s, username: e.target.value}))} /> setNtfySettings(s => ({...s, password: e.target.value}))} /> setNtfySettings(s => ({...s, topic: e.target.value}))} />
new Ntfy(ntfySettings).Test(apiUri).then(()=>alert("Test successful"))}>Test new Ntfy(ntfySettings).Reset(apiUri).then(RefreshInputs)}>Reset new Ntfy(ntfySettings).Create(apiUri).then(RefreshInputs)}>Apply
: <> }
); }