'use client' import type { ExampleLayoutProps } from '../example-layout' const instanceId = Math.random().toString(36).substring(2, 9) const appJsCode = `import { useEffect, useState } from 'react'; import './styles.css'; 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); // Generate a random user ID and avatar const userId = Math.random().toString(36).substring(2, 15); const getAvatarUrl = (id) => \`https://api.dicebear.com/7.x/avataaars/svg?seed=\${id}\`; // Channel name - using a unique ID to ensure both instances connect to the same channel const CHANNEL = 'presence-example-${instanceId}'; export default function App() { const [onlineUsers, setOnlineUsers] = useState([]); const [username, setUsername] = useState(''); const [isConnected, setIsConnected] = useState(false); 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 presence channel const channel = supabase.channel(CHANNEL); // Track presence state channel .on('presence', { event: 'sync' }, () => { const state = channel.presenceState(); const presenceList = []; // Convert presence state to array Object.keys(state).forEach(key => { const presences = state[key]; presenceList.push(...presences); }); setOnlineUsers(presenceList); }) .subscribe(async (status) => { if (status === 'SUBSCRIBED') { // Send presence state when subscribed await channel.track({ user_id: userId, username: randomName, avatar: getAvatarUrl(userId), online_at: new Date().getTime(), }); setIsConnected(true); } }); return () => { // Clean up subscription channel.unsubscribe(); }; }, []); return (