-
Notifications
You must be signed in to change notification settings - Fork 0
feat(users): multi-select (Space/V) + bulk lifecycle actions #9
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
posquit0
wants to merge
2
commits into
main
Choose a base branch
from
feat/users-multiselect-bulk-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.
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 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 |
|---|---|---|
|
|
@@ -6,6 +6,9 @@ package app | |
|
|
||
| import ( | ||
| "context" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| tea "github.com/charmbracelet/bubbletea" | ||
|
|
||
|
|
@@ -16,11 +19,25 @@ import ( | |
| // for the currently-selected user. Falls back to a transient toast | ||
| // when no Users target is available (e.g., the operator fired the | ||
| // command from a non-Users screen). | ||
| // | ||
| // MW-8 — when the active screen publishes a non-empty | ||
| // SelectedUsers() (multi-select set), the confirm targets that | ||
| // entire set. Otherwise falls back to the single-user cursor row | ||
| // via SelectedUserStater. | ||
| func (m Model) openActionConfirm(kind UserActionKind) (tea.Model, tea.Cmd) { | ||
| child, ok := m.screens[m.active] | ||
| if !ok { | ||
| return m, toastCmdInfo("no active screen") | ||
| } | ||
| if bs, ok := child.(BulkSelectedUsersStater); ok { | ||
| if users := bs.SelectedUsers(); len(users) > 0 { | ||
| m.pendingBulkAction = pendingBulkUserAction{Kind: kind, Users: users} | ||
| m.pendingAction = pendingUserAction{} | ||
| m.pendingRule = pendingRuleAction{} | ||
| m.overlay = OverlayActionConfirm | ||
| return m, nil | ||
| } | ||
| } | ||
| stater, ok := child.(SelectedUserStater) | ||
| if !ok { | ||
| return m, toastCmdInfo("action not available on this screen") | ||
|
|
@@ -30,6 +47,7 @@ func (m Model) openActionConfirm(kind UserActionKind) (tea.Model, tea.Cmd) { | |
| return m, toastCmdInfo("no user selected") | ||
| } | ||
| m.pendingAction = pendingUserAction{Kind: kind, User: user} | ||
| m.pendingBulkAction = pendingBulkUserAction{} | ||
| m.pendingRule = pendingRuleAction{} | ||
| m.overlay = OverlayActionConfirm | ||
| return m, nil | ||
|
|
@@ -215,6 +233,106 @@ func runUserActionCmd(port domain.UsersPort, action pendingUserAction) tea.Cmd { | |
| } | ||
| } | ||
|
|
||
| // bulkSleep is overridable so tests can run the bulk runner without | ||
| // waiting on the real 50ms per step. Defaults to time.Sleep. | ||
| var bulkSleep = time.Sleep | ||
|
|
||
| // SetBulkSleepForTests overrides the inter-step sleep. Tests call it | ||
| // with a no-op to keep runs sub-millisecond; production keeps the | ||
| // 50ms throttle that spaces port calls. | ||
| func SetBulkSleepForTests(f func(time.Duration)) { bulkSleep = f } | ||
|
|
||
| // bulkUserCompletedMsg is the internal signal fired at the end of a | ||
| // bulk run. Handled by the App Shell as: emit summary toast + | ||
| // broadcast shared.ClearSelectionMsg via tea.Batch (which drops the | ||
| // active list's pinned rows). | ||
| type bulkUserCompletedMsg struct { | ||
| summary string | ||
| } | ||
|
|
||
| // runBulkUserActionCmd dispatches an entire pendingBulkUserAction | ||
| // against the UsersPort — one port call per user with a 50 ms sleep | ||
| // in between so a bulk deactivate on N users hits the API at a | ||
| // steady pace rather than a spike (MW-8). Errors are counted and | ||
| // rolled up into the summary — one failure doesn't abort the loop | ||
| // (unlike single-user runUserActionCmd which surfaces a red toast). | ||
| // | ||
| // Returns a single Cmd that runs the loop synchronously inside its | ||
| // own goroutine (Bubbletea drains Cmds off the main loop). The | ||
| // summary lands as a bulkUserCompletedMsg the App Shell expands into | ||
| // toast + selection clear. | ||
| func runBulkUserActionCmd(port domain.UsersPort, action pendingBulkUserAction) tea.Cmd { | ||
| if port == nil { | ||
| return toastCmdInfo("UsersPort not wired — action skipped") | ||
| } | ||
| total := len(action.Users) | ||
| if total == 0 { | ||
| return nil | ||
| } | ||
| return func() tea.Msg { | ||
| label := userActionLabel(action.Kind) | ||
| ctx := context.Background() | ||
| var ok, failed int | ||
| var failedLogins []string | ||
| for i, u := range action.Users { | ||
| if i > 0 { | ||
| bulkSleep(50 * time.Millisecond) | ||
| } | ||
| if err := runSingleUserAction(ctx, port, action.Kind, u.ID); err != nil { | ||
| failed++ | ||
| // Fall back to the user ID when the profile has no | ||
| // login — SCIM-provisioned edge cases surface bare | ||
| // IDs and blank names should never mean "empty". | ||
| login := u.Profile.Login | ||
| if login == "" { | ||
| login = u.ID | ||
| } | ||
| failedLogins = append(failedLogins, login) | ||
| } else { | ||
| ok++ | ||
| } | ||
| } | ||
| var summary string | ||
| if failed == 0 { | ||
| summary = label + " completed for " + strconv.Itoa(ok) + " users" | ||
| } else { | ||
| summary = label + " completed · " + strconv.Itoa(ok) + " ok, " + | ||
| strconv.Itoa(failed) + " failed (" + | ||
| strings.Join(failedLogins, ", ") + ")" | ||
| } | ||
| return bulkUserCompletedMsg{summary: summary} | ||
| } | ||
|
Comment on lines
+272
to
+304
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. When a bulk action fails for some users, the summary toast only displays the count of failed actions without indicating which users failed. Collecting the failed logins/IDs and appending them to the summary provides crucial feedback to the operator. return func() tea.Msg {
label := userActionLabel(action.Kind)
ctx := context.Background()
var ok, failed int
var failedLogins []string
for i, u := range action.Users {
if i > 0 {
bulkSleep(50 * time.Millisecond)
}
if err := runSingleUserAction(ctx, port, action.Kind, u.ID); err != nil {
failed++
login := u.Profile.Login
if login == "" {
login = u.ID
}
failedLogins = append(failedLogins, login)
} else {
ok++
}
}
var summary string
if failed == 0 {
summary = label + " completed for " + strconv.Itoa(ok) + " users"
} else {
summary = label + " completed · " + strconv.Itoa(ok) + " ok, " +
strconv.Itoa(failed) + " failed"
if len(failedLogins) > 0 {
summary += " (" + strings.Join(failedLogins, ", ") + ")"
}
}
return bulkUserCompletedMsg{summary: summary}
} |
||
| } | ||
|
|
||
| // runSingleUserAction is the shared per-user dispatcher used by both | ||
| // the single-user runUserActionCmd path and the bulk runner. Any | ||
| // lifecycle Kind that reaches the UsersPort has its route here so | ||
| // the two paths can't drift. | ||
| func runSingleUserAction(ctx context.Context, port domain.UsersPort, kind UserActionKind, id string) error { | ||
| switch kind { | ||
| case UserActionResetPassword: | ||
| _, err := port.ResetPassword(ctx, id, true) | ||
| return err | ||
| case UserActionUnlock: | ||
| return port.Unlock(ctx, id) | ||
| case UserActionResetFactors: | ||
| return port.ResetFactors(ctx, id) | ||
| case UserActionActivate: | ||
| return port.Activate(ctx, id, true) | ||
| case UserActionDeactivate: | ||
| return port.Deactivate(ctx, id, false) | ||
| case UserActionExpirePassword: | ||
| return port.ExpirePassword(ctx, id) | ||
| case UserActionDelete: | ||
| return port.Delete(ctx, id) | ||
| case UserActionSuspend: | ||
| return port.Suspend(ctx, id) | ||
| case UserActionUnsuspend: | ||
| return port.Unsuspend(ctx, id) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // runRuleActionCmd dispatches the active pendingRule against the | ||
| // GroupRulesPort and emits a toast with the result. | ||
| func runRuleActionCmd(port domain.GroupRulesPort, action pendingRuleAction) tea.Cmd { | ||
|
|
||
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.
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.
Import the
stringspackage to support joining failed logins in the bulk action summary.