Skip to content

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

feat: sync user obj to iframes

DarkPhoenix27041ca036f6ba04
diff --git a/packages/nc-gui/app.vue b/packages/nc-gui/app.vue
index e70155963b..6529b05642 100644
--- a/packages/nc-gui/app.vue
+++ b/packages/nc-gui/app.vue
@@ -27,6 +27,8 @@ const { commandPalette, cmdData, cmdPlaceholder, activeScope, loadTemporaryScope
const { cmdK, cmdL, cmdJ, setActiveCmdView } = useCommand()
+useUserSync()
+
useRealtime()
applyNonSelectable()
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasTable.ts b/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasTable.ts
index 009390b6c5..65de665f6f 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasTable.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/composables/useCanvasTable.ts
@@ -210,6 +210,7 @@ export function useCanvasTable({
const { isMobileMode } = useGlobal()
const { $api } = useNuxtApp()
const { t } = useI18n()
+ const { currentUser } = useUserSync()
const { gridViewCols, metaColumnById, updateGridViewColumn } = useViewColumnsOrThrow()
const {
eventBus,
@@ -272,6 +273,7 @@ export function useCanvasTable({
triggerRefreshCanvas,
getDataCache,
scriptEventBus,
+ currentUser,
)
const isGroupBy = computed(() => !!groupByColumns.value?.length)
diff --git a/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts b/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts
index 5122227b1c..48e8e13fbc 100644
--- a/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts
+++ b/packages/nc-gui/components/smartsheet/grid/canvas/loaders/ActionManager.ts
@@ -1,4 +1,5 @@
import type { Api, ButtonType, TableType } from 'nocodb-sdk'
+import type { UserObject } from 'packages/nc-gui/composables/useUserSync'
interface ActionState {
status: 'loading' | 'success' | 'error' | 'queued'
@@ -31,6 +32,8 @@ export class ActionManager {
isRowSortRequiredRows: ComputedRef<Array<Row>>
}
+ private readonly userSync: Ref<UserObject>
+
private eventBus?: any
// Consolidated state maps
@@ -56,6 +59,7 @@ export class ActionManager {
isRowSortRequiredRows: ComputedRef<Array<Row>>
},
eventBus?: any,
+ userSync?: any,
) {
this.api = api
this.loadAutomation = loadAutomation
@@ -64,6 +68,7 @@ export class ActionManager {
this.triggerRefreshCanvas = triggerRefreshCanvas
this.getDataCache = getDataCache
this.eventBus = eventBus
+ this.userSync = userSync
this.setupEventListeners()
}
diff --git a/packages/nc-gui/composables/useUserSync.ts b/packages/nc-gui/composables/useUserSync.ts
new file mode 100644
index 0000000000..6369605478
--- /dev/null
+++ b/packages/nc-gui/composables/useUserSync.ts
@@ -0,0 +1,76 @@
+import { onMounted, onUnmounted, ref, watch } from 'vue'
+
+export interface UserObject {
+ id?: string
+ email?: string
+ display_name?: string
+}
+
+export const useUserSync = createSharedComposable(() => {
+ const isIFrame = ncIsIframe()
+ const { user } = useGlobal()
+ const currentUser = ref<UserObject | null>(null)
+ let syncInterval: NodeJS.Timeout | null = null
+
+ const sendToAllIframes = () => {
+ const userObj = {
+ id: user.value?.id,
+ email: user.value?.email,
+ display_name: user.value?.display_name,
+ }
+
+ document.querySelectorAll('iframe').forEach((iframe) => {
+ try {
+ iframe.contentWindow?.postMessage(
+ {
+ type: '___NC_USER_SYNC',
+ user: userObj,
+ },
+ '*',
+ )
+ } catch (error) {
+ console.warn('Failed to send user data to iframe:', error)
+ }
+ })
+ }
+
+ // IFRAME MODE: Listen for user data
+ if (isIFrame) {
+ window.addEventListener('message', (event) => {
+ if (event.data.type === '___NC_USER_SYNC') {
+ currentUser.value = event.data.user
+ }
+ })
+ } else {
+ // PARENT MODE: Set currentUser to user
+ currentUser.value = user.value
+ watch(
+ () => user.value,
+ (newUser) => {
+ currentUser.value = newUser
+ },
+ { deep: true },
+ )
+ }
+
+ // PARENT MODE: Start syncing
+ onMounted(() => {
+ if (!isIFrame) {
+ sendToAllIframes() // Send immediately
+ syncInterval = setInterval(sendToAllIframes, 5000) // Then every 5 seconds
+
+ // Watch for user changes to send to iframes
+ watch(() => user.value, sendToAllIframes, { deep: true })
+ }
+ })
+
+ onUnmounted(() => {
+ if (syncInterval) {
+ clearInterval(syncInterval)
+ }
+ })
+
+ return {
+ currentUser: readonly(currentUser),
+ }
+})