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

[frontend] refactor online status provider #255

Merged
merged 2 commits into from
Feb 14, 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
20 changes: 2 additions & 18 deletions frontend/app/lib/client-socket-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import { ToastAction } from "@/components/ui/toast";
import { useToast } from "@/components/ui/use-toast";
import { chatSocket } from "@/socket";
import Link from "next/link";
import {
Dispatch,
SetStateAction,
createContext,
useCallback,
useEffect,
useState,
} from "react";
import { useCallback, useEffect } from "react";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that all hooks and utilities are used correctly and efficiently, following React best practices for hooks usage and side effects management.

import { useAuthContext } from "./client-auth";
import {
DenyEvent,
Expand All @@ -26,13 +19,7 @@ import { chatSocket as socket } from "@/socket";
import { useRouter } from "next/navigation";
import { usePathname } from "next/navigation";

export const OnlineContext = createContext<{ [key: number]: number }>({});

export default function SocketProvider({
setOnlineStatus,
}: {
setOnlineStatus: Dispatch<SetStateAction<{ [key: number]: number }>>;
}) {
export default function SocketProvider() {
const { toast } = useToast();
const { currentUser } = useAuthContext();
const pathName = usePathname();
Expand Down Expand Up @@ -131,9 +118,6 @@ export default function SocketProvider({
};

const handleOnlineStatus = (users: { userId: number; status: number }[]) => {
users.forEach((u) => {
setOnlineStatus((prev) => ({ ...prev, [u.userId]: u.status }));
});
toast({
title: "online-status",
description: JSON.stringify(users),
Expand Down
22 changes: 19 additions & 3 deletions frontend/app/lib/hooks/useOnlineStatus.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
"use client";

import { useState } from "react";
import { createContext, useEffect, useState } from "react";
import { chatSocket } from "@/socket";

export function useOnlineStatus() {
export const OnlineContext = createContext<{ [key: number]: number }>({});

export function useOnlineStatus(): { [key: number]: number } {
const [onlineStatus, setOnlineStatus] = useState<{ [key: number]: number }>(
{},
);
return { onlineStatus, setOnlineStatus };
useEffect(() => {
const handleOnlineStatus = (
users: { userId: number; status: number }[],
) => {
users.forEach((u) => {
setOnlineStatus((prev) => ({ ...prev, [u.userId]: u.status }));
});
};
chatSocket.on("online-status", handleOnlineStatus);
return () => {
chatSocket.off("online-status", handleOnlineStatus);
};
});
return onlineStatus;
}
8 changes: 4 additions & 4 deletions frontend/app/onlineProviders.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use client";

import { useOnlineStatus } from "./lib/hooks/useOnlineStatus";
import SocketProvider, { OnlineContext } from "./lib/client-socket-provider";
import { OnlineContext, useOnlineStatus } from "./lib/hooks/useOnlineStatus";
import SocketProvider from "./lib/client-socket-provider";
Comment on lines +3 to +4
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure imports are correctly ordered according to the project's coding standards, typically with external libraries first, followed by internal modules.


export function OnlineProviders({ children }: { children: React.ReactNode }) {
const { onlineStatus, setOnlineStatus } = useOnlineStatus();
const onlineStatus = useOnlineStatus();
return (
<>
<OnlineContext.Provider value={onlineStatus}>
{children}
<SocketProvider setOnlineStatus={setOnlineStatus} />
<SocketProvider />
</OnlineContext.Provider>
</>
);
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/ui/user/user-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { PublicUserEntity } from "@/app/lib/dtos";
import { TooltipProvider } from "@/components/ui/tooltip";
import { Avatar, AvatarSize } from "./avatar";
import { OnlineContext } from "@/app/lib/client-socket-provider";
import { OnlineContext } from "@/app/lib/hooks/useOnlineStatus";
import { useContext } from "react";

export default function UserList({
Expand Down
Loading