Skip to content

Commit c02a837

Browse files
committed
fix: offline mode drafts and jsi quirks
1 parent 6516c4a commit c02a837

File tree

6 files changed

+39
-15
lines changed

6 files changed

+39
-15
lines changed

examples/SampleApp/src/screens/ChannelScreen.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { useChannelMembersStatus } from '../hooks/useChannelMembersStatus';
2424

2525
import type { StackNavigatorParamList } from '../types';
2626
import { NetworkDownIndicator } from '../components/NetworkDownIndicator';
27+
import { useCreateDraftFocusEffect } from '../utils/useCreateDraftFocusEffect.tsx';
2728

2829
export type ChannelScreenNavigationProp = StackNavigationProp<
2930
StackNavigatorParamList,
@@ -63,6 +64,8 @@ const ChannelHeader: React.FC<ChannelHeaderProps> = ({ channel }) => {
6364
}
6465
}, [navigation]);
6566

67+
useCreateDraftFocusEffect();
68+
6669
const onRightContentPress = useCallback(() => {
6770
closePicker();
6871
if (isOneOnOneConversation) {

examples/SampleApp/src/screens/ThreadScreen.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { RouteProp } from '@react-navigation/native';
1010

1111
import type { StackNavigatorParamList } from '../types';
1212
import { LocalMessage, ThreadState, UserResponse } from 'stream-chat';
13+
import { useCreateDraftFocusEffect } from '../utils/useCreateDraftFocusEffect.tsx';
1314

1415
const selector = (nextValue: ThreadState) => ({ parentMessage: nextValue.parentMessage }) as const;
1516

@@ -39,6 +40,8 @@ const ThreadHeader: React.FC<ThreadHeaderProps> = ({ thread }) => {
3940
subtitleText = (parentMessage?.user as UserResponse)?.name;
4041
}
4142

43+
useCreateDraftFocusEffect();
44+
4245
return (
4346
<ScreenHeader
4447
inSafeArea
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { useCallback } from 'react';
2+
import { useMessageComposer } from 'stream-chat-react-native';
3+
import { useFocusEffect, useNavigation } from '@react-navigation/native';
4+
5+
export const useCreateDraftFocusEffect = () => {
6+
const navigation = useNavigation();
7+
const messageComposer = useMessageComposer();
8+
9+
useFocusEffect(
10+
useCallback(() => {
11+
return navigation.addListener('beforeRemove', async () => {
12+
try {
13+
await messageComposer.createDraft();
14+
} catch (e) {
15+
console.error('Failed to save draft before remove', e);
16+
}
17+
});
18+
}, [navigation, messageComposer]),
19+
);
20+
};

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,6 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
280280
}
281281
}, [editing, inputBoxRef]);
282282

283-
// Effect to create draft whenever we un-mount the component.
284-
useEffect(
285-
() => () => {
286-
messageComposer.createDraft();
287-
},
288-
[messageComposer],
289-
);
290-
291283
/**
292284
* Effect to get the draft data for legacy thread composer and set it to message composer.
293285
* TODO: This can be removed once we remove legacy thread composer.

package/src/store/apis/upsertChannels.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const upsertChannels = async ({
4141
}
4242

4343
if (draft) {
44-
queries = queries.concat(await upsertDraft({ draft }));
44+
queries = queries.concat(await upsertDraft({ draft, execute: false }));
4545
}
4646

4747
queries = queries.concat(

package/src/store/apis/upsertDraft.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,23 @@ export const upsertDraft = async ({
4040
draftMessage: storableDraftMessage,
4141
});
4242

43+
const messagesToUpsert = [];
44+
4345
if (draft.quoted_message) {
44-
const query = await upsertMessages({ execute: false, messages: [draft.quoted_message] });
46+
messagesToUpsert.push(draft.quoted_message);
47+
}
48+
49+
if (draft.parent_message) {
50+
messagesToUpsert.push(draft.parent_message);
51+
}
52+
53+
if (messagesToUpsert.length > 0) {
54+
const query = await upsertMessages({ execute: false, messages: messagesToUpsert });
4555
queries.concat(query);
4656
}
4757

4858
if (execute) {
49-
try {
50-
await SqliteClient.executeSqlBatch(queries);
51-
} catch (error) {
52-
console.error(error);
53-
}
59+
await SqliteClient.executeSqlBatch(queries);
5460
}
5561

5662
return queries;

0 commit comments

Comments
 (0)