From aee24ffb317263b12fe90a528a34bdb90bdc315b Mon Sep 17 00:00:00 2001 From: EYHN Date: Wed, 7 Aug 2024 03:31:06 +0000 Subject: [PATCH] fix(core): migration favorite appear again (#7768) --- .../views/sections/migration-favorites/index.tsx | 15 ++++++++------- .../frontend/core/src/modules/properties/index.ts | 2 +- .../src/modules/properties/services/adapter.ts | 12 ++++++++---- .../src/modules/properties/services/schema.ts | 1 + 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/packages/frontend/core/src/modules/explorer/views/sections/migration-favorites/index.tsx b/packages/frontend/core/src/modules/explorer/views/sections/migration-favorites/index.tsx index e0d4e300a8..3babdb8631 100644 --- a/packages/frontend/core/src/modules/explorer/views/sections/migration-favorites/index.tsx +++ b/packages/frontend/core/src/modules/explorer/views/sections/migration-favorites/index.tsx @@ -1,7 +1,7 @@ import { IconButton, useConfirmModal } from '@affine/component'; import { track } from '@affine/core/mixpanel'; import { ExplorerTreeRoot } from '@affine/core/modules/explorer/views/tree'; -import { FavoriteItemsAdapter } from '@affine/core/modules/properties'; +import { MigrationFavoriteItemsAdapter } from '@affine/core/modules/properties'; import { Trans, useI18n } from '@affine/i18n'; import { BroomIcon, HelpIcon } from '@blocksuite/icons/rc'; import { DocsService, useLiveData, useServices } from '@toeverything/infra'; @@ -15,17 +15,18 @@ import * as styles from './styles.css'; export const ExplorerMigrationFavorites = () => { const t = useI18n(); - const { favoriteItemsAdapter, docsService } = useServices({ - FavoriteItemsAdapter, + const { migrationFavoriteItemsAdapter, docsService } = useServices({ + MigrationFavoriteItemsAdapter, DocsService, }); const docs = useLiveData(docsService.list.docs$); const trashDocs = useLiveData(docsService.list.trashDocs$); + const migrated = useLiveData(migrationFavoriteItemsAdapter.migrated$); const { openConfirmModal } = useConfirmModal(); const favorites = useLiveData( - favoriteItemsAdapter.favorites$.map(favs => { + migrationFavoriteItemsAdapter.favorites$.map(favs => { return favs.filter(fav => { if (fav.type === 'doc') { return ( @@ -57,10 +58,10 @@ export const ExplorerMigrationFavorites = () => { cancelText: t['com.affine.rootAppSidebar.migration-data.clean-all.cancel'](), onConfirm() { - favoriteItemsAdapter.clearAll(); + migrationFavoriteItemsAdapter.markFavoritesMigrated(); }, }); - }, [favoriteItemsAdapter, openConfirmModal, t]); + }, [migrationFavoriteItemsAdapter, openConfirmModal, t]); const handleClickHelp = useCallback(() => { openConfirmModal({ @@ -85,7 +86,7 @@ export const ExplorerMigrationFavorites = () => { track.$.navigationPanel.migrationData.openMigrationDataHelp(); }, [handleClickClear, openConfirmModal, t]); - if (favorites.length === 0) { + if (favorites.length === 0 || migrated) { return null; } diff --git a/packages/frontend/core/src/modules/properties/index.ts b/packages/frontend/core/src/modules/properties/index.ts index f20980a80e..135dd49568 100644 --- a/packages/frontend/core/src/modules/properties/index.ts +++ b/packages/frontend/core/src/modules/properties/index.ts @@ -1,6 +1,6 @@ export { CompatibleFavoriteItemsAdapter, - MigrationFavoriteItemsAdapter as FavoriteItemsAdapter, + MigrationFavoriteItemsAdapter, WorkspacePropertiesAdapter, } from './services/adapter'; export { WorkspaceLegacyProperties } from './services/legacy-properties'; diff --git a/packages/frontend/core/src/modules/properties/services/adapter.ts b/packages/frontend/core/src/modules/properties/services/adapter.ts index 4eae1e60ee..3554162051 100644 --- a/packages/frontend/core/src/modules/properties/services/adapter.ts +++ b/packages/frontend/core/src/modules/properties/services/adapter.ts @@ -159,8 +159,8 @@ export class WorkspacePropertiesAdapter extends Service { /** * After the user completes the migration, call this function to clear the favorite data */ - cleanupFavorites() { - this.proxy.favorites = {}; + markFavoritesMigrated() { + this.proxy.favoritesMigrated = true; } } @@ -173,14 +173,18 @@ export class MigrationFavoriteItemsAdapter extends Service { this.getItems().filter(i => i.value) ); + migrated$ = this.adapter.properties$.map( + props => props.favoritesMigrated ?? false + ); + getItems() { return Object.entries(this.adapter.favorites ?? {}) .filter(([k]) => k.includes(':')) .map(([, v]) => v); } - clearAll() { - this.adapter.cleanupFavorites(); + markFavoritesMigrated() { + this.adapter.markFavoritesMigrated(); } } diff --git a/packages/frontend/core/src/modules/properties/services/schema.ts b/packages/frontend/core/src/modules/properties/services/schema.ts index 95e306fbe9..b93a607b3e 100644 --- a/packages/frontend/core/src/modules/properties/services/schema.ts +++ b/packages/frontend/core/src/modules/properties/services/schema.ts @@ -104,6 +104,7 @@ export const WorkspaceAffinePropertiesSchema = z.object({ schema: WorkspaceAffinePropertiesSchemaSchema.optional(), favorites: z.record(WorkspaceFavoriteItemSchema).optional(), pageProperties: z.record(WorkspacePagePropertiesSchema).optional(), + favoritesMigrated: z.boolean().optional(), }); export type PageInfoCustomPropertyMeta = z.infer<