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(popup): add useBrowser hook; avoid importing everywhere #400

Merged
merged 2 commits into from
Jul 8, 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
18 changes: 12 additions & 6 deletions src/popup/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { MainLayout } from '@/popup/components/layout/MainLayout'
import { PopupContextProvider, TranslationContextProvider } from './lib/context'
import {
BrowserContextProvider,
PopupContextProvider,
TranslationContextProvider
} from './lib/context'
import { LazyMotion, domAnimation } from 'framer-motion'
import React from 'react'
import browser from 'webextension-polyfill'
Expand Down Expand Up @@ -50,11 +54,13 @@ const router = createMemoryRouter(routes)
export const Popup = () => {
return (
<LazyMotion features={domAnimation} strict>
<TranslationContextProvider browser={browser}>
<PopupContextProvider>
<RouterProvider router={router} />
</PopupContextProvider>
</TranslationContextProvider>
<BrowserContextProvider browser={browser}>
<TranslationContextProvider>
<PopupContextProvider>
<RouterProvider router={router} />
</PopupContextProvider>
</TranslationContextProvider>
</BrowserContextProvider>
</LazyMotion>
)
}
8 changes: 4 additions & 4 deletions src/popup/components/layout/header.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import React, { useContext, useMemo } from 'react'
import { Link, useLocation } from 'react-router-dom'
import browser from 'webextension-polyfill'
import { ArrowBack, Settings } from '../Icons'
import { ROUTES_PATH } from '@/popup/Popup'
import { PopupStateContext } from '@/popup/lib/context'

const Logo = browser.runtime.getURL('assets/images/logo.svg')
import { PopupStateContext, useBrowser } from '@/popup/lib/context'

const NavigationButton = () => {
const location = useLocation()
Expand All @@ -28,6 +25,9 @@ const NavigationButton = () => {
}

export const Header = () => {
const browser = useBrowser()
const Logo = browser.runtime.getURL('assets/images/logo.svg')

return (
<header className="flex h-8 flex-row items-center justify-between">
<div className="flex flex-row items-center gap-3">
Expand Down
37 changes: 27 additions & 10 deletions src/popup/lib/context.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import browser, { type Browser } from 'webextension-polyfill'
import React, { type PropsWithChildren } from 'react'
import type { Browser } from 'webextension-polyfill'
import { getContextData } from '@/popup/lib/messages'
import { tFactory, type Translation } from '@/shared/helpers'
import type { DeepNonNullable, PopupStore } from '@/shared/types'
Expand All @@ -12,6 +12,7 @@ import {
type BackgroundToPopupMessage
} from '@/shared/messages'

// #region PopupState
export enum ReducerActionType {
SET_DATA = 'SET_DATA',
TOGGLE_WM = 'TOGGLE_WM',
Expand Down Expand Up @@ -102,6 +103,7 @@ interface PopupContextProviderProps {
}

export function PopupContextProvider({ children }: PopupContextProviderProps) {
const browser = useBrowser()
const [isLoading, setIsLoading] = React.useState(true)
const [state, dispatch] = React.useReducer(reducer, {} as PopupState)

Expand Down Expand Up @@ -133,7 +135,7 @@ export function PopupContextProvider({ children }: PopupContextProviderProps) {
return () => {
browser.runtime.onMessage.removeListener(listener)
}
}, [])
}, [browser])

React.useEffect(() => {
const port = browser.runtime.connect({ name: CONNECTION_NAME })
Expand All @@ -150,7 +152,7 @@ export function PopupContextProvider({ children }: PopupContextProviderProps) {
return () => {
port.disconnect()
}
}, [dispatch])
}, [browser])

if (isLoading) {
return <>Loading</>
Expand All @@ -162,16 +164,30 @@ export function PopupContextProvider({ children }: PopupContextProviderProps) {
</PopupStateContext.Provider>
)
}
// #endregion

const TranslationContext = React.createContext<Translation>((v: string) => v)
// #region Browser
const BrowserContext = React.createContext<Browser>({} as Browser)

export const TranslationContextProvider = ({
export const BrowserContextProvider = ({
browser,
children
}: {
browser: Browser
children: React.ReactNode
}) => {
}: PropsWithChildren<{ browser: Browser }>) => {
return (
<BrowserContext.Provider value={browser}>
{children}
</BrowserContext.Provider>
)
}

export const useBrowser = () => React.useContext(BrowserContext)
// #endregion

// #region Translation
const TranslationContext = React.createContext<Translation>((v: string) => v)

export const TranslationContextProvider = ({ children }: PropsWithChildren) => {
const browser = useBrowser()
const t = tFactory(browser)

return (
Expand All @@ -182,3 +198,4 @@ export const TranslationContextProvider = ({
}

export const useTranslation = () => React.useContext(TranslationContext)
// #endregion
4 changes: 2 additions & 2 deletions src/popup/pages/MissingHostPermission.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from 'react'
import browser from 'webextension-polyfill'
import { PERMISSION_HOSTS } from '@/shared/defines'
import { WarningSign } from '@/popup/components/Icons'
import { useTranslation } from '@/popup/lib/context'
import { useBrowser, useTranslation } from '@/popup/lib/context'

export const Component = () => {
const browser = useBrowser()
const t = useTranslation()
return (
<div className="rounded-md bg-orange-50 p-4 text-sm">
Expand Down
Loading