Skip to content

Commit

Permalink
Update device type sniffing
Browse files Browse the repository at this point in the history
  • Loading branch information
ccruzkauppila committed Nov 5, 2024
1 parent ae0864a commit 58087f1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/app/(loggedout)/login/LoginForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const LoginForm = () => {
const [deviceType, setDeviceType] = useState<string>("");
useEffect(() => {
// Getdevicetype references navigator which is not defined before page load
setDeviceType(getDeviceType());
getDeviceType().then((device) => setDeviceType(device));
}, []);

useEffect(() => {
Expand Down
17 changes: 13 additions & 4 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const parseISOString = (s: string) => {
);
};

export type DeviceType = "Mobile" | "Desktop" | "GuildroomTablet";
/**
* Get the type of device the user is browsing on. Used to determine e.g. available login & payment methods.
* Only works in client components!
Expand All @@ -120,14 +121,22 @@ export const parseISOString = (s: string) => {
* - MobilePay QR & manual payment instructions should be shown on desktop and on the guild room tablet
* @returns "Mobile" | "Desktop" | "GuildroomTablet"
*/
export const getDeviceType = (): "Mobile" | "Desktop" | "GuildroomTablet" => {
export const getDeviceType = async (): Promise<DeviceType> => {
if (navigator && "userAgentData" in navigator) {
const uaData = navigator.userAgentData as any;
const deviceData = await uaData?.getHighEntropyValues(["model"]);
const deviceModel = deviceData?.model || "";
const isGuildroomTablet =
deviceModel.includes("Armor") &&
deviceModel.includes("Pad") &&
deviceModel.includes("Pro");

if (isGuildroomTablet) return "GuildroomTablet";
}
if (navigator && "userAgent" in navigator) {
const ua = navigator.userAgent;
// TODO: Check actual model name from user agent string
const isGuildroomTablet =
ua.includes("Armor") && ua.includes("Pad") && ua.includes("Pro");

if (isGuildroomTablet) return "GuildroomTablet";
if (ua.includes("Mobi")) return "Mobile";
return "Desktop";
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/AddFundsDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ const AddFundsStep2 = ({ c }: StepProps) => {
const { Canvas } = useQRCode();
const [deviceType, setDeviceType] = useState<string>("");
useEffect(() => {
setDeviceType(getDeviceType());
getDeviceType().then((device) => setDeviceType(device));
}, []);

const commitAddFunds = async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/RfidSetupDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const RfidSetupDialog = () => {
const [deviceType, setDeviceType] = useState("");
useEffect(() => {
// Getdevicetype references navigator which is not defined before page load
setDeviceType(getDeviceType());
getDeviceType().then((device) => setDeviceType(device));
}, []);

const scan = async () => {
Expand Down

0 comments on commit 58087f1

Please sign in to comment.