Skip to content

Commit

Permalink
[Clipclops] Delete message in dialog (#3849)
Browse files Browse the repository at this point in the history
* delete and copy

* add retry dialog if message send fails

* add layout animation

* fix `nextMessage` being incorrect

---------

Co-authored-by: Samuel Newman <[email protected]>
  • Loading branch information
haileyok and mozzius authored May 5, 2024
1 parent 7448c8f commit c4160c2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
27 changes: 24 additions & 3 deletions src/components/dms/MessageMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import React from 'react'
import {Pressable, View} from 'react-native'
import {LayoutAnimation, Pressable, View} from 'react-native'
import * as Clipboard from 'expo-clipboard'
import {ChatBskyConvoDefs} from '@atproto-labs/api'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {useChat} from 'state/messages'
import {ConvoStatus} from 'state/messages/convo'
import {useSession} from 'state/session'
import * as Toast from '#/view/com/util/Toast'
import {atoms as a, useTheme} from '#/alf'
Expand All @@ -31,7 +33,9 @@ export let MessageMenu = ({
const {_} = useLingui()
const t = useTheme()
const {currentAccount} = useSession()
const chat = useChat()
const deleteControl = usePromptControl()
const retryDeleteControl = usePromptControl()

const isFromSelf = message.sender?.did === currentAccount?.did

Expand All @@ -44,8 +48,14 @@ export let MessageMenu = ({
}, [_, message.text])

const onDelete = React.useCallback(() => {
// TODO delete the message
}, [])
if (chat.status !== ConvoStatus.Ready) return

LayoutAnimation.configureNext(LayoutAnimation.Presets.easeInEaseOut)
chat
.deleteMessage(message.id)
.then(() => Toast.show(_(msg`Message deleted`)))
.catch(() => retryDeleteControl.open())
}, [_, chat, message.id, retryDeleteControl])

const onReport = React.useCallback(() => {
// TODO report the message
Expand Down Expand Up @@ -114,6 +124,17 @@ export let MessageMenu = ({
confirmButtonColor="negative"
onConfirm={onDelete}
/>

<Prompt.Basic
control={retryDeleteControl}
title={_(msg`Failed to delete message`)}
description={_(
msg`An error occurred while trying to delete the message. Please try again.`,
)}
confirmButtonCta={_(msg`Retry`)}
confirmButtonColor="negative"
onConfirm={onDelete}
/>
</>
)
}
Expand Down
24 changes: 12 additions & 12 deletions src/state/messages/convo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type ConvoState =
convo: ChatBskyConvoDefs.ConvoView
error: undefined
isFetchingHistory: boolean
deleteMessage: (messageId: string) => void
deleteMessage: (messageId: string) => Promise<void>
sendMessage: (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => void
Expand All @@ -104,35 +104,35 @@ export type ConvoState =
convo: ChatBskyConvoDefs.ConvoView
error: undefined
isFetchingHistory: boolean
deleteMessage: (messageId: string) => void
deleteMessage: (messageId: string) => Promise<void>
sendMessage: (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => void
fetchMessageHistory: () => void
) => Promise<void>
fetchMessageHistory: () => Promise<void>
}
| {
status: ConvoStatus.Backgrounded
items: ConvoItem[]
convo: ChatBskyConvoDefs.ConvoView
error: undefined
isFetchingHistory: boolean
deleteMessage: (messageId: string) => void
deleteMessage: (messageId: string) => Promise<void>
sendMessage: (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => void
fetchMessageHistory: () => void
) => Promise<void>
fetchMessageHistory: () => Promise<void>
}
| {
status: ConvoStatus.Resuming
items: ConvoItem[]
convo: ChatBskyConvoDefs.ConvoView
error: undefined
isFetchingHistory: boolean
deleteMessage: (messageId: string) => void
deleteMessage: (messageId: string) => Promise<void>
sendMessage: (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => void
fetchMessageHistory: () => void
) => Promise<void>
fetchMessageHistory: () => Promise<void>
}
| {
status: ConvoStatus.Error
Expand Down Expand Up @@ -776,7 +776,7 @@ export class Convo {
}
return true
})
.map((item, i) => {
.map((item, i, arr) => {
let nextMessage = null
const isMessage = isConvoItemMessage(item)

Expand All @@ -786,7 +786,7 @@ export class Convo {
(ChatBskyConvoDefs.isMessageView(item.message) ||
ChatBskyConvoDefs.isDeletedMessageView(item.message))
) {
const next = items[i + 1]
const next = arr[i + 1]

if (
isConvoItemMessage(next) &&
Expand Down

0 comments on commit c4160c2

Please sign in to comment.