feat(blacklist): allow easy import and export of blacktag configuration

This commit is contained in:
Ben Beauchamp
2025-01-25 20:32:28 -06:00
parent 299f358c59
commit 3921e4ce74
4 changed files with 456 additions and 13 deletions

View File

@@ -0,0 +1,431 @@
import Modal from '@app/components/Common/Modal';
import Tooltip from '@app/components/Common/Tooltip';
import { encodeURIExtraParams } from '@app/hooks/useDiscover';
import defineMessages from '@app/utils/defineMessages';
import { Transition } from '@headlessui/react';
import {
ArrowDownIcon,
ClipboardDocumentIcon,
} from '@heroicons/react/24/solid';
import type { TmdbKeywordSearchResponse } from '@server/api/themoviedb/interfaces';
import type { Keyword } from '@server/models/common';
import { useFormikContext } from 'formik';
import {
forwardRef,
useCallback,
useEffect,
useImperativeHandle,
useRef,
useState,
} from 'react';
import { useIntl } from 'react-intl';
import type { ClearIndicatorProps, GroupBase, MultiValue } from 'react-select';
import { components } from 'react-select';
import AsyncSelect from 'react-select/async';
import { useToasts } from 'react-toast-notifications';
import useClipboard from 'react-use-clipboard';
const messages = defineMessages('components.Settings', {
copyBlacktags: 'Copied blacktags to clipboard.',
copyBlacktagsTip: 'Copy blacktag configuration',
importBlacktagsTip: 'Import blacktag configuration',
clearBlacktagsConfirm: 'Are you sure you want to clear the blacktags?',
yes: 'Yes',
no: 'No',
searchKeywords: 'Search keywords…',
starttyping: 'Starting typing to search.',
nooptions: 'No results.',
blacktagImportTitle: 'Import Blacktag Configuration',
blacktagImportInstructions: 'Paste blacktag configuration below.',
valueRequired: 'You must provide a value.',
noSpecialCharacters:
'Configuration must be a comma delimited list of TMDB keyword ids, and must not start or end with a comma.',
invalidKeyword: '{keywordId} is not a TMDB keyword.',
});
type SingleVal = {
label: string;
value: number;
};
type BlacktagsSelectorProps = {
defaultValue?: string;
};
const BlacktagsSelector = ({ defaultValue }: BlacktagsSelectorProps) => {
const { setFieldValue } = useFormikContext();
const [value, setValue] = useState<string | undefined>(defaultValue);
const [selectorValue, setSelectorValue] =
useState<MultiValue<SingleVal> | null>(null);
const update = useCallback(
(value: MultiValue<SingleVal> | null) => {
const strVal = value?.map((v) => v.value).join(',');
setSelectorValue(value);
setValue(strVal);
setFieldValue('blacktags', strVal);
},
[setSelectorValue, setValue, setFieldValue]
);
return (
<>
<ControlledKeywordSelector
value={selectorValue}
onChange={update}
defaultValue={defaultValue}
components={{
DropdownIndicator: undefined,
IndicatorSeparator: undefined,
ClearIndicator: VerifyClearIndicator,
}}
/>
<BlacktagsCopyButton value={value ?? ''} />
<BlacktagsImportButton setSelector={update} />
</>
);
};
type BaseSelectorMultiProps = {
defaultValue?: string;
value: MultiValue<SingleVal> | null;
onChange: (value: MultiValue<SingleVal> | null) => void;
components?: Partial<typeof components>;
};
const ControlledKeywordSelector = ({
defaultValue,
onChange,
components,
value,
}: BaseSelectorMultiProps) => {
const intl = useIntl();
useEffect(() => {
const loadDefaultKeywords = async (): Promise<void> => {
if (!defaultValue) {
return;
}
const keywords = await Promise.all(
defaultValue.split(',').map(async (keywordId) => {
const res = await fetch(`/api/v1/keyword/${keywordId}`);
if (!res.ok) {
throw new Error('Network response was not ok');
}
const keyword: Keyword = await res.json();
return keyword;
})
);
onChange(
keywords.map((keyword) => ({
label: keyword.name,
value: keyword.id,
}))
);
};
loadDefaultKeywords();
}, [defaultValue, onChange]);
const loadKeywordOptions = async (inputValue: string) => {
const res = await fetch(
`/api/v1/search/keyword?query=${encodeURIExtraParams(inputValue)}`
);
if (!res.ok) {
throw new Error('Network response was not ok');
}
const results: TmdbKeywordSearchResponse = await res.json();
return results.results.map((result) => ({
label: result.name,
value: result.id,
}));
};
return (
<AsyncSelect
key={`keyword-select-blacktags`}
inputId="data"
isMulti
className="react-select-container"
classNamePrefix="react-select"
noOptionsMessage={({ inputValue }) =>
inputValue === ''
? intl.formatMessage(messages.starttyping)
: intl.formatMessage(messages.nooptions)
}
value={value}
loadOptions={loadKeywordOptions}
placeholder={intl.formatMessage(messages.searchKeywords)}
onChange={onChange}
components={components}
/>
);
};
type BlacktagsCopyButtonProps = {
value: string;
};
const BlacktagsCopyButton = ({ value }: BlacktagsCopyButtonProps) => {
const intl = useIntl();
const [isCopied, setCopied] = useClipboard(value, {
successDuration: 1000,
});
const { addToast } = useToasts();
useEffect(() => {
if (isCopied) {
addToast(intl.formatMessage(messages.copyBlacktags), {
appearance: 'info',
autoDismiss: true,
});
}
}, [isCopied, addToast, intl]);
return (
<Tooltip
content={intl.formatMessage(messages.copyBlacktagsTip)}
tooltipConfig={{ followCursor: false }}
>
<button
onClick={(e) => {
e.preventDefault();
setCopied();
}}
className="input-action"
type="button"
>
<ClipboardDocumentIcon />
</button>
</Tooltip>
);
};
type BlacktagsImportButton = {
setSelector: (value: MultiValue<SingleVal>) => void;
};
const BlacktagsImportButton = ({ setSelector }: BlacktagsImportButton) => {
const [show, setShow] = useState(false);
const formRef = useRef<HTMLFormElement>(null);
const intl = useIntl();
const onConfirm = useCallback(async () => {
if (formRef.current) {
if (await formRef.current.submitForm()) {
setShow(false);
}
}
}, []);
const onClick = useCallback((event: React.MouseEvent) => {
event.stopPropagation();
setShow(true);
}, []);
return (
<>
<Transition
as="div"
enter="transition-opacity duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition-opacity duration-300"
leaveFrom="opacity-100"
leaveTo="opacity-0"
show={show}
>
<Modal
title={intl.formatMessage(messages.blacktagImportTitle)}
okText="Confirm"
onOk={onConfirm}
onCancel={() => setShow(false)}
>
<BlacktagImportForm ref={formRef} setSelector={setSelector} />
</Modal>
</Transition>
<Tooltip
content={intl.formatMessage(messages.importBlacktagsTip)}
tooltipConfig={{ followCursor: false }}
>
<button className="input-action" onClick={onClick} type="button">
<ArrowDownIcon />
</button>
</Tooltip>
</>
);
};
type BlacktagImportFormProps = BlacktagsImportButton;
const BlacktagImportForm = forwardRef<
Partial<HTMLFormElement>,
BlacktagImportFormProps
>((props, ref) => {
const { setSelector } = props;
const intl = useIntl();
const [formValue, setFormValue] = useState('');
const [errors, setErrors] = useState<string[]>([]);
useImperativeHandle(ref, () => ({
submitForm: handleSubmit,
formValue,
}));
const validate = async () => {
if (formValue.length === 0) {
setErrors([intl.formatMessage(messages.valueRequired)]);
return false;
}
if (!/^(?:\d+,)*\d+$/.test(formValue)) {
setErrors([intl.formatMessage(messages.noSpecialCharacters)]);
return false;
}
const keywords = await Promise.allSettled(
formValue.split(',').map(async (keywordId) => {
const res = await fetch(`/api/v1/keyword/${keywordId}`);
if (!res.ok) {
throw intl.formatMessage(messages.invalidKeyword, { keywordId });
}
const keyword: Keyword = await res.json();
return {
label: keyword.name,
value: keyword.id,
};
})
);
const failures = keywords.filter(
(res) => res.status === 'rejected'
) as PromiseRejectedResult[];
if (failures.length > 0) {
setErrors(failures.map((failure) => `${failure.reason}`));
return false;
}
setSelector(
(keywords as PromiseFulfilledResult<SingleVal>[]).map(
(keyword) => keyword.value
)
);
setErrors([]);
return true;
};
const handleSubmit = validate;
return (
<form onSubmit={handleSubmit}>
<div>
<label htmlFor="value">
{intl.formatMessage(messages.blacktagImportInstructions)}
</label>
<textarea
id="value"
value={formValue}
onChange={(e) => setFormValue(e.target.value)}
className="h-20"
/>
{errors.length > 0 && (
<div className="error">
{errors.map((error) => (
<div key={error}>{error}</div>
))}
</div>
)}
</div>
</form>
);
});
const VerifyClearIndicator = <
Option,
IsMuti extends boolean,
Group extends GroupBase<Option>
>(
props: ClearIndicatorProps<Option, IsMuti, Group>
) => {
const { clearValue } = props;
const [show, setShow] = useState(false);
const intl = useIntl();
const openForm = useCallback(() => {
setShow(true);
}, [setShow]);
const openFormKey = useCallback(
(event: React.KeyboardEvent) => {
if (show) return;
if (event.key === 'Enter' || event.key === 'Space') {
setShow(true);
}
},
[setShow, show]
);
const acceptForm = useCallback(
(event: KeyboardEvent) => {
if (event.key === 'Enter') {
event.stopPropagation();
event.preventDefault();
clearValue();
}
},
[clearValue]
);
useEffect(() => {
if (show) {
window.addEventListener('keydown', acceptForm);
}
return () => window.removeEventListener('keydown', acceptForm);
}, [show, acceptForm]);
return (
<>
<button
type="button"
onClick={openForm}
onKeyDown={openFormKey}
className="react-select__indicator react-select__clear-indicator css-1xc3v61-indicatorContainer cursor-pointer"
>
<components.CrossIcon />
</button>
<Transition
as="div"
enter="transition-opacity duration-300"
enterFrom="opacity-0"
enterTo="opacity-100"
leave="transition-opacity duration-300"
leaveFrom="opacity-100"
leaveTo="opacity-0"
show={show}
>
<Modal
subTitle={intl.formatMessage(messages.clearBlacktagsConfirm)}
okText={intl.formatMessage(messages.yes)}
cancelText={intl.formatMessage(messages.no)}
onOk={clearValue}
onCancel={() => setShow(false)}
>
<form />{' '}
{/* Form prevents accidentally saving settings when pressing enter */}
</Modal>
</Transition>
</>
);
};
export default BlacktagsSelector;

View File

@@ -1,10 +1,10 @@
import BlacktagsSelector from '@app/components/BlacktagsSelector';
import Button from '@app/components/Common/Button';
import LoadingSpinner from '@app/components/Common/LoadingSpinner';
import PageTitle from '@app/components/Common/PageTitle';
import SensitiveInput from '@app/components/Common/SensitiveInput';
import LanguageSelector from '@app/components/LanguageSelector';
import RegionSelector from '@app/components/RegionSelector';
import { KeywordSelector } from '@app/components/Selector';
import CopyButton from '@app/components/Settings/CopyButton';
import SettingsBadge from '@app/components/Settings/SettingsBadge';
import type { AvailableLocale } from '@app/context/LanguageContext';
@@ -392,16 +392,7 @@ const SettingsMain = () => {
</label>
<div className="form-input-area">
<div className="form-input-field relative z-10">
<KeywordSelector
isMulti
onChange={(value) => {
setFieldValue(
'blacktags',
value?.map((v) => v.value).join(',')
);
}}
defaultValue={values.blacktags}
/>
<BlacktagsSelector defaultValue={values.blacktags} />
</div>
</div>
</div>

View File

@@ -10,9 +10,12 @@
"components.Blacklist.blacklistdate": "date",
"components.Blacklist.blacklistedby": "{date} by {user}",
"components.Blacklist.blacklistsettings": "Blacklist Settings",
"components.Blacklist.filterBlacktags": "Blacktags",
"components.Blacklist.filterManual": "Manual",
"components.Blacklist.mediaName": "Name",
"components.Blacklist.mediaTmdbId": "tmdb Id",
"components.Blacklist.mediaType": "Type",
"components.Blacklist.showAllBlacklisted": "Show All Blacklisted Media",
"components.CollectionDetails.numberofmovies": "{count} Movies",
"components.CollectionDetails.overview": "Overview",
"components.CollectionDetails.requestcollection": "Request Collection",
@@ -100,7 +103,6 @@
"components.Discover.StudioSlider.studios": "Studios",
"components.Discover.TvGenreList.seriesgenres": "Series Genres",
"components.Discover.TvGenreSlider.tvgenres": "Series Genres",
"components.DiscoverTvUpcoming.upcomingtv": "Upcoming Series",
"components.Discover.createnewslider": "Create New Slider",
"components.Discover.customizediscover": "Customize Discover",
"components.Discover.discover": "Discover",
@@ -134,6 +136,7 @@
"components.Discover.upcomingtv": "Upcoming Series",
"components.Discover.updatefailed": "Something went wrong updating the discover customization settings.",
"components.Discover.updatesuccess": "Updated discover customization settings.",
"components.DiscoverTvUpcoming.upcomingtv": "Upcoming Series",
"components.DownloadBlock.estimatedtime": "Estimated {time}",
"components.DownloadBlock.formattedTitle": "{title}: Season {seasonNumber} Episode {episodeNumber}",
"components.IssueDetails.IssueComment.areyousuredelete": "Are you sure you want to delete this comment?",
@@ -1050,8 +1053,13 @@
"components.Settings.addsonarr": "Add Sonarr Server",
"components.Settings.advancedTooltip": "Incorrectly configuring this setting may result in broken functionality",
"components.Settings.apiKey": "API key",
"components.Settings.blacktagImportInstructions": "Paste blacktag configuration below.",
"components.Settings.blacktagImportTitle": "Import Blacktag Configuration",
"components.Settings.cancelscan": "Cancel Scan",
"components.Settings.clearBlacktagsConfirm": "Are you sure you want to clear the blacktags?",
"components.Settings.copied": "Copied API key to clipboard.",
"components.Settings.copyBlacktags": "Copied blacktags to clipboard.",
"components.Settings.copyBlacktagsTip": "Copy blacktag configuration",
"components.Settings.currentlibrary": "Current Library: {name}",
"components.Settings.default": "Default",
"components.Settings.default4k": "Default 4K",
@@ -1062,6 +1070,8 @@
"components.Settings.experimentalTooltip": "Enabling this setting may result in unexpected application behavior",
"components.Settings.externalUrl": "External URL",
"components.Settings.hostname": "Hostname or IP Address",
"components.Settings.importBlacktagsTip": "Import blacktag configuration",
"components.Settings.invalidKeyword": "{keywordId} is not a TMDB keyword.",
"components.Settings.invalidurlerror": "Unable to connect to {mediaServerName} server.",
"components.Settings.is4k": "4K",
"components.Settings.jellyfinForgotPasswordUrl": "Forgot Password URL",
@@ -1093,9 +1103,12 @@
"components.Settings.menuPlexSettings": "Plex",
"components.Settings.menuServices": "Services",
"components.Settings.menuUsers": "Users",
"components.Settings.no": "No",
"components.Settings.noDefault4kServer": "A 4K {serverType} server must be marked as default in order to enable users to submit 4K {mediaType} requests.",
"components.Settings.noDefaultNon4kServer": "If you only have a single {serverType} server for both non-4K and 4K content (or if you only download 4K content), your {serverType} server should <strong>NOT</strong> be designated as a 4K server.",
"components.Settings.noDefaultServer": "At least one {serverType} server must be marked as default in order for {mediaType} requests to be processed.",
"components.Settings.noSpecialCharacters": "Configuration must be a comma delimited list of TMDB keyword ids, and must not start or end with a comma.",
"components.Settings.nooptions": "No results.",
"components.Settings.notificationAgentSettingsDescription": "Configure and enable notification agents.",
"components.Settings.notifications": "Notifications",
"components.Settings.notificationsettings": "Notification Settings",
@@ -1115,6 +1128,7 @@
"components.Settings.scan": "Sync Libraries",
"components.Settings.scanbackground": "Scanning will run in the background. You can continue the setup process in the meantime.",
"components.Settings.scanning": "Syncing…",
"components.Settings.searchKeywords": "Search keywords…",
"components.Settings.serverLocal": "local",
"components.Settings.serverRemote": "remote",
"components.Settings.serverSecure": "secure",
@@ -1128,6 +1142,7 @@
"components.Settings.sonarrsettings": "Sonarr Settings",
"components.Settings.ssl": "SSL",
"components.Settings.startscan": "Start Scan",
"components.Settings.starttyping": "Starting typing to search.",
"components.Settings.syncJellyfin": "Sync Libraries",
"components.Settings.syncing": "Syncing",
"components.Settings.tautulliApiKey": "API Key",
@@ -1151,10 +1166,12 @@
"components.Settings.validationUrlBaseLeadingSlash": "URL base must have a leading slash",
"components.Settings.validationUrlBaseTrailingSlash": "URL base must not end in a trailing slash",
"components.Settings.validationUrlTrailingSlash": "URL must not end in a trailing slash",
"components.Settings.valueRequired": "You must provide a value.",
"components.Settings.webAppUrl": "<WebAppLink>Web App</WebAppLink> URL",
"components.Settings.webAppUrlTip": "Optionally direct users to the web app on your server instead of the \"hosted\" web app",
"components.Settings.webhook": "Webhook",
"components.Settings.webpush": "Web Push",
"components.Settings.yes": "Yes",
"components.Setup.back": "Go back",
"components.Setup.configemby": "Configure Emby",
"components.Setup.configjellyfin": "Configure Jellyfin",
@@ -1167,7 +1184,7 @@
"components.Setup.librarieserror": "Validation failed. Please toggle the libraries again to continue.",
"components.Setup.servertype": "Choose Server Type",
"components.Setup.setup": "Setup",
"components.Setup.signin": "Sign In",
"components.Setup.signin": "Sign in to your account",
"components.Setup.signinMessage": "Get started by signing in",
"components.Setup.signinWithEmby": "Enter your Emby details",
"components.Setup.signinWithJellyfin": "Enter your Jellyfin details",

View File

@@ -382,6 +382,10 @@
@apply w-full;
}
.react-select-container:has(+ .input-action) .react-select__control {
@apply rounded-r-none border border-gray-500 bg-gray-700 text-white hover:border-gray-500;
}
.react-select-container .react-select__control {
@apply rounded-md border border-gray-500 bg-gray-700 text-white hover:border-gray-500;
}