refactor: consolidate browser feature detection into typed hook. Update connect dialog messaging to describe requirement for https when conneecting

This commit is contained in:
Dan Ditomaso
2025-01-06 11:43:36 -05:00
parent 94c6eea20b
commit 1c8476df53
2 changed files with 80 additions and 25 deletions
@@ -0,0 +1,32 @@
type Feature = 'Web Bluetooth' | 'Web Serial';
type FeatureKey = 'bluetooth' | 'serial';
interface BrowserFeatureDetection {
hasRequiredFeatures: boolean;
missingFeatures: Feature[];
isSecureContext: boolean;
}
const featureLabels: Record<FeatureKey, Feature> = {
bluetooth: 'Web Bluetooth',
serial: 'Web Serial'
};
export function useBrowserFeatureDetection(): BrowserFeatureDetection {
const { bluetooth, serial } = navigator;
const isSecureContext = window.location.protocol === 'https:' ||
window.location.hostname === 'localhost';
const features = {
bluetooth,
serial
};
return {
hasRequiredFeatures: Object.values(features).every(Boolean),
missingFeatures: Object.entries(features)
.filter(([_, supported]) => !supported)
.map(([feature]) => featureLabels[feature as FeatureKey]),
isSecureContext
};
}