Skip to content

Commit

Permalink
Merge pull request rowyio#1140 from rowyio/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
shamsmosowi authored Feb 26, 2023
2 parents eef7edb + 276033e commit 189b106
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 51 deletions.
20 changes: 20 additions & 0 deletions src/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ import {
tablePageAtom,
updateColumnAtom,
selectedCellAtom,
tableSortsAtom,
tableIdAtom,
} from "@src/atoms/tableScope";
import { projectScope, userSettingsAtom } from "@src/atoms/projectScope";
import { getFieldType, getFieldProp } from "@src/components/fields";
import { useKeyboardNavigation } from "./useKeyboardNavigation";
import { useMenuAction } from "./useMenuAction";
Expand Down Expand Up @@ -98,6 +101,11 @@ export default function Table({

const updateColumn = useSetAtom(updateColumnAtom, tableScope);

// Get user settings and tableId for applying sort sorting
const [userSettings] = useAtom(userSettingsAtom, projectScope);
const [tableId] = useAtom(tableIdAtom, tableScope);
const setTableSorts = useSetAtom(tableSortsAtom, tableScope);

// Store a **state** and reference to the container element
// so the state can re-render `TableBody`, preventing virtualization
// not detecting scroll if the container element was initially `null`
Expand Down Expand Up @@ -233,6 +241,18 @@ export default function Table({
containerRef,
]);

// apply user default sort on first render
const [applySort, setApplySort] = useState(true);
useEffect(() => {
if (applySort && Object.keys(tableSchema).length) {
const userDefaultSort = userSettings.tables?.[tableId]?.sorts || [];
setTableSorts(
userDefaultSort.length ? userDefaultSort : tableSchema.sorts || []
);
setApplySort(false);
}
}, [tableSchema, userSettings, tableId, setTableSorts, applySort]);

return (
<div
ref={(el) => setContainerEl(el)}
Expand Down
1 change: 1 addition & 0 deletions src/components/fields/Formula/DisplayCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
15 changes: 6 additions & 9 deletions src/components/fields/Formula/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)`,
},
]}
/>
Expand Down
2 changes: 1 addition & 1 deletion src/components/fields/Formula/TableSourcePreview.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/components/fields/Formula/formula.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
Expand Down
5 changes: 4 additions & 1 deletion src/components/fields/Formula/useFormula.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}) => {
Expand Down Expand Up @@ -61,6 +63,7 @@ export const useFormula = ({
worker.postMessage({
formulaFn,
row: JSON.stringify(availableFields),
ref: { id: ref.id, path: ref.path },
});

return () => {
Expand Down
5 changes: 3 additions & 2 deletions src/components/fields/Formula/worker.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 0 additions & 2 deletions src/sources/TableSourceFirestore/TableSourceFirestore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ import { getTableSchemaPath } from "@src/utils/table";
import { TableSchema } from "@src/types/table";
import { firebaseDbAtom } from "@src/sources/ProjectSourceFirebase";
import { projectScope } from "@src/atoms/projectScope";
import useApplySorts from "./useApplySorts";

/**
* When rendered, provides atom values for top-level tables and sub-tables
Expand Down Expand Up @@ -142,7 +141,6 @@ export const TableSourceFirestore = memo(function TableSourceFirestore() {
}
);

useApplySorts();
useAuditChange();
useBulkWriteDb();

Expand Down
35 changes: 0 additions & 35 deletions src/sources/TableSourceFirestore/useApplySorts.ts

This file was deleted.

0 comments on commit 189b106

Please sign in to comment.