All files / app/features/translate useShowAllLanguages.ts

70.58% Statements 12/17
77.77% Branches 7/9
66.66% Functions 2/3
73.33% Lines 11/15

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 40 41 42 43 44 45 46 474x 4x 4x   4x     4x               28x 28x   28x     9x           28x                   28x                  
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { showAllLanguagesQueryKey } from "features/queryKeys";
import { useCallback } from "react";
 
const STORAGE_KEY = "showAllLanguages";
 
const isNonProduction =
  process.env.NEXT_PUBLIC_COUCHERS_ENV !== "prod" &&
  process.env.NODE_ENV !== "test";
 
/**
 * Hook to manage the "show all languages" setting for translators.
 * This setting is only available on non-production environments (stage/local dev).
 * When enabled, the language picker shows all languages regardless of translation completion percentage.
 */
export function useShowAllLanguages() {
  const queryClient = useQueryClient();
 
  const { data: showAllLanguages = false } = useQuery({
    queryKey: [showAllLanguagesQueryKey],
    queryFn: () => {
      if (typeof window === "undefined" || !isNonProduction) return false;
      return localStorage.getItem(STORAGE_KEY) === "true";
    },
    staleTime: Infinity,
  });
 
  const setShowAllLanguages = useCallback(
    (value: boolean) => {
      Iif (!isNonProduction) return;
 
      localStorage.setItem(STORAGE_KEY, String(value));
      queryClient.setQueryData([showAllLanguagesQueryKey], value);
    },
    [queryClient],
  );
 
  return {
    /** Whether the feature is available (non-production environment) */
    isAvailable: isNonProduction,
    /** Whether to show all languages in the picker */
    showAllLanguages: isNonProduction && showAllLanguages,
    /** Set the setting to a specific value */
    setShowAllLanguages,
  };
}