diff --git a/js/app/src/router.tsx b/js/app/src/router.tsx index 69c150318..d115b43ec 100644 --- a/js/app/src/router.tsx +++ b/js/app/src/router.tsx @@ -193,9 +193,7 @@ export const AvatarButton = () => { if (userProfile) { source = { uri: userProfile.avatar }; return ( - + { state = reduceChat(state, [localChat], [], []); store.setState(state); - await pdsAgent.com.atproto.repo.createRecord({ - repo: userDID, - collection: "place.stream.chat.message", - record, - }); + try { + await pdsAgent.com.atproto.repo.createRecord({ + repo: userDID, + collection: "place.stream.chat.message", + record, + }); + } catch (err) { + // Remove the optimistic message if the server call fails + const currentState = store.getState(); + const updatedIndex = { ...currentState.chatIndex }; + for (const [key, existingMsg] of Object.entries(updatedIndex)) { + if (existingMsg.uri === localChat.uri) { + delete updatedIndex[key]; + break; + } + } + store.setState({ + ...currentState, + chatIndex: updatedIndex, + chat: Object.keys(updatedIndex) + .sort((a, b) => { + const aTime = parseInt(a.split("-")[0], 10); + const bTime = parseInt(b.split("-")[0], 10); + return bTime - aTime; + }) + .map((key) => updatedIndex[key]), + }); + throw err; + } }; };