'use client'; import { useState, useRef, useEffect } from 'react'; import { getSpaceIdFromCookie } from '@/lib/spaces'; const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000/api'; interface SpaceItem { id: string; name: string; tagline: string; domain: string; logo_url: string | null; } export function SpaceSwitcher() { const [open, setOpen] = useState(false); const [spaces, setSpaces] = useState([]); const [currentSpaceId, setCurrentSpaceId] = useState('default'); const ref = useRef(null); useEffect(() => { setCurrentSpaceId(getSpaceIdFromCookie()); }, []); useEffect(() => { fetch(`${API_URL}/spaces`) .then((res) => (res.ok ? res.json() : [])) .then((data: SpaceItem[]) => setSpaces(data)) .catch(() => {}); }, []); useEffect(() => { function handleClick(e: MouseEvent) { if (ref.current && !ref.current.contains(e.target as Node)) { setOpen(false); } } document.addEventListener('click', handleClick); return () => document.removeEventListener('click', handleClick); }, []); const currentSpace = spaces.find((s) => s.id === currentSpaceId) || { id: 'default', name: 'rSwag', tagline: 'Community Merch, On Demand', domain: 'rswag.online', logo_url: null, }; if (spaces.length <= 1) return null; return (
{open && (
Spaces
{spaces.map((s) => ( setOpen(false)} > {s.logo_url ? ( ) : ( {s.name.slice(0, 2)} )}
{s.name} {s.tagline}
{s.id === currentSpaceId && ( )}
))}
Each space has its own designs & theme
)}
); }