Skip to content

Commit 4c2dcb9

Browse files
authored
support showToast action via code (#1071)
1 parent c29fd56 commit 4c2dcb9

File tree

7 files changed

+49
-2
lines changed

7 files changed

+49
-2
lines changed

.changeset/light-years-do.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@ensembleui/react-framework": patch
3+
"@ensembleui/react-kitchen-sink": patch
4+
"@ensembleui/react-runtime": patch
5+
---
6+
7+
support showToast via code

apps/kitchen-sink/src/ensemble/screens/testActions.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,17 @@ View:
1010
styles:
1111
names: ${ensemble.storage.get('yyy')}
1212
children:
13+
- Button:
14+
label: show Toast
15+
onTap:
16+
showToast:
17+
message: Hello 1
18+
options:
19+
type: warning
20+
- Button:
21+
label: show Toast via code
22+
onTap: |-
23+
ensemble.showToast({ message: 'Hello 2', options: { type: 'info', position: 'topLeft' } })
1324
- Button:
1425
id: navigateScreen
1526
label: navigate screen
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./data";
22
export * from "./navigate";
33
export * from "./modal";
4+
export * from "./toast";
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { get, kebabCase } from "lodash-es";
2+
import type { ShowToastAction } from "../shared/actions";
3+
4+
export const showToast = (
5+
toastAction: ShowToastAction | undefined,
6+
toaster?: (...args: unknown[]) => void,
7+
): void => {
8+
if (!toastAction || !toaster) {
9+
return;
10+
}
11+
12+
const position = kebabCase(
13+
get(toastAction.options, "position", "bottom-right"),
14+
);
15+
const type = get(toastAction.options, "type", "success");
16+
17+
toaster(toastAction.message, {
18+
position,
19+
type,
20+
});
21+
};

packages/framework/src/hooks/useCommandCallback.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
navigateModalScreen,
1919
navigateUrl,
2020
showDialog,
21+
showToast,
2122
} from "../api";
2223
import type {
2324
EnsembleScreenModel,
@@ -27,6 +28,7 @@ import type {
2728
NavigateModalScreenAction,
2829
NavigateScreenAction,
2930
ShowDialogAction,
31+
ShowToastAction,
3032
} from "../shared";
3133
import { deviceAtom } from "./useDeviceObserver";
3234
import { createStorageApi, screenStorageAtom } from "./useEnsembleStorage";
@@ -40,6 +42,7 @@ interface CallbackContext {
4042
inputs?: { [key: string]: unknown };
4143
screen: EnsembleScreenModel;
4244
}>;
45+
toaster?: (...args: unknown[]) => void;
4346
}
4447

4548
export const useCommandCallback = <
@@ -158,6 +161,8 @@ export const useCommandCallback = <
158161
applicationContext.application ?? screenContext.app,
159162
);
160163
},
164+
showToast: (action: ShowToastAction): void =>
165+
showToast(action, callbackContext?.toaster),
161166
},
162167
context: {
163168
...customWidgets,

packages/framework/src/shared/ensemble.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Response } from "../data";
2-
import type { ShowDialogAction } from "./actions";
2+
import type { ShowDialogAction, ShowToastAction } from "./actions";
33

44
export interface EnsembleContext {
55
app: EnsembleAppConfig;
@@ -31,7 +31,7 @@ export interface EnsembleInterface {
3131
stopTimer: (timerId: string) => void;
3232
openCamera: () => void;
3333
navigateBack: () => void;
34-
showToast: (inputs: unknown) => void;
34+
showToast: (action: ShowToastAction) => void;
3535
debug: (value: unknown) => void;
3636
copyToClipboard: (value: unknown) => void;
3737
connectSocket: (name: string) => void;

packages/runtime/src/runtime/hooks/useEnsembleAction.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import {
5353
import { useState, useEffect, useMemo, useCallback, useContext } from "react";
5454
import { useLocation, useNavigate } from "react-router-dom";
5555
import { useQueryClient } from "@tanstack/react-query";
56+
import { toast } from "react-toastify";
5657
import { ModalContext } from "../modal";
5758
import { EnsembleRuntime } from "../runtime";
5859
import { getShowDialogOptions } from "../showDialog";
@@ -152,6 +153,7 @@ export const useExecuteCode: EnsembleActionHook<
152153
modalContext,
153154
render: EnsembleRuntime.render,
154155
EnsembleScreen,
156+
toaster: toast as (...args: unknown[]) => void,
155157
},
156158
);
157159

0 commit comments

Comments
 (0)