mirror of
https://github.com/rowyio/rowy.git
synced 2025-12-29 00:16:39 +01:00
Merge pull request #1136 from rowyio/ROWY-969-formula-field-add-ref-to-context
ROWY-969: Add ref object to formula context
This commit is contained in:
@@ -7,6 +7,7 @@ import { defaultFn, getDisplayCell } from "./util";
|
||||
export default function Formula(props: IDisplayCellProps) {
|
||||
const { result, error, loading } = useFormula({
|
||||
row: props.row,
|
||||
ref: props._rowy_ref,
|
||||
listenerFields: props.column.config?.listenerFields || [],
|
||||
formulaFn: props.column.config?.formulaFn || defaultFn,
|
||||
});
|
||||
|
||||
@@ -3,14 +3,7 @@ import { useDebouncedCallback } from "use-debounce";
|
||||
import { useAtom } from "jotai";
|
||||
import MultiSelect from "@rowy/multiselect";
|
||||
|
||||
import {
|
||||
Grid,
|
||||
InputLabel,
|
||||
Typography,
|
||||
Stack,
|
||||
FormHelperText,
|
||||
Tooltip,
|
||||
} from "@mui/material";
|
||||
import { Grid, InputLabel, Stack, FormHelperText } from "@mui/material";
|
||||
|
||||
import {
|
||||
tableColumnsOrderedAtom,
|
||||
@@ -142,7 +135,11 @@ export default function Settings({
|
||||
additionalVariables={[
|
||||
{
|
||||
key: "row",
|
||||
description: `Current row's data`,
|
||||
description: `row has the value of doc.data() it has type definitions using this table's schema, but you can only access formula's listener fields.`,
|
||||
},
|
||||
{
|
||||
key: "ref",
|
||||
description: `reference object that holds the readonly reference of the row document.(i.e ref.id)`,
|
||||
},
|
||||
]}
|
||||
/>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { useCallback, useEffect } from "react";
|
||||
import { useSetAtom } from "jotai";
|
||||
import { useAtomCallback } from "jotai/utils";
|
||||
import { cloneDeep, findIndex, initial, sortBy } from "lodash-es";
|
||||
import { cloneDeep, findIndex, sortBy } from "lodash-es";
|
||||
|
||||
import {
|
||||
_deleteRowDbAtom,
|
||||
|
||||
4
src/components/fields/Formula/formula.d.ts
vendored
4
src/components/fields/Formula/formula.d.ts
vendored
@@ -1,6 +1,8 @@
|
||||
type RowRef = Pick<FirebaseFirestore.DocumentReference, "id", "path">;
|
||||
|
||||
type FormulaContext = {
|
||||
row: Row;
|
||||
// ref: FirebaseFirestore.DocumentReference;
|
||||
ref: RowRef;
|
||||
// storage: firebasestorage.Storage;
|
||||
// db: FirebaseFirestore.Firestore;
|
||||
};
|
||||
|
||||
@@ -2,17 +2,19 @@ import { useEffect, useMemo, useState } from "react";
|
||||
import { pick, zipObject } from "lodash-es";
|
||||
import { useAtom } from "jotai";
|
||||
|
||||
import { TableRow } from "@src/types/table";
|
||||
import { TableRow, TableRowRef } from "@src/types/table";
|
||||
import { tableColumnsOrderedAtom, tableScope } from "@src/atoms/tableScope";
|
||||
|
||||
import { listenerFieldTypes, useDeepCompareMemoize } from "./util";
|
||||
|
||||
export const useFormula = ({
|
||||
row,
|
||||
ref,
|
||||
listenerFields,
|
||||
formulaFn,
|
||||
}: {
|
||||
row: TableRow;
|
||||
ref: TableRowRef;
|
||||
listenerFields: string[];
|
||||
formulaFn: string;
|
||||
}) => {
|
||||
@@ -61,6 +63,7 @@ export const useFormula = ({
|
||||
worker.postMessage({
|
||||
formulaFn,
|
||||
row: JSON.stringify(availableFields),
|
||||
ref: { id: ref.id, path: ref.path },
|
||||
});
|
||||
|
||||
return () => {
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
onmessage = async ({ data }) => {
|
||||
try {
|
||||
const { formulaFn, row } = data;
|
||||
const { formulaFn, row, ref } = data;
|
||||
const AsyncFunction = async function () {}.constructor as any;
|
||||
const [_, fnBody] = formulaFn.match(/=>\s*({?[\s\S]*}?)$/);
|
||||
if (!fnBody) return;
|
||||
const fn = new AsyncFunction(
|
||||
"row",
|
||||
"ref",
|
||||
`const fn = async () => \n${fnBody}\n return fn();`
|
||||
);
|
||||
const result = await fn(JSON.parse(row));
|
||||
const result = await fn(JSON.parse(row), ref);
|
||||
postMessage({ result });
|
||||
} catch (error: any) {
|
||||
console.error("Error: ", error);
|
||||
|
||||
Reference in New Issue
Block a user