π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
fix(nc-gui): refactor utils
Ramesh Mane99929dc6001f
diff --git a/packages/nc-gui/store/views.ts b/packages/nc-gui/store/views.tsindex 7a0ce1c1cf..c88390578c 100644--- a/packages/nc-gui/store/views.ts+++ b/packages/nc-gui/store/views.ts@@ -1,4 +1,14 @@-import type { CalendarType, FilterType, GalleryType, KanbanType, MapType, RowColoringInfo, SortType, ViewType } from 'nocodb-sdk'+import type {+ CalendarType,+ FilterType,+ GalleryType,+ KanbanType,+ MapType,+ RowColoringInfo,+ SortType,+ ViewSettingOverrideOptions,+ ViewType,+} from 'nocodb-sdk'import { ViewTypes, ViewTypes as _ViewTypes } from 'nocodb-sdk'import { acceptHMRUpdate, defineStore } from 'pinia'import { useTitle } from '@vueuse/core'@@ -788,7 +798,7 @@ export const useViewsStore = defineStore('viewsStore', () => {const onOpenCopyViewConfigFromAnotherViewModal = ({defaultSelectedCopyViewConfigTypes,}: {- defaultSelectedCopyViewConfigTypes?: CopyViewConfigType[]+ defaultSelectedCopyViewConfigTypes?: ViewSettingOverrideOptions[]} = {}) => {if (!isEeUI || !isUIAllowed('viewCreateOrEdit')) returndiff --git a/packages/nc-gui/utils/viewUtils.ts b/packages/nc-gui/utils/viewUtils.tsindex b345b85b12..d3433bba77 100644--- a/packages/nc-gui/utils/viewUtils.ts+++ b/packages/nc-gui/utils/viewUtils.ts@@ -79,178 +79,3 @@ export const getDefaultViewMetas = (viewType: ViewTypes) => {}return {}}--/**- * Enum representing the different types of view configurations that can be copied from one view to another.- * Each type corresponds to a specific aspect of a view's configuration.- */-export enum CopyViewConfigType {- /** Field visibility settings */- FieldVisibility = 'fieldVisibility',- /** Field ordering configuration */- FieldOrder = 'fieldOrder',- /** Column width and row height settings */- ColumnWidthAndRowHeight = 'columnWidthAndRowHeight',- /** Row height settings */- RowHeight = 'rowHeight',- /** Filter configurations */- Filters = 'filters',- /** Grouping configurations */- Groups = 'groups',- /** Sort configurations */- Sorts = 'sorts',- /** Row coloring settings */- RowColoring = 'rowColoring',-}--/**- * Interface representing a copy view config option with its metadata.- * Used to define the properties and constraints for each type of view configuration that can be copied.- */-export interface CopyViewConfigOption {- /** The display order of this option in the UI */- order: number- /** The human-readable label for this option */- label: string- /** The copy view config type value */- value: CopyViewConfigType- /** The i18n translation key for the label */- i18nLabel: string- /** Array of view types that support this configuration option */- supportedViewTypes: ViewTypes[]-}--/**- * A comprehensive mapping of all available copy view configuration options.- *- * This map defines the metadata and constraints for each type of view configuration that can be copied,- * including display order, labels, i18n keys, and which view types support each configuration option.- *- * @remarks- * Use this map to:- * - Get the configuration options available for a specific view type- * - Determine which view types support a particular configuration option- * - Access i18n labels and display ordering for UI rendering- *- * @example- * ```ts- * // Get the field visibility option metadata- * const fieldVisibilityOption = copyViewConfigOptionMap[CopyViewConfigType.FieldVisibility]- * // Check if it's supported for grid views- * const isSupported = fieldVisibilityOption.supportedViewTypes.includes(ViewTypes.GRID)- * ```- */-export const copyViewConfigOptionMap: Record<CopyViewConfigType, CopyViewConfigOption> = {- [CopyViewConfigType.FieldVisibility]: {- order: 1,- label: 'Field visibility',- value: CopyViewConfigType.FieldVisibility,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.FieldVisibility}`,- supportedViewTypes: [ViewTypes.GRID, ViewTypes.GALLERY, ViewTypes.KANBAN, ViewTypes.FORM, ViewTypes.CALENDAR, ViewTypes.MAP],- },- [CopyViewConfigType.FieldOrder]: {- order: 2,- label: 'Field order',- value: CopyViewConfigType.FieldOrder,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.FieldOrder}`,- supportedViewTypes: [ViewTypes.GRID, ViewTypes.GALLERY, ViewTypes.KANBAN, ViewTypes.FORM, ViewTypes.CALENDAR, ViewTypes.MAP],- },- [CopyViewConfigType.ColumnWidthAndRowHeight]: {- order: 3,- label: 'Column width and row height',- value: CopyViewConfigType.ColumnWidthAndRowHeight,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.ColumnWidthAndRowHeight}`,- supportedViewTypes: [ViewTypes.GRID],- },- [CopyViewConfigType.RowHeight]: {- order: 4,- label: 'Row height',- value: CopyViewConfigType.RowHeight,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.RowHeight}`,- supportedViewTypes: [ViewTypes.GRID],- },- [CopyViewConfigType.Filters]: {- order: 5,- label: 'Filters',- value: CopyViewConfigType.Filters,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.Filters}`,- supportedViewTypes: [ViewTypes.GRID, ViewTypes.GALLERY, ViewTypes.KANBAN, ViewTypes.CALENDAR, ViewTypes.MAP],- },- [CopyViewConfigType.Groups]: {- order: 6,- label: 'Groups',- value: CopyViewConfigType.Groups,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.Groups}`,- supportedViewTypes: [ViewTypes.GRID],- },- [CopyViewConfigType.Sorts]: {- order: 7,- label: 'Sorts',- value: CopyViewConfigType.Sorts,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.Sorts}`,- supportedViewTypes: [ViewTypes.GRID, ViewTypes.GALLERY, ViewTypes.KANBAN, ViewTypes.CALENDAR],- },- [CopyViewConfigType.RowColoring]: {- order: 8,- label: 'Row coloring',- value: CopyViewConfigType.RowColoring,- i18nLabel: `objects.copyViewConfig.${CopyViewConfigType.RowColoring}`,- supportedViewTypes: [ViewTypes.GRID, ViewTypes.GALLERY, ViewTypes.KANBAN, ViewTypes.CALENDAR],- },-}--/**- * Retrieves all copy view configuration options with their availability status for a specific view type.- *- * This function returns all available configuration options, marking each as enabled or disabled- * based on whether the source view type supports that particular configuration.- * The returned options are sorted by their display order.- *- * @param copyFromViewType - The view type from which configurations will be copied- * @returns An array of configuration options with a 'disabled' flag indicating support status, sorted by order- *- * @example- * ```ts- * // Get all config options for copying from a Grid view- * const options = getCopyViewConfigOptions(ViewTypes.GRID)- * // Filter to only enabled options- * const enabledOptions = options.filter(opt => !opt.disabled)- * ```- */-export const getCopyViewConfigOptions = (copyFromViewType?: ViewTypes) => {- return Object.values(copyViewConfigOptionMap)- .map((option) => {- const { supportedViewTypes, ...rest } = option- return {- ...rest,- disabled: !ncIsUndefined(copyFromViewType) && !supportedViewTypes.includes(copyFromViewType),- }- })- .sort((a, b) => a.order - b.order)-}--/**- * Filters a list of copy view configuration types to only include those supported by the source view type.- *- * This function validates each configuration type against the source view type's capabilities,- * removing any unsupported or invalid configuration types from the list.- *- * @param copyViewConfigTypes - Array of configuration types to be validated- * @param copyFromViewType - The view type from which configurations will be copied- * @returns A filtered array containing only the configuration types supported by the source view type- *- * @example- * ```ts- * // Validate selected config types for a Form view- * const selectedTypes = [CopyViewConfigType.FieldVisibility, CopyViewConfigType.Filters, CopyViewConfigType.Groups]- * const supportedTypes = extractSupportedCopyViewConfigTypes(selectedTypes, ViewTypes.FORM)- * // Result: Only FieldVisibility will be included since Forms don't support Filters or Groups- * ```- */-export const extractSupportedCopyViewConfigTypes = (copyViewConfigTypes: CopyViewConfigType[], copyFromViewType: ViewTypes) => {- return copyViewConfigTypes.filter((type) => {- if (!copyViewConfigOptionMap[type]) return false-- return copyViewConfigOptionMap[type].supportedViewTypes.includes(copyFromViewType)- })-}diff --git a/packages/nocodb-sdk/src/lib/enums.ts b/packages/nocodb-sdk/src/lib/enums.tsindex a88b1bad61..695a8e7e94 100644--- a/packages/nocodb-sdk/src/lib/enums.ts+++ b/packages/nocodb-sdk/src/lib/enums.ts@@ -527,3 +527,14 @@ export enum WebhookEvents {VIEW = 'view',FIELD = 'field',}++export enum ViewSettingOverrideOptions {+ FIELD_VISIBILITY = 'fieldVisibility',+ FIELD_ORDER = 'fieldOrder',+ COLUMN_WIDTH = 'columnWidth',+ ROW_HEIGHT = 'rowHeight',+ FILTER_CONDITION = 'filterCondition',+ SORT = 'sort',+ GROUP = 'group',+ ROW_COLORING = 'rowColoring',+}diff --git a/packages/nocodb-sdk/src/lib/index.ts b/packages/nocodb-sdk/src/lib/index.tsindex f07da3b691..47c9ea2158 100644--- a/packages/nocodb-sdk/src/lib/index.ts+++ b/packages/nocodb-sdk/src/lib/index.ts@@ -77,3 +77,4 @@ export * from '~/lib/system';export * from '~/lib/timer.utils';export * from '~/lib/commonUtils';export * from '~/lib/CircularRefContext';+export * from '~/lib/viewUtils';diff --git a/packages/nocodb-sdk/src/lib/viewUtils.ts b/packages/nocodb-sdk/src/lib/viewUtils.tsnew file mode 100644index 0000000000..be033eb9e3--- /dev/null+++ b/packages/nocodb-sdk/src/lib/viewUtils.ts@@ -0,0 +1,209 @@+import { ViewSettingOverrideOptions } from './enums';+import { ViewTypes } from './globals';+import { ncIsUndefined } from './is';++/**+ * Interface representing a copy view config option with its metadata.+ * Used to define the properties and constraints for each type of view configuration that can be copied.+ */+export interface CopyViewConfigOption {+ /** The display order of this option in the UI */+ order: number;+ /** The human-readable label for this option */+ label: string;+ /** The copy view config type value */+ value: ViewSettingOverrideOptions;+ /** The i18n translation key for the label */+ i18nLabel: string;+ /** Array of view types that support this configuration option */+ supportedViewTypes: ViewTypes[];+ /** IconMapKey */+ icon: string;+}++/**+ * A comprehensive mapping of all available copy view configuration options.+ *+ * This map defines the metadata and constraints for each type of view configuration that can be copied,+ * including display order, labels, i18n keys, and which view types support each configuration option.+ *+ * @remarks+ * Use this map to:+ * - Get the configuration options available for a specific view type+ * - Determine which view types support a particular configuration option+ * - Access i18n labels and display ordering for UI rendering+ *+ * @example+ * ```ts+ * // Get the field visibility option metadata+ * const fieldVisibilityOption = copyViewConfigOptionMap[ViewSettingOverrideOptions.FIELD_VISIBILITY]+ * // Check if it's supported for grid views+ * const isSupported = fieldVisibilityOption.supportedViewTypes.includes(ViewTypes.GRID)+ * ```+ */+export const copyViewConfigOptionMap: Record<+ ViewSettingOverrideOptions,+ CopyViewConfigOption+> = {+ [ViewSettingOverrideOptions.FIELD_VISIBILITY]: {+ order: 1,+ label: 'Field visibility',+ value: ViewSettingOverrideOptions.FIELD_VISIBILITY,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.FIELD_VISIBILITY}`,+ supportedViewTypes: [+ ViewTypes.GRID,+ ViewTypes.GALLERY,+ ViewTypes.KANBAN,+ ViewTypes.FORM,+ ViewTypes.CALENDAR,+ ViewTypes.MAP,+ ],+ icon: 'eyeSlash',+ },+ [ViewSettingOverrideOptions.FIELD_ORDER]: {+ order: 2,+ label: 'Field order',+ value: ViewSettingOverrideOptions.FIELD_ORDER,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.FIELD_ORDER}`,+ supportedViewTypes: [+ ViewTypes.GRID,+ ViewTypes.GALLERY,+ ViewTypes.KANBAN,+ ViewTypes.FORM,+ ViewTypes.CALENDAR,+ ViewTypes.MAP,+ ],+ icon: 'ncNumberList',+ },+ [ViewSettingOverrideOptions.COLUMN_WIDTH]: {+ order: 3,+ label: 'Column width',+ value: ViewSettingOverrideOptions.COLUMN_WIDTH,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.COLUMN_WIDTH}`,+ supportedViewTypes: [ViewTypes.GRID],+ icon: 'rowHeight',+ },+ [ViewSettingOverrideOptions.ROW_HEIGHT]: {+ order: 4,+ label: 'Row height',+ value: ViewSettingOverrideOptions.ROW_HEIGHT,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.ROW_HEIGHT}`,+ supportedViewTypes: [ViewTypes.GRID],+ icon: 'rowHeight',+ },+ [ViewSettingOverrideOptions.FILTER_CONDITION]: {+ order: 5,+ label: 'Filter condition',+ value: ViewSettingOverrideOptions.FILTER_CONDITION,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.FILTER_CONDITION}`,+ supportedViewTypes: [+ ViewTypes.GRID,+ ViewTypes.GALLERY,+ ViewTypes.KANBAN,+ ViewTypes.CALENDAR,+ ViewTypes.MAP,+ ],+ icon: 'filter',+ },+ [ViewSettingOverrideOptions.GROUP]: {+ order: 6,+ label: 'Group',+ value: ViewSettingOverrideOptions.GROUP,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.GROUP}`,+ supportedViewTypes: [ViewTypes.GRID],+ icon: 'group',+ },+ [ViewSettingOverrideOptions.SORT]: {+ order: 7,+ label: 'Sort',+ value: ViewSettingOverrideOptions.SORT,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.SORT}`,+ supportedViewTypes: [+ ViewTypes.GRID,+ ViewTypes.GALLERY,+ ViewTypes.KANBAN,+ ViewTypes.CALENDAR,+ ],+ icon: 'sort',+ },+ [ViewSettingOverrideOptions.ROW_COLORING]: {+ order: 8,+ label: 'Row coloring',+ value: ViewSettingOverrideOptions.ROW_COLORING,+ i18nLabel: `objects.copyViewConfig.${ViewSettingOverrideOptions.ROW_COLORING}`,+ supportedViewTypes: [+ ViewTypes.GRID,+ ViewTypes.GALLERY,+ ViewTypes.KANBAN,+ ViewTypes.CALENDAR,+ ],+ icon: 'ncPaintRoller',+ },+};++/**+ * Retrieves all copy view configuration options with their availability status for a specific view type.+ *+ * This function returns all available configuration options, marking each as enabled or disabled+ * based on whether the source view type supports that particular configuration.+ * The returned options are sorted by their display order.+ *+ * @param sourceViewType - The view type from which configurations will be copied+ * @returns An array of configuration options with a 'disabled' flag indicating support status, sorted by order+ */+export const getCopyViewConfigOptions = (sourceViewType?: ViewTypes) => {+ return Object.values(copyViewConfigOptionMap)+ .map((option) => {+ const { supportedViewTypes, ...rest } = option;+ return {+ ...rest,+ disabled:+ !ncIsUndefined(sourceViewType) &&+ !supportedViewTypes.includes(sourceViewType),+ };+ })+ .sort((a, b) => a.order - b.order);+};++/**+ * Filters a list of copy view configuration types to only include those supported by the source view type.+ *+ * This function validates each configuration type against the source view type's capabilities,+ * removing any unsupported or invalid configuration types from the list.+ *+ * @param viewSettingOverrideOptions - Array of configuration types to be validated+ * @param sourceViewType - The view type from which configurations will be copied+ * @returns A filtered array containing only the configuration types supported by the source view type+ *+ * @example+ * ```ts+ * // Validate selected config types for a Form view+ * const selectedTypes = [ViewSettingOverrideOptions.FIELD_VISIBILITY, ViewSettingOverrideOptions.FILTER_CONDITION, ViewSettingOverrideOptions.GROUP]+ * const supportedTypes = extractSupportedViewSettingOverrideOptions(selectedTypes, ViewTypes.FORM)+ * // Result: Only FieldVisibility will be included since Forms don't support Filters or Groups+ * ```+ */+export const extractSupportedViewSettingOverrideOptions = (+ viewSettingOverrideOptions: ViewSettingOverrideOptions[],+ sourceViewType: ViewTypes+) => {+ return (viewSettingOverrideOptions || []).filter((type) => {+ if (!copyViewConfigOptionMap[type]) return false;++ return copyViewConfigOptionMap[type].supportedViewTypes.includes(+ sourceViewType+ );+ });+};++/**+ * Retrieves all view setting override options supported by a specific view type.+ */+export const getViewSettingOverrideOptionsByViewType = (+ viewType?: ViewTypes+) => {+ return extractSupportedViewSettingOverrideOptions(+ Object.values(ViewSettingOverrideOptions),+ viewType+ );+};