Skip to content

Commit

Permalink
⚡ Props Drilling 문제 해결 및 케밥 메뉴의 유일성 보장 (#76)
Browse files Browse the repository at this point in the history
* feat: 피드 케밥 팝업창 저장소
* feat: 피드 kebab 메뉴 클릭 관리 메서드 추가
* feat: 케밥 버튼 적용
* feat: 메서드명 수정
* feat: FeedKebabStore 인터페이스로 수정

Closes #67
  • Loading branch information
BangDori authored May 22, 2024
1 parent 985cec8 commit b28debd
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
39 changes: 39 additions & 0 deletions src/widgets/feed-kebab/store/feed-kebab-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';

interface FeedKebabStore {
openedFeedId: number;
isOpen: boolean;
openKebab: (_: number) => void;
closeKebab: () => void;
}

/**
* 피드의 kebab 메뉴를 관리하는 store
*/
export const useFeedKebabStore = create<FeedKebabStore>()(
devtools(
(set): FeedKebabStore => ({
openedFeedId: 0,
isOpen: false,
openKebab: (clickedId: number) =>
set({ openedFeedId: clickedId, isOpen: true }),
closeKebab: () => set({ openedFeedId: -1, isOpen: false }),
}),
{ name: 'feed-kebab-store' },
),
);

/**
* 피드의 kebab 메뉴를 열거나 닫습니다.
* @param feedId 피드 아이디
* @returns
*/
export const onClickFeedKebab = (feedId: number) => {
if (feedId === useFeedKebabStore.getState().openedFeedId) {
useFeedKebabStore.getState().closeKebab();
return;
}

useFeedKebabStore.getState().openKebab(feedId);
};
1 change: 1 addition & 0 deletions src/widgets/feed-kebab/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './feed-kebab-store';
14 changes: 9 additions & 5 deletions src/widgets/feed-kebab/ui/FeedKebabButton.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
import { useToggle } from '@/shared/hooks';
import { Icon } from '@/shared/ui';

import { useFeedKebabStore, onClickFeedKebab } from '../store';

import { KebabMenu } from './KebabMenu';

export const FeedKebabButton: React.FC<{ feedId: number }> = ({ feedId }) => {
const [isVisibilityKebabMenu, toggleVisibility] = useToggle(false);
const { openedFeedId, isOpen, closeKebab } = useFeedKebabStore();

return (
<>
<button className='icon kebab-icon-btn' onClick={toggleVisibility}>
<button
className='icon kebab-icon-btn'
onClick={() => onClickFeedKebab(feedId)}
>
<Icon name='kebab-menu' width='20' height='20' />
</button>
{isVisibilityKebabMenu && (
<KebabMenu feedId={feedId} onClose={toggleVisibility} />
{openedFeedId === feedId && isOpen && (
<KebabMenu feedId={feedId} onClose={closeKebab} />
)}
</>
);
Expand Down

0 comments on commit b28debd

Please sign in to comment.