-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bulk replaying and canceling from the runs list (#1109)
* WIP on multi-select * WIP on simple checkbox * CheckboxWIthLabel and Checkbox * Multi-selection of runs across pages is working * Fix for selection on seconds page * Focus the run filter on page load * Don’t focus the checkbox * BulkActionBar now shows/hides and has buttons * Some state to stop escape clearing the selection when the modals are open * Delete unused formData util * Improvements to the page * Created the replay resource action. It doesn’t do anything useful yet. * Database schema created for BulkActionGroup/BulkActionItem * The BulkActionService is creating the right data, now we need to process it * WIP on bulk processing * Added failed state and made the sourceRun required * Bulk replaying is working * WIP on bulk action filtering * Fixed bulk filters displaying * Filtering by batch is working * Some fixes for the bulk id filtering * Style tweaks * Load the extra info in parallel * Bulk canceling working * Get the most recent 20 bulk actions to display in the filter menu * Even if the run isn’t cancelable add it to the final list * Maximum of 250 runs can be bulk actioned * Don’t let them select more than the maximum (250 currently) * Separate each bulk item action into it’s own separate graphile job to increase resiliency --------- Co-authored-by: Eric Allam <eallam@icloud.com>
- Loading branch information
1 parent
c9ebe7f
commit a5cba37
Showing
29 changed files
with
1,351 additions
and
115 deletions.
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
146 changes: 146 additions & 0 deletions
146
apps/webapp/app/components/primitives/SelectedItemsProvider.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,146 @@ | ||
"use client"; | ||
|
||
import { createContext, useCallback, useContext, useReducer } from "react"; | ||
|
||
type SelectedItemsContext = { | ||
selectedItems: Set<string>; | ||
select: (items: string | string[]) => void; | ||
deselect: (items: string | string[]) => void; | ||
toggle: (items: string | string[]) => void; | ||
deselectAll: () => void; | ||
has: (item: string) => boolean; | ||
hasAll: (items: string[]) => boolean; | ||
}; | ||
|
||
const SelectedItemsContext = createContext<SelectedItemsContext>({} as SelectedItemsContext); | ||
|
||
export function useSelectedItems(enabled = true) { | ||
const context = useContext(SelectedItemsContext); | ||
if (!context && enabled) { | ||
throw new Error("useSelectedItems must be used within a SelectedItemsProvider"); | ||
} | ||
|
||
return context; | ||
} | ||
|
||
export function SelectedItemsProvider({ | ||
initialSelectedItems, | ||
maxSelectedItemCount, | ||
children, | ||
}: { | ||
initialSelectedItems: string[]; | ||
maxSelectedItemCount?: number; | ||
children: React.ReactNode | ((context: SelectedItemsContext) => React.ReactNode); | ||
}) { | ||
const [state, dispatch] = useReducer(selectedItemsReducer, { | ||
items: new Set<string>(initialSelectedItems), | ||
maxSelectedItemCount, | ||
}); | ||
|
||
const select = useCallback((items: string | string[]) => { | ||
dispatch({ type: "select", items: Array.isArray(items) ? items : [items] }); | ||
}, []); | ||
|
||
const deselect = useCallback((items: string | string[]) => { | ||
dispatch({ type: "deselect", items: Array.isArray(items) ? items : [items] }); | ||
}, []); | ||
|
||
const toggle = useCallback((items: string | string[]) => { | ||
dispatch({ type: "toggle", items: Array.isArray(items) ? items : [items] }); | ||
}, []); | ||
|
||
const deselectAll = useCallback(() => { | ||
dispatch({ type: "deselectAll" }); | ||
}, []); | ||
|
||
const has = useCallback((item: string) => state.items.has(item), [state]); | ||
|
||
const hasAll = useCallback( | ||
(items: string[]) => items.every((item) => state.items.has(item)), | ||
[state] | ||
); | ||
|
||
return ( | ||
<SelectedItemsContext.Provider | ||
value={{ selectedItems: state.items, select, deselect, toggle, deselectAll, has, hasAll }} | ||
> | ||
{typeof children === "function" | ||
? children({ | ||
selectedItems: state.items, | ||
select, | ||
deselect, | ||
toggle, | ||
deselectAll, | ||
has, | ||
hasAll, | ||
}) | ||
: children} | ||
</SelectedItemsContext.Provider> | ||
); | ||
} | ||
|
||
type SelectItemsAction = { | ||
type: "select"; | ||
items: string[]; | ||
}; | ||
|
||
type DeSelectItemsAction = { | ||
type: "deselect"; | ||
items: string[]; | ||
}; | ||
|
||
type DeselectAllItemsAction = { | ||
type: "deselectAll"; | ||
}; | ||
|
||
type ToggleItemsAction = { | ||
type: "toggle"; | ||
items: string[]; | ||
}; | ||
|
||
type Action = SelectItemsAction | DeSelectItemsAction | ToggleItemsAction | DeselectAllItemsAction; | ||
|
||
function selectedItemsReducer( | ||
state: { items: Set<string>; maxSelectedItemCount?: number }, | ||
action: Action | ||
) { | ||
switch (action.type) { | ||
case "select": | ||
const items = new Set([...state.items, ...action.items]); | ||
return { ...state, items: cappedSet(items, state.maxSelectedItemCount) }; | ||
case "deselect": | ||
const newItems = new Set(state.items); | ||
action.items.forEach((item) => { | ||
newItems.delete(item); | ||
}); | ||
return { ...state, items: cappedSet(newItems, state.maxSelectedItemCount) }; | ||
case "toggle": | ||
let newSet = new Set(state.items); | ||
action.items.forEach((item) => { | ||
if (newSet.has(item)) { | ||
newSet.delete(item); | ||
} else { | ||
newSet.add(item); | ||
} | ||
}); | ||
return { ...state, items: cappedSet(newSet, state.maxSelectedItemCount) }; | ||
case "deselectAll": | ||
return { ...state, items: new Set<string>() }; | ||
default: | ||
return state; | ||
} | ||
} | ||
|
||
function cappedSet(set: Set<string>, max?: number) { | ||
if (!max) { | ||
return set; | ||
} | ||
|
||
if (set.size <= max) { | ||
return set; | ||
} | ||
|
||
console.warn(`Selected items exceeded the maximum count of ${max}.`); | ||
|
||
return new Set([...set].slice(0, max)); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { ArrowPathIcon, NoSymbolIcon } from "@heroicons/react/20/solid"; | ||
import { BulkActionType } from "@trigger.dev/database"; | ||
import assertNever from "assert-never"; | ||
import { cn } from "~/utils/cn"; | ||
|
||
export function BulkActionStatusCombo({ | ||
type, | ||
className, | ||
iconClassName, | ||
}: { | ||
type: BulkActionType; | ||
className?: string; | ||
iconClassName?: string; | ||
}) { | ||
return ( | ||
<span className={cn("flex items-center gap-1", className)}> | ||
<BulkActionIcon type={type} className={cn("h-4 w-4", iconClassName)} /> | ||
<BulkActionLabel type={type} /> | ||
</span> | ||
); | ||
} | ||
|
||
export function BulkActionLabel({ type }: { type: BulkActionType }) { | ||
return <span className={bulkActionClassName(type)}>{bulkActionTitle(type)}</span>; | ||
} | ||
|
||
export function BulkActionIcon({ type, className }: { type: BulkActionType; className: string }) { | ||
switch (type) { | ||
case "REPLAY": | ||
return <ArrowPathIcon className={cn(bulkActionClassName(type), className)} />; | ||
case "CANCEL": | ||
return <NoSymbolIcon className={cn(bulkActionClassName(type), className)} />; | ||
default: { | ||
assertNever(type); | ||
} | ||
} | ||
} | ||
|
||
export function bulkActionClassName(type: BulkActionType): string { | ||
switch (type) { | ||
case "REPLAY": | ||
return "text-indigo-500"; | ||
case "CANCEL": | ||
return "text-rose-500"; | ||
default: { | ||
assertNever(type); | ||
} | ||
} | ||
} | ||
|
||
export function bulkActionTitle(type: BulkActionType): string { | ||
switch (type) { | ||
case "REPLAY": | ||
return "Replay"; | ||
case "CANCEL": | ||
return "Cancel"; | ||
default: { | ||
assertNever(type); | ||
} | ||
} | ||
} | ||
|
||
export function bulkActionVerb(type: BulkActionType): string { | ||
switch (type) { | ||
case "REPLAY": | ||
return "Replaying"; | ||
case "CANCEL": | ||
return "Canceling"; | ||
default: { | ||
assertNever(type); | ||
} | ||
} | ||
} |
Oops, something went wrong.