import * as React from "react" import intl from "react-intl-universal" import { ServiceConfigsTabProps } from "../service" import { SyncService } from "../../../schema-types" import { Stack, Icon, Label, TextField, PrimaryButton, DefaultButton, Checkbox, MessageBar, MessageBarType, Dropdown, IDropdownOption, } from "@fluentui/react" import DangerButton from "../../utils/danger-button" import { urlTest } from "../../../scripts/utils" import LiteExporter from "./lite-exporter" import { MinifluxConfigs } from "../../../scripts/models/services/miniflux" type MinifluxConfigsTabState = { existing: boolean endpoint: string apiKeyAuth: boolean username: string password: string apiKey: string fetchLimit: number importGroups: boolean } class MinifluxConfigsTab extends React.Component< ServiceConfigsTabProps, MinifluxConfigsTabState > { constructor(props: ServiceConfigsTabProps) { super(props) const configs = props.configs as MinifluxConfigs this.state = { existing: configs.type === SyncService.Miniflux, endpoint: configs.endpoint || "", apiKeyAuth: true, username: "", password: "", apiKey: "", fetchLimit: configs.fetchLimit || 250, importGroups: true, } } fetchLimitOptions = (): IDropdownOption[] => [ { key: 250, text: intl.get("service.fetchLimitNum", { count: 250 }) }, { key: 500, text: intl.get("service.fetchLimitNum", { count: 500 }) }, { key: 750, text: intl.get("service.fetchLimitNum", { count: 750 }) }, { key: 1000, text: intl.get("service.fetchLimitNum", { count: 1000 }) }, { key: 1500, text: intl.get("service.fetchLimitNum", { count: 1500 }) }, { key: Number.MAX_SAFE_INTEGER, text: intl.get("service.fetchUnlimited"), }, ] onFetchLimitOptionChange = (_, option: IDropdownOption) => { this.setState({ fetchLimit: option.key as number }) } authenticationOptions = (): IDropdownOption[] => [ { key: "apiKey", text: "API Key" /*intl.get("service.password")*/ }, { key: "userPass", text: intl.get("service.username") + "/" + intl.get("service.password")} ] onAuthenticationOptionsChange = (_, option: IDropdownOption) => { this.setState({ apiKeyAuth: option.key == "apiKey" }) } handleInputChange = event => { const name: string = event.target.name // @ts-expect-error this.setState({ [name]: event.target.value }) } checkNotEmpty = (v: string) => { return !this.state.existing && v.length == 0 ? intl.get("emptyField") : "" } validateForm = () => { return ( urlTest(this.state.endpoint.trim()) && (this.state.existing || this.state.apiKey || (this.state.username && this.state.password)) ) } save = async () => { let configs: MinifluxConfigs if (this.state.existing) { configs = { ...this.props.configs, endpoint: this.state.endpoint, fetchLimit: this.state.fetchLimit, } as MinifluxConfigs if (this.state.apiKey || this.state.password) configs.authKey = this.state.apiKeyAuth ? this.state.apiKey : Buffer.from(this.state.username + ":" + this.state.password, 'binary').toString('base64') } else { configs = { type: SyncService.Miniflux, endpoint: this.state.endpoint, apiKeyAuth: this.state.apiKeyAuth, authKey: this.state.apiKeyAuth ? this.state.apiKey : Buffer.from(this.state.username + ":" + this.state.password, 'binary').toString('base64'), fetchLimit: this.state.fetchLimit, } if (this.state.importGroups) configs.importGroups = true } this.props.blockActions() const valid = await this.props.authenticate(configs) if (valid) { this.props.save(configs) this.setState({ existing: true }) this.props.sync() } else { this.props.blockActions() window.utils.showErrorBox( intl.get("service.failure"), intl.get("service.failureHint") ) } } remove = async () => { this.props.exit() await this.props.remove() } render() { return ( <> {!this.state.existing && ( {intl.get("service.overwriteWarning")} )} urlTest(v.trim()) ? "" : intl.get("sources.badUrl") } validateOnLoad={false} name="endpoint" value={this.state.endpoint} onChange={this.handleInputChange} /> { this.state.apiKeyAuth && } { !this.state.apiKeyAuth && } { !this.state.apiKeyAuth && } {!this.state.existing && ( this.setState({ importGroups: c }) } /> )} {this.state.existing ? ( ) : ( )} {this.state.existing && ( )} ) } } export default MinifluxConfigsTab