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

⚡ Props Drilling 문제 해결 및 케밥 메뉴의 유일성 보장 #76

Merged
merged 5 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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
Loading