diff --git a/components/bounty-form.js b/components/bounty-form.js index 53a9db3e..ce629af1 100644 --- a/components/bounty-form.js +++ b/components/bounty-form.js @@ -81,7 +81,7 @@ export function BountyForm ({ const prefix = sub?.name ? `/~${sub.name}` : '' await router.push(prefix + '/recent') } - toastDeleteScheduled(toaster, data, !!item, values.text) + toastDeleteScheduled(toaster, data, 'upsertBounty', !!item, values.text) }, [upsertBounty, router] ) diff --git a/components/comment-edit.js b/components/comment-edit.js index 54d95871..1a6818cd 100644 --- a/components/comment-edit.js +++ b/components/comment-edit.js @@ -43,7 +43,7 @@ export default function CommentEdit ({ comment, editThreshold, onSuccess, onCanc if (error) { throw new Error({ message: error.toString() }) } - toastDeleteScheduled(toaster, data, true, values.text) + toastDeleteScheduled(toaster, data, 'upsertComment', true, values.text) if (onSuccess) { onSuccess() } diff --git a/components/discussion-form.js b/components/discussion-form.js index 412fc457..3aaa4b2c 100644 --- a/components/discussion-form.js +++ b/components/discussion-form.js @@ -104,7 +104,7 @@ export function DiscussionForm ({ const prefix = sub?.name ? `/~${sub.name}` : '' await router.push(prefix + '/recent') } - toastDeleteScheduled(toaster, data, !!item, values.text) + toastDeleteScheduled(toaster, data, 'upsertDiscussion', !!item, values.text) }, [upsertDiscussion, router, item, sub, crossposter] ) diff --git a/components/job-form.js b/components/job-form.js index 0b82308c..5d2e835d 100644 --- a/components/job-form.js +++ b/components/job-form.js @@ -82,7 +82,7 @@ export default function JobForm ({ item, sub }) { } else { await router.push(`/~${sub.name}/recent`) } - toastDeleteScheduled(toaster, data, !!item, values.text) + toastDeleteScheduled(toaster, data, 'upsertJob', !!item, values.text) }, [upsertJob, router, logoId] ) diff --git a/components/link-form.js b/components/link-form.js index 3ed84c7a..e2eed965 100644 --- a/components/link-form.js +++ b/components/link-form.js @@ -98,7 +98,7 @@ export function LinkForm ({ item, sub, editThreshold, children }) { const prefix = sub?.name ? `/~${sub.name}` : '' await router.push(prefix + '/recent') } - toastDeleteScheduled(toaster, data, !!item, values.text) + toastDeleteScheduled(toaster, data, 'upsertLink', !!item, values.text) }, [upsertLink, router] ) diff --git a/components/poll-form.js b/components/poll-form.js index 90518388..817a7da3 100644 --- a/components/poll-form.js +++ b/components/poll-form.js @@ -54,7 +54,7 @@ export function PollForm ({ item, sub, editThreshold, children }) { const prefix = sub?.name ? `/~${sub.name}` : '' await router.push(prefix + '/recent') } - toastDeleteScheduled(toaster, data, !!item, values.text) + toastDeleteScheduled(toaster, data, 'upsertPoll', !!item, values.text) }, [upsertPoll, router] ) diff --git a/components/reply.js b/components/reply.js index fa07b900..e675ee27 100644 --- a/components/reply.js +++ b/components/reply.js @@ -99,7 +99,7 @@ export default forwardRef(function Reply ({ item, onSuccess, replyOpen, children const onSubmit = useCallback(async ({ amount, hash, hmac, ...values }, { resetForm }) => { const { data } = await upsertComment({ variables: { parentId, hash, hmac, ...values } }) - toastDeleteScheduled(toaster, data, false, values.text) + toastDeleteScheduled(toaster, data, 'upsertComment', false, values.text) resetForm({ text: '' }) setReply(replyOpen || false) }, [upsertComment, setReply, parentId]) diff --git a/lib/form.js b/lib/form.js index 8f205552..f6f4e248 100644 --- a/lib/form.js +++ b/lib/form.js @@ -13,11 +13,22 @@ export const normalizeForwards = (forward) => { return forward.filter(fwd => fwd.nym || fwd.user?.name).map(fwd => ({ nym: fwd.nym ?? fwd.user?.name, pct: Number(fwd.pct) })) } -export const toastDeleteScheduled = (toaster, upsertResponseData, isEdit, itemText) => { - const keys = Object.keys(upsertResponseData) - const data = upsertResponseData[keys[0]] +export const toastDeleteScheduled = (toaster, upsertResponseData, dataKey, isEdit, itemText) => { + const data = upsertResponseData[dataKey] if (!data) return - const deleteScheduledAt = data.deleteScheduledAt ? new Date(data.deleteScheduledAt) : undefined + + const deleteMentioned = hasDeleteMention(itemText) + const deletedScheduled = !!data.deleteScheduledAt + + if (!deleteMentioned) return + if (deleteMentioned && !deletedScheduled) { + // There's a delete mention but the deletion wasn't scheduled + toaster.warning('it looks like you tried to use the delete bot but it didn\'t work. make sure you use the correct format: "@delete in n units" e.g. "@delete in 2 hours"', 10000) + return + } + + // when we reached this code, we know that a delete was scheduled + const deleteScheduledAt = new Date(data.deleteScheduledAt) if (deleteScheduledAt) { const itemType = { upsertDiscussion: 'discussion post', @@ -26,14 +37,9 @@ export const toastDeleteScheduled = (toaster, upsertResponseData, isEdit, itemTe upsertBounty: 'bounty', upsertJob: 'job', upsertComment: 'comment' - }[keys[0]] ?? 'item' + }[dataKey] ?? 'item' const message = `${itemType === 'comment' ? 'your comment' : isEdit ? `this ${itemType}` : `your new ${itemType}`} will be deleted at ${deleteScheduledAt.toLocaleString()}` toaster.success(message) - return - } - if (hasDeleteMention(itemText)) { - // There's a delete mention but the deletion wasn't scheduled - toaster.warning('it looks like you tried to use the delete bot but it didn\'t work. make sure you use the correct format: "@delete in n units" e.g. "@delete in 2 hours"', 10000) } }