Improve tabs updates (#6956)

Signed-off-by: Kristina Fefelova <kristin.fefelova@gmail.com>
This commit is contained in:
Kristina 2024-10-16 16:03:02 +04:00 committed by GitHub
parent 343a80f642
commit e6d9b8f606
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 19 deletions

View File

@ -221,6 +221,7 @@
selectTab(tabToReplace._id) selectTab(tabToReplace._id)
prevTabIdStore.set(tabToReplace._id) prevTabIdStore.set(tabToReplace._id)
} else { } else {
console.log('Creating new tab on init')
const _id = await client.createDoc(workbench.class.WorkbenchTab, core.space.Workspace, { const _id = await client.createDoc(workbench.class.WorkbenchTab, core.space.Workspace, {
attachedTo: account._id, attachedTo: account._id,
location: url, location: url,

View File

@ -23,7 +23,7 @@
languageStore, languageStore,
locationToUrl locationToUrl
} from '@hcengineering/ui' } from '@hcengineering/ui'
import { ComponentExtensions, getClient } from '@hcengineering/presentation' import { ComponentExtensions, getClient, reduceCalls } from '@hcengineering/presentation'
import { Asset, getResource, translate } from '@hcengineering/platform' import { Asset, getResource, translate } from '@hcengineering/platform'
import { WorkbenchTab } from '@hcengineering/workbench' import { WorkbenchTab } from '@hcengineering/workbench'
import view from '@hcengineering/view' import view from '@hcengineering/view'
@ -40,8 +40,18 @@
let icon: Asset | AnySvelteComponent | undefined let icon: Asset | AnySvelteComponent | undefined
let iconProps: Record<string, any> | undefined let iconProps: Record<string, any> | undefined
let lastLocUrl: string | undefined = undefined
let lastLang: string | undefined = undefined
async function updateTabData (tab: WorkbenchTab, lang: string): Promise<void> { async function updateTabData (tab: WorkbenchTab, lang: string): Promise<void> {
const tabLoc = $tabIdStore === tab._id ? getCurrentLocation() : getTabLocation(tab) const tabLoc = $tabIdStore === tab._id ? getCurrentLocation() : getTabLocation(tab)
const url = locationToUrl(tabLoc)
if (lastLocUrl === url && lastLang === lang) return
lastLocUrl = url
lastLang = lang
const data = await getTabDataByLocation(tabLoc) const data = await getTabDataByLocation(tabLoc)
name = data.name ?? (await translate(data.label, {}, lang)) name = data.name ?? (await translate(data.label, {}, lang))
@ -53,11 +63,15 @@
iconProps = data.iconProps iconProps = data.iconProps
if (tab.name !== name && tab.location === locationToUrl(tabLoc)) { if (tab.name !== name && tab.location === locationToUrl(tabLoc)) {
await client.update(tab, { name }) await client.diffUpdate(tab, { name })
} }
} }
$: void updateTabData(tab, $languageStore) const update = reduceCalls(async function (tab: WorkbenchTab, lang: string): Promise<void> {
await updateTabData(tab, lang)
})
$: void update(tab, $languageStore)
function handleClickTab (): void { function handleClickTab (): void {
selectTab(tab._id) selectTab(tab._id)

View File

@ -19,8 +19,8 @@ import core, {
type Doc, type Doc,
getCurrentAccount, getCurrentAccount,
type Ref, type Ref,
RateLimiter, generateId,
generateId reduceCalls
} from '@hcengineering/core' } from '@hcengineering/core'
import { type Application, workbenchId, type WorkbenchTab } from '@hcengineering/workbench' import { type Application, workbenchId, type WorkbenchTab } from '@hcengineering/workbench'
import { import {
@ -56,20 +56,13 @@ tabIdStore.subscribe((value) => {
prevTabId = value prevTabId = value
}) })
// Use rate limiter to control tab creation, preventing multiple tabs during fast location changing
const limiter = new RateLimiter(1)
workspaceStore.subscribe((workspace) => { workspaceStore.subscribe((workspace) => {
tabIdStore.set(getTabFromLocalStorage(workspace ?? '')) tabIdStore.set(getTabFromLocalStorage(workspace ?? ''))
}) })
locationStore.subscribe((l: Location) => {
void limiter.add(syncTabLoc)
})
tabIdStore.subscribe(saveTabToLocalStorage) tabIdStore.subscribe(saveTabToLocalStorage)
async function syncTabLoc (): Promise<void> { const syncTabLoc = reduceCalls(async (): Promise<void> => {
const loc = getCurrentLocation() const loc = getCurrentLocation()
const workspace = get(workspaceStore) const workspace = get(workspaceStore)
if (workspace == null || workspace === '') return if (workspace == null || workspace === '') return
@ -102,7 +95,7 @@ async function syncTabLoc (): Promise<void> {
} }
const me = getCurrentAccount() const me = getCurrentAccount()
const tab: WorkbenchTab = { const newTab: WorkbenchTab = {
_id: generateId(), _id: generateId(),
_class: workbench.class.WorkbenchTab, _class: workbench.class.WorkbenchTab,
space: core.space.Workspace, space: core.space.Workspace,
@ -113,9 +106,10 @@ async function syncTabLoc (): Promise<void> {
modifiedOn: Date.now(), modifiedOn: Date.now(),
modifiedBy: me._id modifiedBy: me._id
} }
await getClient().createDoc(workbench.class.WorkbenchTab, core.space.Workspace, tab, tab._id) console.log('Creating new tab when pinned location changed', { newLocation: url, pinnedLocation: tab.location })
tabsStore.update((tabs) => [...tabs, tab]) await getClient().createDoc(workbench.class.WorkbenchTab, core.space.Workspace, newTab, newTab._id)
selectTab(tab._id) tabsStore.update((tabs) => [...tabs, newTab])
selectTab(newTab._id)
} else { } else {
// TODO: Fix this // TODO: Fix this
// if ( // if (
@ -127,9 +121,14 @@ async function syncTabLoc (): Promise<void> {
// return // return
// } // }
await getClient().update(tab, { location: url, name }) await getClient().diffUpdate(tab, { location: url, name })
} }
} })
locationStore.subscribe((l: Location) => {
void syncTabLoc()
})
export function syncWorkbenchTab (): void { export function syncWorkbenchTab (): void {
const workspace = get(workspaceStore) const workspace = get(workspaceStore)
tabIdStore.set(getTabFromLocalStorage(workspace ?? '')) tabIdStore.set(getTabFromLocalStorage(workspace ?? ''))