-
Notifications
You must be signed in to change notification settings - Fork 182
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
frontend: refactor row action menus #2621
Open
farodin91
wants to merge
1
commit into
headlamp-k8s:main
Choose a base branch
from
farodin91:cleanup-actions
base: main
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.
+144
−154
Open
Changes from all commits
Commits
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
This file contains 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 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 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
130 changes: 130 additions & 0 deletions
130
frontend/src/components/common/Resource/generateHeaderActions.tsx
This file contains 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,130 @@ | ||
import { has } from 'lodash'; | ||
import { MRT_Row } from 'material-react-table'; | ||
import { isValidElement } from 'react'; | ||
import React from 'react'; | ||
import { KubeObject } from '../../../lib/k8s/KubeObject'; | ||
import { | ||
DefaultHeaderAction, | ||
HeaderAction, | ||
HeaderActionType, | ||
} from '../../../redux/actionButtonsSlice'; | ||
import { useTypedSelector } from '../../../redux/reducers/reducers'; | ||
import { ButtonStyle } from '../ActionButton/ActionButton'; | ||
import ErrorBoundary from '../ErrorBoundary'; | ||
import DeleteButton from './DeleteButton'; | ||
import EditButton from './EditButton'; | ||
import { RestartButton } from './RestartButton'; | ||
import ScaleButton from './ScaleButton'; | ||
|
||
export function generateActions<T extends KubeObject>( | ||
resource: T | null, | ||
buttonStyle: ButtonStyle, | ||
actions: | ||
| ((resource: T | null) => React.ReactNode[] | HeaderAction[] | null) | ||
| React.ReactNode[] | ||
| null | ||
| HeaderAction[], | ||
noDefaultActions?: boolean, | ||
closeMenu?: () => void | ||
): React.ReactNode[] { | ||
const headerActions = useTypedSelector(state => state.actionButtons.headerActions); | ||
const headerActionsProcessors = useTypedSelector( | ||
state => state.actionButtons.headerActionsProcessors | ||
); | ||
function setupAction(headerAction: HeaderAction) { | ||
let Action = has(headerAction, 'action') ? (headerAction as any).action : headerAction; | ||
|
||
if (!noDefaultActions && has(headerAction, 'id')) { | ||
switch ((headerAction as HeaderAction).id) { | ||
case DefaultHeaderAction.RESTART: | ||
Action = RestartButton; | ||
break; | ||
case DefaultHeaderAction.SCALE: | ||
Action = ScaleButton; | ||
break; | ||
case DefaultHeaderAction.EDIT: | ||
Action = EditButton; | ||
break; | ||
case DefaultHeaderAction.DELETE: | ||
Action = DeleteButton; | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
if (!Action || (headerAction as unknown as HeaderAction).action === null) { | ||
return null; | ||
} | ||
|
||
if (isValidElement(Action)) { | ||
return <ErrorBoundary>{Action}</ErrorBoundary>; | ||
} else if (Action === null) { | ||
return null; | ||
} else if (typeof Action === 'function') { | ||
return ( | ||
<ErrorBoundary> | ||
<Action item={resource} buttonStyle={buttonStyle} closeMenu={closeMenu} /> | ||
</ErrorBoundary> | ||
); | ||
} | ||
} | ||
|
||
const defaultActions = [ | ||
{ | ||
id: DefaultHeaderAction.RESTART, | ||
}, | ||
{ | ||
id: DefaultHeaderAction.SCALE, | ||
}, | ||
{ | ||
id: DefaultHeaderAction.EDIT, | ||
}, | ||
{ | ||
id: DefaultHeaderAction.DELETE, | ||
}, | ||
]; | ||
|
||
let hAccs: HeaderAction[] = []; | ||
const accs = typeof actions === 'function' ? actions(resource) || [] : actions; | ||
if (accs !== null) { | ||
hAccs = [...accs].map((action, i): HeaderAction => { | ||
if ((action as HeaderAction)?.id !== undefined) { | ||
return action as HeaderAction; | ||
} else { | ||
return { id: `gen-${i}`, action: action as HeaderActionType }; | ||
} | ||
}); | ||
} | ||
|
||
let actionsProcessed = [...headerActions, ...hAccs, ...defaultActions]; | ||
if (headerActionsProcessors.length > 0) { | ||
for (const headerProcessor of headerActionsProcessors) { | ||
actionsProcessed = headerProcessor.processor(resource, actionsProcessed); | ||
} | ||
} | ||
|
||
const allActions = React.Children.toArray( | ||
(function propsActions() { | ||
const pluginAddedActions = actionsProcessed.map(setupAction); | ||
return React.Children.toArray(pluginAddedActions); | ||
})() | ||
); | ||
return allActions; | ||
} | ||
|
||
export default function generateRowActionsMenu(actions: HeaderAction[] | null | undefined) { | ||
return ({ closeMenu, row }: { closeMenu: () => void; row: MRT_Row<Record<string, any>> }) => { | ||
const actionsProcessed = generateActions( | ||
row.original as any, | ||
'menu', | ||
actions || [], | ||
false, | ||
closeMenu | ||
); | ||
if (actionsProcessed.length === 0) { | ||
return null; | ||
} | ||
return actionsProcessed; | ||
}; | ||
} |
This file contains 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 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to leave this with the previous type because plugins may be using it and it would mean a type break for them.