Skip to content

Commit a134bcc

Browse files
authored
Refactor/#338-K: 초기 접속 시 페이지 이동 UX 개선 (#346)
* refactor: 자동 로그인 위치 App으로 이동 * feat: Workspace에 로딩 페이지 추가 * feat: /workspace/:id url로 접근한 경우 다른 경로로 navigate 하지 않도록 수정
1 parent 8f9d86f commit a134bcc

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

client/src/App.tsx

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { Suspense, useState } from 'react';
2-
import { Route, Routes } from 'react-router-dom';
1+
import { useState, useEffect } from 'react';
2+
import { Route, Routes, useLocation, useNavigate } from 'react-router-dom';
3+
import { getAuth } from 'src/apis/auth';
34
import UserContext from 'src/contexts/user';
45
import {
56
LoadingPage,
@@ -14,18 +15,38 @@ import 'styles/reset.scss';
1415

1516
function App() {
1617
const [user, setUser] = useState<User | null>(null);
18+
const [isLoaded, setIsLoaded] = useState<boolean>(false);
1719

18-
return (
19-
<Suspense fallback={<LoadingPage />}>
20-
<UserContext.Provider value={{ user, setUser }}>
21-
<Routes>
22-
<Route path="/" element={<LoginPage />} />
23-
<Route path="/oauth" element={<OAuthPage />} />
24-
<Route path="/workspace/*" element={<WorkspacePage />} />
25-
<Route path="/404" element={<NotFoundPage />} />
26-
</Routes>
27-
</UserContext.Provider>
28-
</Suspense>
20+
const location = useLocation();
21+
const navigate = useNavigate();
22+
23+
const autoLogin = async () => {
24+
const { user } = await getAuth();
25+
26+
setIsLoaded(true);
27+
28+
setUser(user);
29+
30+
if (user && !/^\/workspace(\/\d)?$/.test(location.pathname)) {
31+
navigate('/workspace');
32+
}
33+
};
34+
35+
useEffect(() => {
36+
autoLogin();
37+
}, []);
38+
39+
return isLoaded ? (
40+
<UserContext.Provider value={{ user, setUser }}>
41+
<Routes>
42+
<Route path="/" element={<LoginPage />} />
43+
<Route path="/oauth" element={<OAuthPage />} />
44+
<Route path="/workspace/*" element={<WorkspacePage />} />
45+
<Route path="/404" element={<NotFoundPage />} />
46+
</Routes>
47+
</UserContext.Provider>
48+
) : (
49+
<LoadingPage />
2950
);
3051
}
3152

client/src/pages/Login/index.tsx

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,12 @@ import BubblesIcon from 'common/Icon/Bubbles';
33
import GithubIcon from 'common/Icon/Github';
44
import LogoIcon from 'common/Icon/Logo';
55
import env from 'config';
6-
import { useEffect } from 'react';
7-
import { useLocation, useNavigate } from 'react-router-dom';
8-
import { getAuth } from 'src/apis/auth';
96
import WabinarIcon from 'src/components/common/Icon/Wabinar';
10-
import useUserContext from 'src/hooks/context/useUserContext';
117

128
import style from './style.module.scss';
139

1410
function LoginPage() {
1511
const GITHUB_AUTH_URL = `https://github.com/login/oauth/authorize?client_id=${env.GITHUB_CLIENT_ID}`;
16-
const navigate = useNavigate();
17-
const location = useLocation();
18-
19-
const { setUser } = useUserContext();
20-
21-
const autoLogin = async () => {
22-
const { user } = await getAuth();
23-
24-
if (!user) {
25-
if (location.pathname.match('/workspace')) navigate('/');
26-
return;
27-
}
28-
29-
setUser(user);
30-
navigate('/workspace');
31-
};
32-
33-
useEffect(() => {
34-
autoLogin();
35-
}, []);
3612

3713
return (
3814
<div className={style.container}>

client/src/pages/Workspace/index.tsx

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
import Workspace from 'components/Workspace';
22
import { useEffect, useState } from 'react';
3-
import { Route, Routes, useNavigate } from 'react-router-dom';
3+
import { Route, Routes, useNavigate, useParams } from 'react-router-dom';
44
import { getWorkspaces } from 'src/apis/user';
55
import DefaultWorkspace from 'src/components/Workspace/DefaultWorkspace';
66
import WorkspacesContext from 'src/contexts/workspaces';
77
import useUserContext from 'src/hooks/context/useUserContext';
8+
import LoadingPage from 'src/pages/Loading';
89
import { Workspace as TWorkspace } from 'src/types/workspace';
910

1011
import Layout from './Layout';
1112

1213
function WorkspacePage() {
1314
const { user } = useUserContext();
15+
16+
const params = useParams();
1417
const navigate = useNavigate();
1518

1619
const [workspaces, setWorkspaces] = useState<TWorkspace[]>([]);
20+
const [isLoaded, setIsLoaded] = useState<boolean>(false);
1721

1822
const loadWorkspaces = async () => {
1923
if (!user) {
@@ -27,13 +31,17 @@ function WorkspacePage() {
2731
id: userId,
2832
});
2933

34+
setIsLoaded(true);
35+
3036
setWorkspaces(userWorkspaces);
3137

3238
if (!userWorkspaces.length) {
3339
navigate('/workspace');
3440
return;
3541
}
3642

43+
if (params['*']?.length) return;
44+
3745
const defaultWorkspace = userWorkspaces[0];
3846
const { id: workspaceId } = defaultWorkspace;
3947

@@ -46,12 +54,16 @@ function WorkspacePage() {
4654

4755
return (
4856
<WorkspacesContext.Provider value={{ workspaces, setWorkspaces }}>
49-
<Routes>
50-
<Route path="/" element={<Layout />}>
51-
<Route index element={<DefaultWorkspace />} />
52-
<Route path="/:id" element={<Workspace />} />
53-
</Route>
54-
</Routes>
57+
{isLoaded ? (
58+
<Routes>
59+
<Route path="/" element={<Layout />}>
60+
<Route index element={<DefaultWorkspace />} />
61+
<Route path="/:id" element={<Workspace />} />
62+
</Route>
63+
</Routes>
64+
) : (
65+
<LoadingPage />
66+
)}
5567
</WorkspacesContext.Provider>
5668
);
5769
}

0 commit comments

Comments
 (0)