Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
37 lines
930 B
TypeScript
37 lines
930 B
TypeScript
import { useCallback, useState } from 'react'
|
|
|
|
// simplified version of react-query useMutation
|
|
export const useMutation = <T extends Array<unknown>, R>(
|
|
fn: (...props: T) => Promise<R>,
|
|
options?: {
|
|
onSuccess?: (data: R) => void
|
|
onError?: (error: unknown) => void
|
|
}
|
|
) => {
|
|
const { onSuccess, onError } = options ?? {}
|
|
const [isLoading, setLoading] = useState(false)
|
|
const [error, setError] = useState<unknown | null>(null)
|
|
const [data, setData] = useState<R | null>(null)
|
|
|
|
const mutate = useCallback(
|
|
async (...props: T) => {
|
|
setLoading(true)
|
|
setError(null)
|
|
try {
|
|
const data = await fn(...props)
|
|
setData(data)
|
|
onSuccess?.(data)
|
|
return data
|
|
} catch (error) {
|
|
setError(error)
|
|
onError?.(error)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
},
|
|
[fn, onError, onSuccess]
|
|
)
|
|
|
|
return { mutate, data, isLoading, error }
|
|
}
|