Skip to content

The open alternative to Salesforce, designed for AI.

Make UI utilities safe without browser globals (#26802)

Raphaël Bosi7d3676c51ab6
diff --git a/packages/twenty-ui/CHANGELOG.md b/packages/twenty-ui/CHANGELOG.md
index 0b53ba516c5..c8a923d3979 100644
--- a/packages/twenty-ui/CHANGELOG.md
+++ b/packages/twenty-ui/CHANGELOG.md
@@ -22,3 +22,7 @@ Keep `twenty-ui`, `twenty-sdk`, and `twenty-client-sdk` versions aligned when up
### Added
- `twenty-ui/utilities` exports `useMediaQuery`, `MOBILE_MEDIA_QUERY`, and `TOUCH_DEVICE_MEDIA_QUERY`. `twenty-ui/testing` exports `overrideMediaQueryMatches`.
+
+### Fixed
+
+- `getUserDevice()` returns `unknown` without a user agent, so device and shortcut helpers work without browser globals. It detects iPhone, iPad and Android user agents as `ios` and `android` instead of `mac` and `linux`; shortcut helpers keep `⌘` on iOS.
diff --git a/packages/twenty-ui/src/utilities/device/__tests__/getOsControlSymbol.test.ts b/packages/twenty-ui/src/utilities/device/__tests__/getOsControlSymbol.test.ts
index ab3cd5d1080..b5c686f2bd5 100644
--- a/packages/twenty-ui/src/utilities/device/__tests__/getOsControlSymbol.test.ts
+++ b/packages/twenty-ui/src/utilities/device/__tests__/getOsControlSymbol.test.ts
@@ -26,4 +26,11 @@ describe('getOsControlSymbol', () => {
);
expect(getOsControlSymbol()).toBe('⌘');
});
+
+ it('should return ⌘ for iPad', () => {
+ userAgentSpy.mockReturnValue(
+ 'Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1',
+ );
+ expect(getOsControlSymbol()).toBe('⌘');
+ });
});
diff --git a/packages/twenty-ui/src/utilities/device/__tests__/getOsShortcutSeparator.test.ts b/packages/twenty-ui/src/utilities/device/__tests__/getOsShortcutSeparator.test.ts
index 60c725066aa..1deec0725ec 100644
--- a/packages/twenty-ui/src/utilities/device/__tests__/getOsShortcutSeparator.test.ts
+++ b/packages/twenty-ui/src/utilities/device/__tests__/getOsShortcutSeparator.test.ts
@@ -26,4 +26,11 @@ describe('getOsShortcutSeparator', () => {
);
expect(getOsShortcutSeparator()).toBe('');
});
+
+ it('should return empty string for iPad', () => {
+ userAgentSpy.mockReturnValue(
+ 'Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1',
+ );
+ expect(getOsShortcutSeparator()).toBe('');
+ });
});
diff --git a/packages/twenty-ui/src/utilities/device/__tests__/getUserDevice.test.ts b/packages/twenty-ui/src/utilities/device/__tests__/getUserDevice.test.ts
new file mode 100644
index 00000000000..ba05b3be52c
--- /dev/null
+++ b/packages/twenty-ui/src/utilities/device/__tests__/getUserDevice.test.ts
@@ -0,0 +1,79 @@
+import { type MockInstance, vi } from 'vitest';
+
+import { getUserDevice } from '../getUserDevice';
+
+describe('getUserDevice', () => {
+ let userAgentSpy: MockInstance<() => string>;
+
+ beforeEach(() => {
+ userAgentSpy = vi.spyOn(window.navigator, 'userAgent', 'get');
+ });
+
+ afterEach(() => {
+ userAgentSpy.mockRestore();
+ vi.unstubAllGlobals();
+ });
+
+ it.each([undefined, null, {}, { userAgent: undefined }, { userAgent: null }])(
+ 'should return unknown when the user agent is unavailable: %j',
+ (navigatorValue) => {
+ vi.stubGlobal('navigator', navigatorValue);
+ expect(getUserDevice()).toBe('unknown');
+ },
+ );
+
+ it.each([
+ {
+ platform: 'Mac',
+ userAgent:
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
+ expectedDevice: 'mac',
+ },
+ {
+ platform: 'Windows',
+ userAgent:
+ 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0',
+ expectedDevice: 'windows',
+ },
+ {
+ platform: 'Linux',
+ userAgent:
+ 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
+ expectedDevice: 'linux',
+ },
+ {
+ platform: 'Android',
+ userAgent:
+ 'Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36',
+ expectedDevice: 'android',
+ },
+ {
+ platform: 'iPhone',
+ userAgent:
+ 'Mozilla/5.0 (iPhone; CPU iPhone OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1',
+ expectedDevice: 'ios',
+ },
+ {
+ platform: 'iPad',
+ userAgent:
+ 'Mozilla/5.0 (iPad; CPU OS 17_4 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Mobile/15E148 Safari/604.1',
+ expectedDevice: 'ios',
+ },
+ {
+ platform: 'an empty user agent',
+ userAgent: '',
+ expectedDevice: 'unknown',
+ },
+ {
+ platform: 'an unrecognized platform',
+ userAgent: 'Mozilla/5.0 (X11; CrOS x86_64 14541.0.0)',
+ expectedDevice: 'unknown',
+ },
+ ])(
+ 'should return $expectedDevice for $platform',
+ ({ userAgent, expectedDevice }) => {
+ userAgentSpy.mockReturnValue(userAgent);
+ expect(getUserDevice()).toBe(expectedDevice);
+ },
+ );
+});
diff --git a/packages/twenty-ui/src/utilities/device/getOsControlSymbol.ts b/packages/twenty-ui/src/utilities/device/getOsControlSymbol.ts
index 3278c3501e9..2d396f0f061 100644
--- a/packages/twenty-ui/src/utilities/device/getOsControlSymbol.ts
+++ b/packages/twenty-ui/src/utilities/device/getOsControlSymbol.ts
@@ -3,5 +3,5 @@ import { getUserDevice } from '@ui/utilities/device/getUserDevice';
export const getOsControlSymbol = () => {
const device = getUserDevice();
- return device === 'mac' ? '⌘' : 'Ctrl';
+ return device === 'mac' || device === 'ios' ? '⌘' : 'Ctrl';
};
diff --git a/packages/twenty-ui/src/utilities/device/getOsShortcutSeparator.ts b/packages/twenty-ui/src/utilities/device/getOsShortcutSeparator.ts
index c7bc029b8e5..db0ae00f2c7 100644
--- a/packages/twenty-ui/src/utilities/device/getOsShortcutSeparator.ts
+++ b/packages/twenty-ui/src/utilities/device/getOsShortcutSeparator.ts
@@ -2,5 +2,5 @@ import { getUserDevice } from '@ui/utilities/device/getUserDevice';
export const getOsShortcutSeparator = () => {
const device = getUserDevice();
- return device === 'mac' ? '' : ' ';
+ return device === 'mac' || device === 'ios' ? '' : ' ';
};
diff --git a/packages/twenty-ui/src/utilities/device/getUserDevice.ts b/packages/twenty-ui/src/utilities/device/getUserDevice.ts
index 1e724e079b1..cc12bb342cb 100644
--- a/packages/twenty-ui/src/utilities/device/getUserDevice.ts
+++ b/packages/twenty-ui/src/utilities/device/getUserDevice.ts
@@ -1,26 +1,40 @@
+import { isString } from '@sniptt/guards';
+
export const getUserDevice = () => {
- const userAgent = navigator.userAgent.toLowerCase();
+ const userAgent = globalThis.navigator?.userAgent;
+
+ if (!isString(userAgent)) {
+ return 'unknown';
+ }
+
+ const normalizedUserAgent = userAgent.toLowerCase();
+
+ if (
+ normalizedUserAgent.includes('ios') ||
+ normalizedUserAgent.includes('iphone') ||
+ normalizedUserAgent.includes('ipad')
+ ) {
+ return 'ios';
+ }
- if (userAgent.includes('mac os x') || userAgent.includes('macos')) {
+ if (
+ normalizedUserAgent.includes('mac os x') ||
+ normalizedUserAgent.includes('macos')
+ ) {
return 'mac';
}
- if (userAgent.includes('windows')) {
+ if (normalizedUserAgent.includes('windows')) {
return 'windows';
}
- if (userAgent.includes('linux')) {
- return 'linux';
+ if (normalizedUserAgent.includes('android')) {
+ return 'android';
}
- if (userAgent.includes('android')) return 'android';
-
- if (
- userAgent.includes('ios') ||
- userAgent.includes('iphone') ||
- userAgent.includes('ipad')
- )
- return 'ios';
+ if (normalizedUserAgent.includes('linux')) {
+ return 'linux';
+ }
return 'unknown';
};