Skip to content

Commit 8590b45

Browse files
committed
fix: get drafts with parent message and cid
1 parent 6e3dd43 commit 8590b45

File tree

5 files changed

+112
-78
lines changed

5 files changed

+112
-78
lines changed

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type MessageInputPropsWithContext = Partial<
120120
>
121121
> &
122122
Pick<AttachmentPickerContextValue, 'bottomInset' | 'bottomSheetRef' | 'selectedPicker'> &
123-
Pick<ChatContextValue, 'client' | 'isOnline'> &
123+
Pick<ChatContextValue, 'isOnline'> &
124124
Pick<ChannelContextValue, 'channel' | 'members' | 'threadList' | 'watchers'> &
125125
Pick<
126126
MessageInputContextValue,
@@ -205,7 +205,6 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
205205
AudioRecordingPreview,
206206
AutoCompleteSuggestionList,
207207
channel,
208-
client,
209208
closeAttachmentPicker,
210209
closePollCreationDialog,
211210
cooldownEndsAt,
@@ -295,29 +294,10 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
295294
*/
296295
useEffect(() => {
297296
const threadId = messageComposer.threadId;
298-
if (!threadId || !messageComposer.channel || !messageComposer.compositionIsEmpty) return;
299-
300-
// This is for the offline db support for getting drafts for legacy thread composer.
301-
if (client.offlineDb && client.userID) {
302-
client.offlineDb
303-
.getDraft({
304-
cid: messageComposer.channel.cid,
305-
currentUserId: client.userID,
306-
parent_id: threadId,
307-
})
308-
.then((draft) => {
309-
if (draft) {
310-
messageComposer.initState({ composition: draft });
311-
}
312-
});
313-
}
297+
if (!threadId) return;
314298

315-
messageComposer.channel.getDraft({ parent_id: threadId }).then(({ draft }) => {
316-
if (draft) {
317-
messageComposer.initState({ composition: draft });
318-
}
319-
});
320-
}, [client.offlineDb, client.userID, messageComposer]);
299+
messageComposer.getDraft();
300+
}, [messageComposer]);
321301

322302
const getMembers = () => {
323303
const result: UserResponse[] = [];
@@ -781,7 +761,7 @@ export type MessageInputProps = Partial<MessageInputPropsWithContext>;
781761
* [Translation Context](https://getstream.io/chat/docs/sdk/reactnative/contexts/translation-context/)
782762
*/
783763
export const MessageInput = (props: MessageInputProps) => {
784-
const { client, isOnline } = useChatContext();
764+
const { isOnline } = useChatContext();
785765
const ownCapabilities = useOwnCapabilitiesContext();
786766

787767
const { channel, members, threadList, watchers } = useChannelContext();
@@ -875,7 +855,6 @@ export const MessageInput = (props: MessageInputProps) => {
875855
CameraSelectorIcon,
876856
channel,
877857
clearEditingState,
878-
client,
879858
closeAttachmentPicker,
880859
closePollCreationDialog,
881860
CommandInput,

package/src/store/apis/getDraft.ts

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,52 @@ import { TableRow } from '../types';
88

99
export const getDraft = async ({
1010
cid,
11-
currentUserId,
11+
userId,
1212
parent_id,
1313
}: {
1414
cid: string;
15-
currentUserId: string;
15+
userId: string;
1616
parent_id?: string;
1717
}) => {
1818
SqliteClient.logger?.('info', 'getDraft', { cid, parent_id });
1919

20-
const draftRowsWithMessage = await selectDraftMessageFromDraft([cid]);
20+
try {
21+
const draftRowsWithMessage = await selectDraftMessageFromDraft({
22+
cid,
23+
parent_id: parent_id ?? null,
24+
});
2125

22-
const draftRowWithMessage = draftRowsWithMessage[0];
26+
console.log('getDraft draftRowsWithMessage', draftRowsWithMessage);
2327

24-
if (!draftRowWithMessage) {
25-
return null;
26-
}
28+
if (!draftRowsWithMessage) return null;
29+
30+
const draftRowWithMessage = draftRowsWithMessage;
31+
32+
if (!draftRowWithMessage) {
33+
return null;
34+
}
35+
36+
const channelQuery = createSelectQuery('channels', ['*'], { cid });
37+
const channelRows = await SqliteClient.executeSql.apply(null, channelQuery);
2738

28-
const channelQuery = createSelectQuery('channels', ['*'], { cid });
29-
const channelRows = await SqliteClient.executeSql.apply(null, channelQuery);
30-
31-
const quotedMessageRows = await selectMessageForId(draftRowWithMessage.quotedMessageId);
32-
33-
const polls = (await SqliteClient.executeSql.apply(
34-
null,
35-
createSelectQuery('poll', ['*'], {
36-
id: quotedMessageRows?.poll_id,
37-
}),
38-
)) as unknown as TableRow<'poll'>[];
39-
40-
return mapStorableToDraft({
41-
channelRow: channelRows[0] as unknown as TableRow<'channels'>,
42-
currentUserId,
43-
draftRow: draftRowWithMessage,
44-
pollRow: polls[0],
45-
quotedMessageRow: quotedMessageRows,
46-
});
39+
const quotedMessageRows = await selectMessageForId(draftRowWithMessage.quotedMessageId);
40+
41+
const polls = (await SqliteClient.executeSql.apply(
42+
null,
43+
createSelectQuery('poll', ['*'], {
44+
id: quotedMessageRows?.poll_id,
45+
}),
46+
)) as unknown as TableRow<'poll'>[];
47+
48+
return mapStorableToDraft({
49+
channelRow: channelRows[0] as unknown as TableRow<'channels'>,
50+
currentUserId: userId,
51+
draftRow: draftRowWithMessage,
52+
pollRow: polls[0],
53+
quotedMessageRow: quotedMessageRows,
54+
});
55+
} catch (error) {
56+
console.error('Error in getDraft:', error);
57+
throw error;
58+
}
4759
};

package/src/store/apis/getDraftsForChannels.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DraftResponse } from 'stream-chat';
22

3-
import { selectDraftMessageFromDraft } from './queries/selectDraftMessageFromDraft';
3+
import { selectDraftMessageFromDraftForChannels } from './queries/selectDraftMessageFromDraftForChannels';
44
import { selectMessageForId } from './queries/selectMessageById';
55

66
import { mapStorableToDraft } from '../mappers/mapStorableToDraft';
@@ -17,7 +17,7 @@ export const getDraftForChannels = async ({
1717
}) => {
1818
SqliteClient.logger?.('info', 'getDraftsForChannel', { channelIds });
1919

20-
const draftRowsWithMessage = await selectDraftMessageFromDraft(channelIds);
20+
const draftRowsWithMessage = await selectDraftMessageFromDraftForChannels(channelIds);
2121

2222
/**
2323
* Filter out drafts without a parent ID, as we will not show them in the channel.
Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
import { tables } from '../../schema';
22
import { SqliteClient } from '../../SqliteClient';
3-
import type { TableRowJoinedDraftMessage } from '../../types';
3+
import { TableRowJoinedDraftMessage } from '../../types';
44

5-
export const selectDraftMessageFromDraft = async (
6-
cids?: string[],
7-
): Promise<TableRowJoinedDraftMessage<'draft'>[]> => {
8-
if (!cids || cids.length === 0) {
9-
return [];
10-
}
11-
12-
const questionMarks = Array(cids.length).fill('?').join(',');
5+
export const selectDraftMessageFromDraft = async ({
6+
cid,
7+
parent_id,
8+
}: {
9+
cid: string;
10+
parent_id: string | null;
11+
}): Promise<TableRowJoinedDraftMessage<'draft'> | undefined> => {
1312
const draftColumnNames = Object.keys(tables.draft.columns)
1413
.map((name) => `'${name}', a.${name}`)
1514
.join(', ');
1615
const draftMessageColumnNames = Object.keys(tables.draftMessage.columns)
1716
.map((name) => `'${name}', b.${name}`)
1817
.join(', ');
1918

20-
SqliteClient.logger?.('info', 'selectDraft', {
21-
cids,
19+
SqliteClient.logger?.('info', 'selectDraftMessageFromDraft', {
20+
cid,
21+
parent_id,
2222
});
2323

2424
const result = await SqliteClient.executeSql(
2525
`SELECT
26-
json_object(
27-
'draftMessage', json_object(
28-
${draftMessageColumnNames}
29-
),
30-
${draftColumnNames}
31-
) as value
32-
FROM draft a
33-
LEFT JOIN
34-
draftMessage b
35-
ON b.id = a.draftMessageId
36-
WHERE cid in (${questionMarks}) ORDER BY datetime(a.createdAt) DESC`,
37-
cids,
26+
json_object(
27+
'draftMessage', json_object(
28+
${draftMessageColumnNames}
29+
),
30+
${draftColumnNames}
31+
) as value
32+
FROM draft a
33+
LEFT JOIN
34+
draftMessage b
35+
ON b.id = a.draftMessageId
36+
WHERE a.cid = ? AND a.parentId is ?`,
37+
[cid, parent_id],
3838
);
3939

40-
return result.map((r) => JSON.parse(r.value));
40+
console.log('selectDraftMessageFromDraft result', result);
41+
42+
return result[0] ? JSON.parse(result[0].value) : undefined;
4143
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { tables } from '../../schema';
2+
import { SqliteClient } from '../../SqliteClient';
3+
import type { TableRowJoinedDraftMessage } from '../../types';
4+
5+
export const selectDraftMessageFromDraftForChannels = async (
6+
cids?: string[],
7+
): Promise<TableRowJoinedDraftMessage<'draft'>[]> => {
8+
if (!cids || cids.length === 0) {
9+
return [];
10+
}
11+
12+
const questionMarks = Array(cids.length).fill('?').join(',');
13+
const draftColumnNames = Object.keys(tables.draft.columns)
14+
.map((name) => `'${name}', a.${name}`)
15+
.join(', ');
16+
const draftMessageColumnNames = Object.keys(tables.draftMessage.columns)
17+
.map((name) => `'${name}', b.${name}`)
18+
.join(', ');
19+
20+
SqliteClient.logger?.('info', 'selectDraftMessageFromDraftForChannels', {
21+
cids,
22+
});
23+
24+
const result = await SqliteClient.executeSql(
25+
`SELECT
26+
json_object(
27+
'draftMessage', json_object(
28+
${draftMessageColumnNames}
29+
),
30+
${draftColumnNames}
31+
) as value
32+
FROM draft a
33+
LEFT JOIN
34+
draftMessage b
35+
ON b.id = a.draftMessageId
36+
WHERE cid in (${questionMarks}) ORDER BY datetime(a.createdAt) DESC`,
37+
cids,
38+
);
39+
40+
return result.map((r) => JSON.parse(r.value));
41+
};

0 commit comments

Comments
 (0)