/** * (c) 2026, Micro:bit Educational Foundation and contributors * * SPDX-License-Identifier: MIT */ import { Box, Button, ExternalLink, Flex, Grid, HStack, Icon, List, ListItem, Modal, ModalBody, ModalFooter, ModalHeader, Stack, Text, TooltipButton, useToast, VStack, } from "@microbit/ui"; import { uiMessage } from "@microbit/ui/messages"; import { ReactNode, RefObject, useCallback, useId } from "react"; import { RiCheckboxBlankLine, RiCheckboxLine, RiErrorWarningLine, } from "react-icons/ri"; import { FormattedMessage, useIntl } from "react-intl"; import { KnownLanguageId, languageFromId, languageOrder } from "./languages"; import { uiPatternsMessage } from "./messages"; export interface LanguageDialogSupportItem { /** Product name as displayed, e.g. "Microsoft MakeCode" or the app name. */ name: string; supported: boolean; } export interface LanguageDialogLanguage { id: KnownLanguageId; /** * Per-product translation coverage. A language with any unsupported * product is listed under a "Partially supported" heading, with a warning * tooltip and a post-selection toast showing this checklist. Omit for * apps without the support-tier concept (a flat grid), or pass it for * every language and let the dialog derive the sections. */ support?: LanguageDialogSupportItem[]; /** * Marks an early-preview translation: the English name gains an asterisk * and the dialog a footnote. */ preview?: boolean; /** * Display-name overrides, pending the family-wide en/en-US naming * decision — ml-trainer passes `name: "English (UK)"` for `en` because it * also offers `en-US`. Prefer the registry names otherwise. */ name?: string; enName?: string; } const isPartiallySupported = (language: LanguageDialogLanguage): boolean => language.support?.some((item) => !item.supported) ?? false; export interface LanguageDialogProps { isOpen: boolean; onClose: () => void; /** Element to return focus to, typically the settings menu button. */ finalFocusRef?: RefObject; /** Languages to offer, in any order; the dialog sorts by registry order. */ languages: LanguageDialogLanguage[]; /** * Applies the selection (store/settings write, editor reload, etc.). The * dialog closes once the returned promise settles. */ onSelectLanguage: (languageId: KnownLanguageId) => void | Promise; /** When given, a "Help translate" external link appears in the footer. */ translationLinkHref?: string; } /** * Language settings dialog — a grid of language cards (endonym over English * name), split into fully/partially supported sections when any language's * support checklist has an unsupported product. Selection applies * immediately: no confirm step, no page reload expected. Endonyms carry * their own `lang`/`dir` so assistive tech pronounces them correctly. */ export const LanguageDialog = ({ isOpen, onClose, finalFocusRef, languages, onSelectLanguage, translationLinkHref, }: LanguageDialogProps) => { const handleChooseLanguage = useCallback( async (languageId: KnownLanguageId) => { await onSelectLanguage(languageId); onClose(); }, [onClose, onSelectLanguage], ); const previewNoticeId = useId(); const ordered = [...languages].sort( (a, b) => languageOrder(a.id) - languageOrder(b.id), ); const fully = ordered.filter((l) => !isPartiallySupported(l)); const partially = ordered.filter(isPartiallySupported); const hasPreviewLanguages = ordered.some((l) => l.preview); // With only a handful of languages the family-size dialog rattles around // (data.microbit.org offers two), so compact to two columns in a narrower // modal. Beyond the threshold the third column starts paying its way. const compact = languages.length <= 6; const languageGrid = (items: LanguageDialogLanguage[]) => ( {items.map((language) => ( ))} ); return ( {partially.length > 0 && ( )} {languageGrid(fully)} {partially.length > 0 && ( <> {languageGrid(partially)} )} {hasPreviewLanguages && ( )} {translationLinkHref && ( )} ); }; const SectionHeading = ({ messageId, spaced = false, }: { messageId: | "ui-patterns.language-fully-supported-heading" | "ui-patterns.language-partially-supported-heading"; spaced?: boolean; }) => ( ); interface LanguageCardProps { language: LanguageDialogLanguage; onChooseLanguage: (languageId: KnownLanguageId) => void | Promise; /** id of the dialog's preview footnote, described-by on preview cards. */ previewNoticeId: string; } const LanguageCard = ({ language, onChooseLanguage, previewNoticeId, }: LanguageCardProps) => { const intl = useIntl(); const toast = useToast(); const registryEntry = languageFromId(language.id); const name = language.name ?? registryEntry.name; const enName = language.enName ?? registryEntry.enName; const nameId = useId(); const enNameId = useId(); const showSupport = isPartiallySupported(language); const handleSelect = useCallback(() => { void onChooseLanguage(language.id); if (showSupport) { toast({ // An element rather than `intl.formatMessage`: this runs in the same // tick as the language change, so a formatted string would freeze the // language the user just left, leaving the title in the old language // above a description that had already re-rendered in the new one. title: ( ), description: , status: "info", isClosable: true, // The dialog (and its support tooltip) is gone by the time this // shows, so give the checklist longer than the 5s default. duration: 10_000, }); } }, [language.id, language.support, onChooseLanguage, showSupport, toast]); // The selection button covers the whole card; the visible content sits // above it, and the warning tooltip trigger is a sibling. // react-aria-components disallows nesting a focusable tooltip trigger // inside a button, so we use this overlay pattern to keep the tooltip // anchored on the warning icon. The button's accessible name comes from // the name elements via aria-labelledby (not aria-label) so their `lang` // attributes carry into the announcement. return (