refactor delete notification (#731)

Co-authored-by: ekzyis <ek@stacker.news>
This commit is contained in:
ekzyis 2024-01-03 23:24:46 +01:00 committed by GitHub
parent 8996fd085a
commit 31a40e15be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 23 additions and 17 deletions

View File

@ -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]
)

View File

@ -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()
}

View File

@ -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]
)

View File

@ -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]
)

View File

@ -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]
)

View File

@ -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]
)

View File

@ -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])

View File

@ -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)
}
}