Skip to content

Commit

Permalink
fix: improve UI dialog behavior when many languages
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 committed Sep 2, 2022
1 parent dfa152a commit 8e5495b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
1 change: 0 additions & 1 deletion packages/core/src/services/CoreService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ describe('CoreService', () => {
test('will return languages returned from api http service', async () => {
expect(await coreService.getLanguages()).toEqual(new Set(dummyLanguages));
expect(mockedFetchJson).toBeCalledTimes(1);
expect(mockedFetchJson).toBeCalledWith(`v2/projects/languages?size=1000`);
});

test('sets preferred languages of properties', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/services/CoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class CoreService {
async getLanguages(): Promise<Set<string>> {
if (!(this.languagePromise instanceof Promise)) {
this.languagePromise = this.apiHttpService.fetchJson(
`v2/projects/languages?size=1000`
`v2/projects/languages?size=1000&sort=tag`
);
}

Expand Down
6 changes: 5 additions & 1 deletion packages/ui/src/KeyDialog/LanguageSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ export const LanguageSelect: React.FC = () => {
return (
<>
{availableLanguages && (
<ScFormControl variant="outlined" size="small">
<ScFormControl
variant="outlined"
size="small"
style={{ maxWidth: 250 }}
>
<Select
multiple
value={selected.map((o) => o.value)}
Expand Down
21 changes: 18 additions & 3 deletions packages/ui/src/KeyDialog/TranslationDialogContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import { ComponentDependencies } from './KeyDialog';
import { sleep } from '../tools/sleep';
import { createProvider } from '../tools/createProvider';
import { isLanguagePermitted } from '../tools/isLanguagePermitted';
import { putBaseLangFirst, putBaseLangFirstTags } from './languageHelpers';

const MAX_LANGUAGES_SELECTED = 5;

export interface ScreenshotInterface {
id: number;
Expand Down Expand Up @@ -347,11 +350,21 @@ export const [DialogProvider, useDialogDispatch, useDialogContext] =
? !coreService.isAuthorizedTo('translations.edit')
: !coreService.isAuthorizedTo('keys.edit'));

const [availableLanguages, setAvailableLanguages] =
const [availableLanguages, _setAvailableLanguages] =
useState<LanguageModel[]>(undefined);
const setAvailableLanguages = (data: LanguageModel[]) => {
_setAvailableLanguages(putBaseLangFirst(data));
};

const getInitialLanguages = () => {
const langs = Array.from(
properties.preferredLanguages || [properties.currentLanguage]
);
return new Set(langs.slice(0, MAX_LANGUAGES_SELECTED));
};

const [selectedLanguages, setSelectedLanguages] = useState(
properties.preferredLanguages || new Set([properties.currentLanguage])
getInitialLanguages()
);

// sets the default value for base language if is not stored already
Expand Down Expand Up @@ -389,6 +402,8 @@ export const [DialogProvider, useDialogDispatch, useDialogContext] =
props.defaultValue,
]);

const baseLang = availableLanguages?.find(({ base }) => base);

const contextValue = {
input: props.input,
dependencies: props.dependencies,
Expand All @@ -398,7 +413,7 @@ export const [DialogProvider, useDialogDispatch, useDialogContext] =
success,
error,
availableLanguages,
selectedLanguages,
selectedLanguages: putBaseLangFirstTags(selectedLanguages, baseLang?.tag),
formDisabled,
translations,
translationsForm,
Expand Down
19 changes: 19 additions & 0 deletions packages/ui/src/KeyDialog/languageHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { LanguageModel } from '@tolgee/core';

export const putBaseLangFirst = (languages: LanguageModel[] | undefined) => {
const base = languages?.find((l) => l.base);
if (base) {
return [base, ...languages.filter((val) => val !== base)];
}
return languages;
};

export const putBaseLangFirstTags = (strings: Set<string>, base?: string) => {
if (base && strings.has(base)) {
return new Set([
base,
...Array.from(strings).filter((val) => val !== base),
]);
}
return strings;
};

0 comments on commit 8e5495b

Please sign in to comment.