import { IconEdit, IconExclamationMark, IconRefresh, IconSquarePlus, IconTrashX, } from '@tabler/icons-react'; import { useCallback, useEffect } from 'react'; import type { MantineBetterMenuInstanceItem } from '../../../MantineBetterMenu'; import type { FormRequestType } from '../../utils/types'; import type { GridlerColumn } from '../Column'; import { type GridlerProps, type GridlerState, useGridlerStore } from '../GridlerStore'; export function GlidlerFormAdaptor(props: { descriptionField?: ((data: Record) => string) | string; getMenuItems?: GridlerProps['getMenuItems']; onReload?: () => void; onRequestForm: ( request: FormRequestType, data: Array> | Record ) => void; showDescriptionInMenu?: boolean; }) { const [getState, mounted, setState, reload] = useGridlerStore((s) => [ s.getState, s.mounted, s.setState, s.reload, ]); const getMenuItems = useCallback( ( id: string, storeState: GridlerState, row?: Record, col?: GridlerColumn, defaultItems?: Array ) => { //console.log('GlidlerFormInterface getMenuItems', id); if (id === 'header-menu') { return defaultItems || []; } const items = [] as Array; if (defaultItems && id === 'cell') { items.push(...(defaultItems as Array)); } const rows = getState('_gridSelection')?.rows.toArray() ?? []; const manyRows = rows.length > 1; if (!row) { const firstRow = rows[0]; if (firstRow !== undefined) { row = storeState.getRowBuffer(firstRow); } } const desc = typeof props.descriptionField === 'string' ? row?.[props.descriptionField] : typeof props.descriptionField === 'function' && row ? props.descriptionField(row) : undefined; if (id === 'other') { items.push({ c: 'blue', label: 'Add', onClick: () => { props.onRequestForm('insert', row as Record); }, }); } if ((id === 'cell' && row) || (id === 'menu' && row)) { items.push({ c: 'teal', label: 'Add', leftSection: , onClick: () => { props.onRequestForm('insert', row as Record); }, }); if (!manyRows) { items.push({ c: 'green', label: `Modify${desc && props.showDescriptionInMenu ? ` (${desc})` : ''}`, leftSection: , onClick: () => { props.onRequestForm('change', row as Record); }, }); items.push({ c: 'red', label: `Remove${desc && props.showDescriptionInMenu ? ` (${desc})` : ''}`, leftSection: , onClick: () => { props.onRequestForm('delete', row as Record); }, }); } else { items.push({ c: 'green', label: `Modify All Selected (${rows.length})`, leftSection: , onClick: () => { props.onRequestForm( 'change', rows.map((r) => storeState.getRowBuffer(r)) as Array> ); }, }); items.push({ c: 'red', label: `Remove All Selected (${rows.length})`, leftSection: , onClick: () => { props.onRequestForm( 'delete', rows.map((r) => storeState.getRowBuffer(r)) as Array> ); }, }); } items.push({ isDivider: true, }); } else if ((id === 'cell' && !row) || (id === 'menu' && !row)) { items.push({ c: 'red', label: `Nothing Selected`, leftSection: , }); } items.push({ c: 'orange', label: 'Refresh', leftSection: , onClick: () => { reload?.(); }, }); const result = props.getMenuItems ? props.getMenuItems(id, storeState, row, col, items) : items; //console.log('GlidlerFormInterface getMenuItems', id, items); if (!items || items.length === 0) { return defaultItems || []; } return result; }, [props.onRequestForm, getState] ); useEffect(() => { if (mounted && typeof setState === 'function') { //console.log('GlidlerFormInterface setState getMenuItems1', mounted); if (getState('getMenuItems') !== getMenuItems) { setState('getMenuItems', getMenuItems); } } return () => {}; }, [props.getMenuItems, mounted]); return <>; }