'use client' import { ExampleLayoutProps } from '../example-layout' const instanceId = Math.random().toString(36).substring(2, 9) const appJsCode = `import { useEffect, useState, useRef } from 'react'; import { createClient } from '@supabase/supabase-js'; // Initialize Supabase client const supabaseUrl = '${process.env.NEXT_PUBLIC_EXAMPLES_SUPABASE_URL}'; const supabaseKey = '${process.env.NEXT_PUBLIC_EXAMPLES_SUPABASE_ANON_KEY}'; const supabase = createClient(supabaseUrl, supabaseKey); // Channel name - using a unique ID to ensure both instances connect to the same channel const CHANNEL = 'emoji-example-${instanceId}'; // List of emojis for the picker const EMOJIS = ['👍', '❤️', '😂', '🎉', '🔥', '👏', '🚀', '✨', '🌈', '🦄']; export default function App() { const [username, setUsername] = useState(''); const [isConnected, setIsConnected] = useState(false); const [activeUsers, setActiveUsers] = useState([]); const [animatingEmojis, setAnimatingEmojis] = useState([]); const userId = useRef(Math.random().toString(36).substring(2, 15)); const channelRef = useRef(null); const containerRef = useRef(null); // Set up Supabase channel useEffect(() => { // Generate a random username const adjectives = ['Happy', 'Clever', 'Brave', 'Bright', 'Kind']; const nouns = ['Panda', 'Tiger', 'Eagle', 'Dolphin', 'Fox']; const randomName = \`\${adjectives[Math.floor(Math.random() * adjectives.length)]}\${ nouns[Math.floor(Math.random() * nouns.length)] }\${Math.floor(Math.random() * 100)}\`; setUsername(randomName); // Subscribe to channel const channel = supabase.channel(CHANNEL, { config: { presence: { key: userId.current, }, }, }); channelRef.current = channel; // Handle presence for user list channel.on('presence', { event: 'sync' }, () => { const state = channel.presenceState(); const users = []; // Convert presence state to array Object.keys(state).forEach(key => { const presences = state[key]; users.push(...presences); }); setActiveUsers(users); }); // Handle emoji click events channel.on('broadcast', { event: 'emoji_click' }, (payload) => { const { emoji, position, senderId } = payload.payload; // Don't animate our own emoji clicks (we already did that) if (senderId === userId.current) return; // Create a new animating emoji createAnimatingEmoji(emoji, position); }); // Subscribe to channel channel.subscribe(async (status) => { if (status === 'SUBSCRIBED') { // Track presence await channel.track({ userId: userId.current, username: randomName, online_at: new Date().getTime() }); setIsConnected(true); } }); return () => { channel.unsubscribe(); }; }, []); // Clean up finished animations useEffect(() => { if (animatingEmojis.length > 0) { const timer = setTimeout(() => { setAnimatingEmojis(emojis => emojis.filter(e => Date.now() - e.timestamp < 2000)); }, 2000); return () => clearTimeout(timer); } }, [animatingEmojis]); // Handle emoji click const handleEmojiClick = (emoji, event) => { if (!isConnected) return; // Get the position of the clicked emoji relative to the container const rect = event.currentTarget.getBoundingClientRect(); const containerRect = containerRef.current.getBoundingClientRect(); const position = { x: rect.left + rect.width / 2 - containerRect.left, y: rect.top - containerRect.top }; // Create a new animating emoji locally createAnimatingEmoji(emoji, position); // Broadcast the emoji click to other clients channelRef.current.send({ type: 'broadcast', event: 'emoji_click', payload: { emoji, position, senderId: userId.current } }); }; // Create a new animating emoji const createAnimatingEmoji = (emoji, position) => { const newEmoji = { id: Date.now() + Math.random(), emoji, position, timestamp: Date.now() }; setAnimatingEmojis(emojis => [...emojis, newEmoji]); }; return (
All connected users will see your reactions in real-time.