Skip to content

Commit

Permalink
Merge pull request #87 from xmtp/rygine/more-exports
Browse files Browse the repository at this point in the history
Export more functions, rename `hasTopic`
  • Loading branch information
rygine authored Sep 8, 2023
2 parents 4bda2e5 + 921d711 commit e911a1a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/shy-birds-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@xmtp/react-sdk": patch
---

- Rename `hasTopic` => `hasConversationTopic`
- Add exports for `hasConversationTopic`, `saveConversation`, `setConversationUpdatedAt`, `updateConversation`, `updateConversationMetadata`, `deleteMessage`, `getLastMessage`, `saveMessage`, `updateMessage`, and `updateMessageMetadata`
8 changes: 4 additions & 4 deletions packages/react-sdk/src/helpers/caching/conversations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getCachedConversationByPeerAddress,
getCachedConversationByTopic,
getConversationByTopic,
hasTopic,
hasConversationTopic,
saveConversation,
setConversationUpdatedAt,
toCachedConversation,
Expand Down Expand Up @@ -198,7 +198,7 @@ describe("setConversationUpdatedAt", () => {
});
});

describe("hasTopic", () => {
describe("hasConversationTopic", () => {
it("should return true if the topic exists", async () => {
const createdAt = new Date();
const testConversation = {
Expand All @@ -213,11 +213,11 @@ describe("hasTopic", () => {
const cachedConversation = await saveConversation(testConversation, db);
expect(cachedConversation).toEqual(testConversation);

expect(await hasTopic("testTopic", db)).toBe(true);
expect(await hasConversationTopic("testTopic", db)).toBe(true);
});

it("should return false if the topic does not exist", async () => {
expect(await hasTopic("testTopic", db)).toBe(false);
expect(await hasConversationTopic("testTopic", db)).toBe(false);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/react-sdk/src/helpers/caching/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const setConversationUpdatedAt = async (
/**
* Check to see if a topic exists in the conversations cache
*/
export const hasTopic = async (topic: string, db: Dexie) => {
export const hasConversationTopic = async (topic: string, db: Dexie) => {
const existing = await getCachedConversationByTopic(topic, db);
return !!existing;
};
Expand Down
11 changes: 5 additions & 6 deletions packages/react-sdk/src/hooks/useConversation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CachedConversation } from "@/helpers/caching/conversations";
import {
getCachedConversationByTopic,
getConversationByTopic,
hasTopic as _hasTopic,
hasConversationTopic as _hasConversationTopic,
saveConversation as _saveConversation,
updateConversation as _updateConversation,
updateConversationMetadata,
Expand Down Expand Up @@ -84,16 +84,15 @@ export const useConversation = () => {
RemoveLastParameter<typeof _getLastMessage>
>(async (topic) => _getLastMessage(topic, db), [db]);

const hasTopic = useCallback<RemoveLastParameter<typeof _hasTopic>>(
async (topic) => _hasTopic(topic, db),
[db],
);
const hasConversationTopic = useCallback<
RemoveLastParameter<typeof _hasConversationTopic>
>(async (topic) => _hasConversationTopic(topic, db), [db]);

return {
getByTopic,
getCachedByTopic,
getCachedByPeerAddress,
getLastMessage,
hasTopic,
hasConversationTopic,
};
};
6 changes: 3 additions & 3 deletions packages/react-sdk/src/hooks/useConversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const useConversations = (options?: UseConversationsOptions) => {
const { client } = useClient();
const { processMessage } = useMessage();
const { saveConversation } = useConversationInternal();
const { hasTopic } = useConversation();
const { hasConversationTopic } = useConversation();
const conversations = useCachedConversations();
// to prevent conversations from being fetched multiple times
const loadingRef = useRef(false);
Expand Down Expand Up @@ -66,7 +66,7 @@ export const useConversations = (options?: UseConversationsOptions) => {
conversationList.map(async (conversation) => {
// only save the conversation and fetch its latest message if it
// doesn't already exist
if (!(await hasTopic(conversation.topic))) {
if (!(await hasConversationTopic(conversation.topic))) {
const cachedConversation = await saveConversation(
toCachedConversation(conversation, client.address),
);
Expand Down Expand Up @@ -106,7 +106,7 @@ export const useConversations = (options?: UseConversationsOptions) => {
client,
saveConversation,
processMessage,
hasTopic,
hasConversationTopic,
]);

return {
Expand Down
10 changes: 10 additions & 0 deletions packages/react-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export {
getCachedConversationByPeerAddress,
getCachedConversationByTopic,
getConversationByTopic,
hasConversationTopic,
saveConversation,
setConversationUpdatedAt,
toCachedConversation,
updateConversation,
updateConversationMetadata,
} from "./helpers/caching/conversations";

// messages
Expand All @@ -66,8 +71,13 @@ export type {
ProcessUnprocessedMessagesOptions,
} from "./helpers/caching/messages";
export {
deleteMessage,
getLastMessage,
getMessageByXmtpID,
saveMessage,
toCachedMessage,
updateMessage,
updateMessageMetadata,
} from "./helpers/caching/messages";

// attachments
Expand Down

0 comments on commit e911a1a

Please sign in to comment.