1
0
mirror of https://github.com/fallenbagel/jellyseerr.git synced 2026-01-11 17:16:50 -05:00
Files
jellyseerr/src/components/LanguageSelector/index.tsx
Danshil Kokil Mungur 5b2a8f682b build(deps): bump dependencies (#2454)
* build(deps): bump react-select from 4.3.1 to 5.2.2

* build(deps): bump axios from 0.21.4 to 0.25.0

* build(deps-dev): bump lint-staged from 12.2.1 to 12.2.2

Bumps [lint-staged](https://github.com/okonet/lint-staged) from 12.2.1 to 12.2.2.
- [Release notes](https://github.com/okonet/lint-staged/releases)
- [Commits](https://github.com/okonet/lint-staged/compare/v12.2.1...v12.2.2)

---
updated-dependencies:
- dependency-name: lint-staged
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* build(deps-dev): bump typescript from 4.5.4 to 4.5.5

Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.5.4 to 4.5.5.
- [Release notes](https://github.com/Microsoft/TypeScript/releases)
- [Commits](https://github.com/Microsoft/TypeScript/commits)

---
updated-dependencies:
- dependency-name: typescript
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* build(deps-dev): bump eslint-plugin-formatjs from 2.20.3 to 2.20.4

Bumps [eslint-plugin-formatjs](https://github.com/formatjs/formatjs) from 2.20.3 to 2.20.4.
- [Release notes](https://github.com/formatjs/formatjs/releases)
- [Commits](https://github.com/formatjs/formatjs/compare/eslint-plugin-formatjs@2.20.3...eslint-plugin-formatjs@2.20.4)

---
updated-dependencies:
- dependency-name: eslint-plugin-formatjs
  dependency-type: direct:development
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* build(deps-dev): bump @commitlint/cli from 16.0.3 to 16.1.0

Bumps [@commitlint/cli](https://github.com/conventional-changelog/commitlint/tree/HEAD/@commitlint/cli) from 16.0.3 to 16.1.0.
- [Release notes](https://github.com/conventional-changelog/commitlint/releases)
- [Changelog](https://github.com/conventional-changelog/commitlint/blob/master/@commitlint/cli/CHANGELOG.md)
- [Commits](https://github.com/conventional-changelog/commitlint/commits/v16.1.0/@commitlint/cli)

---
updated-dependencies:
- dependency-name: "@commitlint/cli"
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>

* build(deps): bump @tanem/react-nprogress from 4.0.3 to 4.0.4

Bumps [@tanem/react-nprogress](https://github.com/tanem/react-nprogress) from 4.0.3 to 4.0.4.
- [Release notes](https://github.com/tanem/react-nprogress/releases)
- [Changelog](https://github.com/tanem/react-nprogress/blob/master/CHANGELOG.md)
- [Commits](https://github.com/tanem/react-nprogress/compare/v4.0.3...v4.0.4)

---
updated-dependencies:
- dependency-name: "@tanem/react-nprogress"
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* fix: cleanup comments

* build(deps-dev): bump lint-staged from 12.2.1 to 12.3.1

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Ryan Cohen <ryan@sct.dev>
2022-01-25 02:10:37 +00:00

175 lines
5.0 KiB
TypeScript

import { sortBy } from 'lodash';
import React, { useMemo } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import Select, { CSSObjectWithLabel } from 'react-select';
import useSWR from 'swr';
import { Language } from '../../../server/lib/settings';
import globalMessages from '../../i18n/globalMessages';
const messages = defineMessages({
originalLanguageDefault: 'All Languages',
languageServerDefault: 'Default ({language})',
});
type OptionType = {
value: string;
label: string;
isFixed?: boolean;
};
const selectStyles = {
multiValueLabel: (base: CSSObjectWithLabel, props: { data: OptionType }) => {
return props.data?.isFixed ? { ...base, paddingRight: 6 } : base;
},
multiValueRemove: (base: CSSObjectWithLabel, props: { data: OptionType }) => {
return props.data?.isFixed ? { ...base, display: 'none' } : base;
},
};
interface LanguageSelectorProps {
value?: string;
setFieldValue: (property: string, value: string) => void;
serverValue?: string;
isUserSettings?: boolean;
}
const LanguageSelector: React.FC<LanguageSelectorProps> = ({
value,
setFieldValue,
serverValue,
isUserSettings = false,
}) => {
const intl = useIntl();
const { data: languages } = useSWR<Language[]>('/api/v1/languages');
const sortedLanguages = useMemo(() => {
languages?.forEach((language) => {
language.name =
intl.formatDisplayName(language.iso_639_1, {
type: 'language',
fallback: 'none',
}) ?? language.english_name;
});
return sortBy(languages, 'name');
}, [intl, languages]);
const languageName = (languageCode: string) =>
sortedLanguages?.find((language) => language.iso_639_1 === languageCode)
?.name ?? languageCode;
const options: OptionType[] =
sortedLanguages?.map((language) => ({
label: language.name,
value: language.iso_639_1,
})) ?? [];
if (isUserSettings) {
options.unshift({
value: 'server',
label: intl.formatMessage(messages.languageServerDefault, {
language: serverValue
? serverValue
.split('|')
.map((value) => languageName(value))
.reduce((prev, curr) =>
intl.formatMessage(globalMessages.delimitedlist, {
a: prev,
b: curr,
})
)
: intl.formatMessage(messages.originalLanguageDefault),
}),
isFixed: true,
});
}
options.unshift({
value: 'all',
label: intl.formatMessage(messages.originalLanguageDefault),
isFixed: true,
});
return (
<Select<OptionType, true>
options={options}
isMulti
className="react-select-container"
classNamePrefix="react-select"
value={
(isUserSettings && value === 'all') || (!isUserSettings && !value)
? {
value: 'all',
label: intl.formatMessage(messages.originalLanguageDefault),
isFixed: true,
}
: (value === '' || !value || value === 'server') && isUserSettings
? {
value: 'server',
label: intl.formatMessage(messages.languageServerDefault, {
language: serverValue
? serverValue
.split('|')
.map((value) => languageName(value))
.reduce((prev, curr) =>
intl.formatMessage(globalMessages.delimitedlist, {
a: prev,
b: curr,
})
)
: intl.formatMessage(messages.originalLanguageDefault),
}),
isFixed: true,
}
: (value
?.split('|')
.map((code) => {
const matchedLanguage = sortedLanguages?.find(
(lang) => lang.iso_639_1 === code
);
if (!matchedLanguage) {
return undefined;
}
return {
label: matchedLanguage.name,
value: matchedLanguage.iso_639_1,
};
})
.filter((option) => option !== undefined) as OptionType[])
}
onChange={(value, options) => {
if (
(options &&
options.action === 'select-option' &&
options.option?.value === 'server') ||
value.every((v) => v.value === 'server')
) {
return setFieldValue('originalLanguage', '');
}
if (
(options &&
options.action === 'select-option' &&
options.option?.value === 'all') ||
value.every((v) => v.value === 'all')
) {
return setFieldValue('originalLanguage', isUserSettings ? 'all' : '');
}
setFieldValue(
'originalLanguage',
value
.map((lang) => lang.value)
.filter((v) => v !== 'all')
.join('|')
);
}}
styles={selectStyles}
/>
);
};
export default LanguageSelector;