Skip to content

feat: Generalize user preferences as per resource kind #793

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@devtron-labs/devtron-fe-common-lib",
"version": "1.15.3-pre-4",
"version": "1.15.3-beta-3",
"description": "Supporting common component library",
"type": "module",
"main": "dist/index.js",
Expand Down
42 changes: 29 additions & 13 deletions src/Shared/Hooks/useUserPreferences/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { ROUTES } from '@Common/Constants'
import { get, getUrlWithSearchParams, patch, showError } from '@Common/index'
import { THEME_PREFERENCE_MAP } from '@Shared/Providers/ThemeProvider/types'
import { BaseAppMetaData } from '@Shared/Services'
import { ResourceKindType } from '@Shared/types'

import { USER_PREFERENCES_ATTRIBUTE_KEY } from './constants'
import {
Expand Down Expand Up @@ -64,17 +63,9 @@ export const getUserPreferences = async (): Promise<UserPreferencesType> => {
parsedResult.computedAppTheme === 'system-dark' || parsedResult.computedAppTheme === 'system-light'
? THEME_PREFERENCE_MAP.auto
: parsedResult.computedAppTheme,
resources: {
[ResourceKindType.devtronApplication]: {
[UserPreferenceResourceActions.RECENTLY_VISITED]:
parsedResult.resources?.[ResourceKindType.devtronApplication]?.[
UserPreferenceResourceActions.RECENTLY_VISITED
] || ([] as BaseAppMetaData[]),
},
},
resources: parsedResult.resources,
}
}

/**
* @description This function updates the user preferences in the server. It constructs a payload with the updated user preferences and sends a PATCH request to the server. If the request is successful, it returns true. If an error occurs, it shows an error message and returns false.
* @param updatedUserPreferences - The updated user preferences to be sent to the server.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls fix the comment

Expand All @@ -87,6 +78,8 @@ export const getUserPreferences = async (): Promise<UserPreferencesType> => {
const getUserPreferencePayload = async ({
path,
value,
resourceKind,
userPreferencesResponse,
}: UserPathValueMapType): Promise<Partial<UserPreferencesPayloadValueType>> => {
switch (path) {
case 'themePreference':
Expand All @@ -100,10 +93,24 @@ const getUserPreferencePayload = async ({
value.pipelineRBACViewSelectedTab === ViewIsPipelineRBACConfiguredRadioTabs.ACCESS_ONLY,
}

case 'resources':
case 'resources': {
const existingResources = userPreferencesResponse?.resources || {}

const updatedResources = {
...existingResources,
[resourceKind]: {
...existingResources[resourceKind],
[UserPreferenceResourceActions.RECENTLY_VISITED]:
getUserPreferenceResourcesMetadata(value as BaseAppMetaData[], resourceKind)[resourceKind]?.[
UserPreferenceResourceActions.RECENTLY_VISITED
] || [],
},
}

return {
resources: getUserPreferenceResourcesMetadata(value as BaseAppMetaData[]),
resources: updatedResources,
}
}
default:
return {}
}
Expand All @@ -112,12 +119,21 @@ const getUserPreferencePayload = async ({
export const updateUserPreferences = async ({
path,
value,
resourceKind,
shouldThrowError = false,
userPreferencesResponse,
}: UserPreferenceResourceProps): Promise<boolean> => {
try {
const payload: UpdateUserPreferencesPayloadType = {
key: USER_PREFERENCES_ATTRIBUTE_KEY,
value: JSON.stringify(await getUserPreferencePayload({ path, value } as UserPathValueMapType)),
value: JSON.stringify(
await getUserPreferencePayload({
path,
value,
resourceKind,
userPreferencesResponse,
} as UserPathValueMapType),
),
}

await patch(`${ROUTES.ATTRIBUTES_USER}/${ROUTES.PATCH}`, payload)
Expand Down
29 changes: 22 additions & 7 deletions src/Shared/Hooks/useUserPreferences/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,23 @@ export enum ViewIsPipelineRBACConfiguredRadioTabs {
export enum UserPreferenceResourceActions {
RECENTLY_VISITED = 'recently-visited',
}
export type PreferredResourceKindType =
| ResourceKindType.devtronApplication
| ResourceKindType.job
| ResourceKindType.appGroup
| ResourceKindType.cluster

export interface UserPreferenceRecentlyVisitedAppsTypes {
appId: number
appName: string
resourceKind: PreferredResourceKindType
}

export interface UserResourceKindActionType {
[UserPreferenceResourceActions.RECENTLY_VISITED]: BaseAppMetaData[]
}
export interface UserPreferenceResourceType {
[ResourceKindType.devtronApplication]: UserResourceKindActionType
export type UserPreferenceResourceType = {
[key in ResourceKindType]?: UserResourceKindActionType
}
export interface GetUserPreferencesParsedDTO {
viewPermittedEnvOnly?: boolean
Expand Down Expand Up @@ -76,30 +88,33 @@ export interface UserPreferencesType {
export interface UpdatedUserPreferencesType extends UserPreferencesType, Pick<ThemeConfigType, 'appTheme'> {}

export interface UseUserPreferencesProps {
userPreferenceResourceKind?: ResourceKindType
migrateUserPreferences?: (userPreferencesResponse: UserPreferencesType) => Promise<UserPreferencesType>
}

export type UserPathValueMapType =
| {
path: 'themePreference'
value: Required<Pick<UpdatedUserPreferencesType, 'themePreference' | 'appTheme'>>
resourceKind?: never
userPreferencesResponse?: never
}
| {
path: 'pipelineRBACViewSelectedTab'
value: Required<Pick<UserPreferencesType, 'pipelineRBACViewSelectedTab'>>
resourceKind?: never
userPreferencesResponse?: never
}
| {
path: 'resources'
value: Required<BaseAppMetaData[]>
resourceKind: PreferredResourceKindType
userPreferencesResponse?
}

export type UserPreferenceResourceProps = UserPathValueMapType & {
shouldThrowError?: boolean
}

export interface UserPreferenceRecentlyVisitedAppsTypes {
appId: number
appName: string
userPreferencesResponse?
}

export interface UserPreferenceFilteredListTypes extends UserPreferenceRecentlyVisitedAppsTypes {
Expand Down
19 changes: 14 additions & 5 deletions src/Shared/Hooks/useUserPreferences/useUserPrefrences.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { useState } from 'react'

import { ServerErrors } from '@Common/ServerError'
import { useTheme } from '@Shared/Providers'
import { ResourceKindType } from '@Shared/types'

import { getUserPreferences, updateUserPreferences } from './service'
import {
Expand All @@ -36,26 +35,36 @@ export const useUserPreferences = ({ migrateUserPreferences }: UseUserPreference

const { handleThemeSwitcherDialogVisibilityChange, handleThemePreferenceChange } = useTheme()

const fetchRecentlyVisitedParsedApps = async ({ appId, appName }: UserPreferenceRecentlyVisitedAppsTypes) => {
const fetchRecentlyVisitedParsedApps = async ({
appId,
appName,
resourceKind,
}: UserPreferenceRecentlyVisitedAppsTypes) => {
const userPreferencesResponse = await getUserPreferences()

const uniqueFilteredApps = getFilteredUniqueAppList({
userPreferencesResponse,
appId,
appName,
resourceKind,
})

setUserPreferences((prev) => ({
...prev,
resources: {
...prev?.resources,
[ResourceKindType.devtronApplication]: {
...prev?.resources?.[ResourceKindType.devtronApplication],
[resourceKind]: {
...prev?.resources?.[resourceKind],
[UserPreferenceResourceActions.RECENTLY_VISITED]: uniqueFilteredApps,
},
},
}))
await updateUserPreferences({ path: 'resources', value: uniqueFilteredApps })
await updateUserPreferences({
path: 'resources',
value: uniqueFilteredApps,
resourceKind,
userPreferencesResponse,
})
}

const handleInitializeUserPreferencesFromResponse = (userPreferencesResponse: UserPreferencesType) => {
Expand Down
12 changes: 7 additions & 5 deletions src/Shared/Hooks/useUserPreferences/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ import { ResourceKindType } from '@Shared/types'

import { UserPreferenceFilteredListTypes, UserPreferenceResourceActions, UserPreferenceResourceType } from './types'

export const getUserPreferenceResourcesMetadata = (recentlyVisited: BaseAppMetaData[]): UserPreferenceResourceType => ({
[ResourceKindType.devtronApplication]: {
export const getUserPreferenceResourcesMetadata = (
recentlyVisited: BaseAppMetaData[],
resourceKind: ResourceKindType,
): UserPreferenceResourceType => ({
[resourceKind]: {
[UserPreferenceResourceActions.RECENTLY_VISITED]: recentlyVisited.map(({ appId, appName }) => ({
appId,
appName,
Expand All @@ -16,11 +19,10 @@ export const getFilteredUniqueAppList = ({
userPreferencesResponse,
appId,
appName,
resourceKind,
}: UserPreferenceFilteredListTypes) => {
const _recentApps =
userPreferencesResponse?.resources?.[ResourceKindType.devtronApplication]?.[
UserPreferenceResourceActions.RECENTLY_VISITED
] || []
userPreferencesResponse?.resources?.[resourceKind]?.[UserPreferenceResourceActions.RECENTLY_VISITED] || []

const isInvalidApp = appId && !appName

Expand Down
1 change: 1 addition & 0 deletions src/Shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ export enum ResourceKindType {
cdPipeline = 'cd-pipeline',
ciPipeline = 'ci-pipeline',
project = 'project',
appGroup = 'app-group',
}

/**
Expand Down