Live video on the AT Protocol
1import { responsiveValue } from "@streamplace/components/src/lib/utils";
2import { useMemo } from "react";
3import { useWindowDimensions } from "react-native";
4import { SharedValue } from "react-native-reanimated";
5
6export interface ResponsiveLayoutConfig {
7 shouldShowChatSidePanel: boolean;
8 shouldShowFloatingMetrics: boolean;
9 chatPanelWidth: number;
10 screenWidth: number;
11 availableHeight: number;
12}
13
14export function useResponsiveLayout({
15 sidebarWidth = 0,
16 sidebarHidden = true,
17 showChatSidePanelOnLandscape = true,
18}: {
19 sidebarWidth?: number | SharedValue<number>;
20 sidebarHidden?: boolean;
21 showChatSidePanelOnLandscape?: boolean;
22} = {}): ResponsiveLayoutConfig & {
23 contentWidth: number;
24} {
25 const { width: screenWidth, height: screenHeight } = useWindowDimensions();
26
27 const sidebarWidthValue = useMemo(() => {
28 if (typeof sidebarWidth === "object" && "value" in sidebarWidth) {
29 return sidebarWidth.value;
30 }
31 return sidebarWidth;
32 }, [
33 sidebarWidth,
34 // weirddd
35 (sidebarWidth as any)?.value,
36 sidebarHidden,
37 showChatSidePanelOnLandscape,
38 ]);
39
40 const isLandscape = screenWidth > screenHeight;
41 const shouldShowChatSidePanel =
42 isLandscape && screenWidth >= 768 && showChatSidePanelOnLandscape;
43
44 const shouldShowFloatingMetrics = screenWidth < 768;
45 const availableHeight = screenHeight;
46
47 const chatPanelWidth = responsiveValue(
48 {
49 md: 320,
50 lg: 400,
51 xl: 480,
52 default: 300,
53 },
54 screenWidth,
55 );
56
57 const availableWidth = screenWidth;
58
59 const contentWidth =
60 !sidebarHidden && sidebarWidthValue > 0
61 ? availableWidth - sidebarWidthValue
62 : availableWidth;
63
64 return {
65 shouldShowChatSidePanel,
66 shouldShowFloatingMetrics,
67 chatPanelWidth,
68 contentWidth,
69 screenWidth,
70 availableHeight,
71 };
72}