import { DEFAULT_LOCALE } from "../../../libs/constants"; import { useLocale } from "../hooks"; import { BASELINE } from "../telemetry/constants"; import { useGleanClick } from "../telemetry/glean-context"; import { Icon } from "../ui/atoms/icon"; import { useLocation } from "react-router"; import "./baseline-indicator.scss"; // web-features doesn't export these types directly so we need to do a little typescript magic: import type { features } from "web-features"; type SupportStatus = (typeof features)[keyof typeof features]["status"] & { asterisk?: boolean; }; type BrowserIdentifier = keyof (typeof features)[keyof typeof features]["status"]["support"]; interface BrowserGroup { name: string; ids: BrowserIdentifier[]; } const ENGINES: { name: string; browsers: BrowserGroup[]; }[] = [ { name: "Blink", browsers: [ { name: "Chrome", ids: ["chrome", "chrome_android"] }, { name: "Edge", ids: ["edge"] }, ], }, { name: "Gecko", browsers: [{ name: "Firefox", ids: ["firefox", "firefox_android"] }], }, { name: "WebKit", browsers: [{ name: "Safari", ids: ["safari", "safari_ios"] }], }, ]; const LOCALIZED_BCD_IDS = { de: "browser-kompatibilität", "en-US": "browser_compatibility", es: "compatibilidad_con_navegadores", fr: "compatibilité_des_navigateurs", ja: "ブラウザーの互換性", ko: "브라우저_호환성", "pt-BR": "compatibilidade_com_navegadores", ru: "совместимость_с_браузерами", "zh-CN": "浏览器兼容性", "zh-TW": "瀏覽器相容性", }; const SURVEY_URL = "https://survey.alchemer.com/s3/7634825/MDN-baseline-feedback"; export function BaselineIndicator({ status }: { status: SupportStatus }) { const gleanClick = useGleanClick(); const locale = useLocale(); const { pathname } = useLocation(); const bcdLink = `#${ LOCALIZED_BCD_IDS[locale] || LOCALIZED_BCD_IDS[DEFAULT_LOCALE] }`; const low_date_range = status.baseline_low_date?.match(/^([^0-9])/)?.[0]; const low_date = status.baseline_low_date ? new Date(status.baseline_low_date.slice(low_date_range ? 1 : 0)) : undefined; const level = status.baseline ? status.baseline : status.baseline === false ? "not" : undefined; const feedbackLink = `${SURVEY_URL}?page=${encodeURIComponent( pathname )}&level=${level}`; const supported = (browser: BrowserGroup) => { return browser.ids .map((id) => status.support?.[id]) .every((version) => Boolean(version)); }; const engineTitle = (browsers: BrowserGroup[]) => browsers .map((browser, index, array) => { const previous = index > 0 ? supported(array[index - 1]) : undefined; const current = supported(browser); const name = browser.name; return typeof previous === "undefined" ? current ? `Supported in ${name}` : `Not widely supported in ${name}` : current === previous ? ` and ${name}` : current ? `, and supported in ${name}` : `, and not widely supported in ${name}`; }) .join(""); return level ? (
e.currentTarget.open && gleanClick(BASELINE.TOGGLE_OPEN)} >
{level !== "not" ? ( <> Baseline{" "} {level === "high" ? "Widely available" : low_date?.getFullYear()} {status.asterisk && " *"} ) : ( Limited availability )}
{level === "low" &&
Newly available
}
{ENGINES.map(({ name, browsers }) => ( {browsers.map((browser) => ( ))} ))}
{level === "high" && low_date ? (

This feature is well established and works across many devices and browser versions. It’s been available across browsers since{" "} {low_date.toLocaleDateString("en-US", { year: "numeric", month: "long", })} .

) : level === "low" && low_date ? (

Since{" "} {low_date.toLocaleDateString("en-US", { year: "numeric", month: "long", })} , this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

) : (

This feature is not Baseline because it does not work in some of the most widely-used browsers.

)} {status.asterisk && (

* Some parts of this feature may have varying levels of support.

)}
) : null; }