import { useRef, useState } from 'react'; import { useRecoilValue } from 'recoil'; import { Key } from 'ts-key-enum'; import { DropdownMenuHeader } from '@/ui/dropdown/components/DropdownMenuHeader'; import { DropdownMenuInput } from '@/ui/dropdown/components/DropdownMenuInput'; import { StyledDropdownMenu } from '@/ui/dropdown/components/StyledDropdownMenu'; import { StyledDropdownMenuItemsContainer } from '@/ui/dropdown/components/StyledDropdownMenuItemsContainer'; import { StyledDropdownMenuSeparator } from '@/ui/dropdown/components/StyledDropdownMenuSeparator'; import { useDropdownButton } from '@/ui/dropdown/hooks/useDropdownButton'; import { IconChevronLeft, IconFileImport, IconTag } from '@/ui/icon'; import { MenuItem } from '@/ui/menu-item/components/MenuItem'; import { useScopedHotkeys } from '@/ui/utilities/hotkey/hooks/useScopedHotkeys'; import { useRecoilScopedValue } from '@/ui/utilities/recoil-scope/hooks/useRecoilScopedValue'; import { ViewFieldsVisibilityDropdownSection } from '@/ui/view-bar/components/ViewFieldsVisibilityDropdownSection'; import { useUpsertView } from '@/ui/view-bar/hooks/useUpsertView'; import { viewsByIdScopedSelector } from '@/ui/view-bar/states/selectors/viewsByIdScopedSelector'; import { viewEditModeState } from '@/ui/view-bar/states/viewEditModeState'; import type { View } from '@/ui/view-bar/types/View'; import { TableOptionsDropdownId } from '../../constants/TableOptionsDropdownId'; import { useTableColumns } from '../../hooks/useTableColumns'; import { TableRecoilScopeContext } from '../../states/recoil-scope-contexts/TableRecoilScopeContext'; import { hiddenTableColumnsScopedSelector } from '../../states/selectors/hiddenTableColumnsScopedSelector'; import { visibleTableColumnsScopedSelector } from '../../states/selectors/visibleTableColumnsScopedSelector'; import { TableOptionsHotkeyScope } from '../../types/TableOptionsHotkeyScope'; type TableOptionsDropdownButtonProps = { onViewsChange?: (views: View[]) => void | Promise; onImport?: () => void; }; type TableOptionsMenu = 'fields'; export function TableOptionsDropdownContent({ onViewsChange, onImport, }: TableOptionsDropdownButtonProps) { const { closeDropdownButton } = useDropdownButton({ dropdownId: TableOptionsDropdownId, }); const [currentMenu, setCurrentMenu] = useState( undefined, ); const viewEditInputRef = useRef(null); const viewEditMode = useRecoilValue(viewEditModeState); const visibleTableColumns = useRecoilScopedValue( visibleTableColumnsScopedSelector, TableRecoilScopeContext, ); const hiddenTableColumns = useRecoilScopedValue( hiddenTableColumnsScopedSelector, TableRecoilScopeContext, ); const viewsById = useRecoilScopedValue( viewsByIdScopedSelector, TableRecoilScopeContext, ); const { upsertView } = useUpsertView({ onViewsChange, scopeContext: TableRecoilScopeContext, }); const { handleColumnVisibilityChange } = useTableColumns(); const handleViewNameSubmit = async () => { const name = viewEditInputRef.current?.value; await upsertView(name); }; const handleSelectMenu = (option: TableOptionsMenu) => { handleViewNameSubmit(); setCurrentMenu(option); }; const resetMenu = () => setCurrentMenu(undefined); useScopedHotkeys( Key.Escape, () => { closeDropdownButton(); }, TableOptionsHotkeyScope.Dropdown, ); useScopedHotkeys( Key.Enter, () => { handleViewNameSubmit(); resetMenu(); closeDropdownButton(); }, TableOptionsHotkeyScope.Dropdown, ); return ( {!currentMenu && ( <> {!!viewEditMode.mode ? ( ) : ( View settings )} handleSelectMenu('fields')} LeftIcon={IconTag} text="Fields" /> {onImport && ( )} )} {currentMenu === 'fields' && ( <> Fields {hiddenTableColumns.length > 0 && ( <> )} )} ); }