Skip to content

Commit 1b86b85

Browse files
Chatのルーティングを修正する (#360)
<!--変更したい場合は、`/.github/pull_request_template.md` を修正して下さい。--> # PRの概要 <!-- 変更の目的 もしくは 関連する Issue 番号 --> <!-- 以下のように書くと Issue にリンクでき、マージ時に自動で Issue を閉じられる--> <!-- closes #1 --> closes #346 https://github.com/user-attachments/assets/f267b164-d395-4c96-98c3-8c6d573ce4fe ## 具体的な変更内容 <!-- ビューの変更がある場合はスクショによる比較などがあるとわかりやすい --> ## 影響範囲 <!-- この関数を変更したのでこの機能にも影響がある、など --> ## 動作要件 <!-- 動作に必要な 環境変数 / 依存関係 / DBの更新 など --> ## 補足 <!-- レビューをする際に見てほしい点、ローカル環境で試す際の注意点、など --> RoomWIndow(実際にチャットするところ)でリロードすると、Headerがホーム/Homeに戻ってしまう問題が発生しました。 Issue → #392 ## レビューリクエストを出す前にチェック! - [x] 改めてセルフレビューしたか - [x] 手動での動作検証を行ったか - [x] server の機能追加ならば、テストを書いたか - 理由: 書いた | server の機能追加ではない - [x] 間違った使い方が存在するならば、それのドキュメントをコメントで書いたか - 理由: 書いた | 間違った使い方は存在しない - [x] わかりやすいPRになっているか <!-- レビューリクエスト後は、Slackでもメンションしてお願いすることを推奨します。 --> --------- Co-authored-by: aster <[email protected]>
1 parent c82e424 commit 1b86b85

File tree

5 files changed

+37
-50
lines changed

5 files changed

+37
-50
lines changed

web/src/App.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
RouterProvider,
77
createBrowserRouter,
88
} from "react-router-dom";
9+
import { RoomWindow } from "./components/chat/RoomWindow";
910
import { NavigateByAuthState } from "./components/common/NavigateByAuthState";
1011
import EditCourses from "./routes/editCourses";
1112
import EditProfile from "./routes/editProfile";
@@ -101,6 +102,14 @@ export default function App() {
101102
</NavigateByAuthState>
102103
),
103104
},
105+
{
106+
path: "chat/:friendId",
107+
element: (
108+
<NavigateByAuthState type="toLoginForUnauthenticated">
109+
<RoomWindow />
110+
</NavigateByAuthState>
111+
),
112+
},
104113
{
105114
path: "edit/profile",
106115
element: (

web/src/components/chat/RoomHeader.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import ArrowBackIcon from "@mui/icons-material/ArrowBack";
22
import { Box, Button, Typography } from "@mui/material";
3+
import { useNavigate } from "react-router-dom";
34
import type { DMOverview } from "../../common/types";
45
import UserAvatar from "../human/avatar";
56
type Props = {
67
room: DMOverview;
7-
setActiveRoom: (room: DMOverview | null) => void;
88
};
99

1010
export function RoomHeader(props: Props) {
11-
const { room, setActiveRoom } = props;
11+
const { room } = props;
12+
const navigation = useNavigate();
1213
return (
1314
<Box
1415
sx={{
@@ -21,7 +22,7 @@ export function RoomHeader(props: Props) {
2122
variant="text"
2223
sx={{ color: "black", padding: "0px", margin: "0px", minWidth: "0px" }}
2324
onClick={() => {
24-
setActiveRoom(null);
25+
navigation("/chat");
2526
}}
2627
>
2728
<ArrowBackIcon />

web/src/components/chat/RoomList.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { Box, List, Typography } from "@mui/material";
2-
import type { DMOverview, RoomOverview } from "../../common/types";
2+
import { useNavigate } from "react-router-dom";
3+
import type { RoomOverview } from "../../common/types";
34
import { HumanListItem } from "../human/humanListItem";
45

56
type RoomListProps = {
67
roomsData: RoomOverview[] | null;
7-
activeRoom: DMOverview | null;
8-
setActiveRoom: (room: DMOverview) => void;
98
};
109

1110
export function RoomList(props: RoomListProps) {
12-
const { roomsData, activeRoom, setActiveRoom } = props;
11+
const { roomsData } = props;
12+
const navigate = useNavigate();
13+
1314
return (
1415
<List disablePadding>
1516
<p
@@ -31,12 +32,9 @@ export function RoomList(props: RoomListProps) {
3132
return (
3233
<Box
3334
key={room.friendId}
34-
onClick={() => setActiveRoom(room)}
35-
sx={{
36-
backgroundColor:
37-
activeRoom?.friendId === room.friendId
38-
? "gainsboro"
39-
: "white",
35+
onClick={() => {
36+
// `state`を使って`room`データを渡す
37+
navigate(`./${room.friendId}`, { state: { room } });
4038
}}
4139
>
4240
<HumanListItem

web/src/components/chat/RoomWindow.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Box, Button, Paper, TextField, Typography } from "@mui/material";
22
import { useSnackbar } from "notistack";
33
import { useCallback, useEffect, useRef, useState } from "react";
4+
import { useLocation } from "react-router-dom";
45
import * as chat from "../../api/chat/chat";
56
import { useMessages } from "../../api/chat/hooks";
67
import * as user from "../../api/user";
@@ -19,18 +20,16 @@ import { socket } from "../data/socket";
1920
import { MessageInput } from "./MessageInput";
2021
import { RoomHeader } from "./RoomHeader";
2122

22-
type Prop = {
23-
room: DMOverview;
24-
setActiveRoom: (room: DMOverview | null) => void;
25-
};
23+
export function RoomWindow() {
24+
const { state: locationState } = useLocation();
25+
const { room } = locationState as { room: DMOverview }; // `room`データを抽出
2626

27-
export function RoomWindow(props: Prop) {
28-
const { room, setActiveRoom } = props;
2927
const {
3028
state: { data: myId },
3129
} = useMyID();
3230
const { state, reload, write } = useMessages(room.friendId);
3331
const [messages, setMessages] = useState(state.data);
32+
3433
useEffect(() => {
3534
setMessages(state.data);
3635
}, [state.data]);
@@ -161,7 +160,7 @@ export function RoomWindow(props: Prop) {
161160
top: "56px",
162161
}}
163162
>
164-
<RoomHeader room={room} setActiveRoom={setActiveRoom} />
163+
<RoomHeader room={room} />
165164
</Box>
166165
<Box
167166
sx={{

web/src/routes/tabs/chat.tsx

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,18 @@
1-
import { Box, Typography } from "@mui/material";
2-
import { useState } from "react";
1+
import { Typography } from "@mui/material";
32
import { useRoomsOverview } from "../../api/chat/hooks";
4-
import type { DMOverview } from "../../common/types";
53
import RoomList from "../../components/chat/RoomList";
6-
import { RoomWindow } from "../../components/chat/RoomWindow";
74
import FullScreenCircularProgress from "../../components/common/FullScreenCircularProgress";
85

96
export default function Chat() {
107
const { state } = useRoomsOverview();
11-
const [activeRoom, setActiveRoom] = useState<DMOverview | null>(null);
128

13-
return (
14-
<Box>
15-
{activeRoom ? (
16-
// activeRoomがtrueの場合、画面全体にRoomWindowを表示
17-
<Box>
18-
<RoomWindow room={activeRoom} setActiveRoom={setActiveRoom} />
19-
</Box>
20-
) : (
21-
// activeRoomがfalseの場合、通常のRoomListを表示
22-
<Box>
23-
{state.current === "loading" ? (
24-
<FullScreenCircularProgress />
25-
) : state.current === "error" ? (
26-
<Typography color="error">Error: {state.error.message}</Typography>
27-
) : (
28-
<RoomList
29-
roomsData={state.data}
30-
activeRoom={activeRoom}
31-
setActiveRoom={setActiveRoom}
32-
/>
33-
)}
34-
</Box>
35-
)}
36-
</Box>
37-
);
9+
if (state.current === "loading") {
10+
return <FullScreenCircularProgress />;
11+
}
12+
13+
if (state.current === "error") {
14+
return <Typography color="error">Error: {state.error.message}</Typography>;
15+
}
16+
17+
return <RoomList roomsData={state.data} />;
3818
}

0 commit comments

Comments
 (0)