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

feat(admin): aside-navigation-menu 추가 #94

Merged
merged 17 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from 14 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
1 change: 1 addition & 0 deletions apps/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@tanstack/react-router": "^1.104.1",
"antd": "^5.24.1",
"ky": "^1.7.5",
"lucide-react": "^0.462.0",
"react": "catalog:react19",
"react-dom": "catalog:react19",
"tailwindcss": "^4.0.6"
Expand Down
Empty file removed apps/admin/src/assets/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions apps/admin/src/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 113 additions & 0 deletions apps/admin/src/components/aside-navigation-menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { Link } from '@tanstack/react-router';
import { Menu, type MenuProps } from 'antd';
import {
Clipboard,
Clock,
FlaskConical,
GraduationCap,
Speech,
Users,
} from 'lucide-react';

import LOGO from '~/assets/logo.svg';
import { useRefreshTokens } from '~/hooks/use-refresh-token';
import { useTokenExpiration } from '~/hooks/use-token-expiration';
import { authServices } from '~/utils/auth';
import { formatExpireTime } from '~/utils/utils';

type MenuItem = Required<MenuProps>['items'][number];

const items: MenuItem[] = [
//TODO: Link 내부 url 변경
{
key: 'user',
label: '회원 관리',
icon: <Users size={20} />,
},
{
key: 'about',
label: '소개',
icon: <GraduationCap size={20} />,
children: [
{ key: 'dept', label: <Link to="/">학부 소개</Link> },
{ key: 'club', label: <Link to="/">동아리 소개</Link> },
{ key: 'contact', label: <Link to="/">찾아오시는 길</Link> },
],
},
{
key: 'professor',
label: <Link to="/">교수진 소개</Link>,
icon: <Speech size={20} />,
},
{
key: 'lab',
label: <Link to="/">연구실 소개</Link>,
icon: <FlaskConical size={20} />,
},
{
key: 'board',
label: '게시판',
icon: <Clipboard size={20} />,
children: [
{ key: 'notice', label: <Link to="/">공지사항</Link> },
{ key: 'news', label: <Link to="/">학부 소식</Link> },
],
},
];

function AsideHeader() {
return (
<div className="flex items-center justify-center gap-2 font-bold border-r border-gray-200 h-22">
<img src={LOGO} alt="logo" />
<div className="leading-4.5">
<p>AI컴퓨터공학부</p>
<p>관리자 시스템</p>
</div>
</div>
);
}

function AsideFooter() {
const { expireTime } = useTokenExpiration();
const refreshMutation = useRefreshTokens();
const { logout } = authServices();

const handleRefreshToken = () => {
refreshMutation.mutate();
};

return (
<div className="flex items-center text-sm p-2 border-r border-gray-200 justify-between">
<div className="flex gap-1.5 items-center">
<Clock size={16} />
<span>{formatExpireTime(expireTime)}</span>
</div>
<div>
<button
type="button"
className="p-2 transition-colors duration-150 rounded-md cursor-pointer hover:bg-gray-300"
onClick={handleRefreshToken}
>
시간연장
</button>
<button
type="button"
className="p-2 transition-colors duration-150 rounded-md cursor-pointer hover:bg-gray-300"
onClick={logout}
>
로그아웃
</button>
</div>
</div>
);
}

export default function AsideNavigationMenu() {
return (
<aside className="flex flex-col h-full select-none bg-slate-100 w-80">
<AsideHeader />
<Menu mode="inline" items={items} className="flex-grow" />
<AsideFooter />
</aside>
);
}
67 changes: 0 additions & 67 deletions apps/admin/src/hooks/use-auth.ts

This file was deleted.

40 changes: 40 additions & 0 deletions apps/admin/src/hooks/use-refresh-token.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useMutation } from '@tanstack/react-query';
import { END_POINT } from '~/constants/api';
import { getToken } from '~/utils/api';
import { authServices } from '~/utils/auth';
import { authHttp } from '~/utils/http';
import type { Tokens } from './use-sign-in';

interface RefreshToken {
refreshToken: string;
}

const useRefreshTokens = () => {
const { setTokens, logout } = authServices();

const refreshMutation = useMutation({
mutationFn: () => {
const [accessToken, refreshToken] = getToken();

if (!accessToken || !refreshToken) {
throw new Error('토큰이 존재하지 않습니다');
}

const response = authHttp.post<RefreshToken>(END_POINT.REISSUE, {
json: { refreshToken },
});
return response.json<Tokens>();
},
onSuccess: (newTokens) => {
setTokens(newTokens);
},
onError: () => {
alert('오류가 발생하여 로그아웃합니다.');
logout();
},
});

return refreshMutation;
};

export { type RefreshToken, useRefreshTokens };
8 changes: 5 additions & 3 deletions apps/admin/src/hooks/use-sign-in.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMutation } from '@tanstack/react-query';
import { useNavigate } from '@tanstack/react-router';
import { END_POINT } from '~/constants/api';
import { useAuth } from '~/hooks/use-auth';
import { authServices } from '~/utils/auth';
import { authHttp } from '~/utils/http';

interface SignInData {
Expand All @@ -14,15 +15,16 @@ interface Tokens {
}

const useSignIn = () => {
const { setTokens } = useAuth();
const { setTokens } = authServices();
const navigate = useNavigate();

return useMutation({
mutationFn: (data: SignInData) => {
return authHttp.post(END_POINT.SIGN_IN, { json: data }).json<Tokens>();
},
onSuccess: (token) => {
console.log('success : ', token);
setTokens(token);
navigate({ to: '/main' });
},
});
};
Expand Down
33 changes: 33 additions & 0 deletions apps/admin/src/hooks/use-token-expiration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect, useState } from 'react';
import { getAccessToken } from '~/utils/api';
import { authServices } from '~/utils/auth';
import { decodeJwt } from '~/utils/jwt';

export const useTokenExpiration = () => {
const { logout } = authServices();
const [expireTime, setExpireTime] = useState<number | null>(null);
const accessToken = getAccessToken();

useEffect(() => {
if (accessToken) {
const decoded = decodeJwt(accessToken);
if (decoded?.exp) {
const expiresIn = decoded.exp * 1000 - Date.now();
setExpireTime(expiresIn);
}
}
}, [accessToken]);

useEffect(() => {
if (expireTime !== null && expireTime <= 0) {
logout();
} else if (expireTime !== null && expireTime > 0) {
const timerId = setTimeout(() => {
setExpireTime(expireTime - 1000);
}, 1000);
return () => clearTimeout(timerId);
}
}, [expireTime, logout]);

return { expireTime };
};
30 changes: 24 additions & 6 deletions apps/admin/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,35 @@
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { Outlet, createRootRoute } from '@tanstack/react-router';
import { Outlet, createRootRoute, useLocation } from '@tanstack/react-router';
import { TanStackRouterDevtools } from '@tanstack/router-devtools';
import { ConfigProvider } from 'antd';
import AsideNavigationMenu from '~/components/aside-navigation-menu';

export const Route = createRootRoute({
component: App,
});

function App() {
const location = useLocation();
const isSigninPage = location.pathname === '/';

return (
<>
<Outlet />
<TanStackRouterDevtools position="top-right" />
<ReactQueryDevtools initialIsOpen={false} />
</>
<ConfigProvider
theme={{
components: {
Menu: {
itemBg: '#f1f5f9',
},
},
}}
>
<main className="relative flex h-dvh">
{!isSigninPage && <AsideNavigationMenu />}
<div className="flex flex-col items-center justify-center w-full h-full">
<Outlet />
</div>
<TanStackRouterDevtools position="top-right" />
<ReactQueryDevtools initialIsOpen={false} />
</main>
</ConfigProvider>
);
}
4 changes: 2 additions & 2 deletions apps/admin/src/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function SignInPage() {
};

return (
<div className="flex justify-center items-center h-screen bg-gray-100">
<Card className="w-96 shadow-lg">
<div className="flex items-center justify-center w-full h-full bg-gray-100">
<Card className="shadow-lg w-96">
<Typography.Title level={3} className="text-center">
로그인
</Typography.Title>
Expand Down
9 changes: 9 additions & 0 deletions apps/admin/src/routes/main/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/main/')({
component: MainPage,
});

function MainPage() {
return <div>main</div>;
}
15 changes: 15 additions & 0 deletions apps/admin/src/styles/globals.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
@import url("https://cdn.jsdelivr.net/gh/orioncactus/pretendard@v1.3.8/dist/web/variable/pretendardvariable-dynamic-subset.css");
@import "tailwindcss";

:root {
font-family:
"Pretendard Variable", Pretendard, -apple-system,
BlinkMacSystemFont, system-ui, Roboto, "Helvetica Neue", "Segoe UI",
"Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", "Apple Color Emoji",
"Segoe UI Emoji", "Segoe UI Symbol", sans-serif;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
font-size: 16px;
}

.ProseMirror ul ul,
.ProseMirror ol ol,
.ProseMirror ul ol,
Expand Down
Empty file removed apps/admin/src/utils/.gitkeep
Empty file.
Loading