import React, { useRef, useState } from "react"; import { Button, Form } from "react-bootstrap"; import { FormattedMessage, useIntl } from "react-intl"; import { SettingSection } from "./SettingSection"; import * as GQL from "src/core/generated-graphql"; import { SettingModal } from "./Inputs"; export interface IStashBoxModal { value: GQL.StashBoxInput; close: (v?: GQL.StashBoxInput) => void; } const defaultMaxRequestsPerMinute = 240; export const StashBoxModal: React.FC = ({ value, close }) => { const intl = useIntl(); const endpoint = useRef(null); const apiKey = useRef(null); const [validate, { data, loading }] = GQL.useValidateStashBoxLazyQuery({ fetchPolicy: "network-only", }); const handleValidate = () => { validate({ variables: { input: { endpoint: endpoint.current?.value ?? "", api_key: apiKey.current?.value ?? "", name: "test", }, }, }); }; return ( headingID="config.stashbox.title" value={value} renderField={(v, setValue) => ( <>
{intl.formatMessage({ id: "config.stashbox.name", })}
0} onChange={(e: React.ChangeEvent) => setValue({ ...v!, name: e.currentTarget.value }) } />
{intl.formatMessage({ id: "config.stashbox.graphql_endpoint", })}
0} onChange={(e: React.ChangeEvent) => setValue({ ...v!, endpoint: e.currentTarget.value.trim() }) } ref={endpoint} />
{intl.formatMessage({ id: "config.stashbox.api_key", })}
0} onChange={(e: React.ChangeEvent) => setValue({ ...v!, api_key: e.currentTarget.value.trim() }) } ref={apiKey} />
{data && ( {data.validateStashBoxCredentials?.status} )}
{intl.formatMessage({ id: "config.stashbox.max_requests_per_minute", })}
= 0 } type="number" onChange={(e: React.ChangeEvent) => setValue({ ...v!, max_requests_per_minute: parseInt(e.currentTarget.value), }) } ref={apiKey} />
)} close={close} /> ); }; interface IStashBoxSetting { value: GQL.StashBoxInput[]; onChange: (v: GQL.StashBoxInput[]) => void; } export const StashBoxSetting: React.FC = ({ value, onChange, }) => { const [isCreating, setIsCreating] = useState(false); const [editingIndex, setEditingIndex] = useState(); function onEdit(index: number) { setEditingIndex(index); } function onDelete(index: number) { onChange(value.filter((v, i) => i !== index)); } function onNew() { setIsCreating(true); } return ( {isCreating ? ( { if (v) onChange([...value, v]); setIsCreating(false); }} /> ) : undefined} {editingIndex !== undefined ? ( { if (v) onChange( value.map((vv, index) => { if (index === editingIndex) { return v; } return vv; }) ); setEditingIndex(undefined); }} /> ) : undefined} {value.map((b, index) => ( // eslint-disable-next-line react/no-array-index-key

{b.name ?? `#${index}`}

{b.endpoint ?? ""}
))}
); };