refactor: native feature management (#2350)
This commit is contained in:
98
packages/snjs/lib/Migrations/Versions/2_202_1.ts
Normal file
98
packages/snjs/lib/Migrations/Versions/2_202_1.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { ApplicationStage } from '@standardnotes/services'
|
||||
import { Migration } from '@Lib/Migrations/Migration'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
import { AllComponentPreferences, ComponentInterface, PrefKey } from '@standardnotes/models'
|
||||
import { Copy, Uuids } from '@standardnotes/utils'
|
||||
import { FindNativeFeature } from '@standardnotes/features'
|
||||
|
||||
export class Migration2_202_1 extends Migration {
|
||||
static override version(): string {
|
||||
return '2.202.1'
|
||||
}
|
||||
|
||||
protected registerStageHandlers(): void {
|
||||
this.registerStageHandler(ApplicationStage.FullSyncCompleted_13, async () => {
|
||||
await this.migrateComponentDataToUserPreferences()
|
||||
await this.migrateActiveComponentsToUserPreferences()
|
||||
await this.deleteComponentsWhichAreNativeFeatures()
|
||||
|
||||
this.markDone()
|
||||
})
|
||||
}
|
||||
|
||||
private async migrateComponentDataToUserPreferences(): Promise<void> {
|
||||
const components = this.services.itemManager.getItems<ComponentInterface>(ContentType.TYPES.Component)
|
||||
|
||||
if (components.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const mutablePreferencesValue = Copy<AllComponentPreferences>(
|
||||
this.services.preferences.getValue(PrefKey.ComponentPreferences) ?? {},
|
||||
)
|
||||
|
||||
for (const component of components) {
|
||||
const componentData = component.legacyComponentData
|
||||
if (!componentData) {
|
||||
continue
|
||||
}
|
||||
|
||||
if (Object.keys(componentData).length === 0) {
|
||||
continue
|
||||
}
|
||||
|
||||
const preferencesLookupKey = FindNativeFeature(component.identifier) ? component.identifier : component.uuid
|
||||
|
||||
const componentPreferences = mutablePreferencesValue[preferencesLookupKey] ?? {}
|
||||
for (const key of Object.keys(componentData)) {
|
||||
componentPreferences[key] = componentData[key]
|
||||
}
|
||||
|
||||
mutablePreferencesValue[preferencesLookupKey] = componentPreferences
|
||||
}
|
||||
|
||||
await this.services.preferences.setValueDetached(PrefKey.ComponentPreferences, mutablePreferencesValue)
|
||||
}
|
||||
|
||||
private async migrateActiveComponentsToUserPreferences(): Promise<void> {
|
||||
const allActiveitems = [
|
||||
...this.services.itemManager.getItems<ComponentInterface>(ContentType.TYPES.Component),
|
||||
...this.services.itemManager.getItems<ComponentInterface>(ContentType.TYPES.Theme),
|
||||
].filter((component) => component.legacyActive)
|
||||
|
||||
if (allActiveitems.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
const activeThemes = allActiveitems.filter((component) => component.isTheme())
|
||||
const activeComponents = allActiveitems.filter((component) => !component.isTheme())
|
||||
|
||||
await this.services.preferences.setValueDetached(PrefKey.ActiveThemes, Uuids(activeThemes))
|
||||
await this.services.preferences.setValueDetached(PrefKey.ActiveComponents, Uuids(activeComponents))
|
||||
}
|
||||
|
||||
private async deleteComponentsWhichAreNativeFeatures(): Promise<void> {
|
||||
const componentsToDelete = [
|
||||
...this.services.itemManager.getItems<ComponentInterface>(ContentType.TYPES.Component),
|
||||
...this.services.itemManager.getItems<ComponentInterface>(ContentType.TYPES.Theme),
|
||||
].filter((candidate) => {
|
||||
const nativeFeature = FindNativeFeature(candidate.identifier)
|
||||
if (!nativeFeature) {
|
||||
return false
|
||||
}
|
||||
|
||||
const isDeprecatedAndThusShouldNotDeleteComponentSinceUserHasItRetained = nativeFeature.deprecated
|
||||
if (isDeprecatedAndThusShouldNotDeleteComponentSinceUserHasItRetained) {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
})
|
||||
|
||||
if (componentsToDelete.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
await this.services.mutator.setItemsToBeDeleted(componentsToDelete)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { ApplicationStage } from '@standardnotes/services'
|
||||
import { FeatureIdentifier } from '@standardnotes/features'
|
||||
import { Migration } from '@Lib/Migrations/Migration'
|
||||
import { SNTheme } from '@standardnotes/models'
|
||||
import { ThemeInterface } from '@standardnotes/models'
|
||||
import { ContentType } from '@standardnotes/domain-core'
|
||||
|
||||
const NoDistractionIdentifier = 'org.standardnotes.theme-no-distraction' as FeatureIdentifier
|
||||
@@ -19,7 +19,7 @@ export class Migration2_42_0 extends Migration {
|
||||
}
|
||||
|
||||
private async deleteNoDistraction(): Promise<void> {
|
||||
const themes = (this.services.itemManager.getItems(ContentType.TYPES.Theme) as SNTheme[]).filter((theme) => {
|
||||
const themes = this.services.itemManager.getItems<ThemeInterface>(ContentType.TYPES.Theme).filter((theme) => {
|
||||
return theme.identifier === NoDistractionIdentifier
|
||||
})
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import { Migration2_36_0 } from './2_36_0'
|
||||
import { Migration2_42_0 } from './2_42_0'
|
||||
import { Migration2_167_6 } from './2_167_6'
|
||||
import { Migration2_168_6 } from './2_168_6'
|
||||
import { Migration2_202_1 } from './2_202_1'
|
||||
|
||||
export const MigrationClasses = [
|
||||
Migration2_0_15,
|
||||
@@ -14,6 +15,7 @@ export const MigrationClasses = [
|
||||
Migration2_42_0,
|
||||
Migration2_167_6,
|
||||
Migration2_168_6,
|
||||
Migration2_202_1,
|
||||
]
|
||||
|
||||
export {
|
||||
@@ -24,4 +26,5 @@ export {
|
||||
Migration2_42_0,
|
||||
Migration2_167_6,
|
||||
Migration2_168_6,
|
||||
Migration2_202_1,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user