-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add conversation caching (#1908)
- Loading branch information
Showing
3 changed files
with
79 additions
and
1 deletion.
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
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,50 @@ | ||
import type { ConversationExport } from '@xmtp/xmtp-js/dist/types/src/conversations/Conversation'; | ||
import { create } from 'zustand'; | ||
import { persist } from 'zustand/middleware'; | ||
|
||
// If any breaking changes to the ConversationExport schema occur, increment the cache version. | ||
const CONVERSATION_CACHE_VERSION = 1; | ||
|
||
/** | ||
* The ConversationCache is a JSON serializable Zustand store that is persisted to LocalStorage | ||
* Persisting conversations to the cache saves on both bandwidth and CPU cycles, as we don't have to re-fetch or re-decrypt conversations on subsequent page loads | ||
*/ | ||
interface ConversationCache { | ||
// Mapping of conversation exports, keyed by wallet address | ||
conversations: { [walletAddress: string]: ConversationExport[] }; | ||
// Overwrite the cache for a given wallet address | ||
setConversations: (walletAddress: string, conversations: ConversationExport[]) => void; | ||
// Add a single conversation to the cache. | ||
// Deduping only happens at the time the cache is loaded, so be careful to not overfill or you will use more LocalStorage space than necessary | ||
addConversation: (walletAddress: string, conversation: ConversationExport) => void; | ||
} | ||
|
||
export const useConversationCache = create<ConversationCache>()( | ||
persist( | ||
(set, get) => ({ | ||
conversations: {}, | ||
setConversations: (walletAddress: string, convos: ConversationExport[]) => | ||
set({ | ||
conversations: { ...get().conversations, [walletAddress]: convos } | ||
}), | ||
addConversation: (walletAddress: string, convo: ConversationExport) => { | ||
const existing = get().conversations; | ||
const existingForWallet = existing[walletAddress] || []; | ||
return set({ | ||
conversations: { | ||
...existing, | ||
[walletAddress]: [...existingForWallet, convo] | ||
} | ||
}); | ||
} | ||
}), | ||
{ | ||
// Ensure that the LocalStorage key includes the network and the cache version | ||
// If any breaking changes to the ConversationExport schema occur, increment the cache version. | ||
name: `lenster:conversations:${ | ||
process.env.NEXT_PUBLIC_LENS_NETWORK || 'unknown' | ||
}:v${CONVERSATION_CACHE_VERSION}`, | ||
partialize: (state) => ({ conversations: state.conversations }) | ||
} | ||
) | ||
); |
0ec8312
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
prerender – ./apps/prerender
prerender-git-main-lenster.vercel.app
prerender-lenster.vercel.app
prerender.lenster.xyz
0ec8312
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
web – ./apps/web
web-lenster.vercel.app
lenster.vercel.app
lenster.xyz
web-git-main-lenster.vercel.app
0ec8312
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.