-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from xmtp/dj/read-receipts
Read Receipt Playground Implementation
- Loading branch information
Showing
13 changed files
with
251 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import db, { Conversation } from "../model/db"; | ||
import { useClient } from "./useClient"; | ||
import { sendMessage } from "../model/messages"; | ||
import { Client } from "@xmtp/xmtp-js"; | ||
import { ContentTypeReadReceipt } from "@xmtp/content-type-read-receipt"; | ||
import { useLiveQuery } from "dexie-react-hooks"; | ||
import { useMessages } from "./useMessages"; | ||
|
||
export function useReadReceipts(conversation: Conversation) { | ||
const client = useClient(); | ||
const messages = useMessages(conversation) || []; | ||
|
||
const isMostRecentMessageFromSelf = messages[messages.length - 1]?.sentByMe; | ||
const lastMessageTimestamp = messages[messages.length - 1]?.sentAt; | ||
|
||
const readReceiptsEnabled = | ||
window.localStorage.getItem("readReceiptsEnabled") === "true"; | ||
|
||
return useLiveQuery(async () => { | ||
try { | ||
const lastReadReceiptMessage = await db.readReceipts.get({ | ||
peerAddress: conversation.peerAddress, | ||
}); | ||
const isMostRecentMessageReadReceipt = | ||
lastMessageTimestamp < | ||
new Date(lastReadReceiptMessage?.timestamp as string); | ||
|
||
if (isMostRecentMessageFromSelf && isMostRecentMessageReadReceipt) { | ||
return true; | ||
} else if ( | ||
isMostRecentMessageFromSelf === false && | ||
isMostRecentMessageReadReceipt === false && | ||
readReceiptsEnabled | ||
) { | ||
void sendMessage( | ||
client as Client, | ||
conversation, | ||
{ | ||
timestamp: new Date().toISOString(), | ||
}, | ||
ContentTypeReadReceipt | ||
); | ||
return false; | ||
} else { | ||
return false; | ||
} | ||
} catch { | ||
console.error("Error sending read receipt"); | ||
} | ||
}, [messages]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.