summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/utilities/languages.ts
blob: 7077da959fe40dfe4dbc9e5a6cc0e4d95f6ecb58 (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
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 =
      query.data
        ?.filter((v) => v.enabled)
        .map((v) => ({ code2: v.code2, name: v.name })) ?? [];

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

  return enabled;
}

export function useLanguageBy(code?: string) {
  const { data } = useLanguages();
  return useMemo(() => data?.find((v) => v.code2 === code), [data, code]);
}

// 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]
  );
}