Skip to content

πŸ”₯ πŸ”₯ πŸ”₯ A Free & Self-hostable Airtable Alternative

feat: kanban, calendar and update ops

mertmitc592e5afb979
diff --git a/packages/nc-gui/composables/useCalendarViewStore.ts b/packages/nc-gui/composables/useCalendarViewStore.ts
index 59bed641c8..5a81a3afed 100644
--- a/packages/nc-gui/composables/useCalendarViewStore.ts
+++ b/packages/nc-gui/composables/useCalendarViewStore.ts
@@ -11,6 +11,7 @@ import type {
ViewType,
} from 'nocodb-sdk'
import type dayjs from 'dayjs'
+import { validateRowFilters } from '~/utils/dataUtils'
const formatData = (
list: Record<string, any>[],
@@ -47,7 +48,7 @@ const [useProvideCalendarViewStore, useCalendarViewStore] = useInjectionState(
const { isUIAllowed } = useRoles()
- const { isMobileMode } = useGlobal()
+ const { isMobileMode, user } = useGlobal()
const { getValidSearchQueryForColumn } = useFieldQuery()
@@ -212,15 +213,21 @@ const [useProvideCalendarViewStore, useCalendarViewStore] = useInjectionState(
const { isMysql } = useBase()
- const { base } = storeToRefs(useBase())
-
- const { $api, $e, $ncSocket } = useNuxtApp()
-
const { t } = useI18n()
const { addUndo, clone, defineViewScope } = useUndoRedo()
- const { sorts, nestedFilters, isSyncedTable, eventBus } = useSmartsheetStoreOrThrow()
+ const baseStore = useBase()
+
+ const { base } = storeToRefs(baseStore)
+
+ const { getBaseType } = baseStore
+
+ const { $e, $api, $ncSocket } = useNuxtApp()
+
+ const { sorts, nestedFilters, eventBus, isSyncedTable, allFilters, validFiltersFromUrlParams } = useSmartsheetStoreOrThrow()
+
+ const { metas } = useMetas()
const { getEvaluatedRowMetaRowColorInfo } = useViewRowColorRender()
@@ -998,6 +1005,236 @@ const [useProvideCalendarViewStore, useCalendarViewStore] = useInjectionState(
const activeDataListener = ref<string | null>(null)
+ const handleDataEvent = (data: DataPayload) => {
+ const { id, action, payload } = data
+
+ if (action === 'add') {
+ try {
+ const isValidationFailed = !validateRowFilters(
+ [...allFilters.value, ...validFiltersFromUrlParams.value],
+ payload,
+ meta.value?.columns as ColumnType[],
+ getBaseType(viewMeta.value?.view?.source_id),
+ metas.value,
+ {
+ currentUser: user.value,
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ },
+ )
+
+ if (isValidationFailed) {
+ return
+ }
+
+ // For 'add', the row definitely doesn't exist yet, so we only need to check if it should be added
+ // Check if new row should be in calendar view
+ const shouldBeInCalendar = isRowInCurrentDateRange(
+ payload,
+ calendarRange.value,
+ activeCalendarView.value!,
+ selectedDate.value,
+ selectedDateRange.value,
+ selectedMonth.value,
+ timezoneDayjs,
+ )
+
+ // Check if new row should be in sidebar
+ const shouldBeInSidebar = isRowMatchingSidebarFilter(
+ payload,
+ sideBarFilterOption.value,
+ calendarRange.value,
+ selectedDate.value,
+ selectedDateRange.value,
+ selectedMonth.value,
+ selectedTime.value,
+ timezoneDayjs,
+ )
+
+ const newRowData = {
+ row: payload,
+ oldRow: { ...payload },
+ rowMeta: {
+ new: false,
+ ...getEvaluatedRowMetaRowColorInfo(payload),
+ },
+ }
+
+ // Add to calendar if it should be there
+ if (shouldBeInCalendar) {
+ formattedData.value.push({ ...newRowData })
+ }
+
+ // Add to sidebar if it should be there
+ if (shouldBeInSidebar) {
+ formattedSideBarData.value.unshift({ ...newRowData })
+ }
+
+ // Update active dates if any row was added
+ if (shouldBeInCalendar || shouldBeInSidebar) {
+ updateActiveDatesForNewRecord(payload)
+ }
+ } catch (e) {
+ console.error('Failed to add calendar row on socket event', e)
+ }
+ } else if (action === 'update') {
+ try {
+ // Check if row currently exists in calendar view
+ const calendarRowIndex = formattedData.value.findIndex((row) => {
+ const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
+ return pk && `${pk}` === `${id}`
+ })
+
+ // Check if row currently exists in sidebar
+ const sidebarRowIndex = formattedSideBarData.value.findIndex((row) => {
+ const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
+ return pk && `${pk}` === `${id}`
+ })
+
+ const isValidationFailed = !validateRowFilters(
+ [...allFilters.value, ...validFiltersFromUrlParams.value],
+ payload,
+ meta.value?.columns as ColumnType[],
+ getBaseType(viewMeta.value?.view?.source_id),
+ metas.value,
+ {
+ currentUser: user.value,
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ },
+ )
+
+ // If validation fails and row exists in either view, delete it
+ if (isValidationFailed && (calendarRowIndex !== -1 || sidebarRowIndex !== -1)) {
+ handleDataEvent({ ...data, action: 'delete' })
+ return
+ }
+
+ // If validation pass but row doesn't exist in either view, add it
+ if (!isValidationFailed && calendarRowIndex === -1 && sidebarRowIndex === -1) {
+ handleDataEvent({ ...data, action: 'add' })
+ return
+ }
+
+ // Check if updated row should be in current calendar view
+ const shouldBeInCalendar = isRowInCurrentDateRange(
+ payload,
+ calendarRange.value,
+ activeCalendarView.value!,
+ selectedDate.value,
+ selectedDateRange.value,
+ selectedMonth.value,
+ timezoneDayjs,
+ )
+
+ // Check if updated row should be in sidebar
+ const shouldBeInSidebar = isRowMatchingSidebarFilter(
+ payload,
+ sideBarFilterOption.value,
+ calendarRange.value,
+ selectedDate.value,
+ selectedDateRange.value,
+ selectedMonth.value,
+ selectedTime.value,
+ timezoneDayjs,
+ )
+
+ // Handle calendar view updates
+ if (calendarRowIndex !== -1 && shouldBeInCalendar) {
+ // Case 1: Row exists in calendar AND should stay β†’ Update in place
+ const existingRow = formattedData.value[calendarRowIndex]
+ Object.assign(existingRow.row, payload)
+ Object.assign(existingRow.oldRow, payload)
+ Object.assign(existingRow.rowMeta, getEvaluatedRowMetaRowColorInfo(existingRow.row))
+ existingRow.rowMeta.changed = false
+ } else if (calendarRowIndex !== -1 && !shouldBeInCalendar) {
+ // Case 2: Row exists in calendar BUT should be removed β†’ Remove from calendar
+ formattedData.value.splice(calendarRowIndex, 1)
+ } else if (calendarRowIndex === -1 && shouldBeInCalendar) {
+ // Case 3: Row doesn't exist in calendar BUT should be added β†’ Add to calendar
+ const newCalendarRow = {
+ row: payload,
+ oldRow: { ...payload },
+ rowMeta: {
+ new: false,
+ changed: false,
+ ...getEvaluatedRowMetaRowColorInfo(payload),
+ },
+ }
+ formattedData.value.push(newCalendarRow)
+ } else {
+ // Case 4: Row doesn't exist in calendar AND shouldn't be added β†’ No action
+ }
+
+ // Handle sidebar updates
+ if (sidebarRowIndex !== -1 && shouldBeInSidebar) {
+ // Case 5: Row exists in sidebar AND should stay β†’ Update in place
+ const existingSidebarRow = formattedSideBarData.value[sidebarRowIndex]
+ Object.assign(existingSidebarRow.row, payload)
+ Object.assign(existingSidebarRow.oldRow, payload)
+ Object.assign(existingSidebarRow.rowMeta, getEvaluatedRowMetaRowColorInfo(existingSidebarRow.row))
+ existingSidebarRow.rowMeta.changed = false
+ } else if (sidebarRowIndex !== -1 && !shouldBeInSidebar) {
+ // Case 6: Row exists in sidebar BUT should be removed β†’ Remove from sidebar
+ formattedSideBarData.value.splice(sidebarRowIndex, 1)
+ } else if (sidebarRowIndex === -1 && shouldBeInSidebar) {
+ // Case 7: Row doesn't exist in sidebar BUT should be added β†’ Add to sidebar
+ const newSidebarRow = {
+ row: payload,
+ oldRow: { ...payload },
+ rowMeta: {
+ new: false,
+ changed: false,
+ ...getEvaluatedRowMetaRowColorInfo(payload),
+ },
+ }
+ formattedSideBarData.value.unshift(newSidebarRow)
+ } else {
+ // Case 8: Row doesn't exist in sidebar AND shouldn't be added β†’ No action
+ }
+
+ // Update active dates after any changes
+ fetchActiveDates()
+ } catch (e) {
+ console.error('Failed to update calendar row on socket event', e)
+ }
+ } else if (action === 'delete') {
+ try {
+ // For delete, we need to remove the row from wherever it exists
+ // We don't need to check filters since we're removing it entirely
+
+ let removedFromCalendar = false
+ let removedFromSidebar = false
+
+ // Remove from calendar view if it exists there
+ const calendarRowIndex = formattedData.value.findIndex((row) => {
+ const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
+ return pk && `${pk}` === `${id}`
+ })
+
+ if (calendarRowIndex !== -1) {
+ formattedData.value.splice(calendarRowIndex, 1)
+ removedFromCalendar = true
+ }
+
+ // Remove from sidebar if it exists there
+ const sidebarRowIndex = formattedSideBarData.value.findIndex((row) => {
+ const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
+ return pk && `${pk}` === `${id}`
+ })
+
+ if (sidebarRowIndex !== -1) {
+ formattedSideBarData.value.splice(sidebarRowIndex, 1)
+ removedFromSidebar = true
+ }
+ // Update active dates if anything was removed
+ if (removedFromCalendar || removedFromSidebar) {
+ fetchActiveDates()
+ }
+ } catch (e) {
+ console.error('Failed to delete calendar row on socket event', e)
+ }
+ }
+ }
+
watch(
meta,
(newMeta: any, oldMeta: any) => {
@@ -1009,195 +1246,7 @@ const [useProvideCalendarViewStore, useCalendarViewStore] = useInjectionState(
}
activeDataListener.value = $ncSocket.onMessage(
`${EventType.DATA_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`,
- (data: DataPayload) => {
- const { id, action, payload } = data
-
- if (action === 'add') {
- try {
- // For 'add', the row definitely doesn't exist yet, so we only need to check if it should be added
- // Check if new row should be in calendar view
- const shouldBeInCalendar = isRowInCurrentDateRange(
- payload,
- calendarRange.value,
- activeCalendarView.value!,
- selectedDate.value,
- selectedDateRange.value,
- selectedMonth.value,
- timezoneDayjs,
- )
-
- // Check if new row should be in sidebar
- const shouldBeInSidebar = isRowMatchingSidebarFilter(
- payload,
- sideBarFilterOption.value,
- calendarRange.value,
- selectedDate.value,
- selectedDateRange.value,
- selectedMonth.value,
- selectedTime.value,
- timezoneDayjs,
- )
-
- const newRowData = {
- row: payload,
- oldRow: { ...payload },
- rowMeta: {
- new: false,
- ...getEvaluatedRowMetaRowColorInfo(payload),
- },
- }
-
- // Add to calendar if it should be there
- if (shouldBeInCalendar) {
- formattedData.value.push({ ...newRowData })
- }
-
- // Add to sidebar if it should be there
- if (shouldBeInSidebar) {
- formattedSideBarData.value.unshift({ ...newRowData })
- }
-
- // Update active dates if any row was added
- if (shouldBeInCalendar || shouldBeInSidebar) {
- updateActiveDatesForNewRecord(payload)
- }
- } catch (e) {
- console.error('Failed to add calendar row on socket event', e)
- }
- } else if (action === 'update') {
- try {
- // Check if row currently exists in calendar view
- const calendarRowIndex = formattedData.value.findIndex((row) => {
- const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
- return pk && `${pk}` === `${id}`
- })
-
- // Check if row currently exists in sidebar
- const sidebarRowIndex = formattedSideBarData.value.findIndex((row) => {
- const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
- return pk && `${pk}` === `${id}`
- })
-
- // Check if updated row should be in current calendar view
- const shouldBeInCalendar = isRowInCurrentDateRange(
- payload,
- calendarRange.value,
- activeCalendarView.value!,
- selectedDate.value,
- selectedDateRange.value,
- selectedMonth.value,
- timezoneDayjs,
- )
-
- // Check if updated row should be in sidebar
- const shouldBeInSidebar = isRowMatchingSidebarFilter(
- payload,
- sideBarFilterOption.value,
- calendarRange.value,
- selectedDate.value,
- selectedDateRange.value,
- selectedMonth.value,
- selectedTime.value,
- timezoneDayjs,
- )
-
- // Handle calendar view updates
- if (calendarRowIndex !== -1 && shouldBeInCalendar) {
- // Case 1: Row exists in calendar AND should stay β†’ Update in place
- const existingRow = formattedData.value[calendarRowIndex]
- Object.assign(existingRow.row, payload)
- Object.assign(existingRow.oldRow, payload)
- Object.assign(existingRow.rowMeta, getEvaluatedRowMetaRowColorInfo(existingRow.row))
- existingRow.rowMeta.changed = false
- } else if (calendarRowIndex !== -1 && !shouldBeInCalendar) {
- // Case 2: Row exists in calendar BUT should be removed β†’ Remove from calendar
- formattedData.value.splice(calendarRowIndex, 1)
- } else if (calendarRowIndex === -1 && shouldBeInCalendar) {
- // Case 3: Row doesn't exist in calendar BUT should be added β†’ Add to calendar
- const newCalendarRow = {
- row: payload,
- oldRow: { ...payload },
- rowMeta: {
- new: false,
- changed: false,
- ...getEvaluatedRowMetaRowColorInfo(payload),
- },
- }
- formattedData.value.push(newCalendarRow)
- } else {
- // Case 4: Row doesn't exist in calendar AND shouldn't be added β†’ No action
- }
-
- // Handle sidebar updates
- if (sidebarRowIndex !== -1 && shouldBeInSidebar) {
- // Case 5: Row exists in sidebar AND should stay β†’ Update in place
- const existingSidebarRow = formattedSideBarData.value[sidebarRowIndex]
- Object.assign(existingSidebarRow.row, payload)
- Object.assign(existingSidebarRow.oldRow, payload)
- Object.assign(existingSidebarRow.rowMeta, getEvaluatedRowMetaRowColorInfo(existingSidebarRow.row))
- existingSidebarRow.rowMeta.changed = false
- } else if (sidebarRowIndex !== -1 && !shouldBeInSidebar) {
- // Case 6: Row exists in sidebar BUT should be removed β†’ Remove from sidebar
- formattedSideBarData.value.splice(sidebarRowIndex, 1)
- } else if (sidebarRowIndex === -1 && shouldBeInSidebar) {
- // Case 7: Row doesn't exist in sidebar BUT should be added β†’ Add to sidebar
- const newSidebarRow = {
- row: payload,
- oldRow: { ...payload },
- rowMeta: {
- new: false,
- changed: false,
- ...getEvaluatedRowMetaRowColorInfo(payload),
- },
- }
- formattedSideBarData.value.unshift(newSidebarRow)
- } else {
- // Case 8: Row doesn't exist in sidebar AND shouldn't be added β†’ No action
- }
-
- // Update active dates after any changes
- fetchActiveDates()
- } catch (e) {
- console.error('Failed to update calendar row on socket event', e)
- }
- } else if (action === 'delete') {
- try {
- // For delete, we need to remove the row from wherever it exists
- // We don't need to check filters since we're removing it entirely
-
- let removedFromCalendar = false
- let removedFromSidebar = false
-
- // Remove from calendar view if it exists there
- const calendarRowIndex = formattedData.value.findIndex((row) => {
- const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
- return pk && `${pk}` === `${id}`
- })
-
- if (calendarRowIndex !== -1) {
- formattedData.value.splice(calendarRowIndex, 1)
- removedFromCalendar = true
- }
-
- // Remove from sidebar if it exists there
- const sidebarRowIndex = formattedSideBarData.value.findIndex((row) => {
- const pk = extractPkFromRow(row.row, meta.value?.columns as ColumnType[])
- return pk && `${pk}` === `${id}`
- })
-
- if (sidebarRowIndex !== -1) {
- formattedSideBarData.value.splice(sidebarRowIndex, 1)
- removedFromSidebar = true
- }
- // Update active dates if anything was removed
- if (removedFromCalendar || removedFromSidebar) {
- fetchActiveDates()
- }
- } catch (e) {
- console.error('Failed to delete calendar row on socket event', e)
- }
- }
- },
+ handleDataEvent,
)
}
},
diff --git a/packages/nc-gui/composables/useInfiniteData.ts b/packages/nc-gui/composables/useInfiniteData.ts
index 5dc97428cb..5bb49336a8 100644
--- a/packages/nc-gui/composables/useInfiniteData.ts
+++ b/packages/nc-gui/composables/useInfiniteData.ts
@@ -2039,337 +2039,337 @@ export function useInfiniteData(args: {
const activeDataListener = ref<string | null>(null)
const activeCommentListener = ref<string | null>(null)
- watch(
- meta,
- (newMeta, oldMeta) => {
- if (newMeta?.fk_workspace_id && newMeta?.base_id && newMeta?.id) {
- if (oldMeta?.id && oldMeta.id === newMeta.id) return
+ const handleDataEvent = (data: DataPayload) => {
+ const { id, action, payload, before } = data
+
+ if (action === 'add') {
+ // Add the new row to the local cache (cachedRows)
+ try {
+ const dataCache = getDataCache()
+
+ const isValidationFailed = !validateRowFilters(
+ [...allFilters.value, ...computedWhereFilter.value],
+ payload,
+ meta.value?.columns as ColumnType[],
+ getBaseType(viewMeta.value?.view?.source_id),
+ metas.value,
+ {
+ currentUser: user.value,
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ },
+ )
- if (activeDataListener.value) {
- $ncSocket.offMessage(activeDataListener.value)
+ // find index to insert the new row
+ if (before) {
+ for (const [rowIndex, cachedRow] of dataCache.cachedRows.value.entries()) {
+ const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
+ if (pk && `${pk}` === `${before}`) {
+ // Insert before the found row
+ const newRowIndex = rowIndex
+ // Use descending order so that we first open the position then place the new record
+ const rowsToShift = Array.from(dataCache.cachedRows.value.entries())
+ .filter(([index]) => index >= newRowIndex)
+ .sort((a, b) => b[0] - a[0])
+
+ for (const [index, rowData] of rowsToShift) {
+ rowData.rowMeta.rowIndex = index + 1
+ dataCache.cachedRows.value.delete(index)
+ dataCache.cachedRows.value.set(index + 1, rowData)
+ }
+
+ if (!isValidationFailed) {
+ dataCache.cachedRows.value.set(newRowIndex, {
+ row: payload,
+ oldRow: {},
+ rowMeta: { new: false, rowIndex: newRowIndex, path: [] },
+ })
+
+ dataCache.totalRows.value++
+ dataCache.actualTotalRows.value = Math.max(dataCache.actualTotalRows.value || 0, dataCache.totalRows.value)
+
+ callbacks?.syncVisibleData?.()
+ }
+ return
+ }
+ }
}
- if (activeCommentListener.value) {
- $ncSocket.offMessage(activeCommentListener.value)
+ if (!isValidationFailed) {
+ // If no order is found, append to the end
+ const newRowIndex = dataCache.totalRows.value
+ dataCache.cachedRows.value.set(newRowIndex, {
+ row: payload,
+ oldRow: {},
+ rowMeta: { new: false, rowIndex: newRowIndex, path: [] },
+ })
+ dataCache.totalRows.value++
+ dataCache.actualTotalRows.value = Math.max(dataCache.actualTotalRows.value || 0, dataCache.totalRows.value)
+
+ callbacks?.syncVisibleData?.()
}
+ } catch (e) {
+ console.error('Failed to add cached row on socket event', e)
+ }
+ } else if (action === 'update') {
+ // Update the row in the local cache (cachedRows)
+ try {
+ const dataCache = getDataCache()
+ let updated = false
+ for (const cachedRow of dataCache.cachedRows.value.values()) {
+ const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
+ if (pk && `${pk}` === `${id}`) {
+ Object.assign(cachedRow.row, payload)
+ Object.assign(cachedRow.oldRow, payload)
+
+ const isValidationFailed = !validateRowFilters(
+ [...allFilters.value, ...computedWhereFilter.value],
+ payload,
+ meta.value?.columns as ColumnType[],
+ getBaseType(viewMeta.value?.view?.source_id),
+ metas.value,
+ {
+ currentUser: user.value,
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ },
+ )
- activeDataListener.value = $ncSocket.onMessage(
- `${EventType.DATA_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`,
- (data: DataPayload) => {
- const { id, action, payload, before } = data
-
- if (action === 'add') {
- // Add the new row to the local cache (cachedRows)
- try {
- const dataCache = getDataCache()
-
- const isValidationFailed = !validateRowFilters(
- [...allFilters.value, ...computedWhereFilter.value],
- payload,
- meta.value?.columns as ColumnType[],
- getBaseType(viewMeta.value?.view?.source_id),
- metas.value,
- {
- currentUser: user.value,
- timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
- },
- )
-
- // find index to insert the new row
- if (before) {
- for (const [rowIndex, cachedRow] of dataCache.cachedRows.value.entries()) {
- const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
- if (pk && `${pk}` === `${before}`) {
- // Insert before the found row
- const newRowIndex = rowIndex
- // Use descending order so that we first open the position then place the new record
- const rowsToShift = Array.from(dataCache.cachedRows.value.entries())
- .filter(([index]) => index >= newRowIndex)
- .sort((a, b) => b[0] - a[0])
-
- for (const [index, rowData] of rowsToShift) {
- rowData.rowMeta.rowIndex = index + 1
- dataCache.cachedRows.value.delete(index)
- dataCache.cachedRows.value.set(index + 1, rowData)
- }
-
- if (!isValidationFailed) {
- dataCache.cachedRows.value.set(newRowIndex, {
- row: payload,
- oldRow: {},
- rowMeta: { new: false, rowIndex: newRowIndex, path: [] },
- })
-
- dataCache.totalRows.value++
- dataCache.actualTotalRows.value = Math.max(
- dataCache.actualTotalRows.value || 0,
- dataCache.totalRows.value,
- )
-
- callbacks?.syncVisibleData?.()
- }
- return
- }
- }
- }
+ cachedRow.rowMeta.isValidationFailed = isValidationFailed
+ cachedRow.rowMeta.changed = false
+ updated = true
+ break
+ }
+ }
+ if (updated) {
+ callbacks?.syncVisibleData?.()
+ } else {
+ handleDataEvent({
+ ...data,
+ action: 'add',
+ })
+ }
+ } catch (e) {
+ console.error('Failed to update cached row on socket event', e)
+ }
+ } else if (action === 'delete') {
+ // Delete the row from the local cache (cachedRows)
+ try {
+ const dataCache = getDataCache()
+ for (const [rowIndex, cachedRow] of dataCache.cachedRows.value.entries()) {
+ const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
+ if (pk && `${pk}` === `${id}`) {
+ dataCache.cachedRows.value.delete(rowIndex)
+
+ const rows = Array.from(dataCache.cachedRows.value.entries())
+ const rowsToShift = rows.filter(([index]) => index > rowIndex)
+ rowsToShift.sort((a, b) => a[0] - b[0])
+
+ for (const [index, row] of rowsToShift) {
+ const newIndex = index - 1
+ row.rowMeta.rowIndex = newIndex
+ dataCache.cachedRows.value.delete(index)
+ dataCache.cachedRows.value.set(newIndex, row)
+ }
- if (!isValidationFailed) {
- // If no order is found, append to the end
- const newRowIndex = dataCache.totalRows.value
- dataCache.cachedRows.value.set(newRowIndex, {
- row: payload,
- oldRow: {},
- rowMeta: { new: false, rowIndex: newRowIndex, path: [] },
- })
- dataCache.totalRows.value++
- dataCache.actualTotalRows.value = Math.max(dataCache.actualTotalRows.value || 0, dataCache.totalRows.value)
-
- callbacks?.syncVisibleData?.()
- }
- } catch (e) {
- console.error('Failed to add cached row on socket event', e)
+ if (rowsToShift.length) {
+ dataCache.chunkStates.value[getChunkIndex(rowsToShift[rowsToShift.length - 1][0])] = undefined
+ }
+
+ dataCache.totalRows.value = (dataCache.totalRows.value || 0) - 1
+ dataCache.actualTotalRows.value = Math.max(0, (dataCache.actualTotalRows.value || 0) - 1)
+ }
+ }
+ callbacks?.syncVisibleData?.()
+ } catch (e) {
+ console.error('Failed to delete cached row on socket event', e)
+ }
+ } else if (action === 'reorder') {
+ // Reorder/move the row in the local cache (cachedRows)
+ try {
+ const dataCache = getDataCache()
+
+ // Find the row to be moved by its primary key
+ let rowToMove: Row | null = null
+ let currentIndex: number | null = null
+
+ for (const [index, cachedRow] of dataCache.cachedRows.value.entries()) {
+ const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
+ if (pk && `${pk}` === `${id}`) {
+ rowToMove = cachedRow
+ currentIndex = index
+ break
+ }
+ }
+
+ if (!rowToMove || currentIndex === null) {
+ console.warn('Row to move not found in cache:', id)
+
+ if (before) {
+ // Find the 'before' row in cache
+ let beforeIndex: number | null = null
+ for (const [index, cachedRow] of dataCache.cachedRows.value.entries()) {
+ const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
+ if (pk && `${pk}` === `${before}`) {
+ beforeIndex = index
+ break
}
- } else if (action === 'update') {
- // Update the row in the local cache (cachedRows)
- try {
- const dataCache = getDataCache()
- let updated = false
- for (const cachedRow of dataCache.cachedRows.value.values()) {
- const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
- if (pk && `${pk}` === `${id}`) {
- Object.assign(cachedRow.row, payload)
- Object.assign(cachedRow.oldRow, payload)
-
- const isValidationFailed = !validateRowFilters(
- [...allFilters.value, ...computedWhereFilter.value],
- payload,
- meta.value?.columns as ColumnType[],
- getBaseType(viewMeta.value?.view?.source_id),
- metas.value,
- {
- currentUser: user.value,
- timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
- },
- )
-
- cachedRow.rowMeta.isValidationFailed = isValidationFailed
- cachedRow.rowMeta.changed = false
- updated = true
- break
- }
- }
- if (updated) {
- callbacks?.syncVisibleData?.()
- }
- } catch (e) {
- console.error('Failed to update cached row on socket event', e)
+ }
+
+ if (beforeIndex !== null) {
+ // The new row position is before an existing cached row
+ // We need to shift all rows from beforeIndex onwards down by 1
+ const newCachedRows = new Map(dataCache.cachedRows.value.entries())
+
+ // Get all rows that need to be shifted (from beforeIndex onwards)
+ const rowsToShift = Array.from((dataCache.cachedRows.value as Map<number, Row>).entries())
+ .filter(([index]) => index >= beforeIndex!)
+ .sort((a, b) => b[0] - a[0])
+
+ // Shift each row down by 1
+ for (const [index, row] of rowsToShift) {
+ const newIndex = index + 1
+ row.rowMeta.rowIndex = newIndex
+ newCachedRows.delete(index)
+ newCachedRows.set(newIndex, row)
}
- } else if (action === 'delete') {
- // Delete the row from the local cache (cachedRows)
- try {
- const dataCache = getDataCache()
- for (const [rowIndex, cachedRow] of dataCache.cachedRows.value.entries()) {
- const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
- if (pk && `${pk}` === `${id}`) {
- dataCache.cachedRows.value.delete(rowIndex)
-
- const rows = Array.from(dataCache.cachedRows.value.entries())
- const rowsToShift = rows.filter(([index]) => index > rowIndex)
- rowsToShift.sort((a, b) => a[0] - b[0])
-
- for (const [index, row] of rowsToShift) {
- const newIndex = index - 1
- row.rowMeta.rowIndex = newIndex
- dataCache.cachedRows.value.delete(index)
- dataCache.cachedRows.value.set(newIndex, row)
- }
-
- if (rowsToShift.length) {
- dataCache.chunkStates.value[getChunkIndex(rowsToShift[rowsToShift.length - 1][0])] = undefined
- }
-
- dataCache.totalRows.value = (dataCache.totalRows.value || 0) - 1
- dataCache.actualTotalRows.value = Math.max(0, (dataCache.actualTotalRows.value || 0) - 1)
- }
+
+ // Invalidate affected chunks
+ if (rowsToShift.length > 0) {
+ const minAffectedChunk = getChunkIndex(beforeIndex!)
+ const maxAffectedChunk = getChunkIndex(rowsToShift[rowsToShift.length - 1][0] + 1)
+ for (let i = minAffectedChunk; i <= maxAffectedChunk; i++) {
+ dataCache.chunkStates.value[i] = undefined
}
- callbacks?.syncVisibleData?.()
- } catch (e) {
- console.error('Failed to delete cached row on socket event', e)
}
- } else if (action === 'reorder') {
- // Reorder/move the row in the local cache (cachedRows)
- try {
- const dataCache = getDataCache()
-
- // Find the row to be moved by its primary key
- let rowToMove: Row | null = null
- let currentIndex: number | null = null
-
- for (const [index, cachedRow] of dataCache.cachedRows.value.entries()) {
- const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
- if (pk && `${pk}` === `${id}`) {
- rowToMove = cachedRow
- currentIndex = index
- break
- }
- }
- if (!rowToMove || currentIndex === null) {
- console.warn('Row to move not found in cache:', id)
-
- if (before) {
- // Find the 'before' row in cache
- let beforeIndex: number | null = null
- for (const [index, cachedRow] of dataCache.cachedRows.value.entries()) {
- const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
- if (pk && `${pk}` === `${before}`) {
- beforeIndex = index
- break
- }
- }
-
- if (beforeIndex !== null) {
- // The new row position is before an existing cached row
- // We need to shift all rows from beforeIndex onwards down by 1
- const newCachedRows = new Map(dataCache.cachedRows.value.entries())
-
- // Get all rows that need to be shifted (from beforeIndex onwards)
- const rowsToShift = Array.from((dataCache.cachedRows.value as Map<number, Row>).entries())
- .filter(([index]) => index >= beforeIndex!)
- .sort((a, b) => b[0] - a[0])
-
- // Shift each row down by 1
- for (const [index, row] of rowsToShift) {
- const newIndex = index + 1
- row.rowMeta.rowIndex = newIndex
- newCachedRows.delete(index)
- newCachedRows.set(newIndex, row)
- }
-
- // Invalidate affected chunks
- if (rowsToShift.length > 0) {
- const minAffectedChunk = getChunkIndex(beforeIndex!)
- const maxAffectedChunk = getChunkIndex(rowsToShift[rowsToShift.length - 1][0] + 1)
- for (let i = minAffectedChunk; i <= maxAffectedChunk; i++) {
- dataCache.chunkStates.value[i] = undefined
- }
- }
-
- // Apply the changes
- dataCache.cachedRows.value = newCachedRows
-
- callbacks?.syncVisibleData?.()
- } else {
- // The 'before' row is not in cache, skip
- console.log('Before row not in cache, skipping reorder operation')
- }
- } else {
- // No 'before' specified means move to end
- // No changes needed to cache since it's moving to the end
- console.log('Row moved to end, no cache changes needed')
- callbacks?.syncVisibleData?.()
- }
+ // Apply the changes
+ dataCache.cachedRows.value = newCachedRows
- return
- }
+ callbacks?.syncVisibleData?.()
+ } else {
+ // The 'before' row is not in cache, skip
+ console.log('Before row not in cache, skipping reorder operation')
+ }
+ } else {
+ // No 'before' specified means move to end
+ // No changes needed to cache since it's moving to the end
+ console.log('Row moved to end, no cache changes needed')
+ callbacks?.syncVisibleData?.()
+ }
- // Row found in cache - proceed with reordering
+ return
+ }
- // Find the target position based on the 'before' parameter
- let targetIndex: number
+ // Row found in cache - proceed with reordering
- if (before) {
- // Find the row that should come after the moved row
- let beforeIndex: number | null = null
- for (const [index, cachedRow] of dataCache.cachedRows.value.entries()) {
- const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
- if (pk && `${pk}` === `${before}`) {
- beforeIndex = index
- break
- }
- }
+ // Find the target position based on the 'before' parameter
+ let targetIndex: number
- if (beforeIndex !== null) {
- targetIndex = beforeIndex
- } else {
- // If 'before' row not found in cache, move to end
- targetIndex = Math.max(...Array.from(dataCache.cachedRows.value.keys())) + 1
- }
- } else {
- // If no 'before' specified, move to the end
- targetIndex = Math.max(...Array.from(dataCache.cachedRows.value.keys())) + 1
- }
+ if (before) {
+ // Find the row that should come after the moved row
+ let beforeIndex: number | null = null
+ for (const [index, cachedRow] of dataCache.cachedRows.value.entries()) {
+ const pk = extractPkFromRow(cachedRow.row, meta.value?.columns as ColumnType[])
+ if (pk && `${pk}` === `${before}`) {
+ beforeIndex = index
+ break
+ }
+ }
- // If the row is already at the target position, no need to move
- if (currentIndex === targetIndex) {
- return
- }
+ if (beforeIndex !== null) {
+ targetIndex = beforeIndex
+ } else {
+ // If 'before' row not found in cache, move to end
+ targetIndex = Math.max(...Array.from(dataCache.cachedRows.value.keys())) + 1
+ }
+ } else {
+ // If no 'before' specified, move to the end
+ targetIndex = Math.max(...Array.from(dataCache.cachedRows.value.keys())) + 1
+ }
- // Create a new cached rows map with the updated positions
- const newCachedRows = new Map(dataCache.cachedRows.value.entries())
-
- // Remove the row from its current position
- newCachedRows.delete(currentIndex)
-
- // Determine the final target index (similar to updateRecordOrder logic)
- const finalTargetIndex = targetIndex > currentIndex ? targetIndex - 1 : targetIndex
-
- // Shift rows to make space for the moved row
- if (finalTargetIndex < currentIndex) {
- // Moving up: shift rows down
- for (let i = currentIndex - 1; i >= finalTargetIndex; i--) {
- const row = newCachedRows.get(i)
- if (row) {
- const newIndex = i + 1
- row.rowMeta.rowIndex = newIndex
- newCachedRows.delete(i)
- newCachedRows.set(newIndex, row)
- }
- }
- } else {
- // Moving down: shift rows up
- for (let i = currentIndex + 1; i <= finalTargetIndex; i++) {
- const row = newCachedRows.get(i)
- if (row) {
- const newIndex = i - 1
- row.rowMeta.rowIndex = newIndex
- newCachedRows.delete(i)
- newCachedRows.set(newIndex, row)
- }
- }
- }
+ // If the row is already at the target position, no need to move
+ if (currentIndex === targetIndex) {
+ return
+ }
- // Place the moved row at its new position
- rowToMove.rowMeta.rowIndex = finalTargetIndex
- newCachedRows.set(finalTargetIndex, rowToMove)
+ // Create a new cached rows map with the updated positions
+ const newCachedRows = new Map(dataCache.cachedRows.value.entries())
- // Update any changed data in the moved row (if payload contains updates)
- if (payload && typeof payload === 'object') {
- Object.assign(rowToMove.row, payload)
- Object.assign(rowToMove.oldRow, payload)
- }
- rowToMove.rowMeta.changed = false
-
- // Invalidate affected chunks
- const targetChunkIndex = getChunkIndex(finalTargetIndex)
- const sourceChunkIndex = getChunkIndex(currentIndex)
- for (
- let i = Math.min(sourceChunkIndex, targetChunkIndex);
- i <= Math.max(sourceChunkIndex, targetChunkIndex);
- i++
- ) {
- dataCache.chunkStates.value[i] = undefined
- }
+ // Remove the row from its current position
+ newCachedRows.delete(currentIndex)
- // Apply the changes
- dataCache.cachedRows.value = newCachedRows
+ // Determine the final target index (similar to updateRecordOrder logic)
+ const finalTargetIndex = targetIndex > currentIndex ? targetIndex - 1 : targetIndex
- callbacks?.syncVisibleData?.()
- } catch (e) {
- console.error('Failed to reorder cached row on socket event', e)
- }
+ // Shift rows to make space for the moved row
+ if (finalTargetIndex < currentIndex) {
+ // Moving up: shift rows down
+ for (let i = currentIndex - 1; i >= finalTargetIndex; i--) {
+ const row = newCachedRows.get(i)
+ if (row) {
+ const newIndex = i + 1
+ row.rowMeta.rowIndex = newIndex
+ newCachedRows.delete(i)
+ newCachedRows.set(newIndex, row)
}
- },
+ }
+ } else {
+ // Moving down: shift rows up
+ for (let i = currentIndex + 1; i <= finalTargetIndex; i++) {
+ const row = newCachedRows.get(i)
+ if (row) {
+ const newIndex = i - 1
+ row.rowMeta.rowIndex = newIndex
+ newCachedRows.delete(i)
+ newCachedRows.set(newIndex, row)
+ }
+ }
+ }
+
+ // Place the moved row at its new position
+ rowToMove.rowMeta.rowIndex = finalTargetIndex
+ newCachedRows.set(finalTargetIndex, rowToMove)
+
+ // Update any changed data in the moved row (if payload contains updates)
+ if (payload && typeof payload === 'object') {
+ Object.assign(rowToMove.row, payload)
+ Object.assign(rowToMove.oldRow, payload)
+ }
+ rowToMove.rowMeta.changed = false
+
+ // Invalidate affected chunks
+ const targetChunkIndex = getChunkIndex(finalTargetIndex)
+ const sourceChunkIndex = getChunkIndex(currentIndex)
+ for (let i = Math.min(sourceChunkIndex, targetChunkIndex); i <= Math.max(sourceChunkIndex, targetChunkIndex); i++) {
+ dataCache.chunkStates.value[i] = undefined
+ }
+
+ // Apply the changes
+ dataCache.cachedRows.value = newCachedRows
+
+ callbacks?.syncVisibleData?.()
+ } catch (e) {
+ console.error('Failed to reorder cached row on socket event', e)
+ }
+ }
+ }
+
+ watch(
+ meta,
+ (newMeta, oldMeta) => {
+ if (newMeta?.fk_workspace_id && newMeta?.base_id && newMeta?.id) {
+ if (oldMeta?.id && oldMeta.id === newMeta.id) return
+
+ if (activeDataListener.value) {
+ $ncSocket.offMessage(activeDataListener.value)
+ }
+
+ if (activeCommentListener.value) {
+ $ncSocket.offMessage(activeCommentListener.value)
+ }
+
+ activeDataListener.value = $ncSocket.onMessage(
+ `${EventType.DATA_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`,
+ handleDataEvent,
)
activeCommentListener.value = $ncSocket.onMessage(
diff --git a/packages/nc-gui/composables/useKanbanViewStore.ts b/packages/nc-gui/composables/useKanbanViewStore.ts
index 6ef81d96d7..f2280a739e 100644
--- a/packages/nc-gui/composables/useKanbanViewStore.ts
+++ b/packages/nc-gui/composables/useKanbanViewStore.ts
@@ -10,6 +10,7 @@ import type {
TableType,
ViewType,
} from 'nocodb-sdk'
+import { validateRowFilters } from '~/utils/dataUtils'
type GroupingFieldColOptionsType = SelectOptionType & { collapsed: boolean }
@@ -31,11 +32,15 @@ const [useProvideKanbanViewStore, useKanbanViewStore] = useInjectionState(
const { api } = useApi()
- const { base } = storeToRefs(useBase())
+ const baseStore = useBase()
+
+ const { base } = storeToRefs(baseStore)
+
+ const { getBaseType } = baseStore
const { $e, $api, $ncSocket } = useNuxtApp()
- const { sorts, nestedFilters, eventBus, xWhere } = useSmartsheetStoreOrThrow()
+ const { sorts, nestedFilters, eventBus, xWhere, allFilters, validFiltersFromUrlParams } = useSmartsheetStoreOrThrow()
const { sharedView, fetchSharedViewData, fetchSharedViewGroupedData } = useSharedView()
@@ -58,7 +63,9 @@ const [useProvideKanbanViewStore, useKanbanViewStore] = useInjectionState(
const { updateViewMeta } = viewStore
- const { setMeta } = useMetas()
+ const { setMeta, metas } = useMetas()
+
+ const { user } = useGlobal()
// save history of stack changes for undo/redo
const moveHistory = ref<{ op: 'added' | 'removed'; pk: string; stack: string; index: number }[]>([])
@@ -781,6 +788,171 @@ const [useProvideKanbanViewStore, useKanbanViewStore] = useInjectionState(
const activeDataListener = ref<string | null>(null)
+ const handleDataEvent = (data: DataPayload) => {
+ const { id, action, payload, before } = data
+
+ // TODO: @mertmit handle filters and sort for newly added and updated records
+ if (action === 'add') {
+ try {
+ const isValidationFailed = !validateRowFilters(
+ [...allFilters.value, ...validFiltersFromUrlParams.value],
+ payload,
+ meta.value?.columns as ColumnType[],
+ getBaseType(viewMeta.value?.view?.source_id),
+ metas.value,
+ {
+ currentUser: user.value,
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ },
+ )
+
+ if (isValidationFailed) {
+ return
+ }
+
+ const stackKey =
+ typeof payload[groupingField.value] === 'string'
+ ? payload[groupingField.value]?.length
+ ? payload[groupingField.value]
+ : null
+ : null
+
+ const newRow: Row = {
+ row: payload,
+ oldRow: { ...payload },
+ rowMeta: { new: false, ...getEvaluatedRowMetaRowColorInfo(payload) },
+ }
+
+ if (!formattedData.value.has(stackKey)) {
+ formattedData.value.set(stackKey, [])
+ countByStack.value.set(stackKey, 0)
+ }
+
+ const stackRows = formattedData.value.get(stackKey)!
+
+ if (before) {
+ const beforeIndex = stackRows.findIndex((row) => {
+ const pk = extractPkFromRow(row.row, meta?.value?.columns as ColumnType[])
+ return pk && `${pk}` === `${before}`
+ })
+ if (beforeIndex !== -1) {
+ stackRows.splice(beforeIndex, 0, newRow)
+ } else {
+ stackRows.push(newRow)
+ }
+ } else {
+ stackRows.push(newRow)
+ }
+
+ countByStack.value.set(stackKey, (countByStack.value.get(stackKey) || 0) + 1)
+ formattedData.value.set(stackKey, [...stackRows])
+ } catch (e) {
+ console.error('Failed to add row to kanban on socket event', e)
+ }
+ } else if (action === 'update') {
+ try {
+ const isValidationFailed = !validateRowFilters(
+ [...allFilters.value, ...validFiltersFromUrlParams.value],
+ payload,
+ meta.value?.columns as ColumnType[],
+ getBaseType(viewMeta.value?.view?.source_id),
+ metas.value,
+ {
+ currentUser: user.value,
+ timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
+ },
+ )
+
+ if (isValidationFailed) {
+ handleDataEvent({ ...data, action: 'delete' })
+ return
+ }
+
+ let found = null
+ let foundStackKey = null
+ let foundIndex = -1
+
+ // Find the row across all stacks
+ for (const [stackKey, rows] of formattedData.value.entries()) {
+ const index = rows.findIndex((row) => {
+ const pk = extractPkFromRow(row.row, meta?.value?.columns as ColumnType[])
+ return pk && `${pk}` === `${id}`
+ })
+ if (index !== -1) {
+ found = rows[index]
+ foundStackKey = stackKey
+ foundIndex = index
+ break
+ }
+ }
+
+ if (!found) {
+ handleDataEvent({ ...data, action: 'add' })
+ return
+ }
+
+ const oldStackKey = foundStackKey
+ const newStackKey =
+ typeof payload[groupingField.value] === 'string'
+ ? payload[groupingField.value]?.length
+ ? payload[groupingField.value]
+ : null
+ : null
+
+ // Update row data
+ Object.assign(found.row, payload)
+ Object.assign(found.oldRow, payload)
+ Object.assign(found.rowMeta, getEvaluatedRowMetaRowColorInfo(payload))
+ found.rowMeta.changed = false
+
+ // Handle stack change
+ if (oldStackKey !== newStackKey) {
+ // Remove from old stack
+ const oldStackRows = formattedData.value.get(oldStackKey)!
+ oldStackRows.splice(foundIndex, 1)
+ formattedData.value.set(oldStackKey, [...oldStackRows])
+ countByStack.value.set(oldStackKey, (countByStack.value.get(oldStackKey) || 0) - 1)
+
+ // Add to new stack
+ if (!formattedData.value.has(newStackKey)) {
+ formattedData.value.set(newStackKey, [])
+ countByStack.value.set(newStackKey, 0)
+ }
+
+ const newStackRows = formattedData.value.get(newStackKey)!
+ newStackRows.push(found)
+ formattedData.value.set(newStackKey, [...newStackRows])
+ countByStack.value.set(newStackKey, (countByStack.value.get(newStackKey) || 0) + 1)
+ } else {
+ // Row stays in same stack
+ const stackRows = formattedData.value.get(foundStackKey)!
+ stackRows[foundIndex] = found
+ formattedData.value.set(foundStackKey, [...stackRows])
+ }
+ } catch (e) {
+ console.error('Failed to update row in kanban on socket event', e)
+ }
+ } else if (action === 'delete') {
+ try {
+ for (const [stackKey, rows] of formattedData.value.entries()) {
+ const index = rows.findIndex((row) => {
+ const pk = extractPkFromRow(row.row, meta?.value?.columns as ColumnType[])
+ return pk && `${pk}` === `${id}`
+ })
+ if (index !== -1) {
+ const stackRows = formattedData.value.get(stackKey)!
+ stackRows.splice(index, 1)
+ formattedData.value.set(stackKey, [...stackRows])
+ countByStack.value.set(stackKey, Math.max((countByStack.value.get(stackKey) || 0) - 1, 0))
+ break
+ }
+ }
+ } catch (e) {
+ console.error('Failed to delete row from kanban on socket event', e)
+ }
+ }
+ }
+
watch(
meta,
(newMeta, oldMeta) => {
@@ -795,134 +967,7 @@ const [useProvideKanbanViewStore, useKanbanViewStore] = useInjectionState(
// Set up data event listener
activeDataListener.value = $ncSocket.onMessage(
`${EventType.DATA_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`,
- (data: DataPayload) => {
- const { id, action, payload, before } = data
-
- // TODO: @mertmit handle filters and sort for newly added and updated records
- if (action === 'add') {
- try {
- const stackKey =
- typeof payload[groupingField.value] === 'string'
- ? payload[groupingField.value]?.length
- ? payload[groupingField.value]
- : null
- : null
-
- const newRow: Row = {
- row: payload,
- oldRow: { ...payload },
- rowMeta: { new: false, ...getEvaluatedRowMetaRowColorInfo(payload) },
- }
-
- if (!formattedData.value.has(stackKey)) {
- formattedData.value.set(stackKey, [])
- countByStack.value.set(stackKey, 0)
- }
-
- const stackRows = formattedData.value.get(stackKey)!
-
- if (before) {
- const beforeIndex = stackRows.findIndex((row) => {
- const pk = extractPkFromRow(row.row, meta?.value?.columns as ColumnType[])
- return pk && `${pk}` === `${before}`
- })
- if (beforeIndex !== -1) {
- stackRows.splice(beforeIndex, 0, newRow)
- } else {
- stackRows.push(newRow)
- }
- } else {
- stackRows.push(newRow)
- }
-
- countByStack.value.set(stackKey, (countByStack.value.get(stackKey) || 0) + 1)
- formattedData.value.set(stackKey, [...stackRows])
- } catch (e) {
- console.error('Failed to add row to kanban on socket event', e)
- }
- } else if (action === 'update') {
- try {
- let found = null
- let foundStackKey = null
- let foundIndex = -1
-
- // Find the row across all stacks
- for (const [stackKey, rows] of formattedData.value.entries()) {
- const index = rows.findIndex((row) => {
- const pk = extractPkFromRow(row.row, meta?.value?.columns as ColumnType[])
- return pk && `${pk}` === `${id}`
- })
- if (index !== -1) {
- found = rows[index]
- foundStackKey = stackKey
- foundIndex = index
- break
- }
- }
-
- if (!found) return
-
- const oldStackKey = foundStackKey
- const newStackKey =
- typeof payload[groupingField.value] === 'string'
- ? payload[groupingField.value]?.length
- ? payload[groupingField.value]
- : null
- : null
-
- // Update row data
- Object.assign(found.row, payload)
- Object.assign(found.oldRow, payload)
- Object.assign(found.rowMeta, getEvaluatedRowMetaRowColorInfo(payload))
- found.rowMeta.changed = false
-
- // Handle stack change
- if (oldStackKey !== newStackKey) {
- // Remove from old stack
- const oldStackRows = formattedData.value.get(oldStackKey)!
- oldStackRows.splice(foundIndex, 1)
- formattedData.value.set(oldStackKey, [...oldStackRows])
- countByStack.value.set(oldStackKey, (countByStack.value.get(oldStackKey) || 0) - 1)
-
- // Add to new stack
- if (!formattedData.value.has(newStackKey)) {
- formattedData.value.set(newStackKey, [])
- countByStack.value.set(newStackKey, 0)
- }
-
- const newStackRows = formattedData.value.get(newStackKey)!
- newStackRows.push(found)
- formattedData.value.set(newStackKey, [...newStackRows])
- countByStack.value.set(newStackKey, (countByStack.value.get(newStackKey) || 0) + 1)
- } else {
- // Row stays in same stack
- const stackRows = formattedData.value.get(foundStackKey)!
- stackRows[foundIndex] = found
- formattedData.value.set(foundStackKey, [...stackRows])
- }
- } catch (e) {
- console.error('Failed to update row in kanban on socket event', e)
- }
- } else if (action === 'delete') {
- try {
- for (const [stackKey, rows] of formattedData.value.entries()) {
- const index = rows.findIndex((row) => {
- const pk = extractPkFromRow(row.row, meta?.value?.columns as ColumnType[])
- return pk && `${pk}` === `${id}`
- })
- if (index !== -1) {
- const stackRows = formattedData.value.get(stackKey)!
- stackRows.splice(index, 1)
- formattedData.value.set(stackKey, [...stackRows])
- countByStack.value.set(stackKey, Math.max((countByStack.value.get(stackKey) || 0) - 1, 0))
- break
- }
- }
- } catch (e) {
- console.error('Failed to delete row from kanban on socket event', e)
- }
- }
- },
+ handleDataEvent,
)
}
},