Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

User Context and User Type created and wrapped chat screen #131

Merged
merged 6 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
153 changes: 76 additions & 77 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 12 additions & 9 deletions client/src/app/(home)/_layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@ import { Stack } from "expo-router";
import { SettingsProvider } from "../../contexts/SettingsContext";
import { SocketProvider } from "../../contexts/SocketContext";
import { LocationProvider } from "../../contexts/LocationContext";
import { UserProvider } from "../../contexts/UserContext";

const AuthLayout = () => {
return (
<LocationProvider>
<SocketProvider>
<SettingsProvider>
<Stack
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="chatchannel" options={{}} />
</Stack>
</SettingsProvider>
<UserProvider>
<SettingsProvider>
<Stack
screenOptions={{
headerShown: false,
}}
>
<Stack.Screen name="chatchannel" options={{}} />
</Stack>
</SettingsProvider>
</UserProvider>
</SocketProvider>
</LocationProvider>
);
Expand Down
14 changes: 9 additions & 5 deletions client/src/components/Chat/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import {
import { ChatInput } from "../Common/CustomInputs";
import { ChatSendButton } from "../Common/CustomButtons";
import MessageChannel from "../Common/MessageChannel";
import { LinearGradient } from "expo-linear-gradient";
import * as Crypto from "expo-crypto";
import { generateName } from "../../utils/scripts";
import { useSettings } from "../../contexts/SettingsContext";
import { SignOutButton } from "../Common/AuthButtons"
import { MessageType } from "../../types/Message";
import { LocationProvider } from "../../contexts/LocationContext";
import { useSocket } from "../../contexts/SocketContext";
import { useSettings } from "../../contexts/SettingsContext";
import { useLocation } from "../../contexts/LocationContext";
import { useUser } from "../../contexts/UserContext"; // imported for when it needs to be used
import { AuthStore } from "../../services/store";

const ChatScreen = () => {
Expand All @@ -32,8 +32,10 @@ const ChatScreen = () => {
const keyboardBehavior = Platform.OS === "ios" ? "padding" : undefined;
const socket = useSocket();
const location = useLocation();
const { user } = AuthStore.useState();

const user = useUser();
const userAuth = AuthStore.useState()
// Note: To prevent complexity, all user information is grabbed from different contexts and services. If we wanted most information inside of UserContext, we would have to import contexts within contexts and have state change as certain things mount, which could cause errors that are difficult to pinpoint.

// Message loading and sending logic
const [messages, setMessages] = React.useState<MessageType[]>([]);
const [messageContent, setMessageContent] = React.useState<string>("");
Expand All @@ -54,7 +56,9 @@ const ChatScreen = () => {
const onHandleSubmit = () => {
if (messageContent.trim() !== "") {
const newMessage: MessageType = {
uid: String(user?.uid),
author: {
uid: String(userAuth.userAuthInfo?.uid),
},
msgId: Crypto.randomUUID(),
msgContent: messageContent.trim(),
timeSent: Date.now(),
Expand Down
2 changes: 1 addition & 1 deletion client/src/components/Common/MessageChannel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const MessageChannel: React.FC<MessageChannelProps> = ({ messages }) => {
renderItem={({ item }) => (
<Message
messageContent={item.msgContent}
author={item.uid} // TODO: call server to get author name from UID. Or should this stored with MessageType?
author={item.author.uid}
time={item.timeSent}
/>
)}
Expand Down
3 changes: 0 additions & 3 deletions client/src/contexts/SettingsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@ export const SettingsProvider = ({
children: React.ReactNode;
}) => {
const [theme, setTheme] = useState("light");
const [displayName, setDisplayName] = useState("");
const [foregroundPfpImage, setForegroundPfpImage] = useState("");
const [backgroundPfpImage, setBackgroundPfpImage] = useState("");

// Initial settings load
useEffect(() => {
Expand Down
6 changes: 0 additions & 6 deletions client/src/contexts/SocketContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { createContext, useContext, useEffect, useState } from "react";
import { io, Socket } from "socket.io-client";
import * as Network from "expo-network";
import { useLocation } from "./LocationContext";
import { EXPO_IP } from "@env";

Expand All @@ -27,11 +26,6 @@ export const SocketProvider = ({ children }: { children: React.ReactNode }) => {
}
});

// socketIo.on("message", (data: MessageType, ack) => {
// console.log("Sending message to server:", data);
// if (ack) console.log("Server acknowledged message:", ack);
// });

return () => {
isMounted = false;
socket?.disconnect();
Expand Down
Loading
Loading