-
Notifications
You must be signed in to change notification settings - Fork 6
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
shivani170
wants to merge
15
commits into
develop
Choose a base branch
from
feat/app-switcher
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
778d3ea
chore: Generalize user preferences as per resource kind
shivani170 dd3130a
chore: resource kind pass dynamically in payload
shivani170 a57ec47
chore: getUserPreferences props passed
shivani170 3d9334e
chore: version bump to 1.15.3-beta-3
shivani170 51576c9
chore: context switcher moved here
shivani170 92672b0
chore: recently visited apps & fetch from useUserPreferences
shivani170 f6c28c1
chore: handling error & reload in context switcher
shivani170 6d0a506
chore: getParsedResourcesMap code refactoring
shivani170 ad82427
chore: classNamePrefix support added
shivani170 8fc790b
chore: removed resource kind spreading from update resource
shivani170 5319e27
chore: payload fixes
shivani170 d41abb6
chore: getParsedResourcesMap fixes
shivani170 374c90c
Merge branch 'develop' into feat/app-switcher
shivani170 8c09454
chore: locally saved user preferences payload
shivani170 6ebd364
chore: undefined user preferences handling
shivani170 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getNoMatchingResultText, SelectPicker, SelectPickerVariantType } from '@Shared/Components' | ||
import { ComponentSizeType } from '@Shared/constants' | ||
|
||
import { ContextSwitcherTypes } from './types' | ||
import { customSelect, getDisabledOptions } from './utils' | ||
|
||
export const ContextSwitcher = ({ | ||
inputId, | ||
options = [], | ||
inputValue, | ||
onInputChange, | ||
isLoading, | ||
value, | ||
onChange, | ||
placeholder, | ||
filterOption, | ||
formatOptionLabel, | ||
}: ContextSwitcherTypes) => { | ||
const selectedOptions = options?.map((section) => ({ | ||
...section, | ||
options: section?.label === 'Recently Visited' ? section.options?.slice(1) : section.options, | ||
})) | ||
return ( | ||
<SelectPicker | ||
inputId={inputId} | ||
options={selectedOptions || []} | ||
inputValue={inputValue} | ||
onInputChange={onInputChange} | ||
isLoading={isLoading} | ||
noOptionsMessage={getNoMatchingResultText} | ||
onChange={onChange} | ||
value={value} | ||
variant={SelectPickerVariantType.BORDER_LESS} | ||
placeholder={placeholder} | ||
isOptionDisabled={getDisabledOptions} | ||
size={ComponentSizeType.xl} | ||
filterOption={filterOption || customSelect} | ||
formatOptionLabel={formatOptionLabel} | ||
/> | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { ContextSwitcher } from './ContextSwitcher' | ||
export type { ContextSwitcherTypes, RecentlyVisitedGroupedOptionsType, RecentlyVisitedOptions } from './types' | ||
export { getMinCharSearchPlaceholderGroup } from './utils' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { GroupBase } from 'react-select' | ||
|
||
import { SelectPickerOptionType, SelectPickerProps } from '../SelectPicker' | ||
|
||
export interface ContextSwitcherTypes | ||
extends Pick< | ||
SelectPickerProps, | ||
| 'placeholder' | ||
| 'onChange' | ||
| 'value' | ||
| 'isLoading' | ||
| 'onInputChange' | ||
| 'inputValue' | ||
| 'inputId' | ||
| 'formatOptionLabel' | ||
| 'filterOption' | ||
> { | ||
options: GroupBase<SelectPickerOptionType<string | number>>[] | ||
isAppDataAvailable?: boolean | ||
} | ||
|
||
export interface RecentlyVisitedOptions extends SelectPickerOptionType<number> { | ||
isDisabled?: boolean | ||
isRecentlyVisited?: boolean | ||
} | ||
|
||
export interface RecentlyVisitedGroupedOptionsType extends GroupBase<SelectPickerOptionType<number>> { | ||
label: string | ||
options: RecentlyVisitedOptions[] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { SelectPickerProps } from '../SelectPicker' | ||
import { RecentlyVisitedGroupedOptionsType, RecentlyVisitedOptions } from './types' | ||
|
||
export const getDisabledOptions = (option: RecentlyVisitedOptions): SelectPickerProps['isDisabled'] => option.isDisabled | ||
|
||
export const customSelect: SelectPickerProps['filterOption'] = (option, searchText: string) => { | ||
const label = option.data.label as string | ||
return option.data.value === 0 || label.toLowerCase().includes(searchText.toLowerCase()) | ||
} | ||
|
||
export const getMinCharSearchPlaceholderGroup = (resourceKind: string): RecentlyVisitedGroupedOptionsType => ({ | ||
label: `All ${resourceKind}`, | ||
options: [{ value: 0, label: 'Type 3 characters to search', isDisabled: true }], | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,10 @@ | |
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 { | ||
BaseRecentlyVisitedEntitiesTypes, | ||
GetUserPreferencesParsedDTO, | ||
GetUserPreferencesQueryParamsType, | ||
UpdateUserPreferencesPayloadType, | ||
|
@@ -34,6 +33,17 @@ import { | |
} from './types' | ||
import { getUserPreferenceResourcesMetadata } from './utils' | ||
|
||
export const getParsedResourcesMap = (resources: GetUserPreferencesParsedDTO['resources']) => { | ||
shivani170 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const resourcesMap = resources || {} | ||
const parsedResourcesMap: UserPreferencesType['resources'] = {} | ||
|
||
Object.entries(resourcesMap).forEach(([resourceKind, resourceActions]) => { | ||
parsedResourcesMap[resourceKind] = resourceActions | ||
}) | ||
|
||
return parsedResourcesMap | ||
} | ||
AbhishekA1509 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @returns UserPreferencesType | ||
* @description This function fetches the user preferences from the server. It uses the `get` method to make a request to the server and retrieves the user preferences based on the `USER_PREFERENCES_ATTRIBUTE_KEY`. The result is parsed and returned as a `UserPreferencesType` object. | ||
|
@@ -64,17 +74,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: getParsedResourcesMap(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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pls fix the comment |
||
|
@@ -87,6 +89,8 @@ export const getUserPreferences = async (): Promise<UserPreferencesType> => { | |
const getUserPreferencePayload = async ({ | ||
path, | ||
value, | ||
resourceKind, | ||
userPreferencesResponse, | ||
}: UserPathValueMapType): Promise<Partial<UserPreferencesPayloadValueType>> => { | ||
switch (path) { | ||
case 'themePreference': | ||
|
@@ -100,10 +104,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 BaseRecentlyVisitedEntitiesTypes[], resourceKind)[ | ||
resourceKind | ||
]?.[UserPreferenceResourceActions.RECENTLY_VISITED] || [], | ||
}, | ||
} | ||
|
||
return { | ||
resources: getUserPreferenceResourcesMetadata(value as BaseAppMetaData[]), | ||
resources: updatedResources, | ||
} | ||
} | ||
default: | ||
return {} | ||
} | ||
|
@@ -112,12 +130,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) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.