actions.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { toast } from "sonner";
  2. type ActionHandler = (
  3. params: Record<string, unknown> | undefined,
  4. ) => Promise<void>;
  5. /**
  6. * Demo action handlers for the playground
  7. *
  8. * These show toast notifications to demonstrate actions work.
  9. * In a real app, these would call APIs or perform state updates.
  10. */
  11. export const actionHandlers: Record<string, ActionHandler> = {
  12. buttonClick: async (params) => {
  13. const message = (params?.message as string) || "Button clicked!";
  14. toast.success(message);
  15. },
  16. formSubmit: async (params) => {
  17. const formName = (params?.formName as string) || "Form";
  18. toast.success(`${formName} submitted successfully!`);
  19. },
  20. linkClick: async (params) => {
  21. const href = (params?.href as string) || "#";
  22. toast.info(`Navigating to: ${href}`);
  23. },
  24. };
  25. /**
  26. * Execute an action by name with the given parameters
  27. */
  28. export async function executeAction(
  29. actionName: string,
  30. params?: Record<string, unknown>,
  31. ): Promise<void> {
  32. const handler = actionHandlers[actionName];
  33. if (handler) {
  34. await handler(params);
  35. } else {
  36. // Fallback for unknown actions - just show a toast
  37. toast.info(`Action: ${actionName}`, {
  38. description: params ? JSON.stringify(params) : undefined,
  39. });
  40. }
  41. }