From 287fe360d37ac1f87a5b333875b3bc13561c0bd8 Mon Sep 17 00:00:00 2001 From: Wahyu Kurniawan Date: Wed, 6 Mar 2024 13:22:17 +0700 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20use=20`useMemo?= =?UTF-8?q?`=20to=20render=20organization=20and=20sidebar=20menu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/components/shared/Sidebar/Sidebar.tsx | 72 +++++++++---------- .../components/shared/Sidebar/constants.tsx | 15 ++++ 2 files changed, 48 insertions(+), 39 deletions(-) create mode 100644 packages/frontend/src/components/shared/Sidebar/constants.tsx diff --git a/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx b/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx index 14ff0f3b..8493cd1e 100644 --- a/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx +++ b/packages/frontend/src/components/shared/Sidebar/Sidebar.tsx @@ -1,4 +1,4 @@ -import React, { useCallback, useEffect, useState } from 'react'; +import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { NavLink, useNavigate, useParams } from 'react-router-dom'; import { Organization, User } from 'gql-client'; import { motion } from 'framer-motion'; @@ -10,12 +10,10 @@ import { useGQLClient } from 'context/GQLClientContext'; import AsyncSelect from 'components/shared/AsyncSelect'; import { ChevronGrabberHorizontal, - FolderIcon, GlobeIcon, LifeBuoyIcon, LogoutIcon, QuestionMarkRoundIcon, - SettingsSlidersIcon, } from 'components/shared/CustomIcon'; import { Tabs } from 'components/shared/Tabs'; import { Logo } from 'components/Logo'; @@ -25,6 +23,7 @@ import { getInitials } from 'utils/geInitials'; import { Button } from 'components/shared/Button'; import { cn } from 'utils/classnames'; import { useMediaQuery } from 'usehooks-ts'; +import { SIDEBAR_MENU } from './constants'; interface SidebarProps { mobileOpen?: boolean; @@ -61,11 +60,39 @@ export const Sidebar = ({ mobileOpen }: SidebarProps) => { setSelectedOrgSlug(orgSlug); }, [orgSlug]); + const renderOrganizations = useMemo(() => { + return organizations.map((org) => ( + + )); + }, [organizations]); + + const renderMenu = useMemo(() => { + return SIDEBAR_MENU(orgSlug).map(({ title, icon, url }, index) => ( + + + {title} + + + )); + }, [orgSlug]); + const handleLogOut = useCallback(() => { disconnect(); navigate('/login'); }, [disconnect, navigate]); - console.log(isDesktop); + return ( { } > {/* // TODO: Show label organization and manage in option */} - {organizations.map((org) => ( - - ))} + {renderOrganizations} - - {[ - { - title: 'Projects', - url: `/${orgSlug}/`, - icon: , - }, - { - title: 'Settings', - url: `/${orgSlug}/settings`, - icon: , - }, - ].map(({ title, icon, url }, index) => ( - - - {title} - - - ))} - + {renderMenu} {/* Bottom navigation */} diff --git a/packages/frontend/src/components/shared/Sidebar/constants.tsx b/packages/frontend/src/components/shared/Sidebar/constants.tsx new file mode 100644 index 00000000..6c303105 --- /dev/null +++ b/packages/frontend/src/components/shared/Sidebar/constants.tsx @@ -0,0 +1,15 @@ +import React from 'react'; +import { FolderIcon, SettingsSlidersIcon } from 'components/shared/CustomIcon'; + +export const SIDEBAR_MENU = (orgSlug?: string) => [ + { + title: 'Projects', + url: `/${orgSlug}/`, + icon: , + }, + { + title: 'Settings', + url: `/${orgSlug}/settings`, + icon: , + }, +];