fix zap optimistic response

This commit is contained in:
keyan 2024-07-01 14:56:17 -05:00
parent ca11ac9fb8
commit 1e9d1ce66c

View File

@ -59,6 +59,7 @@ export default function ItemAct ({ onClose, item, down, children, abortSignal })
}, [onClose, item.id]) }, [onClose, item.id])
const act = useAct() const act = useAct()
const strike = useLightning()
const onSubmit = useCallback(async ({ amount, hash, hmac }) => { const onSubmit = useCallback(async ({ amount, hash, hmac }) => {
if (abortSignal && zapUndoTrigger({ me, amount })) { if (abortSignal && zapUndoTrigger({ me, amount })) {
@ -82,6 +83,7 @@ export default function ItemAct ({ onClose, item, down, children, abortSignal })
optimisticResponse: me optimisticResponse: me
? { ? {
act: { act: {
__typename: 'ItemActPaidAction',
result: { result: {
id: item.id, sats: Number(amount), act: down ? 'DONT_LIKE_THIS' : 'TIP', path: item.path id: item.id, sats: Number(amount), act: down ? 'DONT_LIKE_THIS' : 'TIP', path: item.path
} }
@ -90,12 +92,13 @@ export default function ItemAct ({ onClose, item, down, children, abortSignal })
: undefined, : undefined,
// don't close modal immediately because we want the QR modal to stack // don't close modal immediately because we want the QR modal to stack
onCompleted: () => { onCompleted: () => {
strike()
onClose?.() onClose?.()
if (!me) setItemMeAnonSats({ id: item.id, amount }) if (!me) setItemMeAnonSats({ id: item.id, amount })
} }
}) })
addCustomTip(Number(amount)) addCustomTip(Number(amount))
}, [me, act, down, item.id, onClose, abortSignal]) }, [me, act, down, item.id, onClose, abortSignal, strike])
return ( return (
<Form <Form
@ -174,7 +177,6 @@ export function useAct ({ query = ACT_MUTATION, ...options } = {}) {
// because the mutation name we use varies, // because the mutation name we use varies,
// we need to extract the result/invoice from the response // we need to extract the result/invoice from the response
const getPaidActionResult = data => Object.values(data)[0] const getPaidActionResult = data => Object.values(data)[0]
const strike = useLightning()
const [act] = usePaidMutation(query, { const [act] = usePaidMutation(query, {
...options, ...options,
@ -183,7 +185,6 @@ export function useAct ({ query = ACT_MUTATION, ...options } = {}) {
if (!response) return if (!response) return
modifyActCache(cache, response) modifyActCache(cache, response)
options?.update?.(cache, { data }) options?.update?.(cache, { data })
if (response.result) strike()
}, },
onPayError: (e, cache, { data }) => { onPayError: (e, cache, { data }) => {
const response = getPaidActionResult(data) const response = getPaidActionResult(data)
@ -205,7 +206,7 @@ export function useAct ({ query = ACT_MUTATION, ...options } = {}) {
export function useZap () { export function useZap () {
const act = useAct() const act = useAct()
const me = useMe() const me = useMe()
const strike = useLightning()
const toaster = useToast() const toaster = useToast()
return useCallback(async ({ item, abortSignal }) => { return useCallback(async ({ item, abortSignal }) => {
@ -215,10 +216,11 @@ export function useZap () {
const sats = nextTip(meSats, { ...me?.privates }) const sats = nextTip(meSats, { ...me?.privates })
const variables = { id: item.id, sats, act: 'TIP' } const variables = { id: item.id, sats, act: 'TIP' }
const optimisticResponse = { act: { result: { path: item.path, ...variables } } } const optimisticResponse = { act: { __typename: 'ItemActPaidAction', result: { path: item.path, ...variables } } }
try { try {
await abortSignal.pause({ me, amount: sats }) await abortSignal.pause({ me, amount: sats })
strike()
await act({ variables, optimisticResponse }) await act({ variables, optimisticResponse })
} catch (error) { } catch (error) {
if (error instanceof InvoiceCanceledError || error instanceof ActCanceledError) { if (error instanceof InvoiceCanceledError || error instanceof ActCanceledError) {
@ -229,7 +231,7 @@ export function useZap () {
toaster.danger('zap failed: ' + reason) toaster.danger('zap failed: ' + reason)
} }
}, [me?.id]) }, [me?.id, strike])
} }
export class ActCanceledError extends Error { export class ActCanceledError extends Error {