Skip to content

Commit

Permalink
refactor: Menu 컴포넌트를 범용적으로 사용할 수 있도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ccwnc committed Apr 15, 2024
1 parent 7ef5009 commit 6f66522
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions src/components/header/Menu/index.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
import Link from 'next/link';
import { useRouter } from 'next/router';

import classNames from 'classnames/bind';

import { GAME_NAME_LIST_EN, GAME_PATH_NAME_TO_GAME_NAME_EN } from '@/constants';
import { formatGameToLink } from '@/utils';
import { GAME_NAME_LIST_EN } from '@/constants/games';
import { formatGameToLink } from '@/utils/gameFormatter';

import styles from './Menu.module.scss';
import { useState } from 'react';

const cx = classNames.bind(styles);

const Menu = () => {
const router = useRouter();
const { game: gameName, postType } = router.query;
const hasCreateInPath = router.pathname.includes('create');
const [activatedGame, setActivatedGame] = useState(0);

const isGameActivated = (index: number) => {
return (
GAME_PATH_NAME_TO_GAME_NAME_EN[gameName as keyof typeof GAME_PATH_NAME_TO_GAME_NAME_EN] ===
GAME_NAME_LIST_EN[index]
);
const handleActivateGame = (number: number) => {
setActivatedGame(number);
};

return (
<nav>
<ul className={cx('menu')}>
{GAME_NAME_LIST_EN.map((game, index) => (
<li key={`menu-${index}`}>
<Link
href={{
pathname: `/${formatGameToLink(game)}`,
query: { postType: hasCreateInPath ? 'all' : postType || 'all' },
}}
<a
href={`/${formatGameToLink(game)}`}
className={cx('menu-game', {
'menu-game-activated': isGameActivated(index),
'menu-game-activated': activatedGame === index,
})}
onClick={() => handleActivateGame(index)}
>
{game}
{isGameActivated(index) && <p className={cx('menu-under-line')}></p>}
</Link>
{activatedGame === index && <p className={cx('menu-under-line')}></p>}
</a>
</li>
))}
</ul>
Expand Down

0 comments on commit 6f66522

Please sign in to comment.