From cf45fcb98add12cb7c185d801613f094fa63604b Mon Sep 17 00:00:00 2001 From: Carter Date: Thu, 5 Dec 2024 20:53:57 -0600 Subject: [PATCH] using chats instead of notes for the chat --- package-lock.json | 2 +- src/app/UserWrapper.tsx | 1 + src/app/main.tsx | 10 +++ src/chat/Chat.tsx | 115 ++++++++++++---------------- src/chat/ChatInput.tsx | 63 ---------------- src/chat/ChatMessage.tsx | 132 ++++++++++----------------------- src/chat/ChatOutput.tsx | 32 ++++---- src/pages/Team.tsx | 95 +++++++++++++----------- src/providers/pond/noteAPI.tsx | 35 +++++++++ 9 files changed, 201 insertions(+), 284 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5dcde9d..9b65f43 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4220,7 +4220,7 @@ }, "node_modules/protobuf-ts": { "version": "1.0.0", - "resolved": "git+https://gitlab+deploy-token-50627:hv8mB4WkyvtjBpJKU1rN@gitlab.com/brandx/protobuf-ts.git#81a4e3f24369057b3d022ae0a8ef3e5fab58ea09", + "resolved": "git+https://gitlab+deploy-token-50627:hv8mB4WkyvtjBpJKU1rN@gitlab.com/brandx/protobuf-ts.git#8c31044c11d0b97c40a253cc917a333f33c3cdf4", "dependencies": { "protobufjs": "^6.8.8" } diff --git a/src/app/UserWrapper.tsx b/src/app/UserWrapper.tsx index 70aaf0c..4703598 100644 --- a/src/app/UserWrapper.tsx +++ b/src/app/UserWrapper.tsx @@ -64,6 +64,7 @@ export default function UserWrapper(props: Props) { let user_id = or(useAuth.user?.sub, "") const loadUser = useCallback(() => { + if (!userAPI.getUserWithTeam) return; if (hasFetched.current) return; setLoading(true) userAPI.getUserWithTeam(user_id).then(resp => { diff --git a/src/app/main.tsx b/src/app/main.tsx index bef5202..2746fee 100644 --- a/src/app/main.tsx +++ b/src/app/main.tsx @@ -3,6 +3,16 @@ import { createRoot } from 'react-dom/client' import './index.css' import App from './App.tsx' +// I don't consider providing a disabled button to a Tooltip an error. +// console.error = (message) => { + // if (message.includes('You are providing a disabled `button` child to the Tooltip component.')) { + // return; // Suppress specific warning + // } + // console.error(message); // Other warnings still show + // console.info(message, "error"); // Other warnings still show + +// }; + createRoot(document.getElementById('root')!).render( diff --git a/src/chat/Chat.tsx b/src/chat/Chat.tsx index 3382b95..2fb66dc 100644 --- a/src/chat/Chat.tsx +++ b/src/chat/Chat.tsx @@ -1,4 +1,4 @@ -import { Box, CircularProgress, Theme } from "@mui/material"; +import { Box, CircularProgress } from "@mui/material"; import { useGlobalState, useNoteAPI, useTeamAPI } from "providers"; import React from "react"; import { useCallback } from "react"; @@ -9,7 +9,6 @@ import ChatOutput from "./ChatOutput"; import { Note } from "models"; import { pond } from "protobuf-ts/pond"; import moment from "moment-timezone"; -import { makeStyles } from "@mui/styles"; interface Props { objectKey: string; @@ -17,95 +16,73 @@ interface Props { } export default function Chat(props: Props) { - const [notes, setNotes] = useState([]); + const [chats, setChats] = useState([]); const { objectKey, type } = props; const noteAPI = useNoteAPI(); - const [notesLoaded, setNotesLoaded] = useState(false); + const [loaded, setLoaded] = useState(false); + const [loading, setLoading] = useState(false); const [totalMessages, setTotalMessages] = useState(0); const [scrollPos, setScrollPos] = useState(0); - const [{ team }] = useGlobalState(); + const [{ user }] = useGlobalState(); const [attachmentMap, setAttachmentMap] = useState>(new Map()); - const teamAPI = useTeamAPI(); - // const classes = useStyles(); - const loadNotes = useCallback(() => { - let currentNotes: Note[] = notes; - noteAPI - .listNotes(15, currentNotes.length, "desc", "timestamp", objectKey) - .then(resp => { - setNotesLoaded(true); - if (resp.data.notes.length < 1) { - return; - } - setTotalMessages(resp.data.total); - let tempNotes: Note[] = []; - let tempAttachmentMap: Map = attachmentMap; - resp.data.notes.forEach(note => { - let n = Note.any(note); - tempNotes.push(n); - let refList = resp.data.attachments[n.key()]; - if (refList) { - tempAttachmentMap.set(n.key(), refList.refs); - } - }); - setScrollPos(tempNotes.length); - currentNotes = currentNotes.concat(tempNotes); - currentNotes.reverse(); - setNotes(currentNotes); - setAttachmentMap(tempAttachmentMap); + useEffect(() => { + console.log(totalMessages) + }, [totalMessages]) - if (type) { - // Setting the note type for future use - currentNotes.forEach(note => { - if (note.settings.objectType === pond.NoteType.NOTE_TYPE_UNKNOWN && type) { - note.settings.objectType = type; - noteAPI.updateNote(note.settings); - } - }); - - // Update new chat viewed timestamp - if (type === pond.NoteType.NOTE_TYPE_TEAM) { - team.preferences.chatViewedTimestamp = moment().toISOString(); - teamAPI.updatePreferences(team.key(), team.preferences); - } - } - }) - .catch(_err => {}); - }, [noteAPI, objectKey, notes, type, team, teamAPI, attachmentMap]); + const loadChats = () => { + setLoading(true) + noteAPI.listChats(10, chats.length, "desc", "timestamp", objectKey).then(resp => { + setChats(resp.data.chats ? resp.data.chats.reverse().concat(chats) : []) + setTotalMessages(resp.data.total) + }).finally(() => { + setLoading(false) + setLoaded(true) + }) + } const removeNote = (index: number) => { - let n = notes; + let c = chats setTotalMessages(totalMessages - 1); - n.splice(index, 1); - setNotes([...n]); + c.splice(index, 1); + setChats([...c]) }; const showNewNotes = (newNote: Note) => { - let n = notes; - n.push(newNote); - setScrollPos(n.length); - setNotes([...n]); + let note = pond.Note.create({ + settings: newNote.settings, + status: newNote.status, + }) + let profile = pond.UserProfile.create({ + name: user.name(), + avatar: user.settings.avatar, + }) + let permissions = [ + pond.Permission.PERMISSION_READ, + pond.Permission.PERMISSION_WRITE, + ] + let c = chats; + c.push({ + note: note, + profile: profile, + permissions: permissions + } as any) + setChats([...c]) }; useEffect(() => { - if (!notesLoaded) { - loadNotes(); - } - // setNotesLoaded(true); - }, [notesLoaded, loadNotes]); + loadChats() + }, []); const loadMore = () => { - let n = notes; - n.reverse(); - setNotes(n); - loadNotes(); + loadChats(); }; return ( - {!notesLoaded ? ( - + { loading && !loaded ? ( + ) : ( @@ -114,7 +91,7 @@ export default function Chat(props: Props) { { - // return ( - // { - // setAttachmentDialogOpen(false); - // }}> - // Add Attachments - // - // { - // setUploadingFile(true); - // }} - // uploadEnd={(fileID, fileName) => { - // setUploadingFile(false); - // if (fileID && fileName) { - // let f = cloneDeep(uploadedFiles); - // f.set(fileID, fileName); - // setUploadedFiles(f); - // } - // }} - // /> - // - // - // - // - // - // ); - // }; - return ( - {/* - - */}