Skip to content

Commit

Permalink
[frontend] manage users online status (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
kotto5 authored Feb 7, 2024
1 parent 0812304 commit 82e4d7d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 31 deletions.
7 changes: 0 additions & 7 deletions frontend/app/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -890,10 +890,3 @@ export async function createUserWithOauth(
redirect("/login");
}
}

export const isOnline = async (userId: number) =>
fetch(`${process.env.API_URL}/chat/${userId}/online`, {
headers: {
Authorization: "Bearer " + getAccessToken(),
},
}).then((res) => (res.ok ? res.json() : Promise.reject(res)));
18 changes: 16 additions & 2 deletions frontend/app/lib/client-socket-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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 { useCallback, useEffect } from "react";
import { useCallback, useEffect, useState } from "react";
import { useAuthContext } from "./client-auth";
import {
DenyEvent,
Expand Down Expand Up @@ -93,6 +93,18 @@ export default function SocketProvider() {
});
};

const [onlineStatus, setOnlineStatus] = useState<{ [key: number]: number }>(
{},
);
const handleOnlineStatus = (users: { userId: number; status: number }[]) => {
console.log("users", users);
users.forEach((u) => {
console.log("FOO! u", u);
setOnlineStatus((prev) => ({ ...prev, [u.userId]: u.status }));
});
console.log("online-status", onlineStatus); // TODO: この時点では変更されていない?
};

const showMessageToast = (message: MessageEvent) => {
// TODO: If sender is me, don't show toast
toast({
Expand Down Expand Up @@ -133,7 +145,6 @@ export default function SocketProvider() {
};

useEffect(() => {
chatSocket.connect();
const handler = (event: string, data: any) => {
if (event === "message") {
showMessageToast(data);
Expand All @@ -151,11 +162,14 @@ export default function SocketProvider() {
showDenyPongToast(data);
} else if (event === "error-pong") {
showErrorPongToast(data);
} else if (event === "online-status") {
handleOnlineStatus(data);
} else {
showNotificationToast(data);
}
};
chatSocket.onAny(handler);
chatSocket.connect();
return () => {
chatSocket.offAny(handler);
chatSocket.disconnect();
Expand Down
23 changes: 1 addition & 22 deletions frontend/app/ui/user/user-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type { PublicUserEntity } from "@/app/lib/dtos";
import { TooltipProvider } from "@/components/ui/tooltip";
import { Avatar, AvatarSize } from "./avatar";
import { useEffect, useState } from "react";
import { isOnline } from "@/app/lib/actions";

export default function UserList({
users,
Expand All @@ -13,26 +12,6 @@ export default function UserList({
users: PublicUserEntity[];
avatarSize: AvatarSize;
}) {
const [onlineStatus, setOnlineStatus] = useState<{ [key: string]: boolean }>(
{},
);

const fetchOnlineStatus = async () => {
try {
users.forEach(async (u) => {
const body = await isOnline(u.id);
const online = body.isOnline;
setOnlineStatus((prev) => ({ ...prev, [u.name]: online }));
});
} catch (error) {
console.error("Error fetching online status:", error);
}
};

useEffect(() => {
fetchOnlineStatus();
}, []);

return (
<TooltipProvider delayDuration={0}>
<div className="flex flex-wrap gap-2">
Expand All @@ -43,7 +22,7 @@ export default function UserList({
size={avatarSize}
href={`/user/${u.id}`}
alt={u.name}
online={onlineStatus[u.name]}
online={true}
key={u.id}
/>
))}
Expand Down

0 comments on commit 82e4d7d

Please sign in to comment.