mirror of
https://github.com/rowyio/rowy.git
synced 2026-02-24 04:01:17 +01:00
Create atom to fetch defaultTableSettings from.
Use atom in useSave*** hooks to determine if popups are to be shown (and if not, whether changes are to be applied automatically).
This commit is contained in:
@@ -30,6 +30,13 @@ export const themeOverriddenAtom = atomWithStorage(
|
||||
false
|
||||
);
|
||||
|
||||
/** User's default table settings (affecting saving and popup behavior) */
|
||||
export const defaultTableSettingsAtom = atom((get) => {
|
||||
const userSettings = get(userSettingsAtom);
|
||||
console.log("defaultTableSettings", userSettings.defaultTableSettings);
|
||||
return userSettings.defaultTableSettings;
|
||||
});
|
||||
|
||||
/** Customized base theme based on project and user settings */
|
||||
export const customizedThemesAtom = atom((get) => {
|
||||
const publicSettings = get(publicSettingsAtom);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useState } from "react";
|
||||
import { useAtom } from "jotai";
|
||||
import { useAtom, useAtomValue } from "jotai";
|
||||
import { SnackbarKey, useSnackbar } from "notistack";
|
||||
|
||||
import LoadingButton from "@mui/lab/LoadingButton";
|
||||
@@ -11,17 +11,25 @@ import {
|
||||
tableScope,
|
||||
updateTableSchemaAtom,
|
||||
} from "@src/atoms/tableScope";
|
||||
import { projectScope, updateUserSettingsAtom } from "@src/atoms/projectScope";
|
||||
import {
|
||||
defaultTableSettingsAtom,
|
||||
projectScope,
|
||||
updateUserSettingsAtom,
|
||||
} from "@src/atoms/projectScope";
|
||||
import { TableSort } from "@src/types/table";
|
||||
|
||||
function useSaveTableSorts(canEditColumns: boolean) {
|
||||
const [updateTableSchema] = useAtom(updateTableSchemaAtom, tableScope);
|
||||
const [updateUserSettings] = useAtom(updateUserSettingsAtom, projectScope);
|
||||
const [tableId] = useAtom(tableIdAtom, tableScope);
|
||||
const defaultTableSettings = useAtomValue(
|
||||
defaultTableSettingsAtom,
|
||||
projectScope
|
||||
);
|
||||
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
|
||||
const [snackbarId, setSnackbarId] = useState<SnackbarKey | null>(null);
|
||||
|
||||
// Offer to save when table sorts changes
|
||||
// Offer to save when table sorts changes, depending on user settings
|
||||
const trigger = useCallback(
|
||||
(sorts: TableSort[]) => {
|
||||
if (!updateTableSchema) throw new Error("Cannot update table schema");
|
||||
@@ -33,6 +41,15 @@ function useSaveTableSorts(canEditColumns: boolean) {
|
||||
});
|
||||
}
|
||||
if (!canEditColumns) return;
|
||||
// If the user has disabled the popup, return early
|
||||
if (defaultTableSettings?.saveSortingPopupDisabled) {
|
||||
// If the user has `automaticallyApplySorting` set to true, apply the sorting before returning
|
||||
if (defaultTableSettings?.automaticallyApplySorting) {
|
||||
const updateTable = async () => await updateTableSchema({ sorts });
|
||||
updateTable();
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (snackbarId) {
|
||||
closeSnackbar(snackbarId);
|
||||
}
|
||||
@@ -57,6 +74,7 @@ function useSaveTableSorts(canEditColumns: boolean) {
|
||||
tableId,
|
||||
closeSnackbar,
|
||||
updateTableSchema,
|
||||
defaultTableSettings,
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useSetAtom } from "jotai";
|
||||
import { useAtomValue, useSetAtom } from "jotai";
|
||||
import { useSnackbar } from "notistack";
|
||||
import { useDebounce } from "use-debounce";
|
||||
import { isEqual, isEmpty } from "lodash-es";
|
||||
@@ -13,6 +13,10 @@ import {
|
||||
updateColumnAtom,
|
||||
IUpdateColumnOptions,
|
||||
} from "@src/atoms/tableScope";
|
||||
import {
|
||||
defaultTableSettingsAtom,
|
||||
projectScope,
|
||||
} from "@src/atoms/projectScope";
|
||||
import { DEBOUNCE_DELAY } from "./Table";
|
||||
import { ColumnSizingState } from "@tanstack/react-table";
|
||||
|
||||
@@ -26,14 +30,31 @@ export function useSaveColumnSizing(
|
||||
) {
|
||||
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
|
||||
const updateColumn = useSetAtom(updateColumnAtom, tableScope);
|
||||
const defaultTableSettings = useAtomValue(
|
||||
defaultTableSettingsAtom,
|
||||
projectScope
|
||||
);
|
||||
|
||||
// Debounce for saving to schema
|
||||
const [debouncedColumnSizing] = useDebounce(columnSizing, DEBOUNCE_DELAY, {
|
||||
equalityFn: isEqual,
|
||||
});
|
||||
// Offer to save when column sizing changes
|
||||
// Offer to save when column sizing changes, depending on user settings
|
||||
useEffect(() => {
|
||||
if (!canEditColumns || isEmpty(debouncedColumnSizing)) return;
|
||||
// If the user has disabled the popup, return early
|
||||
if (defaultTableSettings?.saveColumnWidthPopupDisabled) {
|
||||
// If the user has `automaticallyApplyColumnWidth` set to true, apply the column width before returning
|
||||
if (defaultTableSettings?.automaticallyApplyColumnWidth) {
|
||||
const updateTable = async () => {
|
||||
for (const [key, value] of Object.entries(debouncedColumnSizing)) {
|
||||
await updateColumn({ key, config: { width: value } });
|
||||
}
|
||||
};
|
||||
updateTable();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const snackbarId = enqueueSnackbar("Save column sizes for all users?", {
|
||||
action: (
|
||||
@@ -52,6 +73,7 @@ export function useSaveColumnSizing(
|
||||
enqueueSnackbar,
|
||||
closeSnackbar,
|
||||
updateColumn,
|
||||
defaultTableSettings,
|
||||
]);
|
||||
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user