summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/utilities/languages.ts
blob: 282db22a9e6dbeb0eeaf74b2c67dca399571329d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { useLanguageProfiles, useLanguages } from "@/apis/hooks";
import { useMemo } from "react";

export function useLanguageProfileBy(id: number | null | undefined) {
  const { data } = useLanguageProfiles();
  return useMemo(() => data?.find((v) => v.profileId === id), [id, data]);
}

export function useEnabledLanguages() {
  const query = useLanguages();

  const enabled = useMemo(() => {
    const data: Language.Info[] =
      query.data
        ?.filter((v) => v.enabled)
        .map<Language.Info>((v) => ({ code2: v.code2, name: v.name })) ?? [];

    return {
      ...query,
      data,
    };
  }, [query]);

  return enabled;
}

// Convert languageprofile items to language
export function useProfileItemsToLanguages(profile?: Language.Profile) {
  const { data } = useLanguages();

  return useMemo(
    () =>
      profile?.items.map<Language.Info>(({ language: code, hi, forced }) => {
        const name = data?.find((v) => v.code2 === code)?.name ?? "";
        return {
          hi: hi === "True",
          forced: forced === "True",
          code2: code,
          name,
        };
      }) ?? [],
    [data, profile?.items]
  );
}

export function useLanguageFromCode3(code3: string) {
  const { data } = useLanguages();

  return useMemo(
    () => data?.find((value) => value.code3 === code3),
    [data, code3]
  );
}