"use client" import { useState, useRef } from "react" import { Button } from "@/shared/components/ui/button" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, } from "@/shared/components/ui/dialog" import { CrudResource, type CrudResourceProps } from "@/shared/data-view/resource-page" import type { ResourcePageClient, ResourceItem } from "@/shared/data-view/resource-page" import { Card, CardContent } from "@/shared/components/ui/card" export type ResourceSelectorDialogProps = { /** Dialog title shown in the header */ title: string /** Opens/closes the dialog */ open: boolean onOpenChange: (open: boolean) => void /** Called when user confirms selection */ onConfirm: (selectedRows: ResourceItem[]) => void /** CrudResource config for the table (columns, routeKey, getClient, etc.) */ crudProps: Omit, "render" | "tableProps"> /** Optional rowKey for selection identity. Defaults to "id" */ rowKey?: keyof ResourceItem } export function ResourceSelectorDialog({ title, open, onOpenChange, onConfirm, crudProps, rowKey, }: ResourceSelectorDialogProps) { type TItem = ResourceItem const selectedRef = useRef([]) const [count, setCount] = useState(0) const handleSelectionChange = (rows: TItem[]) => { selectedRef.current = rows setCount(rows.length) } const handleConfirm = () => { onConfirm(selectedRef.current) selectedRef.current = [] setCount(0) onOpenChange(false) } const handleCancel = () => { selectedRef.current = [] setCount(0) onOpenChange(false) } return ( { if (!v) handleCancel() }}> {title}
{...crudProps} tableProps={{ selection: { onSelectionChange: handleSelectionChange, ...(rowKey ? { rowKey } : {}), }, }} />
) }