next-action-forge 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -372,13 +372,13 @@ export function RootLayout({ children }: { children: React.ReactNode }) {
372
372
 
373
373
  // Toast messages survive redirects automatically
374
374
  export function DeleteButton({ postId }: { postId: string }) {
375
- const { execute, isExecuting } = useServerAction(deletePost, {
375
+ const { execute, isExecuting, isRedirecting } = useServerAction(deletePost, {
376
376
  showErrorToast: true, // Error toasts persist through redirects
377
377
  });
378
378
 
379
379
  return (
380
- <button onClick={() => execute({ postId })} disabled={isExecuting}>
381
- Delete Post
380
+ <button onClick={() => execute({ postId })} disabled={isExecuting || isRedirecting}>
381
+ {isRedirecting ? "Redirecting..." : isExecuting ? "Deleting..." : "Delete Post"}
382
382
  </button>
383
383
  );
384
384
  }
@@ -532,8 +532,24 @@ const myAction = clientWithErrorHandler
532
532
  ### Hooks
533
533
 
534
534
  - `useServerAction` - Execute server actions with loading state and callbacks
535
+ - Now includes `isRedirecting` state to track when redirects are in progress
535
536
  - `useOptimisticAction` - Optimistic UI updates
536
537
  - `useFormAction` - Integration with React Hook Form (works with `.formAction()` or form-compatible actions)
538
+ - Now includes `isRedirecting` state to track when redirects are in progress
539
+
540
+ **New in v0.3.2:** Both `useServerAction` and `useFormAction` now return an `isRedirecting` boolean state that becomes `true` when a redirect is about to happen. This allows you to show appropriate UI feedback during the redirect transition:
541
+
542
+ ```typescript
543
+ const { form, onSubmit, isSubmitting, isRedirecting } = useFormAction({
544
+ action: loginAction,
545
+ // ...
546
+ });
547
+
548
+ // Show different states to the user
549
+ <button disabled={isSubmitting || isRedirecting}>
550
+ {isRedirecting ? "Redirecting..." : isSubmitting ? "Logging in..." : "Login"}
551
+ </button>
552
+ ```
537
553
 
538
554
  ### Error Handling
539
555
 
@@ -17,6 +17,7 @@ interface UseServerActionReturn<TInput, TOutput> {
17
17
  execute: TInput extends void ? () => Promise<ActionResult<TOutput>> : (input: TInput) => Promise<ActionResult<TOutput>>;
18
18
  result: ActionResult<TOutput> | undefined;
19
19
  isExecuting: boolean;
20
+ isRedirecting: boolean;
20
21
  hasSucceeded: boolean;
21
22
  hasErrored: boolean;
22
23
  reset: () => void;
@@ -97,6 +98,7 @@ interface UseFormActionReturn<TFieldValues extends FieldValues, TOutput> {
97
98
  form: UseFormReturn<TFieldValues>;
98
99
  onSubmit: (e?: React.BaseSyntheticEvent) => void;
99
100
  isSubmitting: boolean;
101
+ isRedirecting: boolean;
100
102
  actionState: ServerActionResponse<TOutput>;
101
103
  reset: () => void;
102
104
  handleSubmit: (data: TFieldValues) => Promise<void>;
@@ -17,6 +17,7 @@ interface UseServerActionReturn<TInput, TOutput> {
17
17
  execute: TInput extends void ? () => Promise<ActionResult<TOutput>> : (input: TInput) => Promise<ActionResult<TOutput>>;
18
18
  result: ActionResult<TOutput> | undefined;
19
19
  isExecuting: boolean;
20
+ isRedirecting: boolean;
20
21
  hasSucceeded: boolean;
21
22
  hasErrored: boolean;
22
23
  reset: () => void;
@@ -97,6 +98,7 @@ interface UseFormActionReturn<TFieldValues extends FieldValues, TOutput> {
97
98
  form: UseFormReturn<TFieldValues>;
98
99
  onSubmit: (e?: React.BaseSyntheticEvent) => void;
99
100
  isSubmitting: boolean;
101
+ isRedirecting: boolean;
100
102
  actionState: ServerActionResponse<TOutput>;
101
103
  reset: () => void;
102
104
  handleSubmit: (data: TFieldValues) => Promise<void>;
@@ -79,6 +79,7 @@ function useServerAction(action, options = {}) {
79
79
  const router = (0, import_navigation.useRouter)();
80
80
  const [result, setResult] = (0, import_react.useState)();
81
81
  const [isExecuting, setIsExecuting] = (0, import_react.useState)(false);
82
+ const [isRedirecting, setIsRedirecting] = (0, import_react.useState)(false);
82
83
  const [, startTransition] = (0, import_react.useTransition)();
83
84
  const {
84
85
  onSuccess,
@@ -95,6 +96,7 @@ function useServerAction(action, options = {}) {
95
96
  const hasErrored = !!result?.serverError || !!result?.validationErrors || !!result?.fetchError;
96
97
  const reset = (0, import_react.useCallback)(() => {
97
98
  setResult(void 0);
99
+ setIsRedirecting(false);
98
100
  }, []);
99
101
  const execute = (0, import_react.useCallback)(
100
102
  async (input) => {
@@ -114,6 +116,7 @@ function useServerAction(action, options = {}) {
114
116
  }
115
117
  await onSuccess?.(response.data);
116
118
  if (response.redirect && !preventRedirect) {
119
+ setIsRedirecting(true);
117
120
  const redirectConfig = typeof response.redirect === "string" ? { url: response.redirect } : response.redirect;
118
121
  const delay = redirectConfig.delay ?? redirectDelay;
119
122
  setTimeout(() => {
@@ -122,6 +125,7 @@ function useServerAction(action, options = {}) {
122
125
  } else {
123
126
  router.push(redirectConfig.url);
124
127
  }
128
+ setIsRedirecting(false);
125
129
  }, delay);
126
130
  }
127
131
  } else if (!response.success && response.error) {
@@ -158,6 +162,7 @@ function useServerAction(action, options = {}) {
158
162
  }
159
163
  import_sonner.toast.error(message, { persistent: true });
160
164
  }
165
+ setIsRedirecting(true);
161
166
  router.push(response.error.redirectTo || "/login");
162
167
  return actionResult;
163
168
  }
@@ -204,6 +209,7 @@ function useServerAction(action, options = {}) {
204
209
  execute: executeWithTransition,
205
210
  result,
206
211
  isExecuting,
212
+ isRedirecting,
207
213
  hasSucceeded,
208
214
  hasErrored,
209
215
  reset
@@ -289,6 +295,17 @@ function useFormAction({
289
295
  redirectDelay = 0
290
296
  }) {
291
297
  const router = (0, import_navigation2.useRouter)();
298
+ const [isRedirecting, setIsRedirecting] = (0, import_react3.useState)(false);
299
+ const onSuccessRef = (0, import_react3.useRef)(onSuccess);
300
+ const onErrorRef = (0, import_react3.useRef)(onError);
301
+ const showSuccessToastRef = (0, import_react3.useRef)(showSuccessToast);
302
+ const showErrorToastRef = (0, import_react3.useRef)(showErrorToast);
303
+ (0, import_react3.useEffect)(() => {
304
+ onSuccessRef.current = onSuccess;
305
+ onErrorRef.current = onError;
306
+ showSuccessToastRef.current = showSuccessToast;
307
+ showErrorToastRef.current = showErrorToast;
308
+ });
292
309
  const form = (0, import_react_hook_form.useForm)({
293
310
  resolver: schema ? (0, import_zod.zodResolver)(schema) : void 0,
294
311
  defaultValues,
@@ -298,6 +315,11 @@ function useFormAction({
298
315
  const [actionState, formAction, isPending] = (0, import_react3.useActionState)(action, initialState);
299
316
  const [isTransitioning, startTransition] = (0, import_react3.useTransition)();
300
317
  (0, import_react3.useEffect)(() => {
318
+ if (actionState.success === true && actionState.data === void 0) {
319
+ return;
320
+ }
321
+ setIsRedirecting(false);
322
+ let timeoutId;
301
323
  if (!isSuccessResponse(actionState) && actionState.error) {
302
324
  const { error } = actionState;
303
325
  if (error.fields) {
@@ -317,8 +339,9 @@ function useFormAction({
317
339
  });
318
340
  }
319
341
  if (error.message && !error.field && !error.fields) {
320
- if (showErrorToast) {
321
- const message = typeof showErrorToast === "function" ? showErrorToast(error) : typeof showErrorToast === "string" ? showErrorToast : error.message;
342
+ const showToast = showErrorToastRef.current;
343
+ if (showToast) {
344
+ const message = typeof showToast === "function" ? showToast(error) : typeof showToast === "string" ? showToast : error.message;
322
345
  import_sonner2.toast.error(message);
323
346
  }
324
347
  form.setError("root", {
@@ -326,30 +349,38 @@ function useFormAction({
326
349
  message: error.message
327
350
  });
328
351
  }
329
- onError?.(error);
352
+ onErrorRef.current?.(error);
330
353
  }
331
354
  if (isSuccessResponse(actionState) && actionState.data !== void 0) {
332
- if (showSuccessToast) {
333
- const message = typeof showSuccessToast === "function" ? showSuccessToast(actionState.data) : typeof showSuccessToast === "string" ? showSuccessToast : "Success!";
355
+ const showToast = showSuccessToastRef.current;
356
+ if (showToast) {
357
+ const message = typeof showToast === "function" ? showToast(actionState.data) : typeof showToast === "string" ? showToast : "Success!";
334
358
  import_sonner2.toast.success(message);
335
359
  }
336
360
  if (resetOnSuccess) {
337
361
  form.reset();
338
362
  }
339
- onSuccess?.(actionState.data);
363
+ onSuccessRef.current?.(actionState.data);
340
364
  if (actionState.redirect && !preventRedirect) {
365
+ setIsRedirecting(true);
341
366
  const redirectConfig = typeof actionState.redirect === "string" ? { url: actionState.redirect } : actionState.redirect;
342
367
  const delay = redirectConfig.delay ?? redirectDelay;
343
- setTimeout(() => {
368
+ timeoutId = setTimeout(() => {
344
369
  if (redirectConfig.replace) {
345
370
  router.replace(redirectConfig.url);
346
371
  } else {
347
372
  router.push(redirectConfig.url);
348
373
  }
349
- }, delay);
374
+ setIsRedirecting(false);
375
+ }, delay || 100);
350
376
  }
351
377
  }
352
- }, [actionState, form, onError, onSuccess, resetOnSuccess, showErrorToast, showSuccessToast, preventRedirect, redirectDelay, router]);
378
+ return () => {
379
+ if (timeoutId) {
380
+ clearTimeout(timeoutId);
381
+ }
382
+ };
383
+ }, [actionState]);
353
384
  const handleSubmit = async (data) => {
354
385
  form.clearErrors();
355
386
  const formData = transformData ? transformData(data) : objectToFormData(data);
@@ -366,6 +397,7 @@ function useFormAction({
366
397
  form,
367
398
  onSubmit,
368
399
  isSubmitting,
400
+ isRedirecting,
369
401
  actionState,
370
402
  reset: form.reset,
371
403
  handleSubmit
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/index.ts","../../src/hooks/use-server-action.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/hooks/use-optimistic-action.ts","../../src/hooks/use-form-action.ts","../../src/hooks/toast-restorer.tsx"],"sourcesContent":["export * from \"./use-server-action\";\nexport * from \"./use-optimistic-action\";\nexport * from \"./use-form-action\";\nexport { ToastRestorer } from \"./toast-restorer\";","import { useCallback, useState, useTransition } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport { toast } from \"sonner\";\nimport type { ServerAction, ActionResult, ServerActionResponse } from \"../types\";\nimport { isNextNavigationError } from \"../next/errors\";\n\nexport interface UseServerActionOptions<TOutput> {\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ActionResult<TOutput>) => void | Promise<void>;\n \n successMessage?: string | ((data: TOutput) => string);\n errorMessage?: string | ((error: ActionResult<TOutput>) => string);\n showSuccessToast?: boolean;\n showErrorToast?: boolean;\n \n redirectOnAuthError?: boolean;\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseServerActionReturn<TInput, TOutput> {\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n result: ActionResult<TOutput> | undefined;\n isExecuting: boolean;\n hasSucceeded: boolean;\n hasErrored: boolean;\n \n reset: () => void;\n}\n\n// Overload for void actions\nexport function useServerAction<TOutput>(\n action: ServerAction<void, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<void, TOutput>;\n\n// Overload for actions with input\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<TInput, TOutput>;\n\n// Implementation\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options: UseServerActionOptions<TOutput> = {}\n): UseServerActionReturn<TInput, TOutput> {\n const router = useRouter();\n const [result, setResult] = useState<ActionResult<TOutput>>();\n const [isExecuting, setIsExecuting] = useState(false);\n const [, startTransition] = useTransition();\n \n const {\n onSuccess,\n onError,\n successMessage,\n errorMessage,\n showSuccessToast = false,\n showErrorToast = true,\n redirectOnAuthError = true,\n preventRedirect = false,\n redirectDelay = 0,\n } = options;\n \n const hasSucceeded = !!result?.data;\n const hasErrored = !!result?.serverError || !!result?.validationErrors || !!result?.fetchError;\n \n const reset = useCallback(() => {\n setResult(undefined);\n }, []);\n \n const execute = useCallback(\n async (input?: TInput): Promise<ActionResult<TOutput>> => {\n setIsExecuting(true);\n \n try {\n const response = await (input === undefined \n ? (action as () => Promise<ServerActionResponse<TOutput>>)()\n : (action as (input: TInput) => Promise<ServerActionResponse<TOutput>>)(input));\n \n let actionResult: ActionResult<TOutput>;\n \n if (response.success) {\n actionResult = { data: response.data };\n \n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Success response:', response);\n console.log('[useServerAction] Has redirect?', !!response.redirect);\n }\n \n if (showSuccessToast && successMessage) {\n const message = typeof successMessage === \"function\" \n ? successMessage(response.data) \n : successMessage;\n toast.success(message);\n }\n \n await onSuccess?.(response.data);\n \n // Handle redirect if present in response\n if (response.redirect && !preventRedirect) {\n const redirectConfig = typeof response.redirect === 'string'\n ? { url: response.redirect }\n : response.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n }, delay);\n }\n \n } else if (!response.success && response.error) {\n actionResult = {\n serverError: response.error,\n validationErrors: response.error.fields,\n };\n \n // Debug logging\n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Error response:', response.error);\n console.log('[useServerAction] Should redirect?', response.error.shouldRedirect);\n console.log('[useServerAction] Redirect to:', response.error.redirectTo);\n }\n \n if (redirectOnAuthError && response.error.shouldRedirect) {\n // Handle toast before redirect\n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n \n // Always save to localStorage for redirect cases\n // This ensures compatibility with both current and future sonner versions\n if (typeof window !== 'undefined') {\n try {\n const storageKey = 'sonner-toasts';\n const existingToasts = JSON.parse(\n localStorage.getItem(storageKey) || '[]'\n );\n \n // Add our toast in the same format as the PR\n const persistentToast = {\n id: Date.now(),\n type: 'error',\n message,\n persistent: true,\n createdAt: Date.now()\n };\n \n existingToasts.push(persistentToast);\n localStorage.setItem(storageKey, JSON.stringify(existingToasts));\n } catch (err) {\n console.error('Failed to persist toast:', err);\n }\n }\n \n // Always show the toast (with persistent if supported)\n (toast.error as any)(message, { persistent: true });\n }\n \n router.push(response.error.redirectTo || \"/login\");\n return actionResult;\n }\n \n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n toast.error(message);\n }\n \n await onError?.(actionResult);\n } else {\n actionResult = { fetchError: \"Unexpected response format\" };\n }\n \n setResult(actionResult);\n return actionResult;\n \n } catch (error) {\n // Check if this is a Next.js navigation error that should be re-thrown\n if (isNextNavigationError(error)) {\n // Re-throw navigation errors to let Next.js handle them\n throw error;\n }\n \n const fetchError = error instanceof Error ? error.message : \"Network error\";\n const actionResult: ActionResult<TOutput> = { fetchError };\n \n setResult(actionResult);\n \n if (showErrorToast) {\n toast.error(fetchError);\n }\n \n await onError?.(actionResult);\n return actionResult;\n } finally {\n setIsExecuting(false);\n }\n },\n [action, router, showSuccessToast, successMessage, showErrorToast, errorMessage, redirectOnAuthError, onSuccess, onError]\n );\n \n const executeWithTransition = useCallback(\n (...args: TInput extends void ? [] : [input: TInput]): Promise<ActionResult<TOutput>> => {\n return new Promise((resolve) => {\n startTransition(async () => {\n const result = await execute(args[0] as TInput);\n resolve(result);\n });\n });\n },\n [execute]\n ) as TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n return {\n execute: executeWithTransition,\n result,\n isExecuting,\n hasSucceeded,\n hasErrored,\n reset,\n };\n}\n\n","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import { useOptimistic, useTransition } from \"react\";\nimport { useServerAction, type UseServerActionOptions } from \"./use-server-action\";\nimport type { ServerAction, ActionResult } from \"../types\";\n\nexport interface UseOptimisticActionOptions<TInput, TOutput, TOptimistic> extends UseServerActionOptions<TOutput> {\n /**\n * Function to update the optimistic state\n */\n updateFn: TInput extends void \n ? (current: TOptimistic) => TOptimistic\n : (current: TOptimistic, input: TInput) => TOptimistic;\n}\n\nexport interface UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n /**\n * The optimistic state\n */\n optimisticState: TOptimistic;\n \n /**\n * Execute the action with optimistic update\n */\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n /**\n * Loading state\n */\n isExecuting: boolean;\n \n /**\n * Success state\n */\n hasSucceeded: boolean;\n \n /**\n * Error state\n */\n hasErrored: boolean;\n \n /**\n * Last result\n */\n result: ActionResult<TOutput> | undefined;\n \n /**\n * Reset state\n */\n reset: () => void;\n}\n\n/**\n * Hook for server actions with optimistic updates\n * \n * @example\n * ```typescript\n * const { optimisticState, execute } = useOptimisticAction(\n * currentTodos,\n * addTodoAction,\n * {\n * updateFn: (todos, newTodo) => [...todos, newTodo],\n * onSuccess: (savedTodo) => {\n * // Update with server response if needed\n * }\n * }\n * );\n * ```\n */\nexport function useOptimisticAction<TInput, TOutput, TOptimistic>(\n initialState: TOptimistic,\n action: ServerAction<TInput, TOutput>,\n options: UseOptimisticActionOptions<TInput, TOutput, TOptimistic>\n): UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n const [optimisticState, addOptimisticUpdate] = useOptimistic(\n initialState,\n options.updateFn\n );\n \n const [, startTransition] = useTransition();\n \n // Extract updateFn from options to pass the rest to useServerAction\n const { updateFn: _, ...serverActionOptions } = options;\n \n const {\n execute: executeAction,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n } = useServerAction(action, serverActionOptions);\n \n const execute = (async (input?: TInput): Promise<ActionResult<TOutput>> => {\n // Apply optimistic update\n startTransition(() => {\n if (input !== undefined) {\n addOptimisticUpdate(input);\n } else {\n addOptimisticUpdate(undefined as any);\n }\n });\n \n // Execute the server action\n return executeAction(input as any);\n }) as any;\n \n return {\n optimisticState,\n execute,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n };\n}","import { useActionState, useEffect, useTransition } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport type { UseFormProps, UseFormReturn, Path, FieldValues, DefaultValues } from \"react-hook-form\";\nimport { useForm } from \"react-hook-form\";\nimport { zodResolver } from \"@hookform/resolvers/zod\";\nimport { toast } from \"sonner\";\nimport type { z } from \"zod\";\nimport type { ServerActionResponse, ServerActionError, RedirectConfig } from \"../types\";\n\nexport interface UseFormActionOptions<TFieldValues extends FieldValues, TOutput> {\n // Required - the server action to execute (form action format)\n action: (prevState: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>>;\n \n // Optional Zod schema for validation\n schema?: z.ZodTypeAny;\n \n // React Hook Form options\n defaultValues?: DefaultValues<TFieldValues>;\n mode?: UseFormProps<TFieldValues>[\"mode\"];\n \n // Transform function if you need custom FormData creation\n transformData?: (data: TFieldValues) => FormData;\n \n // Callbacks\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ServerActionError) => void;\n \n // Behavior options\n resetOnSuccess?: boolean;\n showSuccessToast?: boolean | string | ((data: TOutput) => string);\n showErrorToast?: boolean | string | ((error: ServerActionError) => string);\n \n // Redirect options (same as useServerAction)\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseFormActionReturn<TFieldValues extends FieldValues, TOutput> {\n // React Hook Form instance\n form: UseFormReturn<TFieldValues>;\n \n // Submit handler (pre-bound with handleSubmit)\n onSubmit: (e?: React.BaseSyntheticEvent) => void;\n \n // Loading state (combines isPending and isTransitioning)\n isSubmitting: boolean;\n \n // The current action state\n actionState: ServerActionResponse<TOutput>;\n \n // Utilities\n reset: () => void;\n \n // Raw handlers if needed\n handleSubmit: (data: TFieldValues) => Promise<void>;\n}\n\nfunction isSuccessResponse<T>(response: ServerActionResponse<T>): response is { success: true; data: T; redirect?: string | RedirectConfig } {\n return response.success === true;\n}\n\nfunction objectToFormData(obj: any): FormData {\n const formData = new FormData();\n \n Object.entries(obj).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n if (value instanceof File || value instanceof Blob) {\n formData.append(key, value);\n } else if (Array.isArray(value)) {\n value.forEach((item) => formData.append(`${key}[]`, String(item)));\n } else if (typeof value === \"object\") {\n formData.append(key, JSON.stringify(value));\n } else {\n formData.append(key, String(value));\n }\n }\n });\n \n return formData;\n}\n\nexport function useFormAction<TFieldValues extends FieldValues = FieldValues, TOutput = void>({\n action,\n schema,\n defaultValues,\n mode = \"onChange\",\n transformData,\n onSuccess,\n onError,\n resetOnSuccess = false,\n showSuccessToast = false,\n showErrorToast = true,\n preventRedirect = false,\n redirectDelay = 0,\n}: UseFormActionOptions<TFieldValues, TOutput>): UseFormActionReturn<TFieldValues, TOutput> {\n const router = useRouter();\n // 1. Setup form with React Hook Form\n const form = useForm<TFieldValues>({\n resolver: schema ? (zodResolver as any)(schema) : undefined,\n defaultValues,\n mode,\n } as UseFormProps<TFieldValues>);\n \n // 2. Setup server action state with useActionState\n const initialState: ServerActionResponse<TOutput> = { success: true, data: undefined as TOutput };\n const [actionState, formAction, isPending] = useActionState(action, initialState);\n const [isTransitioning, startTransition] = useTransition();\n \n // 3. Handle server errors and success\n useEffect(() => {\n if (!isSuccessResponse(actionState) && actionState.error) {\n const { error } = actionState;\n \n // Map field errors to form\n if (error.fields) {\n Object.entries(error.fields).forEach(([field, messages]) => {\n if (Array.isArray(messages) && messages.length > 0) {\n form.setError(field as Path<TFieldValues>, {\n type: \"server\",\n message: messages[0],\n });\n }\n });\n }\n \n // Single field error\n if (error.field && error.message && !error.fields) {\n form.setError(error.field as Path<TFieldValues>, {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Global error (no specific field)\n if (error.message && !error.field && !error.fields) {\n if (showErrorToast) {\n const message = typeof showErrorToast === \"function\" \n ? showErrorToast(error)\n : typeof showErrorToast === \"string\"\n ? showErrorToast\n : error.message;\n toast.error(message);\n }\n \n // Also set on root for inline display\n form.setError(\"root\", {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Call error callback\n onError?.(error);\n }\n \n // Handle success\n if (isSuccessResponse(actionState) && actionState.data !== undefined) {\n if (showSuccessToast) {\n const message = typeof showSuccessToast === \"function\"\n ? showSuccessToast(actionState.data)\n : typeof showSuccessToast === \"string\"\n ? showSuccessToast\n : \"Success!\";\n toast.success(message);\n }\n \n if (resetOnSuccess) {\n form.reset();\n }\n \n // Call success callback\n onSuccess?.(actionState.data);\n \n // Handle redirect if present in response\n if (actionState.redirect && !preventRedirect) {\n const redirectConfig = typeof actionState.redirect === 'string'\n ? { url: actionState.redirect }\n : actionState.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n }, delay);\n }\n }\n }, [actionState, form, onError, onSuccess, resetOnSuccess, showErrorToast, showSuccessToast, preventRedirect, redirectDelay, router]);\n \n // 4. Submit handler\n const handleSubmit = async (data: TFieldValues): Promise<void> => {\n // Clear any previous errors\n form.clearErrors();\n \n // Transform to FormData\n const formData = transformData \n ? transformData(data)\n : objectToFormData(data);\n \n // Execute with transition for better UX\n startTransition(() => {\n formAction(formData);\n });\n };\n \n // 5. Combined loading state\n const isSubmitting = isPending || isTransitioning;\n \n // 6. Pre-bound submit handler\n const onSubmit = (e?: React.BaseSyntheticEvent): void => {\n e?.preventDefault();\n void form.handleSubmit(handleSubmit)(e!);\n };\n \n return {\n form,\n onSubmit,\n isSubmitting,\n actionState,\n reset: form.reset,\n handleSubmit,\n };\n}","\"use client\";\n\nimport { useEffect } from \"react\";\nimport { toast } from \"sonner\";\n\nexport function ToastRestorer() {\n useEffect(() => {\n // Only run on client side\n if (typeof window === \"undefined\") return;\n \n const storageKey = \"sonner-toasts\";\n \n try {\n const storedToasts = localStorage.getItem(storageKey);\n \n if (storedToasts) {\n const persistentToasts = JSON.parse(storedToasts);\n \n if (Array.isArray(persistentToasts) && persistentToasts.length > 0) {\n // Clear the storage immediately to prevent duplicate toasts\n localStorage.removeItem(storageKey);\n \n // Filter out old toasts (older than 30 seconds)\n const recentToasts = persistentToasts.filter(\n (t: any) => Date.now() - (t.createdAt || 0) < 30000\n );\n \n // Restore toasts with staggered animation like in the PR\n recentToasts.forEach((persistedToast: any, index: number) => {\n // Skip loading toasts as per the PR implementation\n if (persistedToast.type === \"loading\") return;\n \n setTimeout(() => {\n const toastFunction = toast[persistedToast.type as keyof typeof toast];\n \n if (typeof toastFunction === \"function\") {\n // Check if current sonner supports persistent\n if (toastFunction.length >= 2) {\n // Use persistent if available\n (toastFunction as any)(persistedToast.message, { \n persistent: true,\n id: persistedToast.id \n });\n } else {\n // Fallback to normal toast\n toastFunction(persistedToast.message);\n }\n }\n }, index * 150); // Staggered delay like in the PR\n });\n }\n }\n } catch (error) {\n console.error(\"Failed to restore toasts:\", error);\n // Clean up on error\n localStorage.removeItem(storageKey);\n }\n }, []);\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AAK,aAAa,YAAY,EAAE,CAAE;AAAA,EACzC;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA,OAAO,KAAK;AAAA,IACZ;AAAA,EACF;AACF;;;AC/NA,IAAAC,gBAA0B;AAC1B,IAAAC,iBAAsB;AAEf,SAAS,gBAAgB;AAC9B,+BAAU,MAAM;AAEd,QAAI,OAAO,WAAW,YAAa;AAEnC,UAAM,aAAa;AAEnB,QAAI;AACF,YAAM,eAAe,aAAa,QAAQ,UAAU;AAEpD,UAAI,cAAc;AAChB,cAAM,mBAAmB,KAAK,MAAM,YAAY;AAEhD,YAAI,MAAM,QAAQ,gBAAgB,KAAK,iBAAiB,SAAS,GAAG;AAElE,uBAAa,WAAW,UAAU;AAGlC,gBAAM,eAAe,iBAAiB;AAAA,YACpC,CAAC,MAAW,KAAK,IAAI,KAAK,EAAE,aAAa,KAAK;AAAA,UAChD;AAGA,uBAAa,QAAQ,CAAC,gBAAqB,UAAkB;AAE3D,gBAAI,eAAe,SAAS,UAAW;AAEvC,uBAAW,MAAM;AACf,oBAAM,gBAAgB,qBAAM,eAAe,IAA0B;AAErE,kBAAI,OAAO,kBAAkB,YAAY;AAEvC,oBAAI,cAAc,UAAU,GAAG;AAE7B,kBAAC,cAAsB,eAAe,SAAS;AAAA,oBAC7C,YAAY;AAAA,oBACZ,IAAI,eAAe;AAAA,kBACrB,CAAC;AAAA,gBACH,OAAO;AAEL,gCAAc,eAAe,OAAO;AAAA,gBACtC;AAAA,cACF;AAAA,YACF,GAAG,QAAQ,GAAG;AAAA,UAChB,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,6BAA6B,KAAK;AAEhD,mBAAa,WAAW,UAAU;AAAA,IACpC;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":["RedirectStatusCode","result","import_react","import_react","import_navigation","import_sonner","import_react","import_sonner"]}
1
+ {"version":3,"sources":["../../src/hooks/index.ts","../../src/hooks/use-server-action.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/hooks/use-optimistic-action.ts","../../src/hooks/use-form-action.ts","../../src/hooks/toast-restorer.tsx"],"sourcesContent":["export * from \"./use-server-action\";\nexport * from \"./use-optimistic-action\";\nexport * from \"./use-form-action\";\nexport { ToastRestorer } from \"./toast-restorer\";","import { useCallback, useState, useTransition } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport { toast } from \"sonner\";\nimport type { ServerAction, ActionResult, ServerActionResponse } from \"../types\";\nimport { isNextNavigationError } from \"../next/errors\";\n\nexport interface UseServerActionOptions<TOutput> {\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ActionResult<TOutput>) => void | Promise<void>;\n \n successMessage?: string | ((data: TOutput) => string);\n errorMessage?: string | ((error: ActionResult<TOutput>) => string);\n showSuccessToast?: boolean;\n showErrorToast?: boolean;\n \n redirectOnAuthError?: boolean;\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseServerActionReturn<TInput, TOutput> {\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n result: ActionResult<TOutput> | undefined;\n isExecuting: boolean;\n isRedirecting: boolean;\n hasSucceeded: boolean;\n hasErrored: boolean;\n \n reset: () => void;\n}\n\n// Overload for void actions\nexport function useServerAction<TOutput>(\n action: ServerAction<void, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<void, TOutput>;\n\n// Overload for actions with input\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<TInput, TOutput>;\n\n// Implementation\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options: UseServerActionOptions<TOutput> = {}\n): UseServerActionReturn<TInput, TOutput> {\n const router = useRouter();\n const [result, setResult] = useState<ActionResult<TOutput>>();\n const [isExecuting, setIsExecuting] = useState(false);\n const [isRedirecting, setIsRedirecting] = useState(false);\n const [, startTransition] = useTransition();\n \n const {\n onSuccess,\n onError,\n successMessage,\n errorMessage,\n showSuccessToast = false,\n showErrorToast = true,\n redirectOnAuthError = true,\n preventRedirect = false,\n redirectDelay = 0,\n } = options;\n \n const hasSucceeded = !!result?.data;\n const hasErrored = !!result?.serverError || !!result?.validationErrors || !!result?.fetchError;\n \n const reset = useCallback(() => {\n setResult(undefined);\n setIsRedirecting(false);\n }, []);\n \n const execute = useCallback(\n async (input?: TInput): Promise<ActionResult<TOutput>> => {\n setIsExecuting(true);\n \n try {\n const response = await (input === undefined \n ? (action as () => Promise<ServerActionResponse<TOutput>>)()\n : (action as (input: TInput) => Promise<ServerActionResponse<TOutput>>)(input));\n \n let actionResult: ActionResult<TOutput>;\n \n if (response.success) {\n actionResult = { data: response.data };\n \n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Success response:', response);\n console.log('[useServerAction] Has redirect?', !!response.redirect);\n }\n \n if (showSuccessToast && successMessage) {\n const message = typeof successMessage === \"function\" \n ? successMessage(response.data) \n : successMessage;\n toast.success(message);\n }\n \n await onSuccess?.(response.data);\n \n // Handle redirect if present in response\n if (response.redirect && !preventRedirect) {\n setIsRedirecting(true);\n const redirectConfig = typeof response.redirect === 'string'\n ? { url: response.redirect }\n : response.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n setIsRedirecting(false);\n }, delay);\n }\n \n } else if (!response.success && response.error) {\n actionResult = {\n serverError: response.error,\n validationErrors: response.error.fields,\n };\n \n // Debug logging\n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Error response:', response.error);\n console.log('[useServerAction] Should redirect?', response.error.shouldRedirect);\n console.log('[useServerAction] Redirect to:', response.error.redirectTo);\n }\n \n if (redirectOnAuthError && response.error.shouldRedirect) {\n // Handle toast before redirect\n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n \n // Always save to localStorage for redirect cases\n // This ensures compatibility with both current and future sonner versions\n if (typeof window !== 'undefined') {\n try {\n const storageKey = 'sonner-toasts';\n const existingToasts = JSON.parse(\n localStorage.getItem(storageKey) || '[]'\n );\n \n // Add our toast in the same format as the PR\n const persistentToast = {\n id: Date.now(),\n type: 'error',\n message,\n persistent: true,\n createdAt: Date.now()\n };\n \n existingToasts.push(persistentToast);\n localStorage.setItem(storageKey, JSON.stringify(existingToasts));\n } catch (err) {\n console.error('Failed to persist toast:', err);\n }\n }\n \n // Always show the toast (with persistent if supported)\n (toast.error as any)(message, { persistent: true });\n }\n \n setIsRedirecting(true);\n router.push(response.error.redirectTo || \"/login\");\n return actionResult;\n }\n \n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n toast.error(message);\n }\n \n await onError?.(actionResult);\n } else {\n actionResult = { fetchError: \"Unexpected response format\" };\n }\n \n setResult(actionResult);\n return actionResult;\n \n } catch (error) {\n // Check if this is a Next.js navigation error that should be re-thrown\n if (isNextNavigationError(error)) {\n // Re-throw navigation errors to let Next.js handle them\n throw error;\n }\n \n const fetchError = error instanceof Error ? error.message : \"Network error\";\n const actionResult: ActionResult<TOutput> = { fetchError };\n \n setResult(actionResult);\n \n if (showErrorToast) {\n toast.error(fetchError);\n }\n \n await onError?.(actionResult);\n return actionResult;\n } finally {\n setIsExecuting(false);\n }\n },\n [action, router, showSuccessToast, successMessage, showErrorToast, errorMessage, redirectOnAuthError, onSuccess, onError]\n );\n \n const executeWithTransition = useCallback(\n (...args: TInput extends void ? [] : [input: TInput]): Promise<ActionResult<TOutput>> => {\n return new Promise((resolve) => {\n startTransition(async () => {\n const result = await execute(args[0] as TInput);\n resolve(result);\n });\n });\n },\n [execute]\n ) as TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n return {\n execute: executeWithTransition,\n result,\n isExecuting,\n isRedirecting,\n hasSucceeded,\n hasErrored,\n reset,\n };\n}\n\n","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import { useOptimistic, useTransition } from \"react\";\nimport { useServerAction, type UseServerActionOptions } from \"./use-server-action\";\nimport type { ServerAction, ActionResult } from \"../types\";\n\nexport interface UseOptimisticActionOptions<TInput, TOutput, TOptimistic> extends UseServerActionOptions<TOutput> {\n /**\n * Function to update the optimistic state\n */\n updateFn: TInput extends void \n ? (current: TOptimistic) => TOptimistic\n : (current: TOptimistic, input: TInput) => TOptimistic;\n}\n\nexport interface UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n /**\n * The optimistic state\n */\n optimisticState: TOptimistic;\n \n /**\n * Execute the action with optimistic update\n */\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n /**\n * Loading state\n */\n isExecuting: boolean;\n \n /**\n * Success state\n */\n hasSucceeded: boolean;\n \n /**\n * Error state\n */\n hasErrored: boolean;\n \n /**\n * Last result\n */\n result: ActionResult<TOutput> | undefined;\n \n /**\n * Reset state\n */\n reset: () => void;\n}\n\n/**\n * Hook for server actions with optimistic updates\n * \n * @example\n * ```typescript\n * const { optimisticState, execute } = useOptimisticAction(\n * currentTodos,\n * addTodoAction,\n * {\n * updateFn: (todos, newTodo) => [...todos, newTodo],\n * onSuccess: (savedTodo) => {\n * // Update with server response if needed\n * }\n * }\n * );\n * ```\n */\nexport function useOptimisticAction<TInput, TOutput, TOptimistic>(\n initialState: TOptimistic,\n action: ServerAction<TInput, TOutput>,\n options: UseOptimisticActionOptions<TInput, TOutput, TOptimistic>\n): UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n const [optimisticState, addOptimisticUpdate] = useOptimistic(\n initialState,\n options.updateFn\n );\n \n const [, startTransition] = useTransition();\n \n // Extract updateFn from options to pass the rest to useServerAction\n const { updateFn: _, ...serverActionOptions } = options;\n \n const {\n execute: executeAction,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n } = useServerAction(action, serverActionOptions);\n \n const execute = (async (input?: TInput): Promise<ActionResult<TOutput>> => {\n // Apply optimistic update\n startTransition(() => {\n if (input !== undefined) {\n addOptimisticUpdate(input);\n } else {\n addOptimisticUpdate(undefined as any);\n }\n });\n \n // Execute the server action\n return executeAction(input as any);\n }) as any;\n \n return {\n optimisticState,\n execute,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n };\n}","import { useActionState, useEffect, useTransition, useRef, useState } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport type { UseFormProps, UseFormReturn, Path, FieldValues, DefaultValues } from \"react-hook-form\";\nimport { useForm } from \"react-hook-form\";\nimport { zodResolver } from \"@hookform/resolvers/zod\";\nimport { toast } from \"sonner\";\nimport type { z } from \"zod\";\nimport type { ServerActionResponse, ServerActionError, RedirectConfig } from \"../types\";\n\nexport interface UseFormActionOptions<TFieldValues extends FieldValues, TOutput> {\n // Required - the server action to execute (form action format)\n action: (prevState: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>>;\n \n // Optional Zod schema for validation\n schema?: z.ZodTypeAny;\n \n // React Hook Form options\n defaultValues?: DefaultValues<TFieldValues>;\n mode?: UseFormProps<TFieldValues>[\"mode\"];\n \n // Transform function if you need custom FormData creation\n transformData?: (data: TFieldValues) => FormData;\n \n // Callbacks\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ServerActionError) => void;\n \n // Behavior options\n resetOnSuccess?: boolean;\n showSuccessToast?: boolean | string | ((data: TOutput) => string);\n showErrorToast?: boolean | string | ((error: ServerActionError) => string);\n \n // Redirect options (same as useServerAction)\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseFormActionReturn<TFieldValues extends FieldValues, TOutput> {\n // React Hook Form instance\n form: UseFormReturn<TFieldValues>;\n \n // Submit handler (pre-bound with handleSubmit)\n onSubmit: (e?: React.BaseSyntheticEvent) => void;\n \n // Loading state (combines isPending and isTransitioning)\n isSubmitting: boolean;\n \n // Redirecting state (true when redirect is in progress)\n isRedirecting: boolean;\n \n // The current action state\n actionState: ServerActionResponse<TOutput>;\n \n // Utilities\n reset: () => void;\n \n // Raw handlers if needed\n handleSubmit: (data: TFieldValues) => Promise<void>;\n}\n\nfunction isSuccessResponse<T>(response: ServerActionResponse<T>): response is { success: true; data: T; redirect?: string | RedirectConfig } {\n return response.success === true;\n}\n\nfunction objectToFormData(obj: any): FormData {\n const formData = new FormData();\n \n Object.entries(obj).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n if (value instanceof File || value instanceof Blob) {\n formData.append(key, value);\n } else if (Array.isArray(value)) {\n value.forEach((item) => formData.append(`${key}[]`, String(item)));\n } else if (typeof value === \"object\") {\n formData.append(key, JSON.stringify(value));\n } else {\n formData.append(key, String(value));\n }\n }\n });\n \n return formData;\n}\n\nexport function useFormAction<TFieldValues extends FieldValues = FieldValues, TOutput = void>({\n action,\n schema,\n defaultValues,\n mode = \"onChange\",\n transformData,\n onSuccess,\n onError,\n resetOnSuccess = false,\n showSuccessToast = false,\n showErrorToast = true,\n preventRedirect = false,\n redirectDelay = 0,\n}: UseFormActionOptions<TFieldValues, TOutput>): UseFormActionReturn<TFieldValues, TOutput> {\n const router = useRouter();\n const [isRedirecting, setIsRedirecting] = useState(false);\n \n // Store callbacks in refs to prevent effect re-runs\n const onSuccessRef = useRef(onSuccess);\n const onErrorRef = useRef(onError);\n const showSuccessToastRef = useRef(showSuccessToast);\n const showErrorToastRef = useRef(showErrorToast);\n \n // Update refs when callbacks change\n useEffect(() => {\n onSuccessRef.current = onSuccess;\n onErrorRef.current = onError;\n showSuccessToastRef.current = showSuccessToast;\n showErrorToastRef.current = showErrorToast;\n });\n \n // 1. Setup form with React Hook Form\n const form = useForm<TFieldValues>({\n resolver: schema ? (zodResolver as any)(schema) : undefined,\n defaultValues,\n mode,\n } as UseFormProps<TFieldValues>);\n \n // 2. Setup server action state with useActionState\n const initialState: ServerActionResponse<TOutput> = { success: true, data: undefined as TOutput };\n const [actionState, formAction, isPending] = useActionState(action, initialState);\n const [isTransitioning, startTransition] = useTransition();\n \n // 3. Handle server errors and success\n useEffect(() => {\n // Skip initial state\n if (actionState.success === true && actionState.data === undefined) {\n return;\n }\n \n // Reset redirecting state if we're processing a new action\n setIsRedirecting(false);\n \n // Keep track of timeout for cleanup\n let timeoutId: NodeJS.Timeout | undefined;\n \n if (!isSuccessResponse(actionState) && actionState.error) {\n const { error } = actionState;\n \n // Map field errors to form\n if (error.fields) {\n Object.entries(error.fields).forEach(([field, messages]) => {\n if (Array.isArray(messages) && messages.length > 0) {\n form.setError(field as Path<TFieldValues>, {\n type: \"server\",\n message: messages[0],\n });\n }\n });\n }\n \n // Single field error\n if (error.field && error.message && !error.fields) {\n form.setError(error.field as Path<TFieldValues>, {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Global error (no specific field)\n if (error.message && !error.field && !error.fields) {\n const showToast = showErrorToastRef.current;\n if (showToast) {\n const message = typeof showToast === \"function\" \n ? showToast(error)\n : typeof showToast === \"string\"\n ? showToast\n : error.message;\n toast.error(message);\n }\n \n // Also set on root for inline display\n form.setError(\"root\", {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Call error callback\n onErrorRef.current?.(error);\n }\n \n // Handle success\n if (isSuccessResponse(actionState) && actionState.data !== undefined) {\n const showToast = showSuccessToastRef.current;\n if (showToast) {\n const message = typeof showToast === \"function\"\n ? showToast(actionState.data)\n : typeof showToast === \"string\"\n ? showToast\n : \"Success!\";\n toast.success(message);\n }\n \n if (resetOnSuccess) {\n form.reset();\n }\n \n // Call success callback first\n onSuccessRef.current?.(actionState.data);\n \n // Handle redirect if present in response - delay to ensure state updates complete\n if (actionState.redirect && !preventRedirect) {\n setIsRedirecting(true);\n const redirectConfig = typeof actionState.redirect === 'string'\n ? { url: actionState.redirect }\n : actionState.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n timeoutId = setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n setIsRedirecting(false);\n }, delay || 100); // Add small delay by default\n }\n }\n \n // Always return cleanup function\n return () => {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n };\n }, [actionState]); // Minimal dependencies - only actionState\n \n // 4. Submit handler\n const handleSubmit = async (data: TFieldValues): Promise<void> => {\n // Clear any previous errors\n form.clearErrors();\n \n // Transform to FormData\n const formData = transformData \n ? transformData(data)\n : objectToFormData(data);\n \n // Execute with transition for better UX\n startTransition(() => {\n formAction(formData);\n });\n };\n \n // 5. Combined loading state\n const isSubmitting = isPending || isTransitioning;\n \n // 6. Pre-bound submit handler\n const onSubmit = (e?: React.BaseSyntheticEvent): void => {\n e?.preventDefault();\n void form.handleSubmit(handleSubmit)(e!);\n };\n \n return {\n form,\n onSubmit,\n isSubmitting,\n isRedirecting,\n actionState,\n reset: form.reset,\n handleSubmit,\n };\n}","\"use client\";\n\nimport { useEffect } from \"react\";\nimport { toast } from \"sonner\";\n\nexport function ToastRestorer() {\n useEffect(() => {\n // Only run on client side\n if (typeof window === \"undefined\") return;\n \n const storageKey = \"sonner-toasts\";\n \n try {\n const storedToasts = localStorage.getItem(storageKey);\n \n if (storedToasts) {\n const persistentToasts = JSON.parse(storedToasts);\n \n if (Array.isArray(persistentToasts) && persistentToasts.length > 0) {\n // Clear the storage immediately to prevent duplicate toasts\n localStorage.removeItem(storageKey);\n \n // Filter out old toasts (older than 30 seconds)\n const recentToasts = persistentToasts.filter(\n (t: any) => Date.now() - (t.createdAt || 0) < 30000\n );\n \n // Restore toasts with staggered animation like in the PR\n recentToasts.forEach((persistedToast: any, index: number) => {\n // Skip loading toasts as per the PR implementation\n if (persistedToast.type === \"loading\") return;\n \n setTimeout(() => {\n const toastFunction = toast[persistedToast.type as keyof typeof toast];\n \n if (typeof toastFunction === \"function\") {\n // Check if current sonner supports persistent\n if (toastFunction.length >= 2) {\n // Use persistent if available\n (toastFunction as any)(persistedToast.message, { \n persistent: true,\n id: persistedToast.id \n });\n } else {\n // Fallback to normal toast\n toastFunction(persistedToast.message);\n }\n }\n }, index * 150); // Staggered delay like in the PR\n });\n }\n }\n } catch (error) {\n console.error(\"Failed to restore toasts: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AA,YACpC,CAAC,MAAW,KAAK,IAAI,KAAK,EAAE,aAAa,KAAK;AAAA,UAChD;AAGA,uBAAa,QAAQ,CAAC,gBAAqB,UAAkB;AAE3D,gBAAI,eAAe,SAAS,UAAW;AAEvC,uBAAW,MAAM;AACf,oBAAM,gBAAgB,qBAAM,eAAe,IAA0B;AAErE,kBAAI,OAAO,kBAAkB,YAAY;AAEvC,oBAAI,cAAc,UAAU,GAAG;AAE7B,kBAAC,cAAsB,eAAe,SAAS;AAAA,oBAC7C,YAAY;AAAA,oBACZ,IAAI,eAAe;AAAA,kBACrB,CAAC;AAAA,gBACH,OAAO;AAEL,gCAAc,eAAe,OAAO;AAAA,gBACtC;AAAA,cACF;AAAA,YACF,GAAG,QAAQ,GAAG;AAAA,UAChB,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,6BAA6B,KAAK;AAEhD,mBAAa,WAAW,UAAU;AAAA,IACpC;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":["RedirectStatusCode","result","import_react","import_react","import_navigation","import_sonner","import_react","import_sonner"]}
@@ -50,6 +50,7 @@ function useServerAction(action, options = {}) {
50
50
  const router = useRouter();
51
51
  const [result, setResult] = useState();
52
52
  const [isExecuting, setIsExecuting] = useState(false);
53
+ const [isRedirecting, setIsRedirecting] = useState(false);
53
54
  const [, startTransition] = useTransition();
54
55
  const {
55
56
  onSuccess,
@@ -66,6 +67,7 @@ function useServerAction(action, options = {}) {
66
67
  const hasErrored = !!result?.serverError || !!result?.validationErrors || !!result?.fetchError;
67
68
  const reset = useCallback(() => {
68
69
  setResult(void 0);
70
+ setIsRedirecting(false);
69
71
  }, []);
70
72
  const execute = useCallback(
71
73
  async (input) => {
@@ -85,6 +87,7 @@ function useServerAction(action, options = {}) {
85
87
  }
86
88
  await onSuccess?.(response.data);
87
89
  if (response.redirect && !preventRedirect) {
90
+ setIsRedirecting(true);
88
91
  const redirectConfig = typeof response.redirect === "string" ? { url: response.redirect } : response.redirect;
89
92
  const delay = redirectConfig.delay ?? redirectDelay;
90
93
  setTimeout(() => {
@@ -93,6 +96,7 @@ function useServerAction(action, options = {}) {
93
96
  } else {
94
97
  router.push(redirectConfig.url);
95
98
  }
99
+ setIsRedirecting(false);
96
100
  }, delay);
97
101
  }
98
102
  } else if (!response.success && response.error) {
@@ -129,6 +133,7 @@ function useServerAction(action, options = {}) {
129
133
  }
130
134
  toast.error(message, { persistent: true });
131
135
  }
136
+ setIsRedirecting(true);
132
137
  router.push(response.error.redirectTo || "/login");
133
138
  return actionResult;
134
139
  }
@@ -175,6 +180,7 @@ function useServerAction(action, options = {}) {
175
180
  execute: executeWithTransition,
176
181
  result,
177
182
  isExecuting,
183
+ isRedirecting,
178
184
  hasSucceeded,
179
185
  hasErrored,
180
186
  reset
@@ -220,7 +226,7 @@ function useOptimisticAction(initialState, action, options) {
220
226
  }
221
227
 
222
228
  // src/hooks/use-form-action.ts
223
- import { useActionState, useEffect, useTransition as useTransition3 } from "react";
229
+ import { useActionState, useEffect, useTransition as useTransition3, useRef, useState as useState2 } from "react";
224
230
  import { useRouter as useRouter2 } from "next/navigation";
225
231
  import { useForm } from "react-hook-form";
226
232
  import { zodResolver } from "@hookform/resolvers/zod";
@@ -260,6 +266,17 @@ function useFormAction({
260
266
  redirectDelay = 0
261
267
  }) {
262
268
  const router = useRouter2();
269
+ const [isRedirecting, setIsRedirecting] = useState2(false);
270
+ const onSuccessRef = useRef(onSuccess);
271
+ const onErrorRef = useRef(onError);
272
+ const showSuccessToastRef = useRef(showSuccessToast);
273
+ const showErrorToastRef = useRef(showErrorToast);
274
+ useEffect(() => {
275
+ onSuccessRef.current = onSuccess;
276
+ onErrorRef.current = onError;
277
+ showSuccessToastRef.current = showSuccessToast;
278
+ showErrorToastRef.current = showErrorToast;
279
+ });
263
280
  const form = useForm({
264
281
  resolver: schema ? zodResolver(schema) : void 0,
265
282
  defaultValues,
@@ -269,6 +286,11 @@ function useFormAction({
269
286
  const [actionState, formAction, isPending] = useActionState(action, initialState);
270
287
  const [isTransitioning, startTransition] = useTransition3();
271
288
  useEffect(() => {
289
+ if (actionState.success === true && actionState.data === void 0) {
290
+ return;
291
+ }
292
+ setIsRedirecting(false);
293
+ let timeoutId;
272
294
  if (!isSuccessResponse(actionState) && actionState.error) {
273
295
  const { error } = actionState;
274
296
  if (error.fields) {
@@ -288,8 +310,9 @@ function useFormAction({
288
310
  });
289
311
  }
290
312
  if (error.message && !error.field && !error.fields) {
291
- if (showErrorToast) {
292
- const message = typeof showErrorToast === "function" ? showErrorToast(error) : typeof showErrorToast === "string" ? showErrorToast : error.message;
313
+ const showToast = showErrorToastRef.current;
314
+ if (showToast) {
315
+ const message = typeof showToast === "function" ? showToast(error) : typeof showToast === "string" ? showToast : error.message;
293
316
  toast2.error(message);
294
317
  }
295
318
  form.setError("root", {
@@ -297,30 +320,38 @@ function useFormAction({
297
320
  message: error.message
298
321
  });
299
322
  }
300
- onError?.(error);
323
+ onErrorRef.current?.(error);
301
324
  }
302
325
  if (isSuccessResponse(actionState) && actionState.data !== void 0) {
303
- if (showSuccessToast) {
304
- const message = typeof showSuccessToast === "function" ? showSuccessToast(actionState.data) : typeof showSuccessToast === "string" ? showSuccessToast : "Success!";
326
+ const showToast = showSuccessToastRef.current;
327
+ if (showToast) {
328
+ const message = typeof showToast === "function" ? showToast(actionState.data) : typeof showToast === "string" ? showToast : "Success!";
305
329
  toast2.success(message);
306
330
  }
307
331
  if (resetOnSuccess) {
308
332
  form.reset();
309
333
  }
310
- onSuccess?.(actionState.data);
334
+ onSuccessRef.current?.(actionState.data);
311
335
  if (actionState.redirect && !preventRedirect) {
336
+ setIsRedirecting(true);
312
337
  const redirectConfig = typeof actionState.redirect === "string" ? { url: actionState.redirect } : actionState.redirect;
313
338
  const delay = redirectConfig.delay ?? redirectDelay;
314
- setTimeout(() => {
339
+ timeoutId = setTimeout(() => {
315
340
  if (redirectConfig.replace) {
316
341
  router.replace(redirectConfig.url);
317
342
  } else {
318
343
  router.push(redirectConfig.url);
319
344
  }
320
- }, delay);
345
+ setIsRedirecting(false);
346
+ }, delay || 100);
321
347
  }
322
348
  }
323
- }, [actionState, form, onError, onSuccess, resetOnSuccess, showErrorToast, showSuccessToast, preventRedirect, redirectDelay, router]);
349
+ return () => {
350
+ if (timeoutId) {
351
+ clearTimeout(timeoutId);
352
+ }
353
+ };
354
+ }, [actionState]);
324
355
  const handleSubmit = async (data) => {
325
356
  form.clearErrors();
326
357
  const formData = transformData ? transformData(data) : objectToFormData(data);
@@ -337,6 +368,7 @@ function useFormAction({
337
368
  form,
338
369
  onSubmit,
339
370
  isSubmitting,
371
+ isRedirecting,
340
372
  actionState,
341
373
  reset: form.reset,
342
374
  handleSubmit
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/hooks/use-server-action.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/hooks/use-optimistic-action.ts","../../src/hooks/use-form-action.ts","../../src/hooks/toast-restorer.tsx"],"sourcesContent":["import { useCallback, useState, useTransition } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport { toast } from \"sonner\";\nimport type { ServerAction, ActionResult, ServerActionResponse } from \"../types\";\nimport { isNextNavigationError } from \"../next/errors\";\n\nexport interface UseServerActionOptions<TOutput> {\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ActionResult<TOutput>) => void | Promise<void>;\n \n successMessage?: string | ((data: TOutput) => string);\n errorMessage?: string | ((error: ActionResult<TOutput>) => string);\n showSuccessToast?: boolean;\n showErrorToast?: boolean;\n \n redirectOnAuthError?: boolean;\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseServerActionReturn<TInput, TOutput> {\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n result: ActionResult<TOutput> | undefined;\n isExecuting: boolean;\n hasSucceeded: boolean;\n hasErrored: boolean;\n \n reset: () => void;\n}\n\n// Overload for void actions\nexport function useServerAction<TOutput>(\n action: ServerAction<void, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<void, TOutput>;\n\n// Overload for actions with input\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<TInput, TOutput>;\n\n// Implementation\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options: UseServerActionOptions<TOutput> = {}\n): UseServerActionReturn<TInput, TOutput> {\n const router = useRouter();\n const [result, setResult] = useState<ActionResult<TOutput>>();\n const [isExecuting, setIsExecuting] = useState(false);\n const [, startTransition] = useTransition();\n \n const {\n onSuccess,\n onError,\n successMessage,\n errorMessage,\n showSuccessToast = false,\n showErrorToast = true,\n redirectOnAuthError = true,\n preventRedirect = false,\n redirectDelay = 0,\n } = options;\n \n const hasSucceeded = !!result?.data;\n const hasErrored = !!result?.serverError || !!result?.validationErrors || !!result?.fetchError;\n \n const reset = useCallback(() => {\n setResult(undefined);\n }, []);\n \n const execute = useCallback(\n async (input?: TInput): Promise<ActionResult<TOutput>> => {\n setIsExecuting(true);\n \n try {\n const response = await (input === undefined \n ? (action as () => Promise<ServerActionResponse<TOutput>>)()\n : (action as (input: TInput) => Promise<ServerActionResponse<TOutput>>)(input));\n \n let actionResult: ActionResult<TOutput>;\n \n if (response.success) {\n actionResult = { data: response.data };\n \n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Success response:', response);\n console.log('[useServerAction] Has redirect?', !!response.redirect);\n }\n \n if (showSuccessToast && successMessage) {\n const message = typeof successMessage === \"function\" \n ? successMessage(response.data) \n : successMessage;\n toast.success(message);\n }\n \n await onSuccess?.(response.data);\n \n // Handle redirect if present in response\n if (response.redirect && !preventRedirect) {\n const redirectConfig = typeof response.redirect === 'string'\n ? { url: response.redirect }\n : response.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n }, delay);\n }\n \n } else if (!response.success && response.error) {\n actionResult = {\n serverError: response.error,\n validationErrors: response.error.fields,\n };\n \n // Debug logging\n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Error response:', response.error);\n console.log('[useServerAction] Should redirect?', response.error.shouldRedirect);\n console.log('[useServerAction] Redirect to:', response.error.redirectTo);\n }\n \n if (redirectOnAuthError && response.error.shouldRedirect) {\n // Handle toast before redirect\n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n \n // Always save to localStorage for redirect cases\n // This ensures compatibility with both current and future sonner versions\n if (typeof window !== 'undefined') {\n try {\n const storageKey = 'sonner-toasts';\n const existingToasts = JSON.parse(\n localStorage.getItem(storageKey) || '[]'\n );\n \n // Add our toast in the same format as the PR\n const persistentToast = {\n id: Date.now(),\n type: 'error',\n message,\n persistent: true,\n createdAt: Date.now()\n };\n \n existingToasts.push(persistentToast);\n localStorage.setItem(storageKey, JSON.stringify(existingToasts));\n } catch (err) {\n console.error('Failed to persist toast:', err);\n }\n }\n \n // Always show the toast (with persistent if supported)\n (toast.error as any)(message, { persistent: true });\n }\n \n router.push(response.error.redirectTo || \"/login\");\n return actionResult;\n }\n \n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n toast.error(message);\n }\n \n await onError?.(actionResult);\n } else {\n actionResult = { fetchError: \"Unexpected response format\" };\n }\n \n setResult(actionResult);\n return actionResult;\n \n } catch (error) {\n // Check if this is a Next.js navigation error that should be re-thrown\n if (isNextNavigationError(error)) {\n // Re-throw navigation errors to let Next.js handle them\n throw error;\n }\n \n const fetchError = error instanceof Error ? error.message : \"Network error\";\n const actionResult: ActionResult<TOutput> = { fetchError };\n \n setResult(actionResult);\n \n if (showErrorToast) {\n toast.error(fetchError);\n }\n \n await onError?.(actionResult);\n return actionResult;\n } finally {\n setIsExecuting(false);\n }\n },\n [action, router, showSuccessToast, successMessage, showErrorToast, errorMessage, redirectOnAuthError, onSuccess, onError]\n );\n \n const executeWithTransition = useCallback(\n (...args: TInput extends void ? [] : [input: TInput]): Promise<ActionResult<TOutput>> => {\n return new Promise((resolve) => {\n startTransition(async () => {\n const result = await execute(args[0] as TInput);\n resolve(result);\n });\n });\n },\n [execute]\n ) as TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n return {\n execute: executeWithTransition,\n result,\n isExecuting,\n hasSucceeded,\n hasErrored,\n reset,\n };\n}\n\n","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import { useOptimistic, useTransition } from \"react\";\nimport { useServerAction, type UseServerActionOptions } from \"./use-server-action\";\nimport type { ServerAction, ActionResult } from \"../types\";\n\nexport interface UseOptimisticActionOptions<TInput, TOutput, TOptimistic> extends UseServerActionOptions<TOutput> {\n /**\n * Function to update the optimistic state\n */\n updateFn: TInput extends void \n ? (current: TOptimistic) => TOptimistic\n : (current: TOptimistic, input: TInput) => TOptimistic;\n}\n\nexport interface UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n /**\n * The optimistic state\n */\n optimisticState: TOptimistic;\n \n /**\n * Execute the action with optimistic update\n */\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n /**\n * Loading state\n */\n isExecuting: boolean;\n \n /**\n * Success state\n */\n hasSucceeded: boolean;\n \n /**\n * Error state\n */\n hasErrored: boolean;\n \n /**\n * Last result\n */\n result: ActionResult<TOutput> | undefined;\n \n /**\n * Reset state\n */\n reset: () => void;\n}\n\n/**\n * Hook for server actions with optimistic updates\n * \n * @example\n * ```typescript\n * const { optimisticState, execute } = useOptimisticAction(\n * currentTodos,\n * addTodoAction,\n * {\n * updateFn: (todos, newTodo) => [...todos, newTodo],\n * onSuccess: (savedTodo) => {\n * // Update with server response if needed\n * }\n * }\n * );\n * ```\n */\nexport function useOptimisticAction<TInput, TOutput, TOptimistic>(\n initialState: TOptimistic,\n action: ServerAction<TInput, TOutput>,\n options: UseOptimisticActionOptions<TInput, TOutput, TOptimistic>\n): UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n const [optimisticState, addOptimisticUpdate] = useOptimistic(\n initialState,\n options.updateFn\n );\n \n const [, startTransition] = useTransition();\n \n // Extract updateFn from options to pass the rest to useServerAction\n const { updateFn: _, ...serverActionOptions } = options;\n \n const {\n execute: executeAction,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n } = useServerAction(action, serverActionOptions);\n \n const execute = (async (input?: TInput): Promise<ActionResult<TOutput>> => {\n // Apply optimistic update\n startTransition(() => {\n if (input !== undefined) {\n addOptimisticUpdate(input);\n } else {\n addOptimisticUpdate(undefined as any);\n }\n });\n \n // Execute the server action\n return executeAction(input as any);\n }) as any;\n \n return {\n optimisticState,\n execute,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n };\n}","import { useActionState, useEffect, useTransition } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport type { UseFormProps, UseFormReturn, Path, FieldValues, DefaultValues } from \"react-hook-form\";\nimport { useForm } from \"react-hook-form\";\nimport { zodResolver } from \"@hookform/resolvers/zod\";\nimport { toast } from \"sonner\";\nimport type { z } from \"zod\";\nimport type { ServerActionResponse, ServerActionError, RedirectConfig } from \"../types\";\n\nexport interface UseFormActionOptions<TFieldValues extends FieldValues, TOutput> {\n // Required - the server action to execute (form action format)\n action: (prevState: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>>;\n \n // Optional Zod schema for validation\n schema?: z.ZodTypeAny;\n \n // React Hook Form options\n defaultValues?: DefaultValues<TFieldValues>;\n mode?: UseFormProps<TFieldValues>[\"mode\"];\n \n // Transform function if you need custom FormData creation\n transformData?: (data: TFieldValues) => FormData;\n \n // Callbacks\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ServerActionError) => void;\n \n // Behavior options\n resetOnSuccess?: boolean;\n showSuccessToast?: boolean | string | ((data: TOutput) => string);\n showErrorToast?: boolean | string | ((error: ServerActionError) => string);\n \n // Redirect options (same as useServerAction)\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseFormActionReturn<TFieldValues extends FieldValues, TOutput> {\n // React Hook Form instance\n form: UseFormReturn<TFieldValues>;\n \n // Submit handler (pre-bound with handleSubmit)\n onSubmit: (e?: React.BaseSyntheticEvent) => void;\n \n // Loading state (combines isPending and isTransitioning)\n isSubmitting: boolean;\n \n // The current action state\n actionState: ServerActionResponse<TOutput>;\n \n // Utilities\n reset: () => void;\n \n // Raw handlers if needed\n handleSubmit: (data: TFieldValues) => Promise<void>;\n}\n\nfunction isSuccessResponse<T>(response: ServerActionResponse<T>): response is { success: true; data: T; redirect?: string | RedirectConfig } {\n return response.success === true;\n}\n\nfunction objectToFormData(obj: any): FormData {\n const formData = new FormData();\n \n Object.entries(obj).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n if (value instanceof File || value instanceof Blob) {\n formData.append(key, value);\n } else if (Array.isArray(value)) {\n value.forEach((item) => formData.append(`${key}[]`, String(item)));\n } else if (typeof value === \"object\") {\n formData.append(key, JSON.stringify(value));\n } else {\n formData.append(key, String(value));\n }\n }\n });\n \n return formData;\n}\n\nexport function useFormAction<TFieldValues extends FieldValues = FieldValues, TOutput = void>({\n action,\n schema,\n defaultValues,\n mode = \"onChange\",\n transformData,\n onSuccess,\n onError,\n resetOnSuccess = false,\n showSuccessToast = false,\n showErrorToast = true,\n preventRedirect = false,\n redirectDelay = 0,\n}: UseFormActionOptions<TFieldValues, TOutput>): UseFormActionReturn<TFieldValues, TOutput> {\n const router = useRouter();\n // 1. Setup form with React Hook Form\n const form = useForm<TFieldValues>({\n resolver: schema ? (zodResolver as any)(schema) : undefined,\n defaultValues,\n mode,\n } as UseFormProps<TFieldValues>);\n \n // 2. Setup server action state with useActionState\n const initialState: ServerActionResponse<TOutput> = { success: true, data: undefined as TOutput };\n const [actionState, formAction, isPending] = useActionState(action, initialState);\n const [isTransitioning, startTransition] = useTransition();\n \n // 3. Handle server errors and success\n useEffect(() => {\n if (!isSuccessResponse(actionState) && actionState.error) {\n const { error } = actionState;\n \n // Map field errors to form\n if (error.fields) {\n Object.entries(error.fields).forEach(([field, messages]) => {\n if (Array.isArray(messages) && messages.length > 0) {\n form.setError(field as Path<TFieldValues>, {\n type: \"server\",\n message: messages[0],\n });\n }\n });\n }\n \n // Single field error\n if (error.field && error.message && !error.fields) {\n form.setError(error.field as Path<TFieldValues>, {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Global error (no specific field)\n if (error.message && !error.field && !error.fields) {\n if (showErrorToast) {\n const message = typeof showErrorToast === \"function\" \n ? showErrorToast(error)\n : typeof showErrorToast === \"string\"\n ? showErrorToast\n : error.message;\n toast.error(message);\n }\n \n // Also set on root for inline display\n form.setError(\"root\", {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Call error callback\n onError?.(error);\n }\n \n // Handle success\n if (isSuccessResponse(actionState) && actionState.data !== undefined) {\n if (showSuccessToast) {\n const message = typeof showSuccessToast === \"function\"\n ? showSuccessToast(actionState.data)\n : typeof showSuccessToast === \"string\"\n ? showSuccessToast\n : \"Success!\";\n toast.success(message);\n }\n \n if (resetOnSuccess) {\n form.reset();\n }\n \n // Call success callback\n onSuccess?.(actionState.data);\n \n // Handle redirect if present in response\n if (actionState.redirect && !preventRedirect) {\n const redirectConfig = typeof actionState.redirect === 'string'\n ? { url: actionState.redirect }\n : actionState.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n }, delay);\n }\n }\n }, [actionState, form, onError, onSuccess, resetOnSuccess, showErrorToast, showSuccessToast, preventRedirect, redirectDelay, router]);\n \n // 4. Submit handler\n const handleSubmit = async (data: TFieldValues): Promise<void> => {\n // Clear any previous errors\n form.clearErrors();\n \n // Transform to FormData\n const formData = transformData \n ? transformData(data)\n : objectToFormData(data);\n \n // Execute with transition for better UX\n startTransition(() => {\n formAction(formData);\n });\n };\n \n // 5. Combined loading state\n const isSubmitting = isPending || isTransitioning;\n \n // 6. Pre-bound submit handler\n const onSubmit = (e?: React.BaseSyntheticEvent): void => {\n e?.preventDefault();\n void form.handleSubmit(handleSubmit)(e!);\n };\n \n return {\n form,\n onSubmit,\n isSubmitting,\n actionState,\n reset: form.reset,\n handleSubmit,\n };\n}","\"use client\";\n\nimport { useEffect } from \"react\";\nimport { toast } from \"sonner\";\n\nexport function ToastRestorer() {\n useEffect(() => {\n // Only run on client side\n if (typeof window === \"undefined\") return;\n \n const storageKey = \"sonner-toasts\";\n \n try {\n const storedToasts = localStorage.getItem(storageKey);\n \n if (storedToasts) {\n const persistentToasts = JSON.parse(storedToasts);\n \n if (Array.isArray(persistentToasts) && persistentToasts.length > 0) {\n // Clear the storage immediately to prevent duplicate toasts\n localStorage.removeItem(storageKey);\n \n // Filter out old toasts (older than 30 seconds)\n const recentToasts = persistentToasts.filter(\n (t: any) => Date.now() - (t.createdAt || 0) < 30000\n );\n \n // Restore toasts with staggered animation like in the PR\n recentToasts.forEach((persistedToast: any, index: number) => {\n // Skip loading toasts as per the PR implementation\n if (persistedToast.type === \"loading\") return;\n \n setTimeout(() => {\n const toastFunction = toast[persistedToast.type as keyof typeof toast];\n \n if (typeof toastFunction === \"function\") {\n // Check if current sonner supports persistent\n if (toastFunction.length >= 2) {\n // Use persistent if available\n (toastFunction as any)(persistedToast.message, { \n persistent: true,\n id: persistedToast.id \n });\n } else {\n // Fallback to normal toast\n toastFunction(persistedToast.message);\n }\n }\n }, index * 150); // Staggered delay like in the PR\n });\n }\n }\n } catch (error) {\n console.error(\"Failed to restore toasts:\", error);\n // Clean up on error\n localStorage.removeItem(storageKey);\n }\n }, []);\n \n return null;\n}"],"mappings":";AAAA,SAAS,aAAa,UAAU,qBAAqB;AACrD,SAAS,iBAAiB;AAC1B,SAAS,aAAa;;;ACAtB,IAAK,qBAAL,kBAAKA,wBAAL;AACC,EAAAA,wCAAA,cAAW,OAAX;AACA,EAAAA,wCAAA,uBAAoB,OAApB;AACA,EAAAA,wCAAA,uBAAoB,OAApB;AAHI,SAAAA;AAAA,GAAA;AAML,IAAM,sB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,OAAO,WAAW,YAAa;AAEnC,UAAM,aAAa;AAEnB,QAAI;AACF,YAAM,eAAe,aAAa,QAAQ,UAAU;AAEpD,UAAI,cAAc;AAChB,cAAM,mBAAmB,KAAK,MAAM,YAAY;AAEhD,YAAI,MAAM,QAAQ,gBAAgB,KAAK,iBAAiB,SAAS,GAAG;AAElE,uBAAa,WAAW,UAAU;AAGlC,gBAAM,eAAe,iBAAiB;AAAA,YACpC,CAAC,MAAW,KAAK,IAAI,KAAK,EAAE,aAAa,KAAK;AAAA,UAChD;AAGA,uBAAa,QAAQ,CAAC,gBAAqB,UAAkB;AAE3D,gBAAI,eAAe,SAAS,UAAW;AAEvC,uBAAW,MAAM;AACf,oBAAM,gBAAgBC,OAAM,eAAe,IAA0B;AAErE,kBAAI,OAAO,kBAAkB,YAAY;AAEvC,oBAAI,cAAc,UAAU,GAAG;AAE7B,kBAAC,cAAsB,eAAe,SAAS;AAAA,oBAC7C,YAAY;AAAA,oBACZ,IAAI,eAAe;AAAA,kBACrB,CAAC;AAAA,gBACH,OAAO;AAEL,gCAAc,eAAe,OAAO;AAAA,gBACtC;AAAA,cACF;AAAA,YACF,GAAG,QAAQ,GAAG;AAAA,UAChB,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,6BAA6B,KAAK;AAEhD,mBAAa,WAAW,UAAU;AAAA,IACpC;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":["RedirectStatusCode","result","useTransition","useTransition","useTransition","useRouter","toast","useEffect","toast"]}
1
+ {"version":3,"sources":["../../src/hooks/use-server-action.ts","../../src/next/errors/redirect.ts","../../src/next/errors/http-access-fallback.ts","../../src/next/errors/index.ts","../../src/hooks/use-optimistic-action.ts","../../src/hooks/use-form-action.ts","../../src/hooks/toast-restorer.tsx"],"sourcesContent":["import { useCallback, useState, useTransition } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport { toast } from \"sonner\";\nimport type { ServerAction, ActionResult, ServerActionResponse } from \"../types\";\nimport { isNextNavigationError } from \"../next/errors\";\n\nexport interface UseServerActionOptions<TOutput> {\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ActionResult<TOutput>) => void | Promise<void>;\n \n successMessage?: string | ((data: TOutput) => string);\n errorMessage?: string | ((error: ActionResult<TOutput>) => string);\n showSuccessToast?: boolean;\n showErrorToast?: boolean;\n \n redirectOnAuthError?: boolean;\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseServerActionReturn<TInput, TOutput> {\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n result: ActionResult<TOutput> | undefined;\n isExecuting: boolean;\n isRedirecting: boolean;\n hasSucceeded: boolean;\n hasErrored: boolean;\n \n reset: () => void;\n}\n\n// Overload for void actions\nexport function useServerAction<TOutput>(\n action: ServerAction<void, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<void, TOutput>;\n\n// Overload for actions with input\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options?: UseServerActionOptions<TOutput>\n): UseServerActionReturn<TInput, TOutput>;\n\n// Implementation\nexport function useServerAction<TInput, TOutput>(\n action: ServerAction<TInput, TOutput>,\n options: UseServerActionOptions<TOutput> = {}\n): UseServerActionReturn<TInput, TOutput> {\n const router = useRouter();\n const [result, setResult] = useState<ActionResult<TOutput>>();\n const [isExecuting, setIsExecuting] = useState(false);\n const [isRedirecting, setIsRedirecting] = useState(false);\n const [, startTransition] = useTransition();\n \n const {\n onSuccess,\n onError,\n successMessage,\n errorMessage,\n showSuccessToast = false,\n showErrorToast = true,\n redirectOnAuthError = true,\n preventRedirect = false,\n redirectDelay = 0,\n } = options;\n \n const hasSucceeded = !!result?.data;\n const hasErrored = !!result?.serverError || !!result?.validationErrors || !!result?.fetchError;\n \n const reset = useCallback(() => {\n setResult(undefined);\n setIsRedirecting(false);\n }, []);\n \n const execute = useCallback(\n async (input?: TInput): Promise<ActionResult<TOutput>> => {\n setIsExecuting(true);\n \n try {\n const response = await (input === undefined \n ? (action as () => Promise<ServerActionResponse<TOutput>>)()\n : (action as (input: TInput) => Promise<ServerActionResponse<TOutput>>)(input));\n \n let actionResult: ActionResult<TOutput>;\n \n if (response.success) {\n actionResult = { data: response.data };\n \n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Success response:', response);\n console.log('[useServerAction] Has redirect?', !!response.redirect);\n }\n \n if (showSuccessToast && successMessage) {\n const message = typeof successMessage === \"function\" \n ? successMessage(response.data) \n : successMessage;\n toast.success(message);\n }\n \n await onSuccess?.(response.data);\n \n // Handle redirect if present in response\n if (response.redirect && !preventRedirect) {\n setIsRedirecting(true);\n const redirectConfig = typeof response.redirect === 'string'\n ? { url: response.redirect }\n : response.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n setIsRedirecting(false);\n }, delay);\n }\n \n } else if (!response.success && response.error) {\n actionResult = {\n serverError: response.error,\n validationErrors: response.error.fields,\n };\n \n // Debug logging\n if (process.env.NODE_ENV === 'development') {\n console.log('[useServerAction] Error response:', response.error);\n console.log('[useServerAction] Should redirect?', response.error.shouldRedirect);\n console.log('[useServerAction] Redirect to:', response.error.redirectTo);\n }\n \n if (redirectOnAuthError && response.error.shouldRedirect) {\n // Handle toast before redirect\n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n \n // Always save to localStorage for redirect cases\n // This ensures compatibility with both current and future sonner versions\n if (typeof window !== 'undefined') {\n try {\n const storageKey = 'sonner-toasts';\n const existingToasts = JSON.parse(\n localStorage.getItem(storageKey) || '[]'\n );\n \n // Add our toast in the same format as the PR\n const persistentToast = {\n id: Date.now(),\n type: 'error',\n message,\n persistent: true,\n createdAt: Date.now()\n };\n \n existingToasts.push(persistentToast);\n localStorage.setItem(storageKey, JSON.stringify(existingToasts));\n } catch (err) {\n console.error('Failed to persist toast:', err);\n }\n }\n \n // Always show the toast (with persistent if supported)\n (toast.error as any)(message, { persistent: true });\n }\n \n setIsRedirecting(true);\n router.push(response.error.redirectTo || \"/login\");\n return actionResult;\n }\n \n if (showErrorToast) {\n const message = errorMessage \n ? (typeof errorMessage === \"function\" \n ? errorMessage(actionResult) \n : errorMessage)\n : response.error.message || \"An error occurred\";\n toast.error(message);\n }\n \n await onError?.(actionResult);\n } else {\n actionResult = { fetchError: \"Unexpected response format\" };\n }\n \n setResult(actionResult);\n return actionResult;\n \n } catch (error) {\n // Check if this is a Next.js navigation error that should be re-thrown\n if (isNextNavigationError(error)) {\n // Re-throw navigation errors to let Next.js handle them\n throw error;\n }\n \n const fetchError = error instanceof Error ? error.message : \"Network error\";\n const actionResult: ActionResult<TOutput> = { fetchError };\n \n setResult(actionResult);\n \n if (showErrorToast) {\n toast.error(fetchError);\n }\n \n await onError?.(actionResult);\n return actionResult;\n } finally {\n setIsExecuting(false);\n }\n },\n [action, router, showSuccessToast, successMessage, showErrorToast, errorMessage, redirectOnAuthError, onSuccess, onError]\n );\n \n const executeWithTransition = useCallback(\n (...args: TInput extends void ? [] : [input: TInput]): Promise<ActionResult<TOutput>> => {\n return new Promise((resolve) => {\n startTransition(async () => {\n const result = await execute(args[0] as TInput);\n resolve(result);\n });\n });\n },\n [execute]\n ) as TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n return {\n execute: executeWithTransition,\n result,\n isExecuting,\n isRedirecting,\n hasSucceeded,\n hasErrored,\n reset,\n };\n}\n\n","// Comes from: https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect-error.ts\n\nenum RedirectStatusCode {\n\tSeeOther = 303,\n\tTemporaryRedirect = 307,\n\tPermanentRedirect = 308,\n}\n\nconst REDIRECT_ERROR_CODE = \"NEXT_REDIRECT\";\n\nenum RedirectType {\n\tpush = \"push\",\n\treplace = \"replace\",\n}\n\nexport type RedirectError = Error & {\n\tdigest: `${typeof REDIRECT_ERROR_CODE};${RedirectType};${string};${RedirectStatusCode};`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by the\n * `redirect(url)` helper.\n *\n * @param error the error that may reference a redirect error\n * @returns true if the error is a redirect error\n */\nexport function isRedirectError(error: unknown): error is RedirectError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\n\tconst digest = error.digest.split(\";\");\n\tconst [errorCode, type] = digest;\n\tconst destination = digest.slice(2, -2).join(\";\");\n\tconst status = digest.at(-2);\n\n\tconst statusCode = Number(status);\n\n\treturn (\n\t\terrorCode === REDIRECT_ERROR_CODE &&\n\t\t(type === \"replace\" || type === \"push\") &&\n\t\ttypeof destination === \"string\" &&\n\t\t!isNaN(statusCode) &&\n\t\tstatusCode in RedirectStatusCode\n\t);\n}","// Comes from https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/http-access-fallback/http-access-fallback.ts\n\nconst HTTPAccessErrorStatus = {\n\tNOT_FOUND: 404,\n\tFORBIDDEN: 403,\n\tUNAUTHORIZED: 401,\n};\n\nconst ALLOWED_CODES = new Set(Object.values(HTTPAccessErrorStatus));\n\nconst HTTP_ERROR_FALLBACK_ERROR_CODE = \"NEXT_HTTP_ERROR_FALLBACK\";\n\nexport type HTTPAccessFallbackError = Error & {\n\tdigest: `${typeof HTTP_ERROR_FALLBACK_ERROR_CODE};${string}`;\n};\n\n/**\n * Checks an error to determine if it's an error generated by\n * the HTTP navigation APIs `notFound()`, `forbidden()` or `unauthorized()`.\n *\n * @param error the error that may reference a HTTP access error\n * @returns true if the error is a HTTP access error\n */\nexport function isHTTPAccessFallbackError(error: unknown): error is HTTPAccessFallbackError {\n\tif (typeof error !== \"object\" || error === null || !(\"digest\" in error) || typeof error.digest !== \"string\") {\n\t\treturn false;\n\t}\n\tconst [prefix, httpStatus] = error.digest.split(\";\");\n\n\treturn prefix === HTTP_ERROR_FALLBACK_ERROR_CODE && ALLOWED_CODES.has(Number(httpStatus));\n}\n\nexport function getAccessFallbackHTTPStatus(error: HTTPAccessFallbackError): number {\n\tconst httpStatus = error.digest.split(\";\")[1];\n\treturn Number(httpStatus);\n}","import { isRedirectError } from \"./redirect\";\nimport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\n\nexport { isRedirectError } from \"./redirect\";\nexport type { RedirectError } from \"./redirect\";\n\nexport { isHTTPAccessFallbackError, getAccessFallbackHTTPStatus } from \"./http-access-fallback\";\nexport type { HTTPAccessFallbackError } from \"./http-access-fallback\";\n\n/**\n * Checks if the error is a navigation error that should be re-thrown\n * This includes redirect errors and HTTP access errors (notFound, forbidden, unauthorized)\n */\nexport function isNextNavigationError(error: unknown): boolean {\n\treturn isRedirectError(error) || isHTTPAccessFallbackError(error);\n}\n\n/**\n * Checks if the error is a notFound error\n * Note: Next.js implements notFound() using HTTP_ERROR_FALLBACK with status 404,\n * not as a separate error type like NEXT_REDIRECT\n */\nexport function isNotFoundError(error: unknown): boolean {\n\treturn isHTTPAccessFallbackError(error) && getAccessFallbackHTTPStatus(error as any) === 404;\n}","import { useOptimistic, useTransition } from \"react\";\nimport { useServerAction, type UseServerActionOptions } from \"./use-server-action\";\nimport type { ServerAction, ActionResult } from \"../types\";\n\nexport interface UseOptimisticActionOptions<TInput, TOutput, TOptimistic> extends UseServerActionOptions<TOutput> {\n /**\n * Function to update the optimistic state\n */\n updateFn: TInput extends void \n ? (current: TOptimistic) => TOptimistic\n : (current: TOptimistic, input: TInput) => TOptimistic;\n}\n\nexport interface UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n /**\n * The optimistic state\n */\n optimisticState: TOptimistic;\n \n /**\n * Execute the action with optimistic update\n */\n execute: TInput extends void \n ? () => Promise<ActionResult<TOutput>>\n : (input: TInput) => Promise<ActionResult<TOutput>>;\n \n /**\n * Loading state\n */\n isExecuting: boolean;\n \n /**\n * Success state\n */\n hasSucceeded: boolean;\n \n /**\n * Error state\n */\n hasErrored: boolean;\n \n /**\n * Last result\n */\n result: ActionResult<TOutput> | undefined;\n \n /**\n * Reset state\n */\n reset: () => void;\n}\n\n/**\n * Hook for server actions with optimistic updates\n * \n * @example\n * ```typescript\n * const { optimisticState, execute } = useOptimisticAction(\n * currentTodos,\n * addTodoAction,\n * {\n * updateFn: (todos, newTodo) => [...todos, newTodo],\n * onSuccess: (savedTodo) => {\n * // Update with server response if needed\n * }\n * }\n * );\n * ```\n */\nexport function useOptimisticAction<TInput, TOutput, TOptimistic>(\n initialState: TOptimistic,\n action: ServerAction<TInput, TOutput>,\n options: UseOptimisticActionOptions<TInput, TOutput, TOptimistic>\n): UseOptimisticActionReturn<TInput, TOutput, TOptimistic> {\n const [optimisticState, addOptimisticUpdate] = useOptimistic(\n initialState,\n options.updateFn\n );\n \n const [, startTransition] = useTransition();\n \n // Extract updateFn from options to pass the rest to useServerAction\n const { updateFn: _, ...serverActionOptions } = options;\n \n const {\n execute: executeAction,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n } = useServerAction(action, serverActionOptions);\n \n const execute = (async (input?: TInput): Promise<ActionResult<TOutput>> => {\n // Apply optimistic update\n startTransition(() => {\n if (input !== undefined) {\n addOptimisticUpdate(input);\n } else {\n addOptimisticUpdate(undefined as any);\n }\n });\n \n // Execute the server action\n return executeAction(input as any);\n }) as any;\n \n return {\n optimisticState,\n execute,\n isExecuting,\n hasSucceeded,\n hasErrored,\n result,\n reset\n };\n}","import { useActionState, useEffect, useTransition, useRef, useState } from \"react\";\nimport { useRouter } from \"next/navigation\";\nimport type { UseFormProps, UseFormReturn, Path, FieldValues, DefaultValues } from \"react-hook-form\";\nimport { useForm } from \"react-hook-form\";\nimport { zodResolver } from \"@hookform/resolvers/zod\";\nimport { toast } from \"sonner\";\nimport type { z } from \"zod\";\nimport type { ServerActionResponse, ServerActionError, RedirectConfig } from \"../types\";\n\nexport interface UseFormActionOptions<TFieldValues extends FieldValues, TOutput> {\n // Required - the server action to execute (form action format)\n action: (prevState: ServerActionResponse<TOutput>, formData: FormData) => Promise<ServerActionResponse<TOutput>>;\n \n // Optional Zod schema for validation\n schema?: z.ZodTypeAny;\n \n // React Hook Form options\n defaultValues?: DefaultValues<TFieldValues>;\n mode?: UseFormProps<TFieldValues>[\"mode\"];\n \n // Transform function if you need custom FormData creation\n transformData?: (data: TFieldValues) => FormData;\n \n // Callbacks\n onSuccess?: (data: TOutput) => void | Promise<void>;\n onError?: (error: ServerActionError) => void;\n \n // Behavior options\n resetOnSuccess?: boolean;\n showSuccessToast?: boolean | string | ((data: TOutput) => string);\n showErrorToast?: boolean | string | ((error: ServerActionError) => string);\n \n // Redirect options (same as useServerAction)\n preventRedirect?: boolean;\n redirectDelay?: number;\n}\n\nexport interface UseFormActionReturn<TFieldValues extends FieldValues, TOutput> {\n // React Hook Form instance\n form: UseFormReturn<TFieldValues>;\n \n // Submit handler (pre-bound with handleSubmit)\n onSubmit: (e?: React.BaseSyntheticEvent) => void;\n \n // Loading state (combines isPending and isTransitioning)\n isSubmitting: boolean;\n \n // Redirecting state (true when redirect is in progress)\n isRedirecting: boolean;\n \n // The current action state\n actionState: ServerActionResponse<TOutput>;\n \n // Utilities\n reset: () => void;\n \n // Raw handlers if needed\n handleSubmit: (data: TFieldValues) => Promise<void>;\n}\n\nfunction isSuccessResponse<T>(response: ServerActionResponse<T>): response is { success: true; data: T; redirect?: string | RedirectConfig } {\n return response.success === true;\n}\n\nfunction objectToFormData(obj: any): FormData {\n const formData = new FormData();\n \n Object.entries(obj).forEach(([key, value]) => {\n if (value !== undefined && value !== null) {\n if (value instanceof File || value instanceof Blob) {\n formData.append(key, value);\n } else if (Array.isArray(value)) {\n value.forEach((item) => formData.append(`${key}[]`, String(item)));\n } else if (typeof value === \"object\") {\n formData.append(key, JSON.stringify(value));\n } else {\n formData.append(key, String(value));\n }\n }\n });\n \n return formData;\n}\n\nexport function useFormAction<TFieldValues extends FieldValues = FieldValues, TOutput = void>({\n action,\n schema,\n defaultValues,\n mode = \"onChange\",\n transformData,\n onSuccess,\n onError,\n resetOnSuccess = false,\n showSuccessToast = false,\n showErrorToast = true,\n preventRedirect = false,\n redirectDelay = 0,\n}: UseFormActionOptions<TFieldValues, TOutput>): UseFormActionReturn<TFieldValues, TOutput> {\n const router = useRouter();\n const [isRedirecting, setIsRedirecting] = useState(false);\n \n // Store callbacks in refs to prevent effect re-runs\n const onSuccessRef = useRef(onSuccess);\n const onErrorRef = useRef(onError);\n const showSuccessToastRef = useRef(showSuccessToast);\n const showErrorToastRef = useRef(showErrorToast);\n \n // Update refs when callbacks change\n useEffect(() => {\n onSuccessRef.current = onSuccess;\n onErrorRef.current = onError;\n showSuccessToastRef.current = showSuccessToast;\n showErrorToastRef.current = showErrorToast;\n });\n \n // 1. Setup form with React Hook Form\n const form = useForm<TFieldValues>({\n resolver: schema ? (zodResolver as any)(schema) : undefined,\n defaultValues,\n mode,\n } as UseFormProps<TFieldValues>);\n \n // 2. Setup server action state with useActionState\n const initialState: ServerActionResponse<TOutput> = { success: true, data: undefined as TOutput };\n const [actionState, formAction, isPending] = useActionState(action, initialState);\n const [isTransitioning, startTransition] = useTransition();\n \n // 3. Handle server errors and success\n useEffect(() => {\n // Skip initial state\n if (actionState.success === true && actionState.data === undefined) {\n return;\n }\n \n // Reset redirecting state if we're processing a new action\n setIsRedirecting(false);\n \n // Keep track of timeout for cleanup\n let timeoutId: NodeJS.Timeout | undefined;\n \n if (!isSuccessResponse(actionState) && actionState.error) {\n const { error } = actionState;\n \n // Map field errors to form\n if (error.fields) {\n Object.entries(error.fields).forEach(([field, messages]) => {\n if (Array.isArray(messages) && messages.length > 0) {\n form.setError(field as Path<TFieldValues>, {\n type: \"server\",\n message: messages[0],\n });\n }\n });\n }\n \n // Single field error\n if (error.field && error.message && !error.fields) {\n form.setError(error.field as Path<TFieldValues>, {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Global error (no specific field)\n if (error.message && !error.field && !error.fields) {\n const showToast = showErrorToastRef.current;\n if (showToast) {\n const message = typeof showToast === \"function\" \n ? showToast(error)\n : typeof showToast === \"string\"\n ? showToast\n : error.message;\n toast.error(message);\n }\n \n // Also set on root for inline display\n form.setError(\"root\", {\n type: \"server\",\n message: error.message,\n });\n }\n \n // Call error callback\n onErrorRef.current?.(error);\n }\n \n // Handle success\n if (isSuccessResponse(actionState) && actionState.data !== undefined) {\n const showToast = showSuccessToastRef.current;\n if (showToast) {\n const message = typeof showToast === \"function\"\n ? showToast(actionState.data)\n : typeof showToast === \"string\"\n ? showToast\n : \"Success!\";\n toast.success(message);\n }\n \n if (resetOnSuccess) {\n form.reset();\n }\n \n // Call success callback first\n onSuccessRef.current?.(actionState.data);\n \n // Handle redirect if present in response - delay to ensure state updates complete\n if (actionState.redirect && !preventRedirect) {\n setIsRedirecting(true);\n const redirectConfig = typeof actionState.redirect === 'string'\n ? { url: actionState.redirect }\n : actionState.redirect;\n \n const delay = redirectConfig.delay ?? redirectDelay;\n \n timeoutId = setTimeout(() => {\n if (redirectConfig.replace) {\n router.replace(redirectConfig.url);\n } else {\n router.push(redirectConfig.url);\n }\n setIsRedirecting(false);\n }, delay || 100); // Add small delay by default\n }\n }\n \n // Always return cleanup function\n return () => {\n if (timeoutId) {\n clearTimeout(timeoutId);\n }\n };\n }, [actionState]); // Minimal dependencies - only actionState\n \n // 4. Submit handler\n const handleSubmit = async (data: TFieldValues): Promise<void> => {\n // Clear any previous errors\n form.clearErrors();\n \n // Transform to FormData\n const formData = transformData \n ? transformData(data)\n : objectToFormData(data);\n \n // Execute with transition for better UX\n startTransition(() => {\n formAction(formData);\n });\n };\n \n // 5. Combined loading state\n const isSubmitting = isPending || isTransitioning;\n \n // 6. Pre-bound submit handler\n const onSubmit = (e?: React.BaseSyntheticEvent): void => {\n e?.preventDefault();\n void form.handleSubmit(handleSubmit)(e!);\n };\n \n return {\n form,\n onSubmit,\n isSubmitting,\n isRedirecting,\n actionState,\n reset: form.reset,\n handleSubmit,\n };\n}","\"use client\";\n\nimport { useEffect } from \"react\";\nimport { toast } from \"sonner\";\n\nexport function ToastRestorer() {\n useEffect(() => {\n // Only run on client side\n if (typeof window === \"undefined\") return;\n \n const storageKey = \"sonner-toasts\";\n \n try {\n const storedToasts = localStorage.getItem(storageKey);\n \n if (storedToasts) {\n const persistentToasts = JSON.parse(storedToasts);\n \n if (Array.isArray(persistentToasts) && persistentToasts.length > 0) {\n // Clear the storage immediately to prevent duplicate toasts\n localStorage.removeItem(storageKey);\n \n // Filter out old toasts (older than 30 seconds)\n const recentToasts = persistentToasts.filter(\n (t: any) => Date.now() - (t.createdAt || 0) < 30000\n );\n \n // Restore toasts with staggered animation like in the PR\n recentToasts.forEach((persistedToast: any, index: number) => {\n // Skip loading toasts as per the PR implementation\n if (persistedToast.type === \"loading\") return;\n \n setTimeout(() => {\n const toastFunction = toast[persistedToast.type as keyof typeof toast];\n \n if (typeof toastFunction === \"function\") {\n // Check if current sonner supports persistent\n if (toastFunction.length >= 2) {\n // Use persistent if available\n (toastFunction as any)(persistedToast.message, { \n persistent: true,\n id: persistedToast.id \n });\n } else {\n // Fallback to normal toast\n toastFunction(persistedToast.message);\n }\n }\n }, index * 150); // Staggered delay like in the PR\n });\n }\n }\n } catch (error) {\n console.error(\"Failed to restore toasts:\", error);\n // Clean up on error\n localStorage.removeItem(storageKey);\n }\n }, []);\n \n return null;\n}"],"mappings":";AAAA,SAAS,aAAa,UAAU,qBAAqB;AACrD,SAAS,iBAAiB;AAC1B,SAAS,aAAa;;;ACAtB,IAAK,qBAAL,kBAAKA,wBAAL;AACC,EAAAA,wCAAA,cAAW,OAAX;AACA,EAAAA,wCAAA,uBAAoB,OAApB;AACA,EAAAA,wCAAA,uBAAoB,OAApB;AAHI,SAAAA;AAAA,GAAA;AAML,IAAM,sBAAsB;AAkBrB,SAAS,gBAAgB,OAAw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C,YAAY;AAAA,oBACZ,IAAI,eAAe;AAAA,kBACrB,CAAC;AAAA,gBACH,OAAO;AAEL,gCAAc,eAAe,OAAO;AAAA,gBACtC;AAAA,cACF;AAAA,YACF,GAAG,QAAQ,GAAG;AAAA,UAChB,CAAC;AAAA,QACH;AAAA,MACF;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,6BAA6B,KAAK;AAEhD,mBAAa,WAAW,UAAU;AAAA,IACpC;AAAA,EACF,GAAG,CAAC,CAAC;AAEL,SAAO;AACT;","names":["RedirectStatusCode","result","useTransition","useTransition","useTransition","useState","useRouter","toast","useEffect","toast"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "next-action-forge",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "description": "A simple, type-safe toolkit for Next.js server actions with Zod validation",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",