Skip to content
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

Add on_cancel callback for operator prompt #5348

Merged
merged 5 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 11 additions & 1 deletion app/packages/operators/src/built-in-operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@
unlisted: true,
});
}
useHooks(ctx: ExecutionContext): {} {

Check warning on line 820 in app/packages/operators/src/built-in-operators.ts

View workflow job for this annotation

GitHub Actions / lint / eslint

'ctx' is defined but never used. Allowed unused args must match /^_/u
return { updatePanelState: useUpdatePanelStatePartial() };
}
async execute(ctx: ExecutionContext): Promise<void> {
Expand Down Expand Up @@ -1037,6 +1037,7 @@
inputs.obj("params", { label: "Params" });
inputs.str("on_success", { label: "On success" });
inputs.str("on_error", { label: "On error" });
inputs.str("on_cancel", { label: "On cancel" });
inputs.bool("skip_prompt", { label: "Skip prompt", default: false });
return new types.Property(inputs);
}
Expand All @@ -1045,7 +1046,8 @@
return { triggerEvent };
}
async execute(ctx: ExecutionContext): Promise<void> {
const { params, operator_uri, on_success, on_error } = ctx.params;
const { params, operator_uri, on_success, on_error, on_cancel } =
ctx.params;
const { triggerEvent } = ctx.hooks;
const panelId = ctx.getCurrentPanelId();
const shouldPrompt = !ctx.params.skip_prompt;
Expand All @@ -1054,6 +1056,14 @@
operator: operator_uri,
params,
prompt: shouldPrompt,
onCancel: () => {
if (on_cancel) {
triggerEvent(panelId, {
operator: on_cancel,
params: { operator_uri },
});
}
},
callback: (result: OperatorResult, opts: { ctx: ExecutionContext }) => {
const ctx = opts.ctx;
if (result.error) {
Expand Down
7 changes: 6 additions & 1 deletion app/packages/operators/src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,11 @@ export const useOperatorPrompt = () => {
},
[operator, promptingOperator, cachedResolvedInput]
);
const onCancel = promptingOperator.options?.onCancel;
const cancel = () => {
ritch marked this conversation as resolved.
Show resolved Hide resolved
if (onCancel) onCancel();
close();
};
const close = () => {
setPromptingOperator(null);
setInputFields(null);
Expand Down Expand Up @@ -656,7 +661,7 @@ export const useOperatorPrompt = () => {
isExecuting,
hasResultOrError,
close,
cancel: close,
cancel,
validationErrors,
validate,
validateThrottled,
Expand Down
8 changes: 6 additions & 2 deletions app/packages/operators/src/usePanelEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type HandlerOptions = {
prompt?: boolean;
panelId: string;
callback?: ExecutionCallback;
onCancel?: () => void;
currentPanelState?: any; // most current panel state
};

Expand All @@ -20,7 +21,7 @@ export default function usePanelEvent() {
const { increment, decrement } = useActivePanelEventsCount("");
return usePanelStateByIdCallback((panelId, panelState, args) => {
const options = args[0] as HandlerOptions;
const { params, operator, prompt, currentPanelState } = options;
const { params, operator, prompt, currentPanelState, onCancel } = options;

if (!operator) {
notify({
Expand Down Expand Up @@ -49,7 +50,10 @@ export default function usePanelEvent() {
};

if (prompt) {
promptForOperator(operator, actualParams, { callback: eventCallback });
promptForOperator(operator, actualParams, {
callback: eventCallback,
onCancel,
});
} else {
increment(panelId);
executeOperator(operator, actualParams, { callback: eventCallback });
Expand Down
3 changes: 3 additions & 0 deletions fiftyone/operators/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@ def prompt(
params=None,
on_success=None,
on_error=None,
on_cancel=None,
skip_prompt=False,
):
"""Prompts the user to execute the operator with the given URI.
Expand All @@ -754,6 +755,7 @@ def prompt(
on_success (None): a callback to invoke if the user successfully
executes the operator
on_error (None): a callback to invoke if the execution fails
on_cancel (None): a callback to invoke if the user cancels the prompt
skip_prompt (False): whether to skip the prompt

Returns:
Expand All @@ -769,6 +771,7 @@ def prompt(
"params": params,
"on_success": on_success,
"on_error": on_error,
"on_cancel": on_cancel,
"skip_prompt": skip_prompt,
}
),
Expand Down
Loading