Skip to content

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

fix: better key manager

DarkPhoenix2704fdbfdbba642d
diff --git a/packages/nc-gui/composables/useInfiniteGroups.ts b/packages/nc-gui/composables/useInfiniteGroups.ts
index 47fadbf37a..4e56816a9b 100644
--- a/packages/nc-gui/composables/useInfiniteGroups.ts
+++ b/packages/nc-gui/composables/useInfiniteGroups.ts
@@ -8,9 +8,10 @@ import {
UITypes,
type ViewType,
} from 'nocodb-sdk'
-import { useStorage } from '@vueuse/core'
import { createGroupUniqueIdentifier, generateGroupPath } from '../components/smartsheet/grid/canvas/utils/groupby'
import type { CanvasGroup } from '#imports'
+import { groupKeysManager } from '#imports'
+
const GROUP_CHUNK_SIZE = 100
const MAX_GROUP_CACHE_SIZE = 100
@@ -42,15 +43,8 @@ export const useInfiniteGroups = (
const isPublic = inject(IsPublicInj, ref(false))
const sharedViewPassword = inject(SharedViewPasswordInj, ref(null))
- const activeGroupKeys = useStorage<Record<string, Array<string>>>('active-group-keys', {})
-
const routeQuery = computed(() => router.currentRoute.value.query as Record<string, string>)
- const currentViewGroupKeys = computed(() => {
- if (!view.value?.id) return []
- return activeGroupKeys.value[view.value.id] || []
- })
-
const columnsById = computed(() => {
if (!meta.value?.columns?.length) return {}
return meta.value?.columns.reduce((acc, column) => {
@@ -257,7 +251,7 @@ export const useInfiniteGroups = (
const nestedKey = group.nestedIn.map((n) => `${n.key}-${n.column_name}`).join('_') || 'default'
- group.isExpanded = currentViewGroupKeys.value.includes(nestedKey) || isExpanded
+ group.isExpanded = groupKeysManager.hasKey(view.value.id!, nestedKey) || isExpanded
// Create useInfiniteData for leaf groups
if (level === groupByColumns.value.length - 1) {
@@ -647,24 +641,8 @@ export const useInfiniteGroups = (
group.isExpanded = !group.isExpanded
const nestedKey = group.nestedIn.map((n) => `${n.key}-${n.column_name}`).join('_') || 'default'
- if (group.isExpanded) {
- if (!view.value?.id) return
- if (!activeGroupKeys.value[view.value.id]) {
- activeGroupKeys.value[view.value.id] = []
- }
- if (!activeGroupKeys.value[view.value.id].includes(nestedKey)) {
- activeGroupKeys.value[view.value.id].push(nestedKey)
- }
- } else {
- if (!view.value?.id) return
- const viewKeys = activeGroupKeys.value[view.value.id]
- if (viewKeys) {
- const index = viewKeys.indexOf(nestedKey)
- if (index !== -1) {
- viewKeys.splice(index, 1)
- }
- }
- }
+ if (!view.value?.id) return
+ groupKeysManager.toggleKey(view.value.id, nestedKey, group.isExpanded)
}
const toggleExpandAll = async (path: number[], expand: boolean) => {
@@ -678,7 +656,6 @@ export const useInfiniteGroups = (
} else {
let currentGroups = cachedGroups.value
- // Navigate to the parent level (all path elements except the last one)
for (let i = 0; i < path.length - 1; i++) {
const group = currentGroups.get(path[i])
if (!group || !group.groups) return
@@ -688,29 +665,12 @@ export const useInfiniteGroups = (
targetGroups = currentGroups
}
+ if (!view.value?.id) return
+
targetGroups.forEach((group) => {
const nestedKey = group.nestedIn.map((n) => `${n.key}-${n.column_name}`).join('_') || 'default'
-
group.isExpanded = expand
-
- if (!view.value?.id) return
-
- if (expand) {
- if (!activeGroupKeys.value[view.value.id]) {
- activeGroupKeys.value[view.value.id] = []
- }
- if (!activeGroupKeys.value[view.value.id].includes(nestedKey)) {
- activeGroupKeys.value[view.value.id].push(nestedKey)
- }
- } else {
- const viewKeys = activeGroupKeys.value[view.value.id]
- if (viewKeys) {
- const keyIndex = viewKeys.indexOf(nestedKey)
- if (keyIndex !== -1) {
- viewKeys.splice(keyIndex, 1)
- }
- }
- }
+ groupKeysManager.toggleKey(view.value.id!, nestedKey, expand)
})
callbacks?.syncVisibleData()
diff --git a/packages/nc-gui/helpers/groupKeyManager.ts b/packages/nc-gui/helpers/groupKeyManager.ts
new file mode 100644
index 0000000000..9d328382bb
--- /dev/null
+++ b/packages/nc-gui/helpers/groupKeyManager.ts
@@ -0,0 +1,100 @@
+import { useStorage } from '@vueuse/core'
+import type { GroupKeysStorage } from '#imports'
+
+export class GroupKeysManager {
+ private storage: Ref<GroupKeysStorage>
+ private readonly TTL = 30 * 24 * 60 * 60 * 1000 // 30 days
+
+ constructor() {
+ this.storage = useStorage<GroupKeysStorage>('active-group-keys', {})
+ this.cleanExpired()
+ }
+
+ private cleanExpired(): void {
+ const now = Date.now()
+ const data = this.storage.value
+
+ Object.keys(data).forEach((viewId) => {
+ if (now - data[viewId].lastAccessed > this.TTL) {
+ delete data[viewId]
+ }
+ })
+ }
+
+ private touch(viewId: string): void {
+ if (this.storage.value[viewId]) {
+ this.storage.value[viewId].lastAccessed = Date.now()
+ }
+ }
+
+ private ensureView(viewId: string): void {
+ if (!this.storage.value[viewId]) {
+ this.storage.value[viewId] = {
+ keys: [],
+ lastAccessed: Date.now(),
+ }
+ }
+ }
+
+ hasKey(viewId: string, key: string): boolean {
+ if (!viewId) return false
+
+ const viewData = this.storage.value[viewId]
+ if (!viewData || !Array.isArray(viewData.keys)) return false
+
+ this.touch(viewId)
+ return viewData.keys.includes(key)
+ }
+
+ getKeys(viewId: string): Array<string> {
+ if (!viewId) return []
+
+ const viewData = this.storage.value[viewId]
+ if (!viewData || !Array.isArray(viewData.keys)) return []
+
+ this.touch(viewId)
+ return viewData.keys
+ }
+
+ addKey(viewId: string, key: string): void {
+ if (!viewId) return
+
+ this.ensureView(viewId)
+ const keys = this.storage.value[viewId].keys
+
+ if (!keys.includes(key)) {
+ keys.push(key)
+ }
+
+ this.touch(viewId)
+ }
+
+ removeKey(viewId: string, key: string): void {
+ if (!viewId) return
+
+ const viewData = this.storage.value[viewId]
+ if (!viewData) return
+
+ const index = viewData.keys.indexOf(key)
+ if (index !== -1) {
+ viewData.keys.splice(index, 1)
+ }
+
+ this.touch(viewId)
+ }
+
+ toggleKey(viewId: string, key: string, shouldAdd: boolean): void {
+ if (shouldAdd) {
+ this.addKey(viewId, key)
+ } else {
+ this.removeKey(viewId, key)
+ }
+ }
+
+ clearView(viewId: string): void {
+ if (!viewId) return
+ delete this.storage.value[viewId]
+ }
+}
+
+export const groupKeysManager = new GroupKeysManager()
diff --git a/packages/nc-gui/lib/types.ts b/packages/nc-gui/lib/types.ts
index b4e3603745..4d01ef58c7 100644
--- a/packages/nc-gui/lib/types.ts
+++ b/packages/nc-gui/lib/types.ts
@@ -898,6 +898,13 @@ interface AttachmentCellDropOverType {
path: Array<number>
}
+interface GroupKeysStorage {
+ [viewId: string]: {
+ keys: Array<string>
+ lastAccessed: number // timestamp
+ }
+}
+
export type {
User,
ProjectMetaInfo,
@@ -966,4 +973,5 @@ export type {
NcClipboardDataType,
NcClipboardDataItemType,
AttachmentCellDropOverType,
+ GroupKeysStorage,
}