feat: add multi-step download app dropdown with platform-specific options (#27211)
Co-authored-by: [email protected] <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: pasqualevitiello <[email protected]>
This commit is contained in:
8 files changed
+1150
-160
No files matched your search
@@ -0,0 +1,174 @@
|
||||
import { renderHook } from "@testing-library/react-hooks";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { useUserAgentData } from "./useUserAgentData";
|
||||
|
||||
describe("useUserAgentData hook", () => {
|
||||
const originalNavigator = global.navigator;
|
||||
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
Object.defineProperty(global, "navigator", {
|
||||
value: originalNavigator,
|
||||
writable: true,
|
||||
});
|
||||
});
|
||||
|
||||
function mockUserAgent(userAgent: string) {
|
||||
Object.defineProperty(global, "navigator", {
|
||||
value: { userAgent },
|
||||
writable: true,
|
||||
});
|
||||
}
|
||||
|
||||
describe("OS detection", () => {
|
||||
it("should detect iOS on iPhone", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("ios");
|
||||
expect(result.current.isMobile).toBe(true);
|
||||
});
|
||||
|
||||
it("should detect iOS on iPad", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (iPad; CPU OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("ios");
|
||||
expect(result.current.isMobile).toBe(true);
|
||||
});
|
||||
|
||||
it("should detect Android", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (Linux; Android 13; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Mobile Safari/537.36"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("android");
|
||||
expect(result.current.isMobile).toBe(true);
|
||||
});
|
||||
|
||||
it("should detect macOS", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("macos");
|
||||
expect(result.current.isMobile).toBe(false);
|
||||
});
|
||||
|
||||
it("should detect Windows", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("windows");
|
||||
expect(result.current.isMobile).toBe(false);
|
||||
});
|
||||
|
||||
it("should detect Linux", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("linux");
|
||||
expect(result.current.isMobile).toBe(false);
|
||||
});
|
||||
|
||||
it("should return unknown for unrecognized OS", () => {
|
||||
mockUserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("unknown");
|
||||
expect(result.current.isMobile).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Browser detection", () => {
|
||||
it("should detect Chrome on desktop", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("chrome");
|
||||
});
|
||||
|
||||
it("should detect Chrome on iOS (CriOS)", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/116.0.5845.103 Mobile/15E148 Safari/604.1"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("chrome");
|
||||
});
|
||||
|
||||
it("should detect Safari on macOS", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Safari/605.1.15"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("safari");
|
||||
});
|
||||
|
||||
it("should detect Safari on iOS", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.0 Mobile/15E148 Safari/604.1"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("safari");
|
||||
});
|
||||
|
||||
it("should detect Firefox on desktop", () => {
|
||||
mockUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/116.0");
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("firefox");
|
||||
});
|
||||
|
||||
it("should detect Firefox on iOS (FxiOS)", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (iPhone; CPU iPhone OS 16_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/116.0 Mobile/15E148 Safari/605.1.15"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("firefox");
|
||||
});
|
||||
|
||||
it("should detect Edge", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("edge");
|
||||
});
|
||||
|
||||
it("should not detect Chrome when Edge is present", () => {
|
||||
mockUserAgent(
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.69"
|
||||
);
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("edge");
|
||||
expect(result.current.browser).not.toBe("chrome");
|
||||
});
|
||||
|
||||
it("should return unknown for unrecognized browser", () => {
|
||||
mockUserAgent("Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.browser).toBe("unknown");
|
||||
});
|
||||
});
|
||||
|
||||
describe("SSR handling", () => {
|
||||
it("should return defaults when navigator is undefined", () => {
|
||||
Object.defineProperty(global, "navigator", {
|
||||
value: undefined,
|
||||
writable: true,
|
||||
});
|
||||
const { result } = renderHook(() => useUserAgentData());
|
||||
expect(result.current.os).toBe("unknown");
|
||||
expect(result.current.browser).toBe("unknown");
|
||||
expect(result.current.isMobile).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,72 @@
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
export type OperatingSystem = "macos" | "windows" | "linux" | "ios" | "android" | "unknown";
|
||||
export type Browser = "chrome" | "safari" | "firefox" | "edge" | "unknown";
|
||||
|
||||
export interface UserAgentData {
|
||||
os: OperatingSystem;
|
||||
browser: Browser;
|
||||
isMobile: boolean;
|
||||
}
|
||||
|
||||
function detectOS(userAgent: string): OperatingSystem {
|
||||
const ua = userAgent.toLowerCase();
|
||||
|
||||
if (/iphone|ipad|ipod/.test(ua)) {
|
||||
return "ios";
|
||||
}
|
||||
if (/android/.test(ua)) {
|
||||
return "android";
|
||||
}
|
||||
if (/macintosh|mac os x/.test(ua)) {
|
||||
return "macos";
|
||||
}
|
||||
if (/windows/.test(ua)) {
|
||||
return "windows";
|
||||
}
|
||||
if (/linux/.test(ua)) {
|
||||
return "linux";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
function detectBrowser(userAgent: string): Browser {
|
||||
const ua = userAgent.toLowerCase();
|
||||
|
||||
if (/edg\//.test(ua)) {
|
||||
return "edge";
|
||||
}
|
||||
if (/chrome|chromium|crios/.test(ua) && !/edg\//.test(ua)) {
|
||||
return "chrome";
|
||||
}
|
||||
if (/firefox|fxios/.test(ua)) {
|
||||
return "firefox";
|
||||
}
|
||||
if (/safari/.test(ua) && !/chrome|chromium|crios/.test(ua)) {
|
||||
return "safari";
|
||||
}
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
export function useUserAgentData(): UserAgentData {
|
||||
const [userAgentData, setUserAgentData] = useState<UserAgentData>({
|
||||
os: "unknown",
|
||||
browser: "unknown",
|
||||
isMobile: false,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === "undefined" || typeof navigator === "undefined") {
|
||||
return;
|
||||
}
|
||||
|
||||
const userAgent = navigator.userAgent;
|
||||
const os = detectOS(userAgent);
|
||||
const browser = detectBrowser(userAgent);
|
||||
const isMobile = os === "ios" || os === "android";
|
||||
|
||||
setUserAgentData({ os, browser, isMobile });
|
||||
}, []);
|
||||
|
||||
return userAgentData;
|
||||
}
|
||||
@@ -1,13 +1,11 @@
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
import * as DropdownMenuPrimitive from "@radix-ui/react-dropdown-menu";
|
||||
import Link from "next/link";
|
||||
import type { ComponentProps } from "react";
|
||||
import { forwardRef } from "react";
|
||||
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
|
||||
import type { ButtonColor } from "../button";
|
||||
import { Icon } from "../icon";
|
||||
import type { IconName } from "../icon";
|
||||
import { Icon } from "../icon";
|
||||
|
||||
export const Dropdown = DropdownMenuPrimitive.Root;
|
||||
|
||||
@@ -112,6 +110,45 @@ DropdownMenuCheckboxItem.displayName = "DropdownMenuCheckboxItem";
|
||||
|
||||
export const DropdownMenuRadioGroup = DropdownMenuPrimitive.RadioGroup;
|
||||
|
||||
export const DropdownMenuSub = DropdownMenuPrimitive.Sub;
|
||||
|
||||
type DropdownMenuSubTriggerProps = ComponentProps<(typeof DropdownMenuPrimitive)["SubTrigger"]>;
|
||||
export const DropdownMenuSubTrigger = forwardRef<HTMLDivElement, DropdownMenuSubTriggerProps>(
|
||||
({ className = "", children, ...props }, forwardedRef) => (
|
||||
<DropdownMenuPrimitive.SubTrigger
|
||||
className={classNames(
|
||||
"hover:bg-subtle hover:text-emphasis text-default flex cursor-pointer select-none items-center justify-between rounded-lg p-2 text-sm font-medium outline-none ring-inset first-of-type:rounded-t-[inherit] last-of-type:rounded-b-[inherit] focus:bg-subtle focus:text-emphasis",
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
ref={forwardedRef}>
|
||||
{children}
|
||||
<Icon name="chevron-right" className="ml-auto h-4 w-4" />
|
||||
</DropdownMenuPrimitive.SubTrigger>
|
||||
)
|
||||
);
|
||||
DropdownMenuSubTrigger.displayName = "DropdownMenuSubTrigger";
|
||||
|
||||
type DropdownMenuSubContentProps = ComponentProps<(typeof DropdownMenuPrimitive)["SubContent"]>;
|
||||
export const DropdownMenuSubContent = forwardRef<HTMLDivElement, DropdownMenuSubContentProps>(
|
||||
({ children, sideOffset = 2, alignOffset = -5, ...props }, forwardedRef) => {
|
||||
return (
|
||||
<DropdownMenuPrimitive.SubContent
|
||||
sideOffset={sideOffset}
|
||||
alignOffset={alignOffset}
|
||||
{...props}
|
||||
className={classNames(
|
||||
"shadow-dropdown bg-default border-subtle relative z-50 min-w-[180px] origin-top-left stack-y-px rounded-xl border p-1 text-sm",
|
||||
props.className
|
||||
)}
|
||||
ref={forwardedRef}>
|
||||
{children}
|
||||
</DropdownMenuPrimitive.SubContent>
|
||||
);
|
||||
}
|
||||
);
|
||||
DropdownMenuSubContent.displayName = "DropdownMenuSubContent";
|
||||
|
||||
type DropdownMenuRadioItemProps = ComponentProps<(typeof DropdownMenuPrimitive)["RadioItem"]>;
|
||||
export const DropdownMenuRadioItem = forwardRef<HTMLDivElement, DropdownMenuRadioItemProps>(
|
||||
({ children, ...props }, forwardedRef) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export {
|
||||
Dropdown,
|
||||
ButtonOrLink,
|
||||
Dropdown,
|
||||
DropdownItem,
|
||||
DropdownMenuCheckboxItem,
|
||||
DropdownMenuContent,
|
||||
@@ -8,5 +8,8 @@ export {
|
||||
DropdownMenuLabel,
|
||||
DropdownMenuPortal,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuSub,
|
||||
DropdownMenuSubContent,
|
||||
DropdownMenuSubTrigger,
|
||||
DropdownMenuTrigger,
|
||||
} from "./Dropdown";
|
||||
Reference in new issue
Block a user