spacedrive/interface/hooks/useOperatingSystem.ts
Utku fd8c0f87b3
[ENG-1067] Update phosphor to new package & update sort imports (#1330)
* ianvs > trivago

* @phosphor-icons/react > phosphor-react
2023-09-11 15:26:44 +00:00

34 lines
1.3 KiB
TypeScript

import { useQuery } from '@tanstack/react-query';
import { OperatingSystem, usePlatform } from '../util/Platform';
export function guessOperatingSystem(): OperatingSystem {
let os: OperatingSystem = 'unknown';
if (navigator.userAgent.indexOf('Win') != -1) os = 'windows';
if (navigator.userAgent.indexOf('Mac') != -1) os = 'macOS';
if (navigator.userAgent.indexOf('X11') != -1 || navigator.userAgent.indexOf('Linux') != -1)
os = 'linux';
return os;
}
// This hook will return the current os we are using.
// It will guess the OS on first render until Tauri responds with a more accurate answer.
// This means the app can open insanely quickly without any weird layout shift.
// Setting `realOs` to true will return a best guess of the underlying operating system instead of 'browser'.
export function useOperatingSystem(realOs?: boolean): OperatingSystem {
const platform = usePlatform();
const { data } = useQuery(
['_tauri', 'platform'],
async () => {
return platform.getOs ? await platform.getOs() : guessOperatingSystem();
},
{
// Here we guess the users operating system from the user agent for the first render.
initialData: guessOperatingSystem,
enabled: platform.getOs !== undefined
}
);
return platform.platform === 'web' && !realOs ? 'browser' : data;
}