|
| 1 | +/** |
| 2 | + * Global logo bar matching Dart GlobalLogoBar: same layout, safe area formula, |
| 3 | + * fullscreen-based visibility, haptic on tap, navigate to root. |
| 4 | + * Uses @tma.js/sdk-react for viewport, isFullscreen, and hapticFeedback. |
| 5 | + */ |
| 6 | +import React, { useMemo } from "react"; |
| 7 | +import { View, Pressable, StyleSheet, Platform } from "react-native"; |
| 8 | +import { useRouter } from "expo-router"; |
| 9 | +import { |
| 10 | + viewport, |
| 11 | + hapticFeedback, |
| 12 | + useLaunchParams, |
| 13 | + useSignal, |
| 14 | +} from "@tma.js/sdk-react"; |
| 15 | +import { HyperlinksSpaceLogo } from "./HyperlinksSpaceLogo"; |
| 16 | + |
| 17 | +const LOGO_HEIGHT = 32; |
| 18 | +const BOTTOM_PADDING = 10; |
| 19 | +const HORIZONTAL_PADDING = 15; |
| 20 | +const BROWSER_FALLBACK_TOP_PADDING = 30; |
| 21 | + |
| 22 | +function useLogoTopPadding(): number { |
| 23 | + const safeTop = useSignal(viewport.safeAreaInsetTop); |
| 24 | + const contentTop = useSignal(viewport.contentSafeAreaInsetTop); |
| 25 | + |
| 26 | + return useMemo(() => { |
| 27 | + const safe = Number(safeTop ?? 0); |
| 28 | + const content = Number(contentTop ?? 0); |
| 29 | + if (safe === 0 && content === 0) return BROWSER_FALLBACK_TOP_PADDING; |
| 30 | + const value = safe + content / 2 - 16; |
| 31 | + return Number.isFinite(value) ? value : BROWSER_FALLBACK_TOP_PADDING; |
| 32 | + }, [safeTop, contentTop]); |
| 33 | +} |
| 34 | + |
| 35 | +function useLogoBlockHeight(): number { |
| 36 | + const topPadding = useLogoTopPadding(); |
| 37 | + return topPadding + LOGO_HEIGHT + BOTTOM_PADDING; |
| 38 | +} |
| 39 | + |
| 40 | +function useShouldShowLogo(): boolean { |
| 41 | + const launchParams = useLaunchParams(false); |
| 42 | + const isFullscreen = useSignal(viewport.isFullscreen); |
| 43 | + |
| 44 | + return useMemo(() => { |
| 45 | + const lp = launchParams as |
| 46 | + | { tgWebAppData?: { user?: unknown }; tg_web_app_data?: { user?: unknown } } |
| 47 | + | undefined; |
| 48 | + const hasUser = |
| 49 | + (lp?.tgWebAppData?.user != null || lp?.tg_web_app_data?.user != null) && |
| 50 | + typeof (lp?.tgWebAppData?.user ?? lp?.tg_web_app_data?.user) === "object"; |
| 51 | + if (!hasUser) return true; |
| 52 | + return isFullscreen ?? true; |
| 53 | + }, [launchParams, isFullscreen]); |
| 54 | +} |
| 55 | + |
| 56 | +export function GlobalLogoBar() { |
| 57 | + const router = useRouter(); |
| 58 | + const topPadding = useLogoTopPadding(); |
| 59 | + const blockHeight = useLogoBlockHeight(); |
| 60 | + const shouldShow = useShouldShowLogo(); |
| 61 | + |
| 62 | + const onPress = () => { |
| 63 | + try { |
| 64 | + hapticFeedback.impactOccurred?.("light"); |
| 65 | + } catch { |
| 66 | + if (Platform.OS === "web" && typeof window !== "undefined") { |
| 67 | + try { |
| 68 | + const w = window as unknown as { Telegram?: { WebApp?: { HapticFeedback?: { impactOccurred?: (s: string) => void } } } }; |
| 69 | + w.Telegram?.WebApp?.HapticFeedback?.impactOccurred?.("light"); |
| 70 | + } catch { |
| 71 | + // ignore |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + router.replace("/"); |
| 76 | + }; |
| 77 | + |
| 78 | + if (!shouldShow) { |
| 79 | + return <View style={[styles.container, { height: 0 }]} />; |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <View style={[styles.container, { height: blockHeight }]}> |
| 84 | + <View |
| 85 | + style={[ |
| 86 | + styles.inner, |
| 87 | + { |
| 88 | + paddingTop: topPadding, |
| 89 | + paddingBottom: BOTTOM_PADDING, |
| 90 | + paddingHorizontal: HORIZONTAL_PADDING, |
| 91 | + }, |
| 92 | + ]} |
| 93 | + > |
| 94 | + <Pressable |
| 95 | + onPress={onPress} |
| 96 | + style={styles.logoWrap} |
| 97 | + accessibilityRole="button" |
| 98 | + accessibilityLabel="Go to home" |
| 99 | + > |
| 100 | + <View style={styles.logoBox}> |
| 101 | + <HyperlinksSpaceLogo width={LOGO_HEIGHT} height={LOGO_HEIGHT} /> |
| 102 | + </View> |
| 103 | + </Pressable> |
| 104 | + </View> |
| 105 | + </View> |
| 106 | + ); |
| 107 | +} |
| 108 | + |
| 109 | +const styles = StyleSheet.create({ |
| 110 | + container: { |
| 111 | + width: "100%", |
| 112 | + backgroundColor: "transparent", |
| 113 | + }, |
| 114 | + inner: { |
| 115 | + width: "100%", |
| 116 | + alignItems: "center", |
| 117 | + justifyContent: "center", |
| 118 | + }, |
| 119 | + logoWrap: { |
| 120 | + maxWidth: 600, |
| 121 | + alignItems: "center", |
| 122 | + justifyContent: "center", |
| 123 | + }, |
| 124 | + logoBox: { |
| 125 | + width: LOGO_HEIGHT, |
| 126 | + height: LOGO_HEIGHT, |
| 127 | + }, |
| 128 | +}); |
0 commit comments