Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WEB-2316] chore: use-platform-os hook optimization to not cause re renders #5453

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions packages/ui/src/hooks/use-platform-os.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import { useEffect, useState } from "react";

export const usePlatformOS = () => {
// states
const [isMobile, setIsMobile] = useState(false);

useEffect(() => {
const userAgent = window.navigator.userAgent;
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);

if (isMobile) setIsMobile(isMobile);
}, []);
const userAgent = window.navigator.userAgent;
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);

return { isMobile };
};
35 changes: 13 additions & 22 deletions web/core/hooks/use-platform-os.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
"use client";

import { useEffect, useState } from "react";

export const usePlatformOS = () => {
const [isMobile, setIsMobile] = useState(false);
const [platform, setPlatform] = useState("");
useEffect(() => {
const userAgent = window.navigator.userAgent;
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);
let detectedPlatform = "";
const userAgent = window.navigator.userAgent;
const isMobile = /iPhone|iPad|iPod|Android/i.test(userAgent);
let platform = "";

if (isMobile) {
setIsMobile(isMobile)
if (!isMobile) {
if (userAgent.indexOf("Win") !== -1) {
platform = "Windows";
} else if (userAgent.indexOf("Mac") !== -1) {
platform = "MacOS";
} else if (userAgent.indexOf("Linux") !== -1) {
platform = "Linux";
} else {
if (userAgent.indexOf("Win") !== -1) {
detectedPlatform = "Windows";
} else if (userAgent.indexOf("Mac") !== -1) {
detectedPlatform = "MacOS";
} else if (userAgent.indexOf("Linux") !== -1) {
detectedPlatform = "Linux";
} else {
detectedPlatform = "Unknown";
}
};
setPlatform(detectedPlatform);
}, []);
platform = "Unknown";
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets return the platform for mobile as well.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we take that as a separate task as?, the change will require more testing on the devices

return { isMobile, platform };
};
Loading