diff --git a/src/components/CodeEditor/useMonacoCustomizations.ts b/src/components/CodeEditor/useMonacoCustomizations.ts index 4db51e03..1d8999e5 100644 --- a/src/components/CodeEditor/useMonacoCustomizations.ts +++ b/src/components/CodeEditor/useMonacoCustomizations.ts @@ -14,7 +14,7 @@ import { useTheme } from "@mui/material"; import type { SystemStyleObject, Theme } from "@mui/system"; import { useProjectContext } from "@src/contexts/ProjectContext"; -import { getFieldProp } from "@src/components/fields"; +import { getColumnType, getFieldProp } from "@src/components/fields"; /* eslint-disable import/no-webpack-loader-syntax */ import firestoreDefs from "!!raw-loader!./firestore.d.ts"; @@ -181,11 +181,7 @@ export default function useMonacoCustomizations({ Object.keys(tableState?.columns!) .map((columnKey: string) => { const column = tableState?.columns[columnKey]; - if ( - column.type === "JSON" || - (column.type === "DERIVATIVE" && - column.config.renderFieldType === "JSON") - ) { + if (getColumnType(column) === "JSON") { const interfaceName = columnKey[0].toUpperCase() + columnKey.slice(1); addJsonFieldDefinition(columnKey, interfaceName); diff --git a/src/components/Table/ContextMenu/index.tsx b/src/components/Table/ContextMenu/index.tsx index 7b3fd524..38f1b6e8 100644 --- a/src/components/Table/ContextMenu/index.tsx +++ b/src/components/Table/ContextMenu/index.tsx @@ -12,8 +12,9 @@ export default function ContextMenu() { const selectedColumn = _find(columns, { index: selectedColIndex }); if (!selectedColumn) return <>; const configActions = - getFieldProp("contextMenuActions", getColumnType(selectedColumn)) || + getFieldProp("contextMenuActions", selectedColumn.type) || function empty() {}; + console.log(configActions); const actions = configActions(selectedCell, resetContextMenu) || []; if (!anchorEle || actions.length === 0) return <>; diff --git a/src/components/Table/editors/TextEditor.tsx b/src/components/Table/editors/TextEditor.tsx index 9146e5ac..b10e09e2 100644 --- a/src/components/Table/editors/TextEditor.tsx +++ b/src/components/Table/editors/TextEditor.tsx @@ -6,11 +6,12 @@ import { TextField } from "@mui/material"; import { FieldType } from "@src/constants/fields"; import { getCellValue } from "@src/utils/fns"; import { useProjectContext } from "@src/contexts/ProjectContext"; +import { getColumnType } from "@src/components/fields"; export default function TextEditor({ row, column }: EditorProps) { const { updateCell } = useProjectContext(); - const type = (column as any).config?.renderFieldType ?? (column as any).type; + const type = getColumnType(column as any); const cellValue = getCellValue(row, column.key); const defaultValue = diff --git a/src/components/Table/index.tsx b/src/components/Table/index.tsx index 3976a8ff..38f6724a 100644 --- a/src/components/Table/index.tsx +++ b/src/components/Table/index.tsx @@ -25,7 +25,7 @@ import FinalColumn from "./formatters/FinalColumn"; import TableRow from "./TableRow"; import BulkActions from "./BulkActions"; -import { getFieldProp } from "@src/components/fields"; +import { getColumnType, getFieldProp } from "@src/components/fields"; import { FieldType } from "@src/constants/fields"; import { formatSubTableName } from "@src/utils/fns"; @@ -76,18 +76,12 @@ export default function Table() { frozen: column.fixed, headerRenderer: ColumnHeader, formatter: - getFieldProp( - "TableCell", - column.config?.renderFieldType ?? column.type - ) ?? + getFieldProp("TableCell", getColumnType(column)) ?? function InDev() { return null; }, editor: - getFieldProp( - "TableEditor", - column.config?.renderFieldType ?? column.type - ) ?? + getFieldProp("TableEditor", getColumnType(column)) ?? function InDev() { return null; }, diff --git a/src/components/TableHeader/Filters/FilterInputs.tsx b/src/components/TableHeader/Filters/FilterInputs.tsx index 9e232547..092ba116 100644 --- a/src/components/TableHeader/Filters/FilterInputs.tsx +++ b/src/components/TableHeader/Filters/FilterInputs.tsx @@ -98,13 +98,14 @@ export default function FilterInputs({ }} /> }> - {createElement(getFieldProp("SideDrawerField", columnType), { - column: selectedColumn, - control, - docRef: {}, - disabled, - onChange: () => {}, - })} + {columnType && + createElement(getFieldProp("SideDrawerField", columnType), { + column: selectedColumn, + control, + docRef: {}, + disabled, + onChange: () => {}, + })} )} diff --git a/src/components/TableHeader/Filters/useFilterInputs.ts b/src/components/TableHeader/Filters/useFilterInputs.ts index 0fbf71f4..b81e3388 100644 --- a/src/components/TableHeader/Filters/useFilterInputs.ts +++ b/src/components/TableHeader/Filters/useFilterInputs.ts @@ -36,10 +36,9 @@ export const useFilterInputs = (columns: TableState["columns"]) => { // Get the column config const selectedColumn = _find(filterColumns, ["key", query?.key]); // Get available filters from selected column type - const availableFilters = getFieldProp( - "filter", - getColumnType(selectedColumn) - ); + const availableFilters = selectedColumn + ? getFieldProp("filter", getColumnType(selectedColumn)) + : null; return { filterColumns, diff --git a/src/components/fields/Derivative/ContextMenuActions.tsx b/src/components/fields/Derivative/ContextMenuActions.tsx new file mode 100644 index 00000000..cafb7247 --- /dev/null +++ b/src/components/fields/Derivative/ContextMenuActions.tsx @@ -0,0 +1,63 @@ +import _find from "lodash/find"; +import _get from "lodash/get"; + +import Cut from "@mui/icons-material/ContentCut"; +import CopyCells from "@src/assets/icons/CopyCells"; +import Paste from "@mui/icons-material/ContentPaste"; +import EvalIcon from "@mui/icons-material/Replay"; + +import { useProjectContext } from "@src/contexts/ProjectContext"; +import { useSnackbar } from "notistack"; +import { SelectedCell } from "@src/atoms/ContextMenu"; +import { getFieldProp, getColumnType } from "@src/components/fields"; +import { runRoutes } from "@src/constants/runRoutes"; + +export interface IContextMenuActions { + label: string; + icon: React.ReactNode; + onClick: () => void; +} + +export default function ContextMenuActions( + selectedCell: SelectedCell, + reset: () => void | Promise +): IContextMenuActions[] { + const { tableState, deleteCell, updateCell, rowyRun } = useProjectContext(); + const { enqueueSnackbar } = useSnackbar(); + const columns = tableState?.columns; + const rows = tableState?.rows; + const selectedRowIndex = selectedCell.rowIndex as number; + const selectedColIndex = selectedCell?.colIndex; + const selectedCol = _find(columns, { index: selectedColIndex }); + const selectedRow = rows?.[selectedRowIndex]; + const cellValue = _get(selectedRow, selectedCol.key); + console.log({ + selectedCol, + schemaDocPath: tableState?.config.tableConfig.path, + }); + const handleClose = async () => await reset?.(); + + const handleEvaluate = async () => { + try { + if (!selectedCol || !rowyRun || !selectedRow) return; + rowyRun({ + route: runRoutes.evaluateDerivative, + body: { + ref: { + path: selectedRow.ref.path, + }, + schemaDocPath: tableState?.config.tableConfig.path, + columnKey: selectedCol.key, + }, + }); + } catch (error) { + enqueueSnackbar(`Failed: ${error}`, { variant: "error" }); + } + handleClose(); + }; + const contextMenuActions = [ + { label: "evalute", icon: , onClick: handleEvaluate }, + ]; + + return contextMenuActions; +} diff --git a/src/components/fields/Derivative/index.tsx b/src/components/fields/Derivative/index.tsx index ecde4190..55644011 100644 --- a/src/components/fields/Derivative/index.tsx +++ b/src/components/fields/Derivative/index.tsx @@ -5,7 +5,7 @@ import DerivativeIcon from "@src/assets/icons/Derivative"; import BasicCell from "../_BasicCell/BasicCellNull"; import NullEditor from "@src/components/Table/editors/NullEditor"; import Settings, { settingsValidator } from "./Settings"; - +import ContextMenuActions from "./ContextMenuActions"; export const config: IFieldConfig = { type: FieldType.derivative, name: "Derivative", @@ -20,6 +20,7 @@ export const config: IFieldConfig = { TableCell: withBasicCell(BasicCell), TableEditor: NullEditor as any, SideDrawerField: BasicCell as any, + contextMenuActions: ContextMenuActions, settings: Settings, settingsValidator, }; diff --git a/src/components/fields/_BasicCell/BasicCellContextMenuActions.tsx b/src/components/fields/_BasicCell/BasicCellContextMenuActions.tsx index 32758d99..b9da942d 100644 --- a/src/components/fields/_BasicCell/BasicCellContextMenuActions.tsx +++ b/src/components/fields/_BasicCell/BasicCellContextMenuActions.tsx @@ -55,6 +55,7 @@ export default function BasicContextMenuActions( const handlePaste = async () => { try { + if (!selectedCol) return; const text = await navigator.clipboard.readText(); const cellDataType = getFieldProp("dataType", getColumnType(selectedCol)); let parsed; diff --git a/src/components/fields/index.tsx b/src/components/fields/index.tsx index 04ed6d45..d4340295 100644 --- a/src/components/fields/index.tsx +++ b/src/components/fields/index.tsx @@ -121,9 +121,12 @@ export const hasDataTypes = (dataTypes: string[]) => { ); }; -export const getColumnType = (column?: TableColumn) => - !column - ? null - : column.type === FieldType.derivative +export const getColumnType = (column: { + type: FieldType; + config: { + renderFieldType: FieldType; + }; +}) => + column.type === FieldType.derivative ? column.config.renderFieldType : column.type; diff --git a/src/constants/runRoutes.ts b/src/constants/runRoutes.ts index 16e37fdd..3d5abc17 100644 --- a/src/constants/runRoutes.ts +++ b/src/constants/runRoutes.ts @@ -54,4 +54,8 @@ export const runRoutes = { algoliaAppId: { path: `/algoliaAppId`, method: "GET" } as RunRoute, functionLogs: { path: `/functionLogs`, method: "GET" } as RunRoute, auditChange: { path: `/auditChange`, method: "POST" } as RunRoute, + evaluateDerivative: { + path: `/evaluateDerivative`, + method: "POST", + } as RunRoute, } as const;