Skip to content

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

feat: expanded form realtime

mertmitb06de96e3242
diff --git a/packages/nc-gui/components/smartsheet/expanded-form/presentors/Fields/ColumnList.vue b/packages/nc-gui/components/smartsheet/expanded-form/presentors/Fields/ColumnList.vue
index 993bf4657d..3f31c9b60f 100644
--- a/packages/nc-gui/components/smartsheet/expanded-form/presentors/Fields/ColumnList.vue
+++ b/packages/nc-gui/components/smartsheet/expanded-form/presentors/Fields/ColumnList.vue
@@ -9,7 +9,7 @@ const props = defineProps<{
isHiddenCol?: boolean
}>()
-const { changedColumns, isNew, loadRow: _loadRow, row: _row } = useExpandedFormStoreOrThrow()
+const { changedColumns, localOnlyChanges, isNew, loadRow: _loadRow, row: _row } = useExpandedFormStoreOrThrow()
const { isSqlView } = useSmartsheetStoreOrThrow()
@@ -22,6 +22,14 @@ const readOnly = computed(() => !isUIAllowed('dataEdit') || isPublic.value || is
const showCol = (col: ColumnType) => {
return props.showColCallback?.(col) || !isVirtualCol(col) || !isNew.value || isLinksOrLTAR(col)
}
+
+const revertLocalOnlyChanges = (col: string) => {
+ if (localOnlyChanges.value[col]) {
+ _row.value.row[col] = localOnlyChanges.value[col]
+ changedColumns.value.delete(col)
+ delete localOnlyChanges.value[col]
+ }
+}
</script>
<template>
@@ -37,14 +45,14 @@ const showCol = (col: ColumnType) => {
<div
class="flex items-start nc-expanded-cell min-h-[32px]"
:class="{
- 'flex-row sm:(gap-x-2) <lg:(flex-col w-full)': !props.forceVerticalMode,
+ 'flex-row <lg:(flex-col w-full)': !props.forceVerticalMode,
'flex-col w-full': props.forceVerticalMode,
}"
>
<div
class="flex-none flex items-center rounded-lg overflow-hidden"
:class="{
- 'w-45 <lg:(w-full px-0 mb-2) h-[32px] xs:(h-auto)': !props.forceVerticalMode,
+ 'w-45 <lg:(w-full px-0 mb-2) h-[32px] xs:(h-auto) sm:(mx-2)': !props.forceVerticalMode,
'w-full px-0 mb-2 h-auto': props.forceVerticalMode,
}"
>
@@ -128,6 +136,13 @@ const showCol = (col: ColumnType) => {
</template>
</PermissionsTooltip>
</NcTooltip>
+ <div
+ v-if="col.title && localOnlyChanges[col.title]"
+ class="flex h-full items-center justify-center cursor-pointer relative"
+ @click="revertLocalOnlyChanges(col.title)"
+ >
+ <GeneralIcon class="absolute right-0 top-0 text-gray-500 hover:text-gray-700 my-auto" icon="reload" />
+ </div>
</div>
</div>
</template>
diff --git a/packages/nc-gui/composables/useExpandedFormStore.ts b/packages/nc-gui/composables/useExpandedFormStore.ts
index fb94d08852..1585acc6a5 100644
--- a/packages/nc-gui/composables/useExpandedFormStore.ts
+++ b/packages/nc-gui/composables/useExpandedFormStore.ts
@@ -1,5 +1,6 @@
-import type { AuditType, ColumnType, MetaType, PlanLimitExceededDetailsType, TableType } from 'nocodb-sdk'
+import type { AuditType, ColumnType, DataPayload, MetaType, PlanLimitExceededDetailsType, TableType } from 'nocodb-sdk'
import {
+ EventType,
PermissionEntity,
PermissionKey,
PlanLimitTypes,
@@ -13,7 +14,7 @@ import dayjs from 'dayjs'
const [useProvideExpandedFormStore, useExpandedFormStore] = useInjectionState(
(meta: Ref<TableType>, _row: Ref<Row>, maintainDefaultViewOrder: Ref<boolean>, useMetaFields: boolean) => {
- const { $e, $state, $api } = useNuxtApp()
+ const { $e, $state, $api, $ncSocket } = useNuxtApp()
const { t } = useI18n()
@@ -37,6 +38,8 @@ const [useProvideExpandedFormStore, useExpandedFormStore] = useInjectionState(
const changedColumns = ref(new Set<string>())
+ const localOnlyChanges = ref<Record<string, any>>({})
+
const basesStore = useBases()
const { basesUser } = storeToRefs(basesStore)
@@ -764,6 +767,66 @@ const [useProvideExpandedFormStore, useExpandedFormStore] = useInjectionState(
}
})
+ const activeChannel = 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
+
+ if (activeChannel.value) {
+ $ncSocket.offMessage(activeChannel.value)
+ }
+
+ activeChannel.value = `${EventType.DATA_EVENT}:${newMeta.fk_workspace_id}:${newMeta.base_id}:${newMeta.id}`
+
+ $ncSocket.subscribe(activeChannel.value)
+
+ $ncSocket.onMessage(activeChannel.value, (data: DataPayload) => {
+ const { id, action, payload, before } = data
+
+ const activePk = extractPkFromRow(row.value.row, meta.value?.columns as ColumnType[])
+
+ if (`${id}` === activePk) {
+ if (action === 'update') {
+ try {
+ if (payload) {
+ // Merge payload with local row, but preserve locally changed columns
+ const mergedRow = { ...row.value.row, ...payload }
+ for (const col of changedColumns.value) {
+ if (row.value.row.hasOwnProperty(col)) {
+ mergedRow[col] = row.value.row[col]
+ if (row.value.row[col] !== payload[col]) {
+ // localOnlyChanges.value[col] = payload[col]
+ }
+ }
+ }
+ Object.assign(row.value, {
+ row: mergedRow,
+ oldRow: { ...mergedRow },
+ })
+ // Do NOT clear changedColumns here, as we want to preserve local changes
+ } else {
+ console.warn('No payload provided for update action')
+ }
+ } catch (e) {
+ console.error('Failed to update cached row on socket event', e)
+ }
+ } else if (action === 'delete') {
+ try {
+ //
+ } catch (e) {
+ console.error('Failed to delete cached row on socket event', e)
+ }
+ }
+ }
+ })
+ }
+ },
+ { immediate: true },
+ )
+
return {
...rowStore,
loadComments,
@@ -788,6 +851,7 @@ const [useProvideExpandedFormStore, useExpandedFormStore] = useInjectionState(
displayValue,
save,
changedColumns,
+ localOnlyChanges,
loadRow,
primaryKey,
saveRowAndStay,