data segmentation

This commit is contained in:
talksik
2022-05-28 15:56:14 -05:00
parent e878838bca
commit 0f57843695
3 changed files with 59 additions and 16 deletions
+3 -1
View File
@@ -101,11 +101,13 @@
"electron-squirrel-startup": "^1.0.0", "electron-squirrel-startup": "^1.0.0",
"electron-store": "^8.0.1", "electron-store": "^8.0.1",
"firebase": "^9.8.2", "firebase": "^9.8.2",
"immer": "^9.0.14",
"notistack": "^2.0.5", "notistack": "^2.0.5",
"react": "^18.1.0", "react": "^18.1.0",
"react-dom": "^18.1.0", "react-dom": "^18.1.0",
"react-error-boundary": "^3.1.4", "react-error-boundary": "^3.1.4",
"react-icons": "^4.3.1", "react-icons": "^4.3.1",
"react-use": "^17.4.0" "react-use": "^17.4.0",
"use-immer": "^0.7.0"
} }
} }
+46 -10
View File
@@ -11,16 +11,36 @@ import Navbar from './Navbar';
import Conversation from '@nirvana/core/src/models/conversation.model'; import Conversation from '@nirvana/core/src/models/conversation.model';
import useAuth from '../providers/AuthProvider'; import useAuth from '../providers/AuthProvider';
import { createOneOnOneConversation } from '../firebase/firestore'; import { createOneOnOneConversation } from '../firebase/firestore';
import { Link, AudioClip, Image } from '@nirvana/core/src/models/content.model';
import { useImmer } from 'use-immer';
import { User } from '@nirvana/core/src/models/user.model';
type ConversationMap = {
[conversationId: string]: Conversation;
};
type UserMap = {
[userId: string]: User;
};
type ConversationContentMap = {
[conversationId: string]: { audio: AudioClip[]; links: Link[]; images: Image[] };
};
interface ITerminalContext { interface ITerminalContext {
conversations: Conversation[]; conversationMap: ConversationMap;
userMap: UserMap;
contentMap: ConversationContentMap;
selectedConversation?: Conversation; selectedConversation?: Conversation;
handleQuickDial?: (otherUserId: string) => void; handleQuickDial?: (otherUserId: string) => void;
} }
const TerminalContext = React.createContext<ITerminalContext>({ const TerminalContext = React.createContext<ITerminalContext>({
conversations: [], conversationMap: {},
userMap: {},
contentMap: {},
}); });
// TODOS // TODOS
@@ -39,22 +59,36 @@ const TerminalContext = React.createContext<ITerminalContext>({
// if no conversations, show stale state + create one with nirvana // if no conversations, show stale state + create one with nirvana
// sort based on the different data sources: conversations, audio clips, etc.
export function TerminalProvider({ children }: { children?: React.ReactNode }) { export function TerminalProvider({ children }: { children?: React.ReactNode }) {
const { enqueueSnackbar } = useSnackbar(); const { enqueueSnackbar } = useSnackbar();
const { user } = useAuth(); const { user } = useAuth();
const [selectedConversationId, setSelectedConversationId] = useState<string>(undefined); const [selectedConversationId, setSelectedConversationId] = useState<string>(undefined);
const [conversations, setConversations] = useState<Conversation[]>([]); const [conversationMap, updateConversationMap] = useImmer<ConversationMap>({});
const [userMap, updateUserMap] = useImmer<UserMap>({});
const [contentMap, updatecontentMap] = useImmer<ConversationContentMap>({});
// cache of selected conversation // cache of selected conversation
const selectedConversation: Conversation | undefined = useMemo(() => { const selectedConversation: Conversation | undefined = useMemo(() => {
// get conversation if not here
// select if we do have it // select if we do have it
return undefined; const conversation = conversationMap[selectedConversationId];
}, [selectedConversationId]);
if (!conversation) {
enqueueSnackbar('no conversation found', { variant: 'error' });
console.error('Should have found it... maybe retry once more');
}
return conversation;
// get conversation if not here
// const fetchedConversation = await getConversationById
// return undefined;
}, [selectedConversationId, conversationMap, enqueueSnackbar]);
// handle create or open existing conversation // handle create or open existing conversation
// not 100% consistent to the second, but still works...don't need atomicity // not 100% consistent to the second, but still works...don't need atomicity
@@ -64,7 +98,7 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
// check all current conversations which should be live listened to // check all current conversations which should be live listened to
// if there is already a convo with exactly me and him, then select it // if there is already a convo with exactly me and him, then select it
const findExistingConversation = conversations.find((convo) => { const findExistingConversation = Object.values(conversationMap).find((convo) => {
if ( if (
convo.membersInRoom?.length === 2 && convo.membersInRoom?.length === 2 &&
convo.membersInRoom.includes(user.uid) && convo.membersInRoom.includes(user.uid) &&
@@ -86,11 +120,13 @@ export function TerminalProvider({ children }: { children?: React.ReactNode }) {
enqueueSnackbar('Something went wrong, please try again', { variant: 'error' }); enqueueSnackbar('Something went wrong, please try again', { variant: 'error' });
} }
}, },
[conversations, enqueueSnackbar, user], [conversationMap, enqueueSnackbar, user],
); );
return ( return (
<TerminalContext.Provider value={{ selectedConversation, conversations, handleQuickDial }}> <TerminalContext.Provider
value={{ selectedConversation, conversationMap, contentMap, userMap, handleQuickDial }}
>
<Grid container spacing={0}> <Grid container spacing={0}>
<Grid <Grid
item item
+10 -5
View File
@@ -1389,11 +1389,6 @@
dependencies: dependencies:
"@types/node" "*" "@types/node" "*"
"@types/uuid@^8.3.4":
version "8.3.4"
resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-8.3.4.tgz#bd86a43617df0594787d38b735f55c805becf1bc"
integrity sha512-c/I8ZRb51j+pYGAu5CrFMRxqZ2ke4y2grEBO5AUjgSkSk+qT2Ea+OdWElz/OiMf5MNpn2b17kuVBwZLQJXzihw==
"@types/ws@^8.5.1": "@types/ws@^8.5.1":
version "8.5.3" version "8.5.3"
resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d" resolved "https://registry.yarnpkg.com/@types/ws/-/ws-8.5.3.tgz#7d25a1ffbecd3c4f2d35068d0b283c037003274d"
@@ -4478,6 +4473,11 @@ immediate@~3.0.5:
resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b" resolved "https://registry.yarnpkg.com/immediate/-/immediate-3.0.6.tgz#9db1dbd0faf8de6fbe0f5dd5e56bb606280de69b"
integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps= integrity sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=
immer@^9.0.14:
version "9.0.14"
resolved "https://registry.yarnpkg.com/immer/-/immer-9.0.14.tgz#e05b83b63999d26382bb71676c9d827831248a48"
integrity sha512-ubBeqQutOSLIFCUBN03jGeOS6a3DoYlSYwYJTa+gSKEZKU5redJIqkIdZ3JVv/4RZpfcXdAWH5zCNLWPRv2WDw==
import-fresh@^3.0.0, import-fresh@^3.1.0, import-fresh@^3.2.1: import-fresh@^3.0.0, import-fresh@^3.1.0, import-fresh@^3.2.1:
version "3.3.0" version "3.3.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b" resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
@@ -7553,6 +7553,11 @@ url-parse-lax@^3.0.0:
dependencies: dependencies:
prepend-http "^2.0.0" prepend-http "^2.0.0"
use-immer@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/use-immer/-/use-immer-0.7.0.tgz#e3bfbb806b5e3ff6e37441be74c306d91c1e0962"
integrity sha512-Re4hjrP3a/2ABZjAc0b7AK9s626bnO+H33RO2VUhiDZ2StBz5B663K6WNNlr4QtHWaGUmvLpwt3whFvvWuolQw==
username@^5.1.0: username@^5.1.0:
version "5.1.0" version "5.1.0"
resolved "https://registry.yarnpkg.com/username/-/username-5.1.0.tgz#a7f9325adce2d0166448cdd55d4985b1360f2508" resolved "https://registry.yarnpkg.com/username/-/username-5.1.0.tgz#a7f9325adce2d0166448cdd55d4985b1360f2508"