|
1 | 1 | "use client" |
2 | | -import { useState, useEffect } from "react" |
| 2 | +import { useState, useEffect, useRef, useCallback } from "react" |
| 3 | +import type { RealtimeChannel } from "@supabase/supabase-js" |
3 | 4 | import styled from "styled-components" |
4 | 5 | import { motion } from "framer-motion" |
5 | 6 | import { PotionBackground } from "../components/PotionBackground" |
6 | 7 | import { ErrorBoundary } from "../components/ErrorBoundary" |
7 | 8 | import { Button } from "../components/Button" |
| 9 | +import { supabaseClient } from "@/lib/supabaseClient" |
8 | 10 |
|
9 | 11 | // Components // |
10 | 12 |
|
11 | 13 | export default function Doorbell() { |
12 | 14 | const [isRinging, setIsRinging] = useState(false) |
| 15 | + const channelRef = useRef<RealtimeChannel | null>(null) |
| 16 | + const lastRingIdRef = useRef<string | null>(null) |
13 | 17 |
|
14 | | - const handleDoorbellClick = () => { |
| 18 | + const triggerLocalRing = useCallback(() => { |
15 | 19 | playDoorbellSound() |
16 | 20 | setIsRinging(true) |
| 21 | + }, []) |
| 22 | + |
| 23 | + const broadcastRing = useCallback(async (ringId: string) => { |
| 24 | + if (!channelRef.current) { |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + try { |
| 29 | + const status = await channelRef.current.send({ |
| 30 | + type: "broadcast", |
| 31 | + event: "ring", |
| 32 | + payload: { ringId } |
| 33 | + }) |
| 34 | + |
| 35 | + if (status !== "ok") { |
| 36 | + console.error("Failed to broadcast doorbell ring:", status) |
| 37 | + } |
| 38 | + } catch (error) { |
| 39 | + console.error("Failed to broadcast doorbell ring:", error) |
| 40 | + } |
| 41 | + }, []) |
| 42 | + |
| 43 | + const handleDoorbellClick = () => { |
| 44 | + const ringId = createRingIdentifier() |
| 45 | + lastRingIdRef.current = ringId |
| 46 | + triggerLocalRing() |
| 47 | + void broadcastRing(ringId) |
17 | 48 | } |
18 | 49 |
|
| 50 | + useEffect(() => { |
| 51 | + const client = supabaseClient |
| 52 | + if (!client) { |
| 53 | + return |
| 54 | + } |
| 55 | + |
| 56 | + const channel = client |
| 57 | + .channel("doorbell") |
| 58 | + .on("broadcast", { event: "ring" }, ({ payload }) => { |
| 59 | + const ringPayload = payload as RingPayload | undefined |
| 60 | + if (ringPayload?.ringId && ringPayload.ringId === lastRingIdRef.current) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + triggerLocalRing() |
| 65 | + }) |
| 66 | + .subscribe() |
| 67 | + |
| 68 | + channelRef.current = channel |
| 69 | + |
| 70 | + return () => { |
| 71 | + void channel.unsubscribe() |
| 72 | + client.removeChannel(channel) |
| 73 | + channelRef.current = null |
| 74 | + } |
| 75 | + }, [triggerLocalRing]) |
| 76 | + |
19 | 77 | useEffect(() => { |
20 | 78 | if (isRinging) { |
21 | 79 | const timer = setTimeout(() => { |
@@ -152,6 +210,18 @@ const AnimatedButtonContent = styled(motion.span)` |
152 | 210 |
|
153 | 211 | // Constants // |
154 | 212 |
|
| 213 | +type RingPayload = { |
| 214 | + ringId?: string |
| 215 | +} |
| 216 | + |
| 217 | +const createRingIdentifier = () => { |
| 218 | + if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") { |
| 219 | + return crypto.randomUUID() |
| 220 | + } |
| 221 | + |
| 222 | + return `${Date.now()}-${Math.random().toString(16).slice(2)}` |
| 223 | +} |
| 224 | + |
155 | 225 | const playDoorbellSound = () => { |
156 | 226 | try { |
157 | 227 | const audioContext = new (window.AudioContext || (window as any).webkitAudioContext)() |
|
0 commit comments