Merge pull request #962 from stackernews/more-zap-undo-fixes

More zap undo fixes II
This commit is contained in:
Keyan 2024-03-25 17:32:30 -05:00 committed by GitHub
commit e60f1b80d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 10 deletions

View File

@ -55,7 +55,7 @@ export default function ItemAct ({ onClose, itemId, down, children }) {
const [act, actUpdate] = useAct() const [act, actUpdate] = useAct()
const onSubmit = useCallback(async ({ amount, hash, hmac }, { update }) => { const onSubmit = useCallback(async ({ amount, hash, hmac }, { update, keepOpen }) => {
if (!me) { if (!me) {
const storageKey = `TIP-item:${itemId}` const storageKey = `TIP-item:${itemId}`
const existingAmount = Number(window.localStorage.getItem(storageKey) || '0') const existingAmount = Number(window.localStorage.getItem(storageKey) || '0')
@ -75,7 +75,7 @@ export default function ItemAct ({ onClose, itemId, down, children }) {
// due to optimistic UX on zap undos // due to optimistic UX on zap undos
if (!me || !me.privates.zapUndos) await strike() if (!me || !me.privates.zapUndos) await strike()
addCustomTip(Number(amount)) addCustomTip(Number(amount))
onClose() if (!keepOpen) onClose()
}, [me, act, down, itemId, strike]) }, [me, act, down, itemId, strike])
const onSubmitWithUndos = withToastFlow(toaster)( const onSubmitWithUndos = withToastFlow(toaster)(
@ -115,6 +115,7 @@ export default function ItemAct ({ onClose, itemId, down, children }) {
return { return {
skipToastFlow, skipToastFlow,
flowId, flowId,
tag: itemId,
type: 'zap', type: 'zap',
pendingMessage: `${down ? 'down' : ''}zapped ${sats} sats`, pendingMessage: `${down ? 'down' : ''}zapped ${sats} sats`,
onPending: async () => { onPending: async () => {
@ -127,7 +128,7 @@ export default function ItemAct ({ onClose, itemId, down, children }) {
undoUpdate = update() undoUpdate = update()
setTimeout(() => { setTimeout(() => {
if (canceled) return resolve() if (canceled) return resolve()
onSubmit(values, { flowId, ...args, update: null }) onSubmit(values, { flowId, ...args, update: null, keepOpen: true })
.then(resolve) .then(resolve)
.catch((err) => { .catch((err) => {
undoUpdate() undoUpdate()

View File

@ -67,6 +67,10 @@ export const ToastProvider = ({ children }) => {
})) }))
}, []) }, [])
const endFlow = useCallback((flowId) => {
setToasts(toasts => toasts.filter(toast => toast.flowId !== flowId))
}, [])
const toaster = useMemo(() => ({ const toaster = useMemo(() => ({
success: (body, options) => { success: (body, options) => {
const toast = { const toast = {
@ -99,8 +103,9 @@ export const ToastProvider = ({ children }) => {
...options ...options
} }
return dispatchToast(toast) return dispatchToast(toast)
} },
}), [dispatchToast, removeToast]) endFlow
}), [dispatchToast, removeToast, endFlow])
// Only clear toasts with no cancel function on page navigation // Only clear toasts with no cancel function on page navigation
// since navigation should not interfere with being able to cancel an action. // since navigation should not interfere with being able to cancel an action.
@ -213,9 +218,6 @@ export const withToastFlow = (toaster) => flowFn => {
if (skipToastFlow) return onPending() if (skipToastFlow) return onPending()
// XXX HACK this ends the flow by using flow toast which immediately closes itself
const endFlow = () => toaster.warning('', { ...toastProps, delay: 0, autohide: true, flowId })
toaster.warning(pendingMessage || `${t} pending`, { toaster.warning(pendingMessage || `${t} pending`, {
progressBar: !!timeout, progressBar: !!timeout,
delay: timeout || TOAST_DEFAULT_DELAY_MS, delay: timeout || TOAST_DEFAULT_DELAY_MS,
@ -247,7 +249,7 @@ export const withToastFlow = (toaster) => flowFn => {
const ret = await onPending() const ret = await onPending()
if (!canceled) { if (!canceled) {
if (hideSuccess) { if (hideSuccess) {
endFlow() toaster.endFlow(flowId)
} else { } else {
toaster.success(`${t} successful`, { ...toastProps, flowId }) toaster.success(`${t} successful`, { ...toastProps, flowId })
} }
@ -259,7 +261,7 @@ export const withToastFlow = (toaster) => flowFn => {
if (canceled) return if (canceled) return
const reason = err?.message?.toString().toLowerCase() || 'unknown reason' const reason = err?.message?.toString().toLowerCase() || 'unknown reason'
if (hideError) { if (hideError) {
endFlow() toaster.endFlow(flowId)
} else { } else {
toaster.danger(`${t} failed: ${reason}`, { ...toastProps, flowId }) toaster.danger(`${t} failed: ${reason}`, { ...toastProps, flowId })
} }