import { useState, useEffect } from "react"; import { Check, Plus, X } from "lucide-react"; import { Button } from "~/components/ui/button"; import { Popover, PopoverContent, PopoverPortal, PopoverTrigger, } from "~/components/ui/popover"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "~/components/ui/command"; import { Badge } from "~/components/ui/badge"; import { cn } from "~/lib/utils"; import { useFetcher } from "@remix-run/react"; import { Project } from "../icons/project"; interface Space { id: string; name: string; description?: string; } interface SpaceDropdownProps { episodeIds: string[]; selectedSpaceIds?: string[]; onSpaceChange?: (spaceIds: string[]) => void; className?: string; } export function SpaceDropdown({ episodeIds, selectedSpaceIds = [], onSpaceChange, className, }: SpaceDropdownProps) { const [open, setOpen] = useState(false); const [selectedSpaces, setSelectedSpaces] = useState(selectedSpaceIds); const [spaces, setSpaces] = useState([]); const spacesFetcher = useFetcher<{ spaces: Space[] }>(); const assignFetcher = useFetcher(); // Fetch all spaces useEffect(() => { spacesFetcher.load("/api/v1/spaces"); }, []); // Update spaces when data is fetched useEffect(() => { if (spacesFetcher.data?.spaces) { setSpaces(spacesFetcher.data.spaces); } }, [spacesFetcher.data]); const handleSpaceToggle = (spaceId: string) => { const newSelectedSpaces = selectedSpaces.includes(spaceId) ? selectedSpaces.filter((id) => id !== spaceId) : [...selectedSpaces, spaceId]; setSelectedSpaces(newSelectedSpaces); if (episodeIds) { assignFetcher.submit( { episodeIds: JSON.stringify(episodeIds), spaceId, action: selectedSpaces.includes(spaceId) ? "remove" : "assign", }, { method: "post", action: "/api/v1/episodes/assign-space", encType: "application/json", }, ); } // Call the callback if provided if (onSpaceChange) { onSpaceChange(newSelectedSpaces); } }; const selectedSpaceObjects = spaces.filter((space) => selectedSpaces.includes(space.id), ); const getTrigger = () => { if (selectedSpaceObjects?.length === 1) { return ( <> {selectedSpaceObjects[0].name} ); } if (selectedSpaceObjects?.length > 1) { return ( <> {selectedSpaceObjects.length} Spaces ); } return ( <> {" "} Spaces ); }; return (
{/* + button to add more spaces */} No spaces found. {spaces.map((space) => ( handleSpaceToggle(space.id)} >
{space.name}
))}
); }