Skip to content

Commit

Permalink
feat: 활성화된 탭 페이지 이동 시에도 고정 (#107)
Browse files Browse the repository at this point in the history
* feat: TabsProvider 구현

* refactor: Tabs 컨텍스트로 변경

* refactor: 탭 타입 지정

* refactor: 훅 이름 변경
  • Loading branch information
hyoribogo authored Dec 3, 2023
1 parent 702e3ea commit 8bbff5a
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@tanstack/react-query'
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
import { RouterProvider } from 'react-router-dom'
import { ThemeProvider, ToastProvider } from './components'
import { TabsProvider, ThemeProvider, ToastProvider } from './components'
import { router } from './routes'

const queryClient = new QueryClient({
Expand All @@ -31,7 +31,9 @@ function App() {
<ToastProvider>
<QueryClientProvider client={queryClient}>
<ThemeProvider>
<RouterProvider router={router} />
<TabsProvider>
<RouterProvider router={router} />
</TabsProvider>
</ThemeProvider>
<ReactQueryDevtools initialIsOpen={false} />
</QueryClientProvider>
Expand Down
15 changes: 15 additions & 0 deletions src/components/TabsProvider/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { PropsWithChildren, useState } from 'react'
import { TabsContext } from '@/contexts'
import { Tabs } from '@/types'

const TabsProvider = ({ children }: PropsWithChildren) => {
const [activeTab, setActiveTab] = useState<Tabs>('invited')

return (
<TabsContext.Provider value={{ activeTab, setActiveTab }}>
{children}
</TabsContext.Provider>
)
}

export default TabsProvider
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export { default as SearchBar } from './SearchBar'
export { default as Input } from './Input'
export { default as Header } from './Header'
export { default as ThemeProvider } from './ThemeProvider'
export { default as TabsProvider } from './TabsProvider'
export { default as Dropdown } from './Dropdown'
export { default as Modal } from './Modal'
export { default as ReviewInfo } from './ReviewInfo'
Expand Down
14 changes: 14 additions & 0 deletions src/contexts/TabsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Dispatch, SetStateAction, createContext } from 'react'
import { Tabs } from '@/types'

interface TabsContextType {
activeTab: Tabs
setActiveTab: Dispatch<SetStateAction<Tabs>>
}

const TabsContext = createContext<TabsContextType>({
activeTab: 'invited',
setActiveTab: () => {},
})

export default TabsContext
1 change: 1 addition & 0 deletions src/contexts/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default as ThemeContext } from './ThemeContext'
export { default as ToastContext } from './ToastContext'
export { default as TabsContext } from './TabsContext'
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export { default as useToast } from './useToast'
export { default as useInfiniteScroll } from './useInfiniteScroll'
export { default as useRefine } from './useRefine'
export { default as useCheckHeaderRoute } from './useCheckHeaderRoute'
export { default as useTabs } from './useTabs'
8 changes: 8 additions & 0 deletions src/hooks/useTabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useContext } from 'react'
import { TabsContext } from '@/contexts'

const useTabs = () => {
return useContext(TabsContext)
}

export default useTabs
8 changes: 3 additions & 5 deletions src/pages/MainPage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Suspense, useState } from 'react'
import { Suspense } from 'react'
import { useNavigate } from 'react-router-dom'
import { useToast } from '@/hooks'
import { useTabs, useToast } from '@/hooks'
import { Header, TokenErrorBoundary } from '@/components'
import { useDeleteReview } from '@/apis/hooks'
import { rangerIdle } from '@/assets/images'
Expand All @@ -20,9 +20,7 @@ const MainPage = () => {

const navigate = useNavigate()

const [activeTab, setActiveTab] = useState<
'invited' | 'created' | 'received'
>('invited')
const { activeTab, setActiveTab } = useTabs()

const { addToast } = useToast()

Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './reviews'
export * from './reviewDetailed'
export * from './tabs'
1 change: 1 addition & 0 deletions src/types/tabs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Tabs = 'invited' | 'created' | 'received'

0 comments on commit 8bbff5a

Please sign in to comment.