-
Notifications
You must be signed in to change notification settings - Fork 13
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 #62 from xmtp/rygine/attachments-update
Refactor processing of attachment content types
- Loading branch information
Showing
8 changed files
with
181 additions
and
70 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
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,5 @@ | ||
--- | ||
"@xmtp/react-sdk": minor | ||
--- | ||
|
||
Refactor processing of remote attachment content types. This update removes eager loading of remote data in the message processor in favor of the new `useAttachment` hook. With the `useAttachment` hook, developers can now respond to loading and error states when fetching the remote data. |
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
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,69 @@ | ||
import { useCallback, useEffect, useState } from "react"; | ||
import { | ||
ContentTypeAttachment, | ||
ContentTypeRemoteAttachment, | ||
RemoteAttachmentCodec, | ||
} from "@xmtp/content-type-remote-attachment"; | ||
import type { | ||
RemoteAttachment, | ||
Attachment, | ||
} from "@xmtp/content-type-remote-attachment"; | ||
import { useDb } from "./useDb"; | ||
import type { CachedMessage } from "@/helpers/caching/messages"; | ||
import { useClient } from "@/hooks/useClient"; | ||
|
||
/** | ||
* This hook returns the attachment data of a cached message | ||
*/ | ||
export const useAttachment = (message: CachedMessage) => { | ||
const { client } = useClient(); | ||
const { db } = useDb(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
const [attachment, setAttachment] = useState<Attachment | undefined>( | ||
undefined, | ||
); | ||
|
||
const loadRemoteAttachment = useCallback(async () => { | ||
if (client) { | ||
try { | ||
setIsLoading(true); | ||
const loadedAttachment = await RemoteAttachmentCodec.load<Attachment>( | ||
message.content as RemoteAttachment, | ||
client, | ||
); | ||
setIsLoading(false); | ||
setAttachment(loadedAttachment); | ||
} catch (e) { | ||
setError(new Error("Unable to load remote attachment")); | ||
} | ||
} else { | ||
setError(new Error("XMTP client is required to load remote attachments")); | ||
} | ||
}, [client, message.content]); | ||
|
||
useEffect(() => { | ||
const getAttachment = async () => { | ||
switch (message.contentType) { | ||
case ContentTypeAttachment.toString(): { | ||
setAttachment(message.content as Attachment); | ||
break; | ||
} | ||
case ContentTypeRemoteAttachment.toString(): { | ||
await loadRemoteAttachment(); | ||
break; | ||
} | ||
default: | ||
setError(new Error("Message is not an attachment content type")); | ||
} | ||
}; | ||
void getAttachment(); | ||
}, [db, loadRemoteAttachment, message]); | ||
|
||
return { | ||
attachment, | ||
error, | ||
isLoading, | ||
retry: loadRemoteAttachment, | ||
}; | ||
}; |
Oops, something went wrong.