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

refactor: Optimize performance with comprehensive component optimization and modular architecture #194

Merged
merged 25 commits into from
Dec 4, 2024

Conversation

rhino-ty
Copy link
Collaborator

@rhino-ty rhino-ty commented Dec 4, 2024

๐Ÿ“‚ ์ž‘์—… ๋‚ด์šฉ

closes #164

  • ํผํฌ๋จผ์Šค ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ์ปดํฌ๋„ŒํŠธ ๋ฆฌํŒฉํ† ๋ง
  • ๋‹จ์ถ•ํ‚ค ๊ธฐ๋Šฅ ๊ฐœ์„  ๋ฐ ํ™•์žฅ
  • ํƒ€์ด๋จธ ๋กœ์ง ๋ถ„๋ฆฌ ๋ฐ ๊ฐœ์„ 
  • ๊ทธ ์™ธ UX ํ–ฅ์ƒ ๊ธฐ๋Šฅ ๊ตฌํ˜„

๐Ÿ’ก ์ž์„ธํ•œ ์„ค๋ช…

1. ์„ฑ๋Šฅ ์ตœ์ ํ™”

  • Zustand ์Šคํ† ์–ด ์ ‘๊ทผ ๋ฐฉ์‹ ๊ฐœ์„ 

    • ๊ฐœ๋ณ„ selector ์‚ฌ์šฉ์œผ๋กœ ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง ๋ฐฉ์ง€
    • ๊ฐ์ฒด ๊ตฌ์กฐ ๋ถ„ํ•ด ๋Œ€์‹  ๊ฐœ๋ณ„ ์ƒํƒœ ๊ตฌ๋… ๋ฐฉ์‹ ๋„์ž…
    • PlayerCardList, Chat, Setting ๋“ฑ ์ฃผ์š” ์ปดํฌ๋„ŒํŠธ ์ตœ์ ํ™”
  • ์ปดํฌ๋„ŒํŠธ ๋ชจ๋“ˆํ™”

    • Chat ์ปดํฌ๋„ŒํŠธ๋ฅผ ChatContainer, ChatList, ChatInput, ChatBubble๋กœ ๋ถ„๋ฆฌ
    • ๊ฐ ์ปดํฌ๋„ŒํŠธ์— memo ์ ์šฉ์œผ๋กœ ๋ฆฌ๋ Œ๋”๋ง ์ตœ์†Œํ™”
    • Setting ์ปดํฌ๋„ŒํŠธ์˜ ๊ด€์‹ฌ์‚ฌ ๋ถ„๋ฆฌ ๋ฐ ๊ตฌ์กฐ ๊ฐœ์„ 

2. ๋‹จ์ถ•ํ‚ค ์‹œ์Šคํ…œ ๊ฐœ์„ 

  • SHORTCUT_KEYS ํ™•์žฅ

    • ํ•œ๊ธ€ ๋‹จ์ถ•ํ‚ค ์ง€์› ์ถ”๊ฐ€
    • ์„ค์ • ๊ด€๋ จ ๋‹จ์ถ•ํ‚ค ์ง๊ด€์  ๋ณ€๊ฒฝ (Q,W,E โ†’ 1,2,3)
    • ๋Œ€์ฒด ํ‚ค ์ง€์›์œผ๋กœ ์‚ฌ์šฉ์„ฑ ํ–ฅ์ƒ
  • useShortcuts ์ปค์Šคํ…€ ํ›… ๊ตฌํ˜„

    • ๋™์  ๋‹จ์ถ•ํ‚ค ์„ค์ • ์ง€์›
    • input ์š”์†Œ์—์„œ ์ž๋™ ๋น„ํ™œ์„ฑํ™”
    • ์—ฌ๋Ÿฌ ์ปดํฌ๋„ŒํŠธ์— ํ†ตํ•ฉ ์ ์šฉ

3. ํƒ€์ด๋จธ ๋กœ์ง ๊ฐœ์„ 

  • ๋…๋ฆฝ์ ์ธ Timer ์Šคํ† ์–ด ๊ตฌํ˜„
    • gameSocket.store์—์„œ ๋ถ„๋ฆฌ
    • ๋ถˆํ•„์š”ํ•œ ๋ฆฌ๋ Œ๋”๋ง ๋ฐฉ์ง€
    • ํƒ€์ด๋จธ ์ƒํƒœ ๊ด€๋ฆฌ ํšจ์œจํ™”

4. ๊ทธ ์™ธ

  • ์›น์•ฑ ๊ด€๋ จ ์„ค์ • ์ˆ˜์ •

    • mobile-web-app-capable ๋ฉ”ํƒ€ ํƒœ๊ทธ ์ˆ˜์ •์œผ๋กœ ๋ชจ๋ฐ”์ผ ์›น์•ฑ ํ™˜๊ฒฝ ๊ฐœ์„ 
  • UI/UX ๊ฐœ์„ 

    • ๋ณผ๋ฅจ ์ปจํŠธ๋กค์„ ์ˆ˜์ง์—์„œ ์ˆ˜ํ‰์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์‚ฌ์šฉ์„ฑ ํ–ฅ์ƒ
    • ๊ฒŒ์ž„ ๋ ˆ์ด์•„์›ƒ ์ตœ์†Œ ๋†’์ด๋ฅผ 45rem์œผ๋กœ ์กฐ์ •
    • ๊ฒŒ์ž„ ์‹œ์ž‘ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋กœ๋”ฉ ์ƒํƒœ ๋ฐ ๋ฉ”์‹œ์ง€ ์ถ”๊ฐ€
    • ๋ผ์šด๋“œ ์ง„ํ–‰ ์‹œ ์Šน๋ฆฌ/ํŒจ๋ฐฐ ์‚ฌ์šด๋“œ ํšจ๊ณผ ์ถ”๊ฐ€
    • ๋ผ์šด๋“œ ์ข…๋ฃŒ ์‹œ ํŒจ๋ฐฐ ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ถ”๊ฐ€
  • ์ฝ”๋“œ ์ •๋ฆฌ

    • useDrawingSocket, createSocket์˜ ๋ถˆํ•„์š”ํ•œ console.log ์ œ๊ฑฐ
    • @tanstack/react-query ํŒจํ‚ค์ง€ ์ œ๊ฑฐ

๐Ÿ“— ์ฐธ๊ณ  ์ž๋ฃŒ & ๊ตฌํ˜„ ๊ฒฐ๊ณผ

  • ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ฒฐ๊ณผ๋Š” React DevTools Profiler๋กœ ํ™•์ธ ๊ฐ€๋Šฅ
  • ๋‹จ์ถ•ํ‚ค ์‹œ์Šคํ…œ ์‚ฌ์šฉ๋ฒ•์€ SHORTCUT_KEYS ์ƒ์ˆ˜ ์ฐธ๊ณ 

๐Ÿ“ข ๋ฆฌ๋ทฐ ์š”๊ตฌ ์‚ฌํ•ญ

  • ์„ฑ๋Šฅ ์ตœ์ ํ™”๊ฐ€ ์ ์ ˆํ•˜๊ฒŒ ์ด๋ฃจ์–ด์กŒ๋Š”์ง€ ํ™•์ธ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค
  • ๋‹จ์ถ•ํ‚ค ์‹œ์Šคํ…œ์˜ ์‚ฌ์šฉ์„ฑ ๊ฒ€ํ† ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค
  • Timer ์Šคํ† ์–ด ๋ถ„๋ฆฌ๊ฐ€ ์ ์ ˆํ•œ์ง€ ๊ฒ€ํ†  ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค

โœ… ์…€ํ”„ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  • PR ์ œ๋ชฉ์„ ํ˜•์‹์— ๋งž๊ฒŒ ์ž‘์„ฑํ–ˆ๋‚˜์š”?
  • ๋ธŒ๋žœ์น˜ ์ „๋žต์— ๋งž๋Š” ๋ธŒ๋žœ์น˜์— PR์„ ์˜ฌ๋ฆฌ๊ณ  ์žˆ๋‚˜์š”? (main์ด ์•„๋‹™๋‹ˆ๋‹ค.)
  • ์ด์Šˆ๋Š” close ํ–ˆ๋‚˜์š”?
  • Reviewers, Labels๋ฅผ ๋“ฑ๋กํ–ˆ๋‚˜์š”?
  • ์ž‘์—… ๋„์ค‘ ๋ฌธ์„œ ์ˆ˜์ •์ด ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์ž˜ ์ˆ˜์ •ํ–ˆ๋‚˜์š”?
  • ํ…Œ์ŠคํŠธ๋Š” ์ž˜ ํ†ต๊ณผํ–ˆ๋‚˜์š”?
  • ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ๋Š” ์ œ๊ฑฐํ–ˆ๋‚˜์š”?

- Wrap PlayerCardList component with memo to prevent unnecessary re-renders
- Replace object destructuring from useGameSocketStore with individual selectors
- Split store access into separate selectors for better performance:
 - players
 - hostId
 - roundAssignedRole
 - currentPlayerId
- Maintain same functionality while reducing re-renders from unrelated state changes
โ€ฆmize performance

- Decompose monolithic Chat component into smaller focused components:
 - ChatContainer: Top-level socket management
 - ChatList: Message display and scroll management
 - ChatInput: Message input and submission
 - ChatBubble: Individual message UI
- Add memo to all components to prevent unnecessary re-renders
- Implement individual selectors for Zustand store access
- Enhance useDrawingSocket hook with submitDrawing event handler
- Improve code organization and separation of concerns
- Optimize state subscription
  - Prevent unnecessary re-renders by separating useGameSocketStore selectors
  - Change to individually subscribe only to roomSettings and isHost states
- Component memoization
  - Apply memo to Setting component
  - Memoize handleSettingChange callback function with useCallback
- Improve component structure
  - Separate concerns by extracting SettingContent component
  - Define RoomSettingItem type and separate setting constants
  - Reduce unnecessary JSX nesting
- Enhance type safety
  - Add SettingKey type alias
  - Export RoomSettingItem interface for reusability
โ€ฆpport

- Rename SHORTCUT_KEY to SHORTCUT_KEYS for consistency
- Add description for each shortcut
- Include alternative keys for most shortcuts
- Add Korean character alternatives for game-related shortcuts
- Group shortcuts by category (settings and game-related)
- Change number keys for dropdown settings (1, 2, 3 instead of q, w, e)
- Use 'as const' for type safety
- Create useShortcuts custom hook for managing keyboard shortcuts
- Add support for main and alternative keys defined in SHORTCUT_KEYS
- Implement shortcut disabling for input elements (input, textarea, select)
- Allow dynamic configuration of shortcuts with action and disabled state
- Prevent default browser behavior when shortcut is triggered
- Ensure proper cleanup of event listener on component unmount
โ€ฆality

- Add useShortcuts hook
  โ€ข ChatInput
  โ€ข InviteButton
  โ€ข StartButton
  โ€ข Setting
  โ€ข SettingItem
  โ€ข Dropdown
- Integrate useShortcuts in useDropdown and useStartButton hooks
- Update ROOM_SETTINGS to include shortcutKey for each setting item
- Replace individual keydown event listeners with useShortcuts hook
- Enhance code consistency and maintainability across the application
- Replace direct zustand store access with selectors
  โ€ข InviteButton
  โ€ข NavigationModal
  โ€ข RoleModal
  โ€ข RoundEndModal
  โ€ข QuizStage
  โ€ข ToastContainer
  โ€ข BrowserNavigationGuard
  โ€ข GameHeader
  โ€ข GameLayout
- Update hooks to use zustand selectors
  โ€ข useCreateRoom
  โ€ข useTimer
  โ€ข useDrawingState
  โ€ข useChatSocket
  โ€ข useDrawingSocket
  โ€ข useGameSocket
- Improve performance by minimizing unnecessary re-renders
- Enhance code readability and maintainability
โ€ฆs dependency array and interval management
โ€ฆary re-renders

- Created a dedicated Timer store using Zustand to manage timer states.
- Implemented actions for updating, decreasing, and resetting timers.
- Ensured that the Timer store operates independently to enhance performance and maintainability.
- Changed the source of the timer logic from the game socket store to the timer store
- Improved performance by preventing unnecessary re-renders with an independent timer store
- Change the volume input orientation from vertical to horizontal
- Remove vertical-related styling and logic
- Adjust layout for horizontal display
@rhino-ty rhino-ty added โœจ Feature New feature or request ๐Ÿ”จ Refactor Code Refactoring ๐Ÿ’ป FE Frontend Code labels Dec 4, 2024
@rhino-ty rhino-ty added this to the Week 6 milestone Dec 4, 2024
@rhino-ty rhino-ty self-assigned this Dec 4, 2024
Copy link
Contributor

github-actions bot commented Dec 4, 2024

๐Ÿ“š Storybook preview deployed to: Visit Storybook Preview

- Simplify dependency array in useEffect to prevent unnecessary re-renders
- Improve timer interval handling to avoid creating/clearing intervals on every render
- Apply z-10 class to GameHeader component to ensure it appears above background elements
- Resolve potential overlap issues with other page elements
Copy link
Contributor

github-actions bot commented Dec 4, 2024

๐Ÿ“š Storybook preview deployed to: Visit Storybook Preview

@rhino-ty rhino-ty merged commit b8b7dd6 into develop Dec 4, 2024
2 checks passed
Copy link
Contributor

github-actions bot commented Dec 4, 2024

๐Ÿงน Storybook preview for PR #194 has been removed.

github-actions bot added a commit that referenced this pull request Dec 4, 2024
@rhino-ty rhino-ty deleted the refactor/#164 branch December 4, 2024 06:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
๐Ÿ’ป FE Frontend Code โœจ Feature New feature or request ๐Ÿ”จ Refactor Code Refactoring
Projects
None yet
3 participants