Skip to content

Commit f5f9977

Browse files
committed
fix: thread draft bugs, thread draft pending tasks, schema and mapping
1 parent c02a837 commit f5f9977

File tree

5 files changed

+17
-11
lines changed

5 files changed

+17
-11
lines changed

examples/SampleApp/src/utils/useCreateDraftFocusEffect.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@ export const useCreateDraftFocusEffect = () => {
99
useFocusEffect(
1010
useCallback(() => {
1111
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-
}
12+
await messageComposer.createDraft();
1713
});
1814
}, [navigation, messageComposer]),
1915
);

package/src/store/SqliteClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { PreparedBatchQueries, PreparedQueries, Scalar, Table } from './typ
2828
* This way usage @op-engineering/op-sqlite package is scoped to a single class/file.
2929
*/
3030
export class SqliteClient {
31-
static dbVersion = 10;
31+
static dbVersion = 11;
3232

3333
static dbName = DB_NAME;
3434
static dbLocation = DB_LOCATION;

package/src/store/apis/addPendingTask.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,23 @@ import { SqliteClient } from '../SqliteClient';
1414
*/
1515
export const addPendingTask = async (task: PendingTask) => {
1616
const storable = mapTaskToStorable(task);
17-
const { channelId, channelType, payload, type } = storable;
18-
const query = createUpsertQuery('pendingTasks', storable);
17+
const { channelId, channelType, parentId, payload, type } = storable;
18+
const queries = [];
19+
if (type === 'create-draft' || type === 'delete-draft') {
20+
// Only one draft pending task is allowed per entity (i.e thread, channel etc).
21+
// If multiple arrive, we'll simply take the last one (since deleteDraft does not
22+
// fail as an API if a draft doesn't exist).
23+
queries.push(createDeleteQuery('pendingTasks', { channelId, channelType, parentId }));
24+
}
25+
queries.push(createUpsertQuery('pendingTasks', storable));
1926
SqliteClient.logger?.('info', 'addPendingTask', {
2027
channelId,
2128
channelType,
2229
id: task.id,
2330
type,
2431
});
2532

26-
await SqliteClient.executeSql.apply(null, query);
33+
await SqliteClient.executeSqlBatch(queries);
2734

2835
return async () => {
2936
SqliteClient.logger?.('info', 'deletePendingTaskAfterAddition', {
@@ -39,6 +46,6 @@ export const addPendingTask = async (task: PendingTask) => {
3946
type,
4047
});
4148

42-
await SqliteClient.executeSql.apply(null, query);
49+
await SqliteClient.executeSqlBatch([query]);
4350
};
4451
};

package/src/store/mappers/mapStorableToTask.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { PendingTask } from 'stream-chat';
33
import type { TableRowJoinedUser } from '../types';
44

55
export const mapStorableToTask = (row: TableRowJoinedUser<'pendingTasks'>): PendingTask => {
6-
const { channelId, channelType, id, messageId, type } = row;
6+
const { channelId, channelType, parentId, id, messageId, type } = row;
77
return {
88
channelId,
99
channelType,
1010
id,
1111
messageId,
12+
parentId,
1213
payload: JSON.parse(row.payload),
1314
type,
1415
};

package/src/store/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ export const tables: Tables = {
175175
createdAt: 'TEXT',
176176
id: 'INTEGER PRIMARY KEY AUTOINCREMENT',
177177
messageId: 'TEXT',
178+
parentId: 'TEXT',
178179
payload: 'TEXT',
179180
type: 'TEXT',
180181
},
@@ -367,6 +368,7 @@ export type Schema = {
367368
createdAt: string;
368369
id: number;
369370
messageId: string;
371+
parentId: string;
370372
payload: string;
371373
type: ValueOf<PendingTaskTypes>;
372374
};

0 commit comments

Comments
 (0)