refactor: nav and search.

feat: use recoil to replace redux
feat: use react-native

THIS IS NOT FINISHED. DONT USE THIS
This commit is contained in:
Wentao Lyu 2023-03-28 20:36:21 +08:00
parent d8ccc5b870
commit af3d74b104
33 changed files with 1142 additions and 473 deletions

View file

@ -0,0 +1,67 @@
import React, { useEffect } from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import Landing from "../components/ui/Landing";
import Messages from "../components/Messages";
import TextChat from "../components/Input";
import store from "~/store";
import manualSWR from "~/utils/fetchers";
// import TextChat from './components/Main/TextChat';
// {/* <TextChat messages={messages} /> */}
export default function Chat() {
const [conversation, setConversation] = useRecoilState(store.conversation);
const setMessages = useSetRecoilState(store.messages);
const messagesTree = useRecoilValue(store.messagesTree);
const { newConversation } = store.useConversation();
const { conversationId } = useParams();
const navigate = useNavigate();
const { trigger: messagesTrigger } = manualSWR(
`/api/messages/${conversation?.conversationId}`,
"get"
);
const { trigger: conversationTrigger } = manualSWR(
`/api/convos/${conversationId}`,
"get"
);
// when conversation changed or conversationId (in url) changed
useEffect(() => {
if (conversation === null) {
// no current conversation, we need to do something
if (conversationId == "new") {
// create new
newConversation();
} else {
// fetch it from server
conversationTrigger().then(setConversation);
setMessages(null);
console.log("NEED TO FETCH DATA");
}
} else if (conversation?.conversationId !== conversationId)
// conversationId (in url) should always follow conversation?.conversationId, unless conversation is null
navigate(`/chat/${conversation?.conversationId}`);
}, [conversation, conversationId]);
// when messagesTree is null (<=> messages is null)
// we need to fetch message list from server
useEffect(() => {
if (messagesTree === null) {
messagesTrigger().then(setMessages);
}
}, [conversation?.conversationId]);
if (conversation?.conversationId !== conversationId) return null;
return (
<>
{conversationId == "new" ? <Landing /> : <Messages />}
<TextChat />
</>
);
}