mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
* Update zh-tw string table (till 975343d2)
* Prepare localization table
* Implement i18n for Performers & Tags
* Add "add" action strings
* Use Lodash merge for deep merging language JSONs
The original implementation does not properly merge language files, causing unexpected localization string fallback behavior.
* Localize pagination strings
* Use Field name value as null id fallback
...otherwise FormattedMessage is gonna throw when the ID is null
* Use localized "Path" string for all instances
* Localize the "Interface" tab under settings
* Localize scene & performer cards
* Rename locale folder for better compatibility with i18n-ally
* Localize majority of the categories and features
380 lines
9.1 KiB
TypeScript
380 lines
9.1 KiB
TypeScript
import React from "react";
|
|
import {
|
|
Form,
|
|
Col,
|
|
Row,
|
|
InputGroup,
|
|
Button,
|
|
FormControl,
|
|
Badge,
|
|
} from "react-bootstrap";
|
|
import { CollapseButton, Icon, Modal } from "src/components/Shared";
|
|
import _ from "lodash";
|
|
import { FormattedMessage, useIntl } from "react-intl";
|
|
|
|
export class ScrapeResult<T> {
|
|
public newValue?: T;
|
|
public originalValue?: T;
|
|
public scraped: boolean = false;
|
|
public useNewValue: boolean = false;
|
|
|
|
public constructor(originalValue?: T | null, newValue?: T | null) {
|
|
this.originalValue = originalValue ?? undefined;
|
|
this.newValue = newValue ?? undefined;
|
|
|
|
const valuesEqual = _.isEqual(originalValue, newValue);
|
|
this.useNewValue = !!this.newValue && !valuesEqual;
|
|
this.scraped = this.useNewValue;
|
|
}
|
|
|
|
public setOriginalValue(value?: T) {
|
|
this.originalValue = value;
|
|
this.newValue = value;
|
|
}
|
|
|
|
public cloneWithValue(value?: T) {
|
|
const ret = _.clone(this);
|
|
|
|
ret.newValue = value;
|
|
ret.useNewValue = !_.isEqual(ret.newValue, ret.originalValue);
|
|
ret.scraped = ret.useNewValue;
|
|
|
|
return ret;
|
|
}
|
|
|
|
public getNewValue() {
|
|
if (this.useNewValue) {
|
|
return this.newValue;
|
|
}
|
|
}
|
|
}
|
|
|
|
interface IHasName {
|
|
name: string;
|
|
}
|
|
|
|
interface IScrapedFieldProps<T> {
|
|
result: ScrapeResult<T>;
|
|
}
|
|
|
|
interface IScrapedRowProps<T, V extends IHasName>
|
|
extends IScrapedFieldProps<T> {
|
|
title: string;
|
|
renderOriginalField: (result: ScrapeResult<T>) => JSX.Element | undefined;
|
|
renderNewField: (result: ScrapeResult<T>) => JSX.Element | undefined;
|
|
onChange: (value: ScrapeResult<T>) => void;
|
|
newValues?: V[];
|
|
onCreateNew?: (newValue: V) => void;
|
|
}
|
|
|
|
function renderButtonIcon(selected: boolean) {
|
|
const className = selected ? "text-success" : "text-muted";
|
|
|
|
return (
|
|
<Icon
|
|
className={`fa-fw ${className}`}
|
|
icon={selected ? "check" : "times"}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export const ScrapeDialogRow = <T, V extends IHasName>(
|
|
props: IScrapedRowProps<T, V>
|
|
) => {
|
|
function handleSelectClick(isNew: boolean) {
|
|
const ret = _.clone(props.result);
|
|
ret.useNewValue = isNew;
|
|
props.onChange(ret);
|
|
}
|
|
|
|
function hasNewValues() {
|
|
return props.newValues && props.newValues.length > 0 && props.onCreateNew;
|
|
}
|
|
|
|
if (!props.result.scraped && !hasNewValues()) {
|
|
return <></>;
|
|
}
|
|
|
|
function renderNewValues() {
|
|
if (!hasNewValues()) {
|
|
return;
|
|
}
|
|
|
|
const ret = (
|
|
<>
|
|
{props.newValues!.map((t) => (
|
|
<Badge
|
|
className="tag-item"
|
|
variant="secondary"
|
|
key={t.name}
|
|
onClick={() => props.onCreateNew!(t)}
|
|
>
|
|
{t.name}
|
|
<Button className="minimal ml-2">
|
|
<Icon className="fa-fw" icon="plus" />
|
|
</Button>
|
|
</Badge>
|
|
))}
|
|
</>
|
|
);
|
|
|
|
const minCollapseLength = 10;
|
|
|
|
if (props.newValues!.length >= minCollapseLength) {
|
|
return (
|
|
<CollapseButton text={`Missing (${props.newValues!.length})`}>
|
|
{ret}
|
|
</CollapseButton>
|
|
);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
return (
|
|
<Row className="px-3 pt-3">
|
|
<Form.Label column lg="3">
|
|
{props.title}
|
|
</Form.Label>
|
|
|
|
<Col lg="9">
|
|
<Row>
|
|
<Col xs="6">
|
|
<InputGroup>
|
|
<InputGroup.Prepend className="bg-secondary text-white border-secondary">
|
|
<Button
|
|
variant="secondary"
|
|
onClick={() => handleSelectClick(false)}
|
|
>
|
|
{renderButtonIcon(!props.result.useNewValue)}
|
|
</Button>
|
|
</InputGroup.Prepend>
|
|
{props.renderOriginalField(props.result)}
|
|
</InputGroup>
|
|
</Col>
|
|
<Col xs="6">
|
|
<InputGroup>
|
|
<InputGroup.Prepend>
|
|
<Button
|
|
variant="secondary"
|
|
onClick={() => handleSelectClick(true)}
|
|
>
|
|
{renderButtonIcon(props.result.useNewValue)}
|
|
</Button>
|
|
</InputGroup.Prepend>
|
|
{props.renderNewField(props.result)}
|
|
</InputGroup>
|
|
{renderNewValues()}
|
|
</Col>
|
|
</Row>
|
|
</Col>
|
|
</Row>
|
|
);
|
|
};
|
|
|
|
interface IScrapedInputGroupProps {
|
|
isNew?: boolean;
|
|
placeholder?: string;
|
|
result: ScrapeResult<string>;
|
|
onChange?: (value: string) => void;
|
|
}
|
|
|
|
const ScrapedInputGroup: React.FC<IScrapedInputGroupProps> = (props) => {
|
|
return (
|
|
<FormControl
|
|
placeholder={props.placeholder}
|
|
value={props.isNew ? props.result.newValue : props.result.originalValue}
|
|
readOnly={!props.isNew}
|
|
onChange={(e) => {
|
|
if (props.isNew && props.onChange) {
|
|
props.onChange(e.target.value);
|
|
}
|
|
}}
|
|
className="bg-secondary text-white border-secondary"
|
|
/>
|
|
);
|
|
};
|
|
|
|
interface IScrapedInputGroupRowProps {
|
|
title: string;
|
|
placeholder?: string;
|
|
result: ScrapeResult<string>;
|
|
onChange: (value: ScrapeResult<string>) => void;
|
|
}
|
|
|
|
export const ScrapedInputGroupRow: React.FC<IScrapedInputGroupRowProps> = (
|
|
props
|
|
) => {
|
|
return (
|
|
<ScrapeDialogRow
|
|
title={props.title}
|
|
result={props.result}
|
|
renderOriginalField={() => (
|
|
<ScrapedInputGroup
|
|
placeholder={props.placeholder || props.title}
|
|
result={props.result}
|
|
/>
|
|
)}
|
|
renderNewField={() => (
|
|
<ScrapedInputGroup
|
|
placeholder={props.placeholder || props.title}
|
|
result={props.result}
|
|
isNew
|
|
onChange={(value) =>
|
|
props.onChange(props.result.cloneWithValue(value))
|
|
}
|
|
/>
|
|
)}
|
|
onChange={props.onChange}
|
|
/>
|
|
);
|
|
};
|
|
|
|
const ScrapedTextArea: React.FC<IScrapedInputGroupProps> = (props) => {
|
|
return (
|
|
<FormControl
|
|
as="textarea"
|
|
placeholder={props.placeholder}
|
|
value={props.isNew ? props.result.newValue : props.result.originalValue}
|
|
readOnly={!props.isNew}
|
|
onChange={(e) => {
|
|
if (props.isNew && props.onChange) {
|
|
props.onChange(e.target.value);
|
|
}
|
|
}}
|
|
className="bg-secondary text-white border-secondary scene-description"
|
|
/>
|
|
);
|
|
};
|
|
|
|
export const ScrapedTextAreaRow: React.FC<IScrapedInputGroupRowProps> = (
|
|
props
|
|
) => {
|
|
return (
|
|
<ScrapeDialogRow
|
|
title={props.title}
|
|
result={props.result}
|
|
renderOriginalField={() => (
|
|
<ScrapedTextArea
|
|
placeholder={props.placeholder || props.title}
|
|
result={props.result}
|
|
/>
|
|
)}
|
|
renderNewField={() => (
|
|
<ScrapedTextArea
|
|
placeholder={props.placeholder || props.title}
|
|
result={props.result}
|
|
isNew
|
|
onChange={(value) =>
|
|
props.onChange(props.result.cloneWithValue(value))
|
|
}
|
|
/>
|
|
)}
|
|
onChange={props.onChange}
|
|
/>
|
|
);
|
|
};
|
|
|
|
interface IScrapedImageProps {
|
|
isNew?: boolean;
|
|
className?: string;
|
|
placeholder?: string;
|
|
result: ScrapeResult<string>;
|
|
}
|
|
|
|
const ScrapedImage: React.FC<IScrapedImageProps> = (props) => {
|
|
const value = props.isNew
|
|
? props.result.newValue
|
|
: props.result.originalValue;
|
|
|
|
if (!value) {
|
|
return <></>;
|
|
}
|
|
|
|
return (
|
|
<img className={props.className} src={value} alt={props.placeholder} />
|
|
);
|
|
};
|
|
|
|
interface IScrapedImageRowProps {
|
|
title: string;
|
|
className?: string;
|
|
result: ScrapeResult<string>;
|
|
onChange: (value: ScrapeResult<string>) => void;
|
|
}
|
|
|
|
export const ScrapedImageRow: React.FC<IScrapedImageRowProps> = (props) => {
|
|
return (
|
|
<ScrapeDialogRow
|
|
title={props.title}
|
|
result={props.result}
|
|
renderOriginalField={() => (
|
|
<ScrapedImage
|
|
result={props.result}
|
|
className={props.className}
|
|
placeholder={props.title}
|
|
/>
|
|
)}
|
|
renderNewField={() => (
|
|
<ScrapedImage
|
|
result={props.result}
|
|
className={props.className}
|
|
placeholder={props.title}
|
|
isNew
|
|
/>
|
|
)}
|
|
onChange={props.onChange}
|
|
/>
|
|
);
|
|
};
|
|
|
|
interface IScrapeDialogProps {
|
|
title: string;
|
|
renderScrapeRows: () => JSX.Element;
|
|
onClose: (apply?: boolean) => void;
|
|
}
|
|
|
|
export const ScrapeDialog: React.FC<IScrapeDialogProps> = (
|
|
props: IScrapeDialogProps
|
|
) => {
|
|
const intl = useIntl();
|
|
return (
|
|
<Modal
|
|
show
|
|
icon="pencil-alt"
|
|
header={props.title}
|
|
accept={{
|
|
onClick: () => {
|
|
props.onClose(true);
|
|
},
|
|
text: intl.formatMessage({ id: "actions.apply" }),
|
|
}}
|
|
cancel={{
|
|
onClick: () => props.onClose(),
|
|
text: intl.formatMessage({ id: "actions.cancel" }),
|
|
variant: "secondary",
|
|
}}
|
|
modalProps={{ size: "lg", dialogClassName: "scrape-dialog" }}
|
|
>
|
|
<div className="dialog-container">
|
|
<Form>
|
|
<Row className="px-3 pt-3">
|
|
<Col lg={{ span: 9, offset: 3 }}>
|
|
<Row>
|
|
<Form.Label column xs="6">
|
|
<FormattedMessage id="dialogs.scrape_results_existing" />
|
|
</Form.Label>
|
|
<Form.Label column xs="6">
|
|
<FormattedMessage id="dialogs.scrape_results_scraped" />
|
|
</Form.Label>
|
|
</Row>
|
|
</Col>
|
|
</Row>
|
|
|
|
{props.renderScrapeRows()}
|
|
</Form>
|
|
</div>
|
|
</Modal>
|
|
);
|
|
};
|