// Reusable async handler for CRUD actions with toast and state management export type ToastFn = (params: { title: string; description: string; variant: 'default' | 'destructive' }) => void export interface HandleAsyncActionOptions { action: (...args: T) => Promise args?: T toast: ToastFn successMessage: string errorMessage: string onSuccess?: (result: R) => void onError?: (error: unknown) => void onFinally?: (isSuccess: boolean) => void } export async function handleAsyncAction({ action, args = [] as unknown as T, toast, successMessage, errorMessage, onSuccess, onError, onFinally, }: HandleAsyncActionOptions) { let isSuccess = false try { const result = await action(...args) if (result.success) { toast({ title: 'Berhasil', description: successMessage, variant: 'default' }) isSuccess = true if (onSuccess) onSuccess(result) } else { toast({ title: 'Gagal', description: errorMessage, variant: 'destructive' }) if (onError) onError(result) } } catch (error) { toast({ title: 'Gagal', description: errorMessage, variant: 'destructive' }) if (onError) onError(error) } finally { if (onFinally) onFinally(isSuccess) } }