"use client"; import { useState, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { useChannels, useChannel, useUpdateChannel } from "@/hooks/use-channels"; import type { LibraryItemFull, ScheduleConfig } from "@/lib/types"; import { WEEKDAYS } from "@/lib/types"; interface Props { selectedItems: LibraryItemFull[]; } export function AddToBlockDialog({ selectedItems }: Props) { const [open, setOpen] = useState(false); const [channelId, setChannelId] = useState(""); const [blockId, setBlockId] = useState(""); const { data: channels } = useChannels(); const { data: channel } = useChannel(channelId); const updateChannel = useUpdateChannel(); const manualBlocks = useMemo(() => { if (!channel) return []; const seen = new Set(); const result: { id: string; name: string }[] = []; for (const day of WEEKDAYS) { for (const block of channel.schedule_config.day_blocks[day] ?? []) { if (block.content.type === "manual" && !seen.has(block.id)) { seen.add(block.id); result.push({ id: block.id, name: block.name }); } } } return result; }, [channel]); async function handleConfirm() { if (!channel || !blockId) return; const updatedDayBlocks = { ...channel.schedule_config.day_blocks }; for (const day of WEEKDAYS) { updatedDayBlocks[day] = (updatedDayBlocks[day] ?? []).map(block => { if (block.id !== blockId || block.content.type !== "manual") return block; return { ...block, content: { ...block.content, items: [...block.content.items, ...selectedItems.map(i => i.id)], }, }; }); } const scheduleConfig: ScheduleConfig = { day_blocks: updatedDayBlocks }; await updateChannel.mutateAsync({ id: channelId, data: { schedule_config: scheduleConfig }, }); setOpen(false); } return ( <> Add to existing block

Channel

{channelId && (

Manual block

{manualBlocks.length === 0 ? (

No manual blocks in this channel.

) : ( )}
)}

Adding {selectedItems.length} item(s) to selected block.

); }