Skip to content

Commit c001eeb

Browse files
committed
feat: implement selective fetching from offline db
1 parent d4fb3d4 commit c001eeb

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

package/src/components/MessageMenu/MessageUserReactions.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo } from 'react';
1+
import React, { useEffect, useMemo } from 'react';
22
import { StyleSheet, Text, View } from 'react-native';
33
import { FlatList } from 'react-native-gesture-handler';
44

@@ -80,6 +80,12 @@ export const MessageUserReactions = (props: MessageUserReactionsProps) => {
8080
setSelectedReaction(reactionType);
8181
};
8282

83+
useEffect(() => {
84+
if (selectedReaction && reactionTypes.length > 0 && !reactionTypes.includes(selectedReaction)) {
85+
setSelectedReaction(reactionTypes[0]);
86+
}
87+
}, [reactionTypes, selectedReaction]);
88+
8389
const messageReactions = useMemo(
8490
() =>
8591
reactionTypes.reduce<ReactionData[]>((acc, reaction) => {
@@ -120,6 +126,8 @@ export const MessageUserReactions = (props: MessageUserReactionsProps) => {
120126
} = useTheme();
121127
const { t } = useTranslationContext();
122128

129+
console.log('TESTTEST: ', fetchedReactions);
130+
123131
const reactions = useMemo(
124132
() =>
125133
propReactions ||

package/src/components/MessageMenu/hooks/useFetchReactions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export const useFetchReactions = ({
3636
const reactionsFromDB = await getReactionsForFilterSort({
3737
currentMessageId: messageId,
3838
filters: reactionType ? { type: reactionType } : {},
39+
limit,
3940
sort,
4041
});
4142
if (reactionsFromDB) {

package/src/store/apis/getReactionsforFilterSort.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ReactionFilters, ReactionResponse, ReactionSort } from 'stream-chat';
1+
import type { ReactionResponse, ReactionSort } from 'stream-chat';
22

33
import { getReactions } from './getReactions';
44
import { selectReactionsForMessages } from './queries/selectReactionsForMessages';
@@ -10,15 +10,18 @@ import { SqliteClient } from '../SqliteClient';
1010
* @param currentMessageId The message ID for which reactions are to be fetched.
1111
* @param filters The filters to be applied while fetching reactions.
1212
* @param sort The sort to be applied while fetching reactions.
13+
* @param limit The limit of how many reactions should be returned.
1314
*/
1415
export const getReactionsForFilterSort = async ({
1516
currentMessageId,
1617
filters,
1718
sort,
19+
limit,
1820
}: {
1921
currentMessageId: string;
20-
filters?: ReactionFilters;
22+
filters?: { type?: string };
2123
sort?: ReactionSort;
24+
limit?: number;
2225
}): Promise<ReactionResponse[] | null> => {
2326
if (!filters && !sort) {
2427
console.warn('Please provide the query (filters/sort) to fetch channels from DB');
@@ -27,7 +30,7 @@ export const getReactionsForFilterSort = async ({
2730

2831
SqliteClient.logger?.('info', 'getReactionsForFilterSort', { filters, sort });
2932

30-
const reactions = await selectReactionsForMessages([currentMessageId]);
33+
const reactions = await selectReactionsForMessages([currentMessageId], limit, filters, sort);
3134

3235
if (!reactions) {
3336
return null;
@@ -37,7 +40,8 @@ export const getReactionsForFilterSort = async ({
3740
return [];
3841
}
3942

40-
const filteredReactions = reactions.filter((reaction) => reaction.type === filters?.type);
43+
// const filteredReactions = reactions.filter((reaction) => reaction.type === filters?.type);
44+
// console.log('FILTERED', filteredReactions);
4145

42-
return getReactions({ reactions: filteredReactions });
46+
return getReactions({ reactions });
4347
};

package/src/store/apis/queries/selectReactionsForMessages.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
1+
import type { ReactionSort } from 'stream-chat';
2+
13
import { tables } from '../../schema';
24
import { SqliteClient } from '../../SqliteClient';
35
import type { TableRowJoinedUser } from '../../types';
46

57
/**
68
* Fetches reactions for a message from the database for messageIds.
79
* @param messageIds The message IDs for which reactions are to be fetched.
10+
* @param limit The limit of how many reactions should be returned.
11+
* @param filters A ReactionFilter for the reactions we want to fetch. Only type is currently supported.
12+
* @param sort A sort for reactions to be used when querying. Custom data is currently not supported for sorting.
813
*/
914
export const selectReactionsForMessages = async (
1015
messageIds: string[],
16+
limit: number = 25,
17+
filters?: { type?: string },
18+
sort?: ReactionSort,
1119
): Promise<TableRowJoinedUser<'reactions'>[]> => {
1220
const questionMarks = Array(messageIds.length).fill('?').join(',');
1321
const reactionsColumnNames = Object.keys(tables.reactions.columns)
@@ -16,6 +24,13 @@ export const selectReactionsForMessages = async (
1624
const userColumnNames = Object.keys(tables.users.columns)
1725
.map((name) => `'${name}', b.${name}`)
1826
.join(', ');
27+
const filterValue = filters?.type ? [filters.type] : [];
28+
const createdAtSort = Array.isArray(sort)
29+
? sort.find((s) => !!s.created_at)?.created_at
30+
: sort?.created_at;
31+
const orderByClause = createdAtSort
32+
? `ORDER BY cast(strftime('%s', a.createdAt) AS INTEGER) ${createdAtSort === 1 ? 'DESC' : 'ASC'}`
33+
: '';
1934

2035
SqliteClient.logger?.('info', 'selectReactionsForMessages', {
2136
messageIds,
@@ -33,8 +48,10 @@ export const selectReactionsForMessages = async (
3348
LEFT JOIN
3449
users b
3550
ON b.id = a.userId
36-
WHERE a.messageId in (${questionMarks})`,
37-
messageIds,
51+
WHERE a.messageId in (${questionMarks}) ${filters?.type ? `AND a.type = ?` : ''}
52+
${orderByClause}
53+
LIMIT ?`,
54+
[...messageIds, ...filterValue, limit],
3855
);
3956

4057
return result.map((r) => JSON.parse(r.value));

0 commit comments

Comments
 (0)