import PropTypes from 'prop-types' import { useContext, useMemo, useState } from 'react' import ObjectListViewContext from '../context/ObjectListViewContext' import { Button, Divider, Flex, Modal, Popover, Radio, Select, Typography } from 'antd' import GridIcon from '../../Icons/GridIcon' import ListIcon from '../../Icons/ListIcon' import KanbanIcon from '../../Icons/KanbanIcon' import SettingsIcon from '../../Icons/SettingsIcon' import InfoCircleIcon from '../../Icons/InfoCircleIcon' import { getModelByName } from '../../../database/ObjectModels' import { getViewModeType, isKanbanCategoryProperty, normalizeViewMode } from './viewModeUtils' const VIEW_MODE_OPTIONS = [ { type: 'list', label: 'List' }, { type: 'cards', label: 'Cards' }, { type: 'kanban', label: 'Kanban' } ] const { Text } = Typography const ObjectTableViewButton = ({ objectType, viewMode: viewModeProp, setViewMode: setViewModeProp, showEndingDivider = false, ...buttonProps }) => { const objectListView = useContext(ObjectListViewContext) const viewMode = viewModeProp ?? objectListView?.viewMode const setViewMode = setViewModeProp ?? objectListView?.setViewMode const [settingsOpen, setSettingsOpen] = useState(false) const normalizedViewMode = normalizeViewMode(viewMode) const model = getModelByName(objectType) const categoryProperties = useMemo( () => model?.properties?.filter((property) => isKanbanCategoryProperty(property) ) || [], [model] ) const hasKanbanOption = categoryProperties.length > 0 const defaultCategoryProperty = categoryProperties.find((property) => property.type === 'state')?.name || categoryProperties[0]?.name const availableViewModes = useMemo( () => VIEW_MODE_OPTIONS.filter( (option) => option.type !== 'kanban' || hasKanbanOption ).map((option) => option.type), [hasKanbanOption] ) const handleTypeChange = (nextType) => { if (nextType === 'kanban') { const categoryProperty = normalizedViewMode.type === 'kanban' ? normalizedViewMode.settings?.categoryProperty || defaultCategoryProperty : defaultCategoryProperty const nextMode = { type: 'kanban', settings: { categoryProperty } } if ( normalizedViewMode.type === 'kanban' && Array.isArray(normalizedViewMode.settings?.columns) ) { nextMode.settings.columns = normalizedViewMode.settings.columns } setViewMode(nextMode) return } setViewMode({ type: nextType }) } const handleCategoryPropertyChange = (categoryProperty) => { setViewMode({ type: 'kanban', settings: { categoryProperty } }) } const { onClick: buttonOnClick, ...restButtonProps } = buttonProps const handleCycleView = (event) => { buttonOnClick?.(event) const currentType = getViewModeType(normalizedViewMode) const currentIndex = availableViewModes.indexOf(currentType) const nextIndex = currentIndex === -1 ? 0 : (currentIndex + 1) % availableViewModes.length handleTypeChange(availableViewModes[nextIndex]) } const activeIcon = normalizedViewMode.type === 'cards' ? ( ) : normalizedViewMode.type === 'kanban' ? ( ) : ( ) const content = ( <> handleTypeChange(e.target.value)} > {VIEW_MODE_OPTIONS.filter( (option) => option.type !== 'kanban' || hasKanbanOption ).map((option) => ( {option.label} {option.type === 'kanban' && (