Skip to content

Commit 0d6e641

Browse files
authored
Merge pull request #1030 from devtron-labs/fix/user-preferences-types
chore: add user preferences migration logic
2 parents c13cadb + 6898c23 commit 0d6e641

6 files changed

Lines changed: 42 additions & 8 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "1.23.4-beta-2",
3+
"version": "1.23.4-beta-3",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Shared/Components/Table/InternalTable.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,12 @@ const InternalTable = <
156156
data: getRowsResult,
157157
error: getRowsError,
158158
refetch: reloadGetRows,
159+
// eslint-disable-next-line @tanstack/query/exhaustive-deps
159160
} = useQuery<unknown, RowsResultType<RowData>, unknown[], false>({
160161
queryFn: async ({ signal }) => {
161162
const { rows: newRows, totalRows } = await getRows(filterData, signal)
162163
return { filteredRows: newRows, totalRows }
163164
},
164-
// eslint-disable-next-line @tanstack/query/exhaustive-deps
165165
queryKey: [
166166
searchKey,
167167
sortBy,

src/Shared/Hooks/useUserPreferences/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*/
1616

1717
export * from './constants'
18-
export { getUserPreferences, updateAndPersistUserPreferences, updateUserPreferences } from './service'
18+
export { getUserPreferences, updateUserPreferences } from './service'
1919
export * from './types'
2020
export { useUserPreferences } from './useUserPrefrences'

src/Shared/Hooks/useUserPreferences/service.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import { ROUTES } from '@Common/Constants'
1818
import { get, getUrlWithSearchParams, patch, showError } from '@Common/index'
19+
import { ResourceKindType } from '@Shared/index'
1920
import { THEME_PREFERENCE_MAP } from '@Shared/Providers/ThemeProvider/types'
2021

2122
import { USER_PREFERENCES_ATTRIBUTE_KEY } from './constants'
@@ -177,27 +178,56 @@ export const updateUserPreferences = async ({
177178
}
178179
}
179180

181+
const migrateUserPreferences = async () => {
182+
try {
183+
const userPreferences: UserPreferencesType = await JSON.parse(
184+
localStorage.getItem(USER_PREFERENCES_ATTRIBUTE_KEY),
185+
)
186+
if (userPreferences && userPreferences.version !== 'v1') {
187+
const migratedPreferences: UserPreferencesType = {
188+
...userPreferences,
189+
resources: {
190+
...userPreferences.resources,
191+
[ResourceKindType.devtronApplication]: {
192+
[UserPreferenceResourceActions.RECENTLY_VISITED]: (
193+
userPreferences.resources?.[ResourceKindType.devtronApplication]?.[
194+
UserPreferenceResourceActions.RECENTLY_VISITED
195+
] || []
196+
).map(({ id, name }) => ({ id: +id, name })),
197+
},
198+
},
199+
version: 'v1',
200+
}
201+
localStorage.setItem(USER_PREFERENCES_ATTRIBUTE_KEY, JSON.stringify(migratedPreferences))
202+
}
203+
} catch {
204+
// do nothing
205+
}
206+
}
207+
180208
/**
181209
* Optimized function to get updated user preferences with resource filtering
182210
* Can work with provided userPreferences or fetch from server/localStorage
183211
* Centralizes all resource updating logic
184212
*/
185-
export const getUpdatedUserPreferences = async ({
213+
const getUpdatedUserPreferences = async ({
186214
id,
187215
name,
188216
resourceKind,
189217
userPreferencesResponse,
190218
}: UserPreferenceFilteredListTypes & {
191219
userPreferencesResponse?: UserPreferencesType
192220
}): Promise<UserPreferencesType> => {
221+
await migrateUserPreferences()
222+
193223
// Get base user preferences from multiple sources (priority: provided > localStorage > server)
194224
let baseUserPreferences: UserPreferencesType
195225

196226
if (userPreferencesResponse) {
197227
baseUserPreferences = userPreferencesResponse
198228
} else {
199229
try {
200-
const localStorageData = localStorage.getItem('userPreferences')
230+
const localStorageData = localStorage.getItem(USER_PREFERENCES_ATTRIBUTE_KEY)
201231
if (localStorageData) {
202232
baseUserPreferences = JSON.parse(localStorageData)
203233
} else {
@@ -257,7 +287,7 @@ export const updateAndPersistUserPreferences = async ({
257287

258288
// Update localStorage if requested
259289
if (updateLocalStorage) {
260-
localStorage.setItem('userPreferences', JSON.stringify(updatedPreferences))
290+
localStorage.setItem(USER_PREFERENCES_ATTRIBUTE_KEY, JSON.stringify(updatedPreferences))
261291
}
262292

263293
// Update server with the new resource list if resourceKind is provided

src/Shared/Hooks/useUserPreferences/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export interface UserPreferencesType extends Pick<GetUserPreferencesParsedDTO, '
102102
* @description resources object
103103
*/
104104
resources?: GetUserPreferencesParsedDTO['resources']
105+
/**
106+
* @description version of the user preferences, to be used for migration purposes
107+
*/
108+
version?: string
105109
}
106110

107111
export interface UpdatedUserPreferencesType extends UserPreferencesType, Pick<ThemeConfigType, 'appTheme'> {}

0 commit comments

Comments
 (0)