|
| 1 | +import { ArrowsClockwise, WifiHigh, WifiSlash } from "@phosphor-icons/react"; |
| 2 | +import { useService } from "@posthog/di/react"; |
| 3 | +import { useConnectivity } from "@posthog/ui/hooks/useConnectivity"; |
| 4 | +import { Box } from "@radix-ui/themes"; |
| 5 | +import { AnimatePresence, motion } from "framer-motion"; |
| 6 | +import { useEffect, useRef, useState } from "react"; |
| 7 | +import { |
| 8 | + CONNECTIVITY_CLIENT, |
| 9 | + type ConnectivityClient, |
| 10 | +} from "./connectivityClient"; |
| 11 | + |
| 12 | +const BACK_ONLINE_VISIBLE_MS = 2_500; |
| 13 | + |
| 14 | +/** |
| 15 | + * Shell banner for the global connectivity state: while offline, offers a Retry |
| 16 | + * that forces an immediate probe; briefly shows "Back online" on recovery. |
| 17 | + */ |
| 18 | +export function ConnectivityBanner() { |
| 19 | + const { isOnline } = useConnectivity(); |
| 20 | + const client = useService<ConnectivityClient>(CONNECTIVITY_CLIENT); |
| 21 | + const [isChecking, setIsChecking] = useState(false); |
| 22 | + const [showBackOnline, setShowBackOnline] = useState(false); |
| 23 | + const wasOnlineRef = useRef(isOnline); |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + const wasOnline = wasOnlineRef.current; |
| 27 | + wasOnlineRef.current = isOnline; |
| 28 | + |
| 29 | + if (!wasOnline && isOnline) { |
| 30 | + setIsChecking(false); |
| 31 | + setShowBackOnline(true); |
| 32 | + const timer = setTimeout( |
| 33 | + () => setShowBackOnline(false), |
| 34 | + BACK_ONLINE_VISIBLE_MS, |
| 35 | + ); |
| 36 | + return () => clearTimeout(timer); |
| 37 | + } |
| 38 | + |
| 39 | + if (!isOnline) { |
| 40 | + setShowBackOnline(false); |
| 41 | + } |
| 42 | + |
| 43 | + return undefined; |
| 44 | + }, [isOnline]); |
| 45 | + |
| 46 | + const handleRetry = () => { |
| 47 | + if (isChecking) return; |
| 48 | + setIsChecking(true); |
| 49 | + void client |
| 50 | + .checkNow() |
| 51 | + .catch(() => undefined) |
| 52 | + .finally(() => setIsChecking(false)); |
| 53 | + }; |
| 54 | + |
| 55 | + const isVisible = !isOnline || showBackOnline; |
| 56 | + |
| 57 | + return ( |
| 58 | + <AnimatePresence> |
| 59 | + {isVisible && ( |
| 60 | + <motion.div |
| 61 | + initial={{ height: 0, opacity: 0 }} |
| 62 | + animate={{ height: "auto", opacity: 1 }} |
| 63 | + exit={{ height: 0, opacity: 0 }} |
| 64 | + transition={{ duration: 0.2, ease: "easeInOut" }} |
| 65 | + className="no-drag shrink-0 overflow-hidden" |
| 66 | + > |
| 67 | + <Box className="px-2 pt-2"> |
| 68 | + {isOnline ? ( |
| 69 | + <BackOnlineRow /> |
| 70 | + ) : ( |
| 71 | + <OfflineRow isChecking={isChecking} onRetry={handleRetry} /> |
| 72 | + )} |
| 73 | + </Box> |
| 74 | + </motion.div> |
| 75 | + )} |
| 76 | + </AnimatePresence> |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function OfflineRow({ |
| 81 | + isChecking, |
| 82 | + onRetry, |
| 83 | +}: { |
| 84 | + isChecking: boolean; |
| 85 | + onRetry: () => void; |
| 86 | +}) { |
| 87 | + return ( |
| 88 | + <div className="flex w-full items-center gap-2.5 rounded-md border border-(--amber-6) bg-(--amber-3) px-3 py-2 text-(--amber-11) text-[13px]"> |
| 89 | + <WifiSlash size={16} weight="duotone" className="shrink-0" /> |
| 90 | + <div className="flex min-w-0 flex-1 flex-col gap-0.5"> |
| 91 | + <span className="font-medium">You're offline</span> |
| 92 | + <span className="text-(--amber-a11) text-[11px]"> |
| 93 | + {isChecking |
| 94 | + ? "Checking connection…" |
| 95 | + : "Network actions are paused — reconnecting automatically."} |
| 96 | + </span> |
| 97 | + </div> |
| 98 | + <button |
| 99 | + type="button" |
| 100 | + disabled={isChecking} |
| 101 | + onClick={onRetry} |
| 102 | + className="flex shrink-0 items-center gap-1.5 rounded-2 bg-(--amber-a4) px-2 py-1 font-medium text-(--amber-11) text-[12px] transition-colors hover:bg-(--amber-a5) disabled:opacity-60" |
| 103 | + > |
| 104 | + <ArrowsClockwise |
| 105 | + size={13} |
| 106 | + className={isChecking ? "animate-spin" : undefined} |
| 107 | + /> |
| 108 | + {isChecking ? "Checking…" : "Retry"} |
| 109 | + </button> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
| 113 | + |
| 114 | +function BackOnlineRow() { |
| 115 | + return ( |
| 116 | + <motion.div |
| 117 | + initial={{ opacity: 0 }} |
| 118 | + animate={{ opacity: 1 }} |
| 119 | + exit={{ opacity: 0 }} |
| 120 | + transition={{ duration: 0.15 }} |
| 121 | + className="flex w-full items-center gap-2.5 rounded-md border border-(--green-a5) bg-(--green-a3) px-3 py-2 text-(--green-11) text-[13px]" |
| 122 | + > |
| 123 | + <WifiHigh size={16} weight="duotone" className="shrink-0" /> |
| 124 | + <span className="font-medium">Back online</span> |
| 125 | + </motion.div> |
| 126 | + ); |
| 127 | +} |
0 commit comments