From 0aeb627cf99610f433abb934a0d5dad036ec682b Mon Sep 17 00:00:00 2001 From: Alexey Zinoviev Date: Tue, 11 Feb 2025 05:55:19 +0400 Subject: [PATCH] uberf-9435: restore workbench tab preferences (#7965) Signed-off-by: Alexey Zinoviev --- models/preference/src/index.ts | 8 +++---- models/workbench/src/index.ts | 3 ++- models/workbench/src/migration.ts | 23 +++++++++++++++++-- plugins/preference/src/index.ts | 2 +- .../src/components/Workbench.svelte | 4 ++-- plugins/workbench-resources/src/workbench.ts | 7 ++---- plugins/workbench/src/index.ts | 3 ++- 7 files changed, 34 insertions(+), 16 deletions(-) diff --git a/models/preference/src/index.ts b/models/preference/src/index.ts index 0e2ae21cae..9ce15d9bfc 100644 --- a/models/preference/src/index.ts +++ b/models/preference/src/index.ts @@ -13,8 +13,8 @@ // limitations under the License. // -import { type Doc, type Ref, type Space } from '@hcengineering/core' -import { type Builder, Model, Prop, TypeRef } from '@hcengineering/model' +import { type Ref, type Space } from '@hcengineering/core' +import { type Builder, Model, Prop, TypeRef, TypeString } from '@hcengineering/model' import core, { TDoc } from '@hcengineering/model-core' import preference, { DOMAIN_PREFERENCE, type Preference, type SpacePreference } from '@hcengineering/preference' @@ -24,8 +24,8 @@ export { preference as default } @Model(preference.class.Preference, core.class.Doc, DOMAIN_PREFERENCE) export class TPreference extends TDoc implements Preference { - @Prop(TypeRef(core.class.Doc), core.string.AttachedTo) - attachedTo!: Ref + @Prop(TypeString(), core.string.AttachedTo) + attachedTo!: string } @Model(preference.class.SpacePreference, preference.class.Preference) diff --git a/models/workbench/src/index.ts b/models/workbench/src/index.ts index 96dae1a6f1..325f8272d5 100644 --- a/models/workbench/src/index.ts +++ b/models/workbench/src/index.ts @@ -13,7 +13,7 @@ // limitations under the License. // -import { type Class, DOMAIN_MODEL, type Ref, type Space, type AccountRole } from '@hcengineering/core' +import { type Class, DOMAIN_MODEL, type Ref, type Space, type AccountRole, type PersonId } from '@hcengineering/core' import { type Builder, Mixin, Model, Prop, TypeRef, UX } from '@hcengineering/model' import preference, { TPreference } from '@hcengineering/model-preference' import { createAction } from '@hcengineering/model-view' @@ -106,6 +106,7 @@ export class TTxSidebarEvent extends TTx implements TxSidebarEvent { @Model(workbench.class.WorkbenchTab, preference.class.Preference) @UX(workbench.string.Tab) export class TWorkbenchTab extends TPreference implements WorkbenchTab { + declare attachedTo: PersonId location!: string name?: string isPinned!: boolean diff --git a/models/workbench/src/migration.ts b/models/workbench/src/migration.ts index f938e35b9f..dc4116f42f 100644 --- a/models/workbench/src/migration.ts +++ b/models/workbench/src/migration.ts @@ -19,8 +19,9 @@ import { tryMigrate } from '@hcengineering/model' import { DOMAIN_PREFERENCE } from '@hcengineering/preference' -import workbench from '@hcengineering/workbench' -import core, { DOMAIN_TX } from '@hcengineering/core' +import workbench, { type WorkbenchTab } from '@hcengineering/workbench' +import core, { DOMAIN_TX, MeasureMetricsContext } from '@hcengineering/core' +import { getSocialIdByOldAccount } from '@hcengineering/model-core' import { workbenchId } from '.' @@ -28,6 +29,20 @@ async function removeTabs (client: MigrationClient): Promise { await client.deleteMany(DOMAIN_PREFERENCE, { _class: workbench.class.WorkbenchTab }) } +async function migrateTabsToSocialIds (client: MigrationClient): Promise { + const ctx = new MeasureMetricsContext('workbench migrateTabsToSocialIds', {}) + ctx.info('migrating workbench tabs to social ids...') + const socialIdByAccount = await getSocialIdByOldAccount(client) + const tabs = await client.find(DOMAIN_PREFERENCE, { _class: workbench.class.WorkbenchTab }) + for (const tab of tabs) { + const newAttachedTo = socialIdByAccount[tab.attachedTo] + if (newAttachedTo != null && newAttachedTo !== tab.attachedTo) { + await client.update(DOMAIN_PREFERENCE, { _id: tab._id }, { attachedTo: newAttachedTo }) + } + } + ctx.info('migrating workbench tabs to social ids completed...') +} + export const workbenchOperation: MigrateOperation = { async migrate (client: MigrationClient): Promise { await tryMigrate(client, workbenchId, [ @@ -43,6 +58,10 @@ export const workbenchOperation: MigrateOperation = { _class: core.class.TxUpdateDoc }) } + }, + { + state: 'tabs-accounts-to-social-ids', + func: migrateTabsToSocialIds } ]) }, diff --git a/plugins/preference/src/index.ts b/plugins/preference/src/index.ts index 8e0f77befb..c43c45eba2 100644 --- a/plugins/preference/src/index.ts +++ b/plugins/preference/src/index.ts @@ -21,7 +21,7 @@ import { plugin } from '@hcengineering/platform' * @public */ export interface Preference extends Doc { - attachedTo: Ref + attachedTo: string } /** diff --git a/plugins/workbench-resources/src/components/Workbench.svelte b/plugins/workbench-resources/src/components/Workbench.svelte index 56cea9807c..ad8896d935 100644 --- a/plugins/workbench-resources/src/components/Workbench.svelte +++ b/plugins/workbench-resources/src/components/Workbench.svelte @@ -190,7 +190,7 @@ const query = createQuery() $: query.query( workbench.class.WorkbenchTab, - { attachedTo: me }, + { attachedTo: { $in: account.socialIds } }, (res) => { tabs = res tabsStore.set(tabs) @@ -240,7 +240,7 @@ } else { console.log('Creating new tab on init') const _id = await client.createDoc(workbench.class.WorkbenchTab, core.space.Workspace, { - attachedTo: me, + attachedTo: account.primarySocialId, location: url, isPinned: false }) diff --git a/plugins/workbench-resources/src/workbench.ts b/plugins/workbench-resources/src/workbench.ts index 4937429664..5e71182750 100644 --- a/plugins/workbench-resources/src/workbench.ts +++ b/plugins/workbench-resources/src/workbench.ts @@ -42,7 +42,6 @@ import notification, { notificationId } from '@hcengineering/notification' import { locationWorkspaceStore } from './utils' import workbench from './plugin' -import { getCurrentEmployee } from '@hcengineering/contact' export const tabIdStore = writable | undefined>() export const prevTabIdStore = writable | undefined>() @@ -98,14 +97,13 @@ const syncTabLoc = reduceCalls(async (): Promise => { } const me = getCurrentAccount() - const currentEmployee = getCurrentEmployee() const newTab: WorkbenchTab = { _id: generateId(), _class: workbench.class.WorkbenchTab, space: core.space.Workspace, location: url, name, - attachedTo: currentEmployee, + attachedTo: me.primarySocialId, isPinned: false, modifiedOn: Date.now(), modifiedBy: me.primarySocialId @@ -198,7 +196,6 @@ export async function closeTab (tab: WorkbenchTab): Promise { export async function createTab (): Promise { const loc = getCurrentLocation() const client = getClient() - const me = getCurrentEmployee() let defaultUrl = `${workbenchId}/${loc.path[1]}/${notificationId}` try { @@ -213,7 +210,7 @@ export async function createTab (): Promise { const name = await translate(notification.string.Inbox, {}, get(languageStore)) const tab = await client.createDoc(workbench.class.WorkbenchTab, core.space.Workspace, { - attachedTo: me, + attachedTo: getCurrentAccount().primarySocialId, location: defaultUrl, isPinned: false, name diff --git a/plugins/workbench/src/index.ts b/plugins/workbench/src/index.ts index caa6978a17..39f3c0c5cc 100644 --- a/plugins/workbench/src/index.ts +++ b/plugins/workbench/src/index.ts @@ -13,7 +13,7 @@ // limitations under the License. // -import type { AccountRole, Class, Doc, Mixin, Obj, Ref, Space, Tx } from '@hcengineering/core' +import type { AccountRole, Class, Doc, Mixin, Obj, PersonId, Ref, Space, Tx } from '@hcengineering/core' import { DocNotifyContext, InboxNotification } from '@hcengineering/notification' import type { Asset, IntlString, Metadata, Plugin, Resource } from '@hcengineering/platform' import { plugin } from '@hcengineering/platform' @@ -116,6 +116,7 @@ export interface TxSidebarEvent = Record