Skip to content

Commit

Permalink
[🐴] Reorg convo files (#3909)
Browse files Browse the repository at this point in the history
* Remove unused prop

* Reorganize
  • Loading branch information
estrattonbailey authored May 8, 2024
1 parent 814ec2b commit 56f7130
Show file tree
Hide file tree
Showing 8 changed files with 296 additions and 278 deletions.
12 changes: 6 additions & 6 deletions src/components/dms/MessageMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ 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 {useConvo} from 'state/messages/convo'
import {ConvoStatus} from 'state/messages/convo/types'
import {useSession} from 'state/session'
import * as Toast from '#/view/com/util/Toast'
import {atoms as a, useTheme} from '#/alf'
Expand All @@ -33,7 +33,7 @@ export let MessageMenu = ({
const {_} = useLingui()
const t = useTheme()
const {currentAccount} = useSession()
const chat = useChat()
const convo = useConvo()
const deleteControl = usePromptControl()
const retryDeleteControl = usePromptControl()

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

const onDelete = React.useCallback(() => {
if (chat.status !== ConvoStatus.Ready) return
if (convo.status !== ConvoStatus.Ready) return

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

const onReport = React.useCallback(() => {
// TODO report the message
Expand Down
2 changes: 1 addition & 1 deletion src/screens/Messages/Conversation/MessageListError.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {View} from 'react-native'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {ConvoItem, ConvoItemError} from '#/state/messages/convo'
import {ConvoItem, ConvoItemError} from '#/state/messages/convo/types'
import {atoms as a, useTheme} from '#/alf'
import {CircleInfo_Stroke2_Corner0_Rounded as CircleInfo} from '#/components/icons/CircleInfo'
import {InlineLinkText} from '#/components/Link'
Expand Down
22 changes: 11 additions & 11 deletions src/screens/Messages/Conversation/MessagesList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {msg, Trans} from '@lingui/macro'
import {useLingui} from '@lingui/react'

import {isIOS} from '#/platform/detection'
import {useChat} from '#/state/messages'
import {ConvoItem, ConvoStatus} from '#/state/messages/convo'
import {useConvo} from '#/state/messages/convo'
import {ConvoItem, ConvoStatus} from '#/state/messages/convo/types'
import {ScrollProvider} from 'lib/ScrollContext'
import {isWeb} from 'platform/detection'
import {List} from 'view/com/util/List'
Expand Down Expand Up @@ -86,7 +86,7 @@ function onScrollToIndexFailed() {
}

export function MessagesList() {
const chat = useChat()
const convo = useConvo()
const flatListRef = useRef<FlatList>(null)

// We need to keep track of when the scroll offset is at the bottom of the list to know when to scroll as new items
Expand Down Expand Up @@ -153,20 +153,20 @@ export function MessagesList() {
// The check for `hasInitiallyScrolled` prevents an initial fetch on mount. FlatList triggers `onStartReached`
// immediately on mount, since we are in fact at an offset of zero, so we have to ignore those initial calls.
const onStartReached = useCallback(() => {
if (chat.status === ConvoStatus.Ready && hasInitiallyScrolled) {
chat.fetchMessageHistory()
if (convo.status === ConvoStatus.Ready && hasInitiallyScrolled) {
convo.fetchMessageHistory()
}
}, [chat, hasInitiallyScrolled])
}, [convo, hasInitiallyScrolled])

const onSendMessage = useCallback(
(text: string) => {
if (chat.status === ConvoStatus.Ready) {
chat.sendMessage({
if (convo.status === ConvoStatus.Ready) {
convo.sendMessage({
text,
})
}
},
[chat],
[convo],
)

const onScroll = React.useCallback(
Expand Down Expand Up @@ -229,7 +229,7 @@ export function MessagesList() {
<ScrollProvider onScroll={onScroll} onMomentumEnd={onMomentumEnd}>
<List
ref={flatListRef}
data={chat.items}
data={convo.items}
renderItem={renderItem}
keyExtractor={keyExtractor}
disableVirtualization={true}
Expand All @@ -248,7 +248,7 @@ export function MessagesList() {
onScrollToIndexFailed={onScrollToIndexFailed}
scrollEventThrottle={100}
ListHeaderComponent={
<MaybeLoader isLoading={chat.isFetchingHistory} />
<MaybeLoader isLoading={convo.isFetchingHistory} />
}
/>
</ScrollProvider>
Expand Down
28 changes: 14 additions & 14 deletions src/screens/Messages/Conversation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import {useGate} from '#/lib/statsig/statsig'
import {useCurrentConvoId} from '#/state/messages/current-convo-id'
import {BACK_HITSLOP} from 'lib/constants'
import {isWeb} from 'platform/detection'
import {ChatProvider, useChat} from 'state/messages'
import {ConvoStatus} from 'state/messages/convo'
import {ConvoProvider, useConvo} from 'state/messages/convo'
import {ConvoStatus} from 'state/messages/convo/types'
import {PreviewableUserAvatar} from 'view/com/util/UserAvatar'
import {CenteredView} from 'view/com/util/Views'
import {MessagesList} from '#/screens/Messages/Conversation/MessagesList'
Expand Down Expand Up @@ -46,23 +46,23 @@ export function MessagesConversationScreen({route}: Props) {
if (!gate('dms')) return <ClipClopGate />

return (
<ChatProvider convoId={convoId}>
<ConvoProvider convoId={convoId}>
<Inner />
</ChatProvider>
</ConvoProvider>
)
}

function Inner() {
const chat = useChat()
const convo = useConvo()

if (
chat.status === ConvoStatus.Uninitialized ||
chat.status === ConvoStatus.Initializing
convo.status === ConvoStatus.Uninitialized ||
convo.status === ConvoStatus.Initializing
) {
return <ListMaybePlaceholder isLoading />
}

if (chat.status === ConvoStatus.Error) {
if (convo.status === ConvoStatus.Error) {
// TODO
return (
<View>
Expand All @@ -71,7 +71,7 @@ function Inner() {
<Button
label="Retry"
onPress={() => {
chat.error.retry()
convo.error.retry()
}}>
<ButtonText>Retry</ButtonText>
</Button>
Expand All @@ -81,13 +81,13 @@ function Inner() {
}

/*
* Any other chat states (atm) are "ready" states
* Any other convo states (atm) are "ready" states
*/

return (
<KeyboardProvider>
<CenteredView style={{flex: 1}} sideBorders>
<Header profile={chat.recipients[0]} />
<Header profile={convo.recipients[0]} />
<MessagesList />
</CenteredView>
</KeyboardProvider>
Expand All @@ -103,7 +103,7 @@ let Header = ({
const {_} = useLingui()
const {gtTablet} = useBreakpoints()
const navigation = useNavigation<NavigationProp>()
const chat = useChat()
const convo = useConvo()

const onPressBack = useCallback(() => {
if (isWeb) {
Expand Down Expand Up @@ -157,9 +157,9 @@ let Header = ({
{profile.displayName}
</Text>
</View>
{chat.status === ConvoStatus.Ready ? (
{convo.status === ConvoStatus.Ready ? (
<ConvoMenu
convo={chat.convo}
convo={convo.convo}
profile={profile}
onUpdateConvo={onUpdateConvo}
currentScreen="conversation"
Expand Down
183 changes: 10 additions & 173 deletions src/state/messages/convo.ts → src/state/messages/convo/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,178 +9,16 @@ import {nanoid} from 'nanoid/non-secure'

import {logger} from '#/logger'
import {isNative} from '#/platform/detection'

export type ConvoParams = {
convoId: string
agent: BskyAgent
__tempFromUserDid: string
}

export enum ConvoStatus {
Uninitialized = 'uninitialized',
Initializing = 'initializing',
Ready = 'ready',
Error = 'error',
Backgrounded = 'backgrounded',
Suspended = 'suspended',
}

export enum ConvoItemError {
HistoryFailed = 'historyFailed',
PollFailed = 'pollFailed',
Network = 'network',
}

export enum ConvoErrorCode {
InitFailed = 'initFailed',
}

export type ConvoError = {
code: ConvoErrorCode
exception?: Error
retry: () => void
}

export enum ConvoDispatchEvent {
Init = 'init',
Ready = 'ready',
Resume = 'resume',
Background = 'background',
Suspend = 'suspend',
Error = 'error',
}

export type ConvoDispatch =
| {
event: ConvoDispatchEvent.Init
}
| {
event: ConvoDispatchEvent.Ready
}
| {
event: ConvoDispatchEvent.Resume
}
| {
event: ConvoDispatchEvent.Background
}
| {
event: ConvoDispatchEvent.Suspend
}
| {
event: ConvoDispatchEvent.Error
payload: ConvoError
}

export type ConvoItem =
| {
type: 'message' | 'pending-message'
key: string
message: ChatBskyConvoDefs.MessageView
nextMessage:
| ChatBskyConvoDefs.MessageView
| ChatBskyConvoDefs.DeletedMessageView
| null
}
| {
type: 'deleted-message'
key: string
message: ChatBskyConvoDefs.DeletedMessageView
nextMessage:
| ChatBskyConvoDefs.MessageView
| ChatBskyConvoDefs.DeletedMessageView
| null
}
| {
type: 'pending-retry'
key: string
retry: () => void
}
| {
type: 'error-recoverable'
key: string
code: ConvoItemError
retry: () => void
}

export type ConvoState =
| {
status: ConvoStatus.Uninitialized
items: []
convo: undefined
error: undefined
sender: undefined
recipients: undefined
isFetchingHistory: false
deleteMessage: undefined
sendMessage: undefined
fetchMessageHistory: undefined
}
| {
status: ConvoStatus.Initializing
items: []
convo: undefined
error: undefined
sender: undefined
recipients: undefined
isFetchingHistory: boolean
deleteMessage: undefined
sendMessage: undefined
fetchMessageHistory: undefined
}
| {
status: ConvoStatus.Ready
items: ConvoItem[]
convo: ChatBskyConvoDefs.ConvoView
error: undefined
sender: AppBskyActorDefs.ProfileViewBasic
recipients: AppBskyActorDefs.ProfileViewBasic[]
isFetchingHistory: boolean
deleteMessage: (messageId: string) => Promise<void>
sendMessage: (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => void
fetchMessageHistory: () => void
}
| {
status: ConvoStatus.Suspended
items: ConvoItem[]
convo: ChatBskyConvoDefs.ConvoView
error: undefined
sender: AppBskyActorDefs.ProfileViewBasic
recipients: AppBskyActorDefs.ProfileViewBasic[]
isFetchingHistory: boolean
deleteMessage: (messageId: string) => Promise<void>
sendMessage: (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => Promise<void>
fetchMessageHistory: () => Promise<void>
}
| {
status: ConvoStatus.Backgrounded
items: ConvoItem[]
convo: ChatBskyConvoDefs.ConvoView
error: undefined
sender: AppBskyActorDefs.ProfileViewBasic
recipients: AppBskyActorDefs.ProfileViewBasic[]
isFetchingHistory: boolean
deleteMessage: (messageId: string) => Promise<void>
sendMessage: (
message: ChatBskyConvoSendMessage.InputSchema['message'],
) => Promise<void>
fetchMessageHistory: () => Promise<void>
}
| {
status: ConvoStatus.Error
items: []
convo: undefined
error: any
sender: undefined
recipients: undefined
isFetchingHistory: false
deleteMessage: undefined
sendMessage: undefined
fetchMessageHistory: undefined
}
import {
ConvoDispatch,
ConvoDispatchEvent,
ConvoErrorCode,
ConvoItem,
ConvoItemError,
ConvoParams,
ConvoState,
ConvoStatus,
} from '#/state/messages/convo/types'

const ACTIVE_POLL_INTERVAL = 1e3
const BACKGROUND_POLL_INTERVAL = 10e3
Expand Down Expand Up @@ -235,7 +73,6 @@ export class Convo {
private headerItems: Map<string, ConvoItem> = new Map()

private isProcessingPendingMessages = false
private pendingPoll: Promise<void> | undefined
private nextPoll: NodeJS.Timeout | undefined

convoId: string
Expand Down
Loading

0 comments on commit 56f7130

Please sign in to comment.