You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
33 lines
896 B
33 lines
896 B
import { useMemo } from "react";
|
|
|
|
export type BrowserFeature = "Web Bluetooth" | "Web Serial" | "Secure Context";
|
|
|
|
interface BrowserSupport {
|
|
supported: BrowserFeature[];
|
|
unsupported: BrowserFeature[];
|
|
}
|
|
|
|
export function useBrowserFeatureDetection(): BrowserSupport {
|
|
const support = useMemo(() => {
|
|
const features: [BrowserFeature, boolean][] = [
|
|
["Web Bluetooth", !!navigator.bluetooth],
|
|
["Web Serial", !!navigator.serial],
|
|
[
|
|
"Secure Context",
|
|
globalThis.location.protocol === "https:" ||
|
|
globalThis.location.hostname === "localhost",
|
|
],
|
|
];
|
|
|
|
return features.reduce<BrowserSupport>(
|
|
(acc, [feature, isSupported]) => {
|
|
const list = isSupported ? acc.supported : acc.unsupported;
|
|
list.push(feature);
|
|
return acc;
|
|
},
|
|
{ supported: [], unsupported: [] },
|
|
);
|
|
}, []);
|
|
|
|
return support;
|
|
}
|
|
|