Skip to content

Commit

Permalink
broke everything
Browse files Browse the repository at this point in the history
  • Loading branch information
willhuff0 committed Nov 19, 2024
1 parent 21f319d commit b85981c
Show file tree
Hide file tree
Showing 15 changed files with 557 additions and 10,862 deletions.
16 changes: 8 additions & 8 deletions client/app/contexts/LocationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export const LocationProvider = ({
children: React.ReactNode;
}) => {
const [location, setLocation] = useState<LocationType>({
latitude: 99999, // Impossible starting value
longitude: 99999,
lat: 99999, // Impossible starting value
lon: 99999,
});
const [isLocationEnabled, setIsLocationEnabled] = useState(false);

Expand All @@ -36,11 +36,11 @@ export const LocationProvider = ({

// Set up the interval once after permission is granted
interval = setInterval(async () => {
const locationData = await getLocation();
const locationData = await getLocation();
if (locationData && locationData.coords) {
const { latitude, longitude } = locationData.coords;
if (latitude !== location.latitude || longitude !== location.longitude) {
setLocation({ latitude, longitude });
if (latitude !== location.lat || longitude !== location.lon) {
setLocation({ lat: latitude, lon: longitude });
} else {
console.log("Location has not changed");
}
Expand All @@ -57,14 +57,14 @@ export const LocationProvider = ({
console.log("[LOG]: Cleaning up location useEffect");
}
};
}, []);
}, []);

Check warning on line 60 in client/app/contexts/LocationContext.tsx

View workflow job for this annotation

GitHub Actions / lint (21.x)

React Hook useEffect has missing dependencies: 'location.lat' and 'location.lon'. Either include them or remove the dependency array


return (
<LocationContext.Provider
value={{
longitude: location.longitude,
latitude: location.latitude,
lon: location.lon,
lat: location.lat,
isLocationEnabled,
}}>
{children}
Expand Down
14 changes: 14 additions & 0 deletions client/app/contexts/NearbyUserContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { createContext, useContext, useState } from "react";
import { UserProfile } from "../types/User";

const NearbyUsersContext = createContext<{ [uid: string]: UserProfile }>({});

export const userNearbyUsers = () => {
return useContext(NearbyUsersContext);

Check failure on line 7 in client/app/contexts/NearbyUserContext.tsx

View workflow job for this annotation

GitHub Actions / lint (21.x)

React Hook "useContext" is called in function "userNearbyUsers" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
};

export const NearbyUsersProvider = ({ children }: { children: React.ReactNode }) => {
const [user, setNearbyUsers] = useState<{ [uid: string]: UserProfile }>({});

Check warning on line 11 in client/app/contexts/NearbyUserContext.tsx

View workflow job for this annotation

GitHub Actions / lint (21.x)

'setNearbyUsers' is assigned a value but never used

return <NearbyUsersContext.Provider value={user}>{children}</NearbyUsersContext.Provider>;
};
8 changes: 4 additions & 4 deletions client/app/contexts/SocketContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ export const SocketProvider = ({ children }: { children: React.ReactNode }) => {
if (
socket &&
locationContext &&
locationContext?.latitude !== 9999 &&
locationContext?.longitude !== 9999
locationContext?.lat !== 9999 &&
locationContext?.lon !== 9999
) {
sendLocationUpdate(socket, locationContext.latitude, locationContext.longitude);
sendLocationUpdate(socket, locationContext.lat, locationContext.lon);
}
}, [locationContext?.latitude, locationContext?.longitude, socket]);
}, [locationContext?.lat, locationContext?.lon, socket]);

return (
<SocketContext.Provider value={socket}>
Expand Down
15 changes: 0 additions & 15 deletions client/app/contexts/UserContext.tsx

This file was deleted.

48 changes: 21 additions & 27 deletions client/app/screens/chat/ChatScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@ import { useSocket } from "../../contexts/SocketContext";
import { AuthStore } from "../../services/AuthStore";
import { Message } from "../../types/Message";
import { useState, useEffect } from "react";
import { useUser } from "@app/contexts/UserContext";
import NearbyHeader from "@app/components/chat/NearbyHeader";
import React from "react";
import NearbyUserDrawer from "@app/components/chat/NearbyUserDrawer";
import { sendMessage } from "@app/services/SocketService";
import { userNearbyUsers } from "@app/contexts/NearbyUserContext";

const ChatScreen = () => {
const settings = useSettings();
const screenHeight = Dimensions.get("window").height;
const keyboardBehavior = Platform.OS === "ios" ? "padding" : undefined;
const socket = useSocket();
const location = useLocation();
const user = useUser();
const nearbyUsers = userNearbyUsers();
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.

Expand All @@ -53,31 +54,24 @@ const ChatScreen = () => {

// For when the user sends a message (fired by the send button)
const onHandleSubmit = () => {
if (messageContent.trim() !== "") {
const newMessage: Message = {
author: {
uid: String(userAuth.userAuthInfo?.uid),
displayName: String(user?.displayName),
},
msgId: Crypto.randomUUID(),
msgContent: messageContent.trim(),
timestamp: Date.now(),
lastUpdated: Date.now(),
location: {
lat: Number(location?.latitude),
lon: Number(location?.longitude),
},
isReply: false,
replyTo: "",
reactions: {},
};

if (socket !== null) {
socket.emit("message", newMessage);
}

setMessageContent("");
}
if (messageContent.trim() === "") return;
if (socket === null) return;

const newMessage: Message = {
author: String(userAuth.userAuthInfo?.uid),
//msgId: Crypto.randomUUID(),
content: { text: messageContent.trim(), },
location: {
lat: Number(location?.lat),
lon: Number(location?.lon),
},
replyTo: undefined,
reactions: {},
};

sendMessage(socket, newMessage);

setMessageContent("");
};

return (
Expand Down
35 changes: 9 additions & 26 deletions client/app/services/AuthStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
} from "@firebase/auth";
import { Store } from "pullstate";

import { auth , db } from "../configs/firebaseConfig";
import { doc , setDoc } from "@firebase/firestore"; // Import Firestore functions
import { auth, db } from "../configs/firebaseConfig";
import { doc, setDoc } from "@firebase/firestore"; // Import Firestore functions
import { UserProfile } from "@app/types/User";

interface AuthStoreInterface {
isLoggedin: boolean;
Expand All @@ -22,27 +23,13 @@ export const AuthStore = new Store<AuthStoreInterface>({
userAuthInfo: null,
});

const createUserConfig = async (userId: string) => {
// TODO: Must call notifyUpdateProfile if connected to socket server
export const updateUserProfile = async (userId: string, profile: UserProfile) => {
try {
const docRef = doc(db, "UserConfigs", userId);

await setDoc(docRef, {
// TODO: create a matching UserConfig type in the app/types folder.
// In documentation: make explicit that the key for UserConfig documents is the same as a uid from the user auth collection.
isConnected: false,
lastConnectionTime: "",
displayName: "",
userIcon: {
imageType: 0,
color: "#02efdb"
},
darkMode: false,
notificationsEnabled: false,
language: "English",

});
} catch (e){
console.error("Error creating UserConfig: ", e);
const docRef = doc(db, "users", userId);
await setDoc(docRef, profile);
} catch (e) {
console.error("Error updating user profile: ", e);
}
}

Expand Down Expand Up @@ -94,10 +81,6 @@ export const appSignUp = async (email: string, password: string) => {
store.isLoggedin = !!response.user;
});

if (response.user) {
await createUserConfig(response.user.uid);
}

return { user: auth.currentUser };
} catch (e) {
return { error: e };
Expand Down
60 changes: 50 additions & 10 deletions client/app/services/SocketService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { io, Socket } from "socket.io-client";
import { AuthStore } from "../services/AuthStore";
import { EXPO_IP } from "@env";
import { LocationType } from "@app/types/Location";
import { UserProfile } from "@app/types/User";
import { Completer } from "@app/utils/completer";
import { Message } from "@app/types/Message";

export const initializeSocket = async (token: string): Promise<Socket> => {
const socketIo = io(`http://${EXPO_IP}:8080`, {
Expand All @@ -21,21 +25,57 @@ export const getToken = async (): Promise<string | null> => {
};


// Methods

export const sendLocationUpdate = (
export const updateLocation = (
socket: Socket,
latitude: number,
longitude: number
location: LocationType,
) => {
console.log("Sending location update to server:", latitude, longitude);
console.log("Sending location update to server:", location);
socket.emit(
"updateLocation",
{
lat: latitude,
lon: longitude,
},
location,
(ack: string) => {
console.log("updateLocation ack:", ack);
}
);
};

export const getNearbyUsers = async (
socket: Socket,
): Promise<{ [uid: string]: UserProfile }> => {
const completer = new Completer<{ [uid: string]: UserProfile }>();

socket.emit(
"getNearbyUsers",
(nearbyUsersMap: { [uid: string]: UserProfile }) => {
completer.complete(nearbyUsersMap);
}
);

return await completer.promise;
}

export const notifyUpdateProfile = (
socket: Socket,
) => {
socket.emit(
"notifyUpdateProfile",
(ack: string) => {
console.log("Location update ack:", ack);
console.log("notifyUpdateProfile ack:", ack);
}
)
}

export const sendMessage = (
socket: Socket,
message: Message,
) => {
socket.emit("sendMessage", message,
(ack: string) => {
console.log("sendMessage ack:", ack);
}
);
};
}

//
13 changes: 0 additions & 13 deletions client/app/services/UserService.ts

This file was deleted.

16 changes: 8 additions & 8 deletions client/app/types/Location.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
export interface LocationContextProps {
longitude: number;
latitude: number;
isLocationEnabled: boolean;
}
lon: number;
lat: number;
isLocationEnabled: boolean;
}

export interface LocationType {
longitude: number;
latitude: number;
}
lon: number;
lat: number;
}
27 changes: 11 additions & 16 deletions client/app/types/Message.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
import { LocationType } from "./Location";

export interface Message {
author: {
uid: string;
displayName: string;
};
msgId: string;
msgContent: string;
timestamp: number;
lastUpdated: number;
location: {
lat: number;
lon: number;
};
isReply: boolean;
replyTo: string;
author: string,
content: {
text?: string,
attachment?: string,
},
location: LocationType,
replyTo?: string,
reactions: {
[key: string]: number;
};
[key: string]: number,
}
}
7 changes: 2 additions & 5 deletions client/app/types/User.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
export interface UserType {
export interface UserProfile {
displayName: string;
userIcon?: {
imagePath: string;
colorHex: string;
};
profilePicture: number;
}
Loading

0 comments on commit b85981c

Please sign in to comment.