diff --git a/src/components/Dialog/NewDeviceDialog.tsx b/src/components/Dialog/NewDeviceDialog.tsx index f821c05b..eb25e185 100644 --- a/src/components/Dialog/NewDeviceDialog.tsx +++ b/src/components/Dialog/NewDeviceDialog.tsx @@ -18,9 +18,7 @@ import { TabsList, TabsTrigger, } from "@components/UI/Tabs.tsx"; -import { Subtle } from "@components/UI/Typography/Subtle.tsx"; import { AlertCircle } from "lucide-react"; -import { useMemo } from "react"; import { Trans, useTranslation } from "react-i18next"; import { Link } from "../UI/Typography/Link.tsx"; @@ -29,6 +27,7 @@ export interface TabElementProps { } export interface TabManifest { + id: "HTTP" | "BLE" | "Serial"; label: string; element: React.FC; isDisabled: boolean; @@ -41,29 +40,28 @@ export interface NewDeviceProps { interface FeatureErrorProps { missingFeatures: BrowserFeature[]; + tabId: "HTTP" | "BLE" | "Serial"; } -const links: { [key: string]: string } = { - "Web Bluetooth": - "https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API#browser_compatibility", - "Web Serial": - "https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API#browser_compatibility", - "Secure Context": - "https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts", +const errors: Record = { + "Web Bluetooth": { + href: + "https://developer.mozilla.org/en-US/docs/Web/API/Web_Bluetooth_API#browser_compatibility", + i18nKey: "newDeviceDialog.validation.requiresWebBluetooth", + }, + "Web Serial": { + href: + "https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API#browser_compatibility", + i18nKey: "newDeviceDialog.validation.requiresWebSerial", + }, + "Secure Context": { + href: + "https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts", + i18nKey: "newDeviceDialog.validation.requiresSecureContext", + }, }; -const ErrorMessage = ({ missingFeatures }: FeatureErrorProps) => { - const { i18n } = useTranslation("dialog"); - - const listFormatter = useMemo( - () => - new Intl.ListFormat(i18n.language, { - style: "long", - type: "disjunction", - }), - [i18n.language], - ); - +const ErrorMessage = ({ missingFeatures, tabId }: FeatureErrorProps) => { if (missingFeatures.length === 0) return null; const browserFeatures = missingFeatures.filter( @@ -71,37 +69,32 @@ const ErrorMessage = ({ missingFeatures }: FeatureErrorProps) => { ); const needsSecureContext = missingFeatures.includes("Secure Context"); - const formatFeatureList = (features: string[]) => { - const parts = listFormatter.formatToParts(features); - return parts.map((part) => { - if (part.type === "element") { - return ( - - {part.value} - - ); - } - return {part.value}; - }); - }; - - const featureNodes = formatFeatureList(browserFeatures); + const needsFeature = + (tabId === "BLE" && browserFeatures.includes("Web Bluetooth")) + ? "Web Bluetooth" + : (tabId === "Serial" && browserFeatures.includes("Web Serial")) + ? "Web Serial" + : undefined; return ( - +
-

- {browserFeatures.length > 0 && ( +

+ {needsFeature && ( {featureNodes}, - }} + i18nKey={errors[needsFeature].i18nKey} + components={[ + , + ]} /> )} - {browserFeatures.length > 0 && needsSecureContext && " "} + {needsFeature && needsSecureContext && " "} {needsSecureContext && ( 0 @@ -110,17 +103,17 @@ const ErrorMessage = ({ missingFeatures }: FeatureErrorProps) => { components={{ "0": ( ), }} /> )} -

+
- +
); }; @@ -133,17 +126,20 @@ export const NewDeviceDialog = ({ const tabs: TabManifest[] = [ { + id: "HTTP", label: t("newDeviceDialog.tabHttp"), element: HTTP, isDisabled: false, }, { + id: "BLE", label: t("newDeviceDialog.tabBluetooth"), element: BLE, isDisabled: unsupported.includes("Web Bluetooth") || unsupported.includes("Secure Context"), }, { + id: "Serial", label: t("newDeviceDialog.tabSerial"), element: Serial, isDisabled: unsupported.includes("Web Serial") || @@ -161,21 +157,27 @@ export const NewDeviceDialog = ({ {tabs.map((tab) => ( - + {tab.label} ))} {tabs.map((tab) => ( - +
- {(tab.label !== "HTTP" && + {(tab.id !== "HTTP" && tab.isDisabled) - ? - : null} - onOpenChange(false)} - /> + ? ( + + ) + : ( + onOpenChange(false)} + /> + )}
))} diff --git a/src/i18n/locales/en/dialog.json b/src/i18n/locales/en/dialog.json index ddf1f352..a6cc54af 100644 --- a/src/i18n/locales/en/dialog.json +++ b/src/i18n/locales/en/dialog.json @@ -64,7 +64,8 @@ "newDeviceButton": "New device" }, "validation": { - "requiresFeatures": "This connection type requires <0>. Please use a supported browser, like Chrome or Edge.", + "requiresWebBluetooth": "This connection type requires <0>Web Bluetooth. Please use a supported browser, like Chrome or Edge.", + "requiresWebSerial": "This connection type requires <0>Web Serial. Please use a supported browser, like Chrome or Edge.", "requiresSecureContext": "This application requires a <0>secure context. Please connect using HTTPS or localhost.", "additionallyRequiresSecureContext": "Additionally, it requires a <0>secure context. Please connect using HTTPS or localhost." }