react-user-agent-client-hints
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -10,15 +10,36 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
import { useEffect, useState } from "react"; | ||
import { useEffect, useReducer } from "react"; | ||
const initialState = { | ||
userAgentData: null, | ||
error: null | ||
}; | ||
function reducer(state, action) { | ||
switch (action.type) { | ||
case "SET_USER_AGENT_DATA": | ||
return Object.assign(Object.assign({}, state), { userAgentData: action.payload }); | ||
case "SET_ERROR": | ||
return Object.assign(Object.assign({}, state), { error: action.payload }); | ||
default: | ||
return state; | ||
} | ||
} | ||
export function useUserAgentData(hints) { | ||
const [userAgentData, setUserAgentData] = useState(null); | ||
const [error, setError] = useState(null); | ||
const [state, dispatch] = useReducer(reducer, initialState); | ||
useEffect(() => { | ||
function getHighEntropyUAData() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!isNavigatorUA(navigator)) | ||
return; | ||
try { | ||
if (!isNavigatorUA(navigator)) { | ||
dispatch({ | ||
type: "SET_ERROR", | ||
payload: new Error("High entropy user agent data not available in this browser") | ||
}); | ||
return; | ||
} | ||
// check if the `navigator.userAgentData` object is available | ||
if (navigator.userAgentData === undefined) { | ||
setError(new Error("User-agent client hints API is undefined.")); | ||
dispatch({ | ||
type: "SET_ERROR", | ||
payload: new Error("User-agent client hints API is undefined.") | ||
}); | ||
return; | ||
@@ -28,3 +49,6 @@ } | ||
if (navigator.userAgentData.getHighEntropyValues === undefined) { | ||
setError(new Error("Permission denied accessing user-agent data")); | ||
dispatch({ | ||
type: "SET_ERROR", | ||
payload: new Error("Permission denied accessing user-agent data") | ||
}); | ||
return; | ||
@@ -34,6 +58,9 @@ } | ||
const agentData = yield navigator.userAgentData.getHighEntropyValues(hints); | ||
setUserAgentData(agentData); | ||
dispatch({ type: "SET_USER_AGENT_DATA", payload: agentData }); | ||
} | ||
catch (err) { | ||
setError(new Error("Failed to get user-agent data")); | ||
dispatch({ | ||
type: "SET_ERROR", | ||
payload: new Error("Failed to get user-agent data") | ||
}); | ||
} | ||
@@ -44,3 +71,3 @@ }); | ||
}, [hints]); | ||
return [userAgentData, error]; | ||
return [state.userAgentData, state.error]; | ||
} | ||
@@ -47,0 +74,0 @@ export function getLowEntropyUserAgentData() { |
{ | ||
"name": "react-user-agent-client-hints", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "", | ||
"main": "./dist/index.js", | ||
"scripts": { | ||
"test": "jest --config jest.config.js", | ||
"start": "node webpack-server.js", | ||
"build": "npx webpack", | ||
"e2e": "npx playwright test ./tests", | ||
"start": "cd example && npm install && npm run start", | ||
"build": "tsc", | ||
"build:ts": "tsc" | ||
@@ -21,7 +21,3 @@ }, | ||
"@babel/register": "^7.17.7", | ||
"@jest/types": "^28.1.0", | ||
"@playwright/test": "^1.29.2", | ||
"@testing-library/jest-dom": "^5.16.4", | ||
"@testing-library/react": "^13.4.0", | ||
"@types/jest": "^27.5.1", | ||
"@types/node": "^17.0.34", | ||
@@ -32,3 +28,2 @@ "@types/react": "^18.0.9", | ||
"@typescript-eslint/parser": "^5.25.0", | ||
"babel-jest": "^28.1.0", | ||
"babel-loader": "^8.2.5", | ||
@@ -39,5 +34,2 @@ "babel-preset-react-app": "^10.0.1", | ||
"eslint-plugin-react-hooks": "^4.5.0", | ||
"identity-obj-proxy": "^3.0.0", | ||
"jest": "^28.1.0", | ||
"jest-environment-jsdom": "^28.1.0", | ||
"prettier": "^2.6.2", | ||
@@ -44,0 +36,0 @@ "react": "^18.1.0", |
Sorry, the diff of this file is not supported yet
12315
25
119
11