(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
|
|
@ -10,6 +10,7 @@ import { useTTP } from "@tensamin/ttp";
|
|||
import { log, toast } from "@tensamin/shared/log";
|
||||
import { cn, useIsMobile } from "@tensamin/ui";
|
||||
import { encryptText } from "@tensamin/crypto/worker";
|
||||
import { useSession } from "@tensamin/storage/session";
|
||||
|
||||
export default function InputComponent({
|
||||
value,
|
||||
|
|
@ -23,6 +24,7 @@ export default function InputComponent({
|
|||
const { send } = useTTP();
|
||||
const { addLiveMessage, sharedSecret, userId, inputBoxRef } = useChat();
|
||||
const { load } = useStorage();
|
||||
const { moveUserIdToTop } = useSession();
|
||||
|
||||
React.useEffect(() => {
|
||||
void load("settings.reverse_enter_behavior").then((shouldInvert) => {
|
||||
|
|
@ -78,6 +80,8 @@ export default function InputComponent({
|
|||
toast("error", "Failed to send message");
|
||||
});
|
||||
|
||||
moveUserIdToTop(userId);
|
||||
|
||||
setValue("");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ import { useUser } from "@tensamin/user/context";
|
|||
import { useStorage } from "@tensamin/storage/context";
|
||||
import { useTTP } from "@tensamin/ttp";
|
||||
import { log } from "@tensamin/shared/log";
|
||||
import { useSession } from "@tensamin/storage/session";
|
||||
|
||||
export const context = createContext<contextType | undefined>(undefined);
|
||||
|
||||
|
|
@ -59,6 +60,7 @@ export default function Provider(props: { children: ReactNode }) {
|
|||
const { get } = useUser();
|
||||
const { load } = useStorage();
|
||||
const { send, subscribePush } = useTTP();
|
||||
const { moveUserIdToTop } = useSession();
|
||||
|
||||
const [liveMessagesState, setLiveMessagesState] = useState<LiveMessage[]>([]);
|
||||
const [currentSharedSecretState, setCurrentSharedSecretState] = useState<{
|
||||
|
|
@ -171,32 +173,39 @@ export default function Provider(props: { children: ReactNode }) {
|
|||
[send, userIdValue, currentSharedSecret, decryptText],
|
||||
);
|
||||
|
||||
const addLiveMessage = useCallback((message: RawMessage) => {
|
||||
const localId =
|
||||
globalThis.crypto?.randomUUID?.() ??
|
||||
`${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
||||
const addLiveMessage = useCallback(
|
||||
(message: RawMessage) => {
|
||||
const localId =
|
||||
globalThis.crypto?.randomUUID?.() ??
|
||||
`${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
||||
|
||||
setLiveMessagesState((prev) => [
|
||||
...prev,
|
||||
{
|
||||
...message,
|
||||
localId,
|
||||
failed: false,
|
||||
},
|
||||
]);
|
||||
if (!message.sent_by_self) {
|
||||
moveUserIdToTop(userIdValue);
|
||||
}
|
||||
|
||||
return {
|
||||
setFailed: (failed: boolean) => {
|
||||
setLiveMessagesState((prev) =>
|
||||
prev.map((liveMessage) =>
|
||||
liveMessage.localId === localId
|
||||
? { ...liveMessage, failed }
|
||||
: liveMessage,
|
||||
),
|
||||
);
|
||||
},
|
||||
};
|
||||
}, []);
|
||||
setLiveMessagesState((prev) => [
|
||||
...prev,
|
||||
{
|
||||
...message,
|
||||
localId,
|
||||
failed: false,
|
||||
},
|
||||
]);
|
||||
|
||||
return {
|
||||
setFailed: (failed: boolean) => {
|
||||
setLiveMessagesState((prev) =>
|
||||
prev.map((liveMessage) =>
|
||||
liveMessage.localId === localId
|
||||
? { ...liveMessage, failed }
|
||||
: liveMessage,
|
||||
),
|
||||
);
|
||||
},
|
||||
};
|
||||
},
|
||||
[userIdValue, moveUserIdToTop],
|
||||
);
|
||||
|
||||
const clearLiveMessages = useCallback(() => {
|
||||
setLiveMessagesState([]);
|
||||
|
|
|
|||
Loading…
Reference in a new issue