(feat): add cache for conversations and communities
(feat): conversations now get moved to the top when engaged with (qol): update todo
This commit is contained in:
parent
006c222cc5
commit
fd15215f15
14 changed files with 178 additions and 67 deletions
|
|
@ -4,6 +4,7 @@
|
|||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"exports": {
|
||||
"./session": "./src/session.tsx",
|
||||
"./context": "./src/context.tsx",
|
||||
"./indexed-db": "./src/indexed-db.ts"
|
||||
},
|
||||
|
|
@ -14,6 +15,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@tensamin/shared": "workspace:*",
|
||||
"@tensamin/ttp": "workspace:*",
|
||||
"@tensamin/ui": "*",
|
||||
"react": "^19.2.0",
|
||||
"react-dom": "^19.2.0"
|
||||
|
|
|
|||
84
packages/storage/src/session.tsx
Normal file
84
packages/storage/src/session.tsx
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { useTTP } from "@tensamin/ttp";
|
||||
import {
|
||||
createContext,
|
||||
type ReactNode,
|
||||
useState,
|
||||
useContext,
|
||||
useEffect,
|
||||
} from "react";
|
||||
import { useStorage } from "./context";
|
||||
import type { Contacts, Communities } from "@tensamin/shared/data";
|
||||
|
||||
interface SessionContextType {
|
||||
contacts: Contacts;
|
||||
communities: Communities;
|
||||
moveUserIdToTop: (userId: number) => void;
|
||||
}
|
||||
|
||||
const SessionContext = createContext<SessionContextType | undefined>(undefined);
|
||||
|
||||
export default function SessionProvider({ children }: { children: ReactNode }) {
|
||||
const { freshContacts, freshCommunities } = useTTP();
|
||||
const { load, save } = useStorage();
|
||||
const [contacts, setContacts] = useState<Contacts>([]);
|
||||
const [communities, setCommunities] = useState<Communities>([]);
|
||||
|
||||
// Get cached data and merge fresh data
|
||||
useEffect(() => {
|
||||
load("cached_contacts").then((cachedData) => {
|
||||
setContacts([
|
||||
...freshContacts,
|
||||
...cachedData.filter(
|
||||
(item) =>
|
||||
!freshContacts.some((fresh) => fresh.user_id === item.user_id),
|
||||
),
|
||||
]);
|
||||
});
|
||||
load("cached_communities").then((cachedData) => {
|
||||
if (cachedData && freshCommunities) {
|
||||
setCommunities([
|
||||
...freshCommunities,
|
||||
...cachedData.filter(
|
||||
(item) =>
|
||||
!freshCommunities.some(
|
||||
(fresh) => fresh.community_id === item.community_id,
|
||||
),
|
||||
),
|
||||
]);
|
||||
}
|
||||
});
|
||||
}, [load, freshContacts, freshCommunities]);
|
||||
|
||||
// Save data
|
||||
useEffect(() => {
|
||||
save("cached_contacts", contacts);
|
||||
}, [contacts, save]);
|
||||
useEffect(() => {
|
||||
save("cached_communities", communities);
|
||||
}, [communities, save]);
|
||||
|
||||
const moveUserIdToTop = (userId: number) => {
|
||||
setContacts((prevContacts) => {
|
||||
const userIndex = prevContacts.findIndex(
|
||||
(contact) => contact.user_id === userId,
|
||||
);
|
||||
if (userIndex === -1) return prevContacts;
|
||||
const [user] = prevContacts.splice(userIndex, 1);
|
||||
return [user, ...prevContacts];
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<SessionContext.Provider value={{ contacts, communities, moveUserIdToTop }}>
|
||||
{children}
|
||||
</SessionContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useSession(): SessionContextType {
|
||||
const context = useContext(SessionContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useSession must be used within a SessionProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
1
packages/storage/todo.md
Normal file
1
packages/storage/todo.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
- Add `notifications: number` to contacts
|
||||
Loading…
Reference in a new issue