All files / src/utils useVariant.ts

100% Statements 10/10
100% Branches 12/12
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                          317x   317x       97x   96x 97x 97x 97x 97x   97x                      
import type { VariantOverwrite } from "@/api/models/Service";
import type { I18n } from "vue-i18n";
 
import { inject, unref } from "vue";
import { I18nInjectionKey } from "vue-i18n";
 
export interface Variant {
  id: number;
  name: string;
  hint?: string;
}
 
export function useVariant(t: (key: string) => string) {
  const i18n = inject<I18n | null>(I18nInjectionKey, null);
 
  return (
    id: number | null,
    overwrite?: VariantOverwrite
  ): Variant | undefined => {
    if (id == null) return undefined;
 
    const locale = unref(i18n?.global.locale) ?? "de-DE";
    const shortOverwrite = overwrite?.[locale.split("-")[0]];
    const fullOverwrite = overwrite?.[locale];
    const hintKey = `variants.${id}.hint`;
    const translatedHint = t(hintKey);
 
    return {
      id,
      name:
        shortOverwrite?.name ?? fullOverwrite?.name ?? t(`variants.${id}.name`),
      hint:
        shortOverwrite?.hint ??
        fullOverwrite?.hint ??
        (translatedHint === hintKey ? undefined : translatedHint),
    };
  };
}