// Copyright (c) 2025-2026 Probo Inc . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. import { useStateWithRef } from "@probo/hooks"; import { useTranslate } from "@probo/i18n"; import { EditableCell, selectCell, SelectValue, Spinner } from "@probo/ui"; import { useEditableCellRef } from "@probo/ui/src/Molecules/Table/EditableCell"; import { useEditableRowContext } from "@probo/ui/src/Molecules/Table/EditableRow"; import { getKey } from "@probo/ui/src/Molecules/Table/utils"; import { Command } from "cmdk"; import { type ReactNode, Suspense } from "react"; import { useLazyLoadQuery } from "react-relay"; import type { GraphQLTaggedNode, OperationType, VariablesOf, } from "relay-runtime"; type Props = { name: string; query: GraphQLTaggedNode; variables: VariablesOf; items: (v: ReturnType>) => T[]; itemRenderer: (v: { item: T; onRemove?: (item: T) => void }) => ReactNode; } & ( | { defaultValue?: T; multiple?: undefined } | { defaultValue: T[]; multiple: true } ); export function GraphQLCell>(props: Props) { const [value, setValue, valueRef] = useStateWithRef( props.defaultValue, ); const cellRef = useEditableCellRef(); const { __ } = useTranslate(); const filteredValue = Array.isArray(value) ? value.filter(Boolean) : value ? [value] : []; const usedKeys = new Set(filteredValue.map(getKey).filter(Boolean) as string[]); const { onUpdate } = useEditableRowContext(); const onSelect = (item: T) => { if (props.multiple) { setValue([...((valueRef.current as T[]) ?? []), item]); return; } setValue(item); cellRef.current?.close(); }; const onClose = () => { if (valueRef.current === props.defaultValue) { return; } // Only send ids when updating the value onUpdate( props.name, Array.isArray(valueRef.current) ? valueRef.current.map(getKey) : getKey(valueRef.current), ); }; const classNames = selectCell(); return ( } onClose={onClose} ref={cellRef} >
{" "}
{" "} {props.multiple && ( )} )} >
); } function ItemList( props: Props & { className: string; onSelect: (item: T) => void; usedKeys: Set; }, ) { const data = useLazyLoadQuery(props.query, props.variables, { fetchPolicy: "network-only", }); const items = props.items(data); return ( <> {items .filter(item => !props.usedKeys.has(getKey(item) ?? "")) .map(item => ( props.onSelect(item)} > {props.itemRenderer({ item })} ))} ); }