forked from artsy/eigen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration.ts
More file actions
204 lines (199 loc) · 6.02 KB
/
migration.ts
File metadata and controls
204 lines (199 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// easy-peasy ships with a fork of immer so let's use that instead of adding another copy of immer to our bundle.
import { LegacyNativeModules } from "app/NativeModules/LegacyNativeModules"
import { echoLaunchJson } from "app/utils/jsonFiles"
import { produce } from "immer-peasy"
import { Platform } from "react-native"
/**
* IMPORTANT
* Before you modify this file please read docs/adding_state_migrations.md
*/
export const Versions = {
AddSearchesAndNativeAndBottomTabs: 1,
AddConsignments: 2,
RenameConsignmentsToMyCollection: 3,
RemoveMyCollectionNavigationState: 4,
AddAuthAndConfigState: 5,
AddFeatureFlagInfra: 6,
RefactorConfigModel: 7,
FixEnvironmentMigrationBug: 8,
AddUserIsDev: 9,
AddAuthOnboardingState: 10,
RenameUserEmail: 11,
AddToastModel: 12,
PendingPushNotification: 13,
CopyIOSNativeSessionAuthToTS: 14,
AddExperimentsModel: 15,
AddPreviousSessionUserID: 16,
RemoveNativeOnboardingState: 17,
AddUserPreferences: 18,
AddVisualClueModel: 19,
AddArtworkSubmissionModel: 20,
AddArtworkViewOption: 21,
RenameModelsAndAddDarkModeSupport: 22,
}
export const CURRENT_APP_VERSION = Versions.RenameModelsAndAddDarkModeSupport
export type Migrations = Record<number, (oldState: any) => any>
export const artsyAppMigrations: Migrations = {
[Versions.AddSearchesAndNativeAndBottomTabs]: (_) => ({
bottomTabs: {},
native: {},
search: { recentSearches: [] },
}),
[Versions.AddConsignments]: (state) => {
state.consignments = {
artwork: {},
navigation: {},
}
},
[Versions.RenameConsignmentsToMyCollection]: (state) => {
state.myCollection = state.consignments
delete state.consignments
},
[Versions.RemoveMyCollectionNavigationState]: (state) => {
delete state.myCollection.navigation
},
[Versions.AddAuthAndConfigState]: (state) => {
state.auth = {
userID: null,
userAccessToken: null,
userAccessTokenExpiresIn: null,
xAppToken: null,
xApptokenExpiresIn: null,
}
state.config = {}
},
[Versions.AddFeatureFlagInfra]: (state) => {
state.config.adminFeatureFlagOverrides = {}
state.config.echoState = echoLaunchJson()
},
[Versions.RefactorConfigModel]: (state) => {
const newConfig = {} as any
newConfig.features = { adminOverrides: state.config.adminFeatureFlagOverrides }
newConfig.echo = { state: state.config.echoState }
newConfig.environment = { adminOverrides: {}, env: "staging" }
state.config = newConfig
},
[Versions.FixEnvironmentMigrationBug]: (state) => {
state.config.environment.env = __TEST__ ? "staging" : "production"
},
[Versions.AddUserIsDev]: (state) => {
state.auth.androidUserEmail = null
state.config.userIsDev = { flipValue: false }
},
[Versions.AddAuthOnboardingState]: (state) => {
state.auth.onboardingState = "none"
},
[Versions.RenameUserEmail]: (state) => {
if (Platform.OS === "ios") {
state.auth.userEmail = LegacyNativeModules.ARTemporaryAPIModule.getUserEmail()
}
if (Platform.OS === "android") {
state.auth.userEmail = state.auth.androidUserEmail
}
state.auth.userEmail = state.auth.userEmail ?? null
delete state.auth.androidUserEmail
},
[Versions.AddToastModel]: (state) => {
state.toast = {}
},
[Versions.PendingPushNotification]: (state) => {
state.pendingPushNotification = { notification: null }
},
[Versions.CopyIOSNativeSessionAuthToTS]: (state) => {
if (Platform.OS === "ios") {
const nativeState = LegacyNativeModules.ARNotificationsManager.nativeState
state.auth.userAccessToken = nativeState.authenticationToken
state.auth.onboardingState = (nativeState as any).onboardingState ?? "none"
state.auth.userID = nativeState.userID
}
},
[Versions.AddExperimentsModel]: (state) => {
state.config.experiments = {}
},
[Versions.AddPreviousSessionUserID]: (state) => {
state.auth.previousSessionUserID = null
},
[Versions.RemoveNativeOnboardingState]: (state) => {
delete state.native.onboardingState
},
[Versions.AddUserPreferences]: (state) => {
state.userPreferences = { currency: "USD", metric: "" }
},
[Versions.AddVisualClueModel]: (state) => {
state.visualClue = {
seenVisualClues: [],
}
},
[Versions.AddArtworkSubmissionModel]: (state) => {
state.artworkSubmission = {
submission: {
submissionId: "",
artworkDetails: {
artist: "",
artistId: "",
title: "",
year: "",
medium: "",
attributionClass: null,
editionNumber: "",
editionSizeFormatted: "",
dimensionsMetric: "in",
height: "",
width: "",
depth: "",
provenance: "",
state: "DRAFT",
utmMedium: "",
utmSource: "",
utmTerm: "",
location: {
city: "",
state: "",
country: "",
},
},
photos: {
photos: [],
},
},
}
},
[Versions.AddArtworkViewOption]: (state) => {
state.userPreferences.artworkViewOption = "grid"
},
[Versions.RenameModelsAndAddDarkModeSupport]: (state) => {
state.artsyPrefs = state.config ?? {}
state.userPrefs = state.userPreferences ?? {}
state.devicePrefs = {
usingSystemColorScheme: false,
forcedColorScheme: "light",
}
delete state.config
delete state.userPreferences
},
}
export function migrate<State extends { version: number }>({
state,
migrations = artsyAppMigrations,
toVersion = CURRENT_APP_VERSION,
}: {
state: State
migrations?: Migrations
toVersion?: number
}): {
version: number
} {
if (typeof state.version !== "number") {
throw new Error("Bad state.version " + JSON.stringify(state))
}
while (state.version < toVersion) {
const nextVersion = state.version + 1
const migrator = migrations[nextVersion]
if (!migrator) {
throw new Error("No migrator found for app version " + nextVersion)
}
state = produce(state, migrator)
state.version = nextVersion
}
return state
}