import { useEffect, useState } from "react"; import { useDebouncedCallback } from "use-debounce"; import { get } from "lodash-es"; import { useAtom } from "jotai"; import { Button, Checkbox, Grid, InputAdornment, List, ListItemIcon, ListItemText, MenuItem, TextField, Typography, Radio, } from "@mui/material"; import SearchIcon from "@mui/icons-material/Search"; import { IConnectorSelectProps } from "."; import Loading from "@src/components/Loading"; import { getLabel } from "@src/components/fields/Connector/utils"; import { useSnackbar } from "notistack"; import { projectScope, rowyRunAtom } from "@src/atoms/projectScope"; import { tableScope, tableSettingsAtom } from "@src/atoms/tableScope"; import { getTableSchemaPath } from "@src/utils/table"; export interface IPopupContentsProps extends Omit {} // TODO: Implement infinite scroll here export default function PopupContents({ value = [], onChange, column, _rowy_ref, }: IPopupContentsProps) { const [rowyRun] = useAtom(rowyRunAtom, projectScope); const [tableSettings] = useAtom(tableSettingsAtom, tableScope); const { enqueueSnackbar } = useSnackbar(); // const url = config.url ; const { config } = column; const elementId = config.elementId; const multiple = Boolean(config.multiple); // Webservice search query const [query, setQuery] = useState(""); // Webservice response const [response, setResponse] = useState(null); const [hits, setHits] = useState([]); useEffect(() => { console.log(response); if (response?.success === false) { enqueueSnackbar(response.message, { variant: "error" }); } else if (Array.isArray(response?.hits)) { setHits(response.hits); } else { setHits([]); //enqueueSnackbar("response is not any array", { variant: "error" }); } }, [response]); const search = useDebouncedCallback( async (query: string) => { const resp = await rowyRun!({ route: { method: "POST", path: "/connector" }, body: { columnKey: column.key, query: query, schemaDocPath: getTableSchemaPath(tableSettings), rowDocPath: _rowy_ref.path, }, }); setResponse(resp); }, 1000, { leading: true } ); useEffect(() => { search(query); }, [query]); if (!response) return ; const select = (hit: any) => () => { if (multiple) onChange([...value, hit]); else onChange([hit]); }; const deselect = (hit: any) => () => { if (multiple) onChange(value.filter((v) => v[elementId] !== hit[elementId])); else onChange([]); }; const selectedValues = value?.map((item) => get(item, elementId)); const clearSelection = () => onChange([]); return ( setQuery(e.target.value)} fullWidth variant="filled" label="Search items" hiddenLabel placeholder="Search items" InputProps={{ startAdornment: ( ), }} InputLabelProps={{ className: "visually-hidden" }} onClick={(e) => e.stopPropagation()} onKeyDown={(e) => e.stopPropagation()} /> {hits.map((hit) => { const isSelected = selectedValues.some((v) => v === hit[elementId]); return ( = config.max} disableGutters style={{ margin: 0, width: "100%" }} > {multiple ? ( ) : ( )} ); })} {multiple && ( {value?.length} of {hits?.length} )} ); }