|
| 1 | +import React, {FC, useCallback, useContext, useEffect, useMemo} from 'react' |
| 2 | +import {useDispatch, useSelector} from 'react-redux' |
| 3 | + |
| 4 | +// Components |
| 5 | +import SelectorTitle from 'src/dataExplorer/components/SelectorTitle' |
| 6 | +import FilterList from 'src/shared/components/FilterList/FilterList' |
| 7 | +import FluxDocsTooltipContent from 'src/shared/components/functions/perFunction/FluxDocsTooltipContent' |
| 8 | +import Fn from 'src/shared/components/FilterList/MinimalistInjectOption' |
| 9 | + |
| 10 | +// State |
| 11 | +import {getFluxPackages} from 'src/shared/actions/fluxDocs' |
| 12 | +import {getAllFluxFunctions} from 'src/shared/selectors/app' |
| 13 | +import {EditorContext} from 'src/shared/contexts/editor' |
| 14 | + |
| 15 | +// Types |
| 16 | +import {FluxFunction} from 'src/types/shared' |
| 17 | + |
| 18 | +// Utils |
| 19 | +import {event} from 'src/cloud/utils/reporting' |
| 20 | +import {getFluxExample} from 'src/shared/utils/fluxExample' |
| 21 | +import {sortFuncs} from 'src/shared/components/functions/utils' |
| 22 | +import './FluxBrowser.scss' |
| 23 | + |
| 24 | +const TOOLTIP = `The flux standard library contains several packages, \ |
| 25 | +functions, and variables which may be useful when constructing your flux query.` |
| 26 | + |
| 27 | +const FluxBrowser: FC = () => { |
| 28 | + const {editor, injectFunction} = useContext(EditorContext) |
| 29 | + |
| 30 | + const dispatch = useDispatch() |
| 31 | + const fluxFunctions = useSelector(getAllFluxFunctions) |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + if (fluxFunctions.length === 0) { |
| 35 | + dispatch(getFluxPackages()) |
| 36 | + } |
| 37 | + }, []) |
| 38 | + |
| 39 | + const handleSelectItem = useCallback( |
| 40 | + (func: FluxFunction) => { |
| 41 | + injectFunction(getFluxExample(func), _ => null) |
| 42 | + event('flux.function.injected', {name: `${func.package}.${func.name}`}) |
| 43 | + }, |
| 44 | + [injectFunction, editor] |
| 45 | + ) |
| 46 | + |
| 47 | + const render = useCallback( |
| 48 | + fn => ( |
| 49 | + <Fn |
| 50 | + onClick={handleSelectItem} |
| 51 | + extractor={fn => |
| 52 | + `${(fn as FluxFunction).package}.${(fn as FluxFunction).name}` |
| 53 | + } |
| 54 | + key={`${fn.name}_${fn.desc}`} |
| 55 | + option={fn} |
| 56 | + testID={fn.name} |
| 57 | + ToolTipContent={FluxDocsTooltipContent} |
| 58 | + /> |
| 59 | + ), |
| 60 | + [handleSelectItem, editor] |
| 61 | + ) |
| 62 | + |
| 63 | + return useMemo( |
| 64 | + () => ( |
| 65 | + <div className="qx-builder-flux-browser"> |
| 66 | + <SelectorTitle title="Flux library" info={TOOLTIP} /> |
| 67 | + <FilterList |
| 68 | + placeholder="Search for package or function..." |
| 69 | + emptyMessage="No results matched your search" |
| 70 | + extractor={fn => |
| 71 | + `${(fn as FluxFunction).name} ${(fn as FluxFunction).package}` |
| 72 | + } |
| 73 | + items={fluxFunctions.sort(sortFuncs)} |
| 74 | + renderItem={render} |
| 75 | + /> |
| 76 | + </div> |
| 77 | + ), |
| 78 | + [editor, render, fluxFunctions, injectFunction] |
| 79 | + ) |
| 80 | +} |
| 81 | + |
| 82 | +export default FluxBrowser |
0 commit comments