import type { Language, Niche } from '../runtime'; import { NicheHelper } from '../utils/niche-helper'; /** Возвращает generateLink — генерирует путь с учётом текущей ниши и локали. */ export const useGenerateLink = () => { const niche = useState('niche'); const localePath = useLocalePath(); /** * Генерирует путь с учетом ниши и локали * @param path - относительный путь (например: '/videos/abc123') * @param localeArg - локаль * @returns string - корректный путь для и navigateTo() */ const generateLink = (path: string, localeArg?: Language): string => { const cleanPath = path.startsWith('/') ? path : `/${path}`; // Ниша сайта — из окружения (тем же значением уходит в заголовок X-Niche), // список ниш — из per-site конфига проекта. const defaultNiche = NicheHelper.getSiteDefaultNiche(); const projectNiches = useAppConfig().niches as Niche[]; const isOneNiche = projectNiches.length === 1; const withNiche = niche.value === defaultNiche || isOneNiche ? cleanPath : `/${niche.value}${cleanPath}`; const locale = localeArg || undefined; return localePath(withNiche, locale); }; return { generateLink }; };