import React, { useContext, useState, useMemo } from 'react'; import { Popover, List, ListSubheader, ListItemButton, ListItemText, ListItemIcon, Divider } from '@mui/material'; import parse from 'autosuggest-highlight/parse'; import match from 'autosuggest-highlight/match'; import { ChevronRight, ChevronLeft } from '@nebula.js/ui/icons'; import { useTheme } from '@nebula.js/ui/theme'; import useSessionModel from '@nebula.js/nucleus/src/hooks/useSessionModel'; import useLayout from '@nebula.js/nucleus/src/hooks/useLayout'; import useLibraryList from '../../hooks/useLibraryList'; import AppContext from '../../contexts/AppContext'; import Search from './Search'; function Field({ field, onSelect, sub, parts }) { return ( onSelect(field.qName)} data-key={field.qName}> {parts.map((part, ix) => ( {part.text} ))} {sub && } ); } function LibraryItem({ item, onSelect, parts }) { return ( onSelect(item.qInfo)} data-key={item.qInfo.qId}> {parts.map((part, ix) => ( {part.text} ))} ); } function Aggr({ aggr, field, onSelect }) { return ( onSelect(aggr)} data-key={aggr}> {`${aggr}(${field})`} ); } function LibraryList({ app, onSelect, title = '', type = 'dimension', searchTerm = '' }) { const [libraryItems] = useLibraryList(app, type); const sortedLibraryItems = useMemo( () => libraryItems.slice().sort((a, b) => a.qData.title.toLowerCase().localeCompare(b.qData.title.toLowerCase())), [libraryItems] ); return libraryItems.length > 0 ? ( <> {title} {sortedLibraryItems.map((item) => { const matches = match(item.qData.title, searchTerm); const parts = parse(item.qData.title, matches); if (searchTerm && matches.length === 0) return null; return ; })} ) : null; } export default function FieldsPopover({ alignTo, show, close, onSelected, type }) { const app = useContext(AppContext); const [selectedField, setSelectedField] = useState(null); const [searchTerm, setSearchTerm] = useState(''); const theme = useTheme(); const [model] = useSessionModel( { qInfo: { qType: 'FieldList', qId: 'FieldList', }, qFieldListDef: { qShowDerivedFelds: false, qShowHidden: false, qShowSemantic: true, qShowSrcTables: true, qShowSystem: false, }, }, app ); const [layout] = useLayout(model); const fields = useMemo( () => (layout ? layout.qFieldList.qItems || [] : []) .slice() .sort((a, b) => a.qName.toLowerCase().localeCompare(b.qName.toLowerCase())), [layout] ); const onSelect = (s) => { if (s && s.qId) { onSelected(s); close(); } else if (type === 'measure') { setSelectedField(s); } else { onSelected({ field: s, }); close(); } }; const onAggregateSelected = (s) => { onSelected({ field: selectedField, aggregation: s, }); close(); }; return ( {!selectedField && } {selectedField && ( setSelectedField(null)}> Back Aggregation {['sum', 'count', 'avg', 'min', 'max'].map((v) => ( ))} )} {!selectedField && fields.length > 0 && ( Fields {fields.map((field) => { const matches = match(field.qName, searchTerm); const parts = parse(field.qName, matches); if (searchTerm && matches.length === 0) return null; return ; })} )} ); }