diff --git a/src/components/AccessDeniedDescription.tsx b/src/components/AccessDeniedDescription.tsx index 4b334f1..00fb63d 100644 --- a/src/components/AccessDeniedDescription.tsx +++ b/src/components/AccessDeniedDescription.tsx @@ -1,10 +1,9 @@ -import { usePlatform } from 'containers/PlatformContext' import { GITHUB_OAUTH } from 'env' +import { platform } from 'platforms' import { GitHub } from 'platforms/GitHub' import * as React from 'react' export function AccessDeniedDescription({ hasToken }: { hasToken: boolean }) { - const platform = usePlatform() return (
Access Denied
diff --git a/src/components/FileExplorer.tsx b/src/components/FileExplorer.tsx index db4e259..95fdc88 100644 --- a/src/components/FileExplorer.tsx +++ b/src/components/FileExplorer.tsx @@ -3,10 +3,10 @@ import { LoadingIndicator } from 'components/LoadingIndicator' import { Node } from 'components/Node' import { SearchBar } from 'components/SearchBar' import { useConfigs } from 'containers/ConfigsContext' -import { usePlatform } from 'containers/PlatformContext' import { connect } from 'driver/connect' import { FileExplorerCore } from 'driver/core' import { ConnectorState, Props } from 'driver/core/FileExplorer' +import { platform } from 'platforms' import * as React from 'react' import { useEvent, usePrevious } from 'react-use' import { FixedSizeList as List, ListChildComponentProps, ListProps } from 'react-window' @@ -200,7 +200,6 @@ function ListView({ } }, [listRef.current, focusedNode]) - const platform = usePlatform() const lastNodeLength = usePrevious(nodes.length) React.useEffect(() => { if (listRef.current && !focusedNode && lastNodeLength !== nodes.length) { diff --git a/src/components/Gitako.tsx b/src/components/Gitako.tsx index fa417c7..7c68258 100644 --- a/src/components/Gitako.tsx +++ b/src/components/Gitako.tsx @@ -1,19 +1,16 @@ import { SideBar } from 'components/SideBar' import { ConfigsContext, ConfigsContextWrapper } from 'containers/ConfigsContext' -import { PlatformContextWrapper } from 'containers/PlatformContext' import * as React from 'react' import { ErrorBoundary } from './ErrorBoundary' export function Gitako() { return ( - - - - {configContext => configContext && } - - - + + + {configContext => configContext && } + + ) } diff --git a/src/components/Resizable.tsx b/src/components/Resizable.tsx index 8b5761c..2d6b594 100644 --- a/src/components/Resizable.tsx +++ b/src/components/Resizable.tsx @@ -1,6 +1,6 @@ import { HorizontalResizeHandler } from 'components/ResizeHandler' import { useConfigs } from 'containers/ConfigsContext' -import { usePlatform } from 'containers/PlatformContext' +import { platform } from 'platforms' import * as React from 'react' import { useWindowSize } from 'react-use' import { cx } from 'utils/cx' @@ -33,7 +33,6 @@ export function Resizable({ baseSize, className, children }: React.PropsWithChil configContext.set({ sideBarWidth: size }) }, [size]) - const platform = usePlatform() platform.useResizeStylesheets(size) const onResize = React.useCallback((size: number) => { diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index 365b1e5..224e63e 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -7,7 +7,6 @@ import { Resizable } from 'components/Resizable' import { SettingsBar } from 'components/settings/SettingsBar' import { ToggleShowButton } from 'components/ToggleShowButton' import { useConfigs } from 'containers/ConfigsContext' -import { usePlatform } from 'containers/PlatformContext' import { connect } from 'driver/connect' import { SideBarCore } from 'driver/core' import { ConnectorState, Props } from 'driver/core/SideBar' @@ -23,7 +22,6 @@ import * as keyHelper from 'utils/keyHelper' const RawGitako: React.FC = function RawGitako(props) { const configContext = useConfigs() const accessToken = props.configContext.val.access_token - const platform = usePlatform() const intelligentToggle = configContext.val.intelligentToggle React.useEffect(() => { diff --git a/src/components/settings/AccessTokenSettings.tsx b/src/components/settings/AccessTokenSettings.tsx index 30c9f33..7dc852c 100644 --- a/src/components/settings/AccessTokenSettings.tsx +++ b/src/components/settings/AccessTokenSettings.tsx @@ -1,7 +1,7 @@ import { Button, TextInput } from '@primer/components' import { wikiLinks } from 'components/settings/SettingsBar' import { useConfigs } from 'containers/ConfigsContext' -import { usePlatform } from 'containers/PlatformContext' +import { platform } from 'platforms' import { Gitee } from 'platforms/Gitee' import * as React from 'react' import { useStates } from 'utils/hooks/useStates' @@ -17,7 +17,6 @@ export function AccessTokenSettings(props: React.PropsWithChildren) { const useAccessToken = useStates('') const useAccessTokenHint = useStates('') const focusInput = useStates(false) - const platform = usePlatform() const { val: accessTokenHint } = useAccessTokenHint const { val: accessToken } = useAccessToken diff --git a/src/components/settings/SettingsBar.tsx b/src/components/settings/SettingsBar.tsx index ceb222a..a7ba060 100644 --- a/src/components/settings/SettingsBar.tsx +++ b/src/components/settings/SettingsBar.tsx @@ -1,7 +1,7 @@ import { Link } from '@primer/components' import { Icon } from 'components/Icon' -import { usePlatform } from 'containers/PlatformContext' import { VERSION } from 'env' +import { platform } from 'platforms' import { GitHub } from 'platforms/GitHub' import * as React from 'react' import { useStates } from 'utils/hooks/useStates' @@ -29,7 +29,6 @@ function SettingsBarContent() { const useReloadHint = useStates('') const { val: reloadHint } = useReloadHint - const platform = usePlatform() const moreFields: SimpleField[] = platform === GitHub ? [ diff --git a/src/containers/PlatformContext.tsx b/src/containers/PlatformContext.tsx deleted file mode 100644 index ce4729e..0000000 --- a/src/containers/PlatformContext.tsx +++ /dev/null @@ -1,17 +0,0 @@ -import { resolvePlatformP } from 'platforms' -import { dummyPlatformForTypeSafety } from 'platforms/dummyPlatformForTypeSafety' -import * as React from 'react' - -const PlatformContext = React.createContext(dummyPlatformForTypeSafety) - -export function PlatformContextWrapper({ children }: React.PropsWithChildren<{}>) { - const [platform, setPlatform] = React.useState(dummyPlatformForTypeSafety) - React.useEffect(() => { - resolvePlatformP.then(setPlatform) - }, []) - return {children} -} - -export function usePlatform() { - return React.useContext(PlatformContext) -} diff --git a/src/content.tsx b/src/content.tsx index b65f0ab..f4ed9b5 100644 --- a/src/content.tsx +++ b/src/content.tsx @@ -1,35 +1,33 @@ import { withErrorLog } from 'analytics' import { Gitako } from 'components/Gitako' import { addMiddleware } from 'driver/connect' -import { platform, resolvePlatformP } from 'platforms' +import { platform } from 'platforms' import * as React from 'react' import * as ReactDOM from 'react-dom' import './content.scss' -resolvePlatformP.then(() => { - if (platform.resolveMeta()) { - addMiddleware(withErrorLog) +if (platform.resolveMeta()) { + addMiddleware(withErrorLog) - function init() { - // injects a copy of stylesheets so that other extensions(e.g. dark reader) could read - function injectStyles(url: string) { - var linkElement = document.createElement('link') - linkElement.rel = 'stylesheet' - linkElement.setAttribute('href', url) - document.head.appendChild(linkElement) - } - - injectStyles(browser.extension.getURL('content.css')) - - const SideBarElement = document.createElement('div') - document.body.appendChild(SideBarElement) - ReactDOM.render(, SideBarElement) + function init() { + // injects a copy of stylesheets so that other extensions(e.g. dark reader) could read + function injectStyles(url: string) { + var linkElement = document.createElement('link') + linkElement.rel = 'stylesheet' + linkElement.setAttribute('href', url) + document.head.appendChild(linkElement) } - if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', init) - } else { - init() - } + injectStyles(browser.extension.getURL('content.css')) + + const SideBarElement = document.createElement('div') + document.body.appendChild(SideBarElement) + ReactDOM.render(, SideBarElement) } -}) + + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', init) + } else { + init() + } +} diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 079fdc7..0c02ac0 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -1,5 +1,5 @@ -import { usePlatform } from 'containers/PlatformContext' import { GITHUB_OAUTH } from 'env' +import { platform } from 'platforms' import * as React from 'react' import { useEvent } from 'react-use' import { bodySpacingClassName } from 'utils/DOMHelper' @@ -167,7 +167,6 @@ export const GitHub: Platform = { } export function useGitHubAttachCopySnippetButton(copySnippetButton: boolean) { - const platform = usePlatform() const attachCopySnippetButton = React.useCallback( function attachCopySnippetButton() { if (platform !== GitHub) return @@ -180,7 +179,6 @@ export function useGitHubAttachCopySnippetButton(copySnippetButton: boolean) { } export function useGitHubAttachCopyFileButton(copyFileButton: boolean) { - const platform = usePlatform() const attachCopyFileButton = React.useCallback( function attachCopyFileButton() { if (platform !== GitHub) return diff --git a/src/platforms/Gitee/index.ts b/src/platforms/Gitee/index.ts index 6584a2e..e71b19e 100644 --- a/src/platforms/Gitee/index.ts +++ b/src/platforms/Gitee/index.ts @@ -1,5 +1,5 @@ -import { usePlatform } from 'containers/PlatformContext' import { GITEE_OAUTH } from 'env' +import { platform } from 'platforms' import * as React from 'react' import { useEvent } from 'react-use' import { bodySpacingClassName } from 'utils/DOMHelper' @@ -168,7 +168,6 @@ export const Gitee: Platform = { } export function useGiteeAttachCopySnippetButton(copySnippetButton: boolean) { - const platform = usePlatform() const attachCopySnippetButton = React.useCallback( function attachCopySnippetButton() { if (platform !== Gitee) return diff --git a/src/platforms/index.ts b/src/platforms/index.ts index 881e0fd..cb97c65 100644 --- a/src/platforms/index.ts +++ b/src/platforms/index.ts @@ -1,4 +1,3 @@ -import * as storageHelper from 'utils/storageHelper' import { dummyPlatformForTypeSafety } from './dummyPlatformForTypeSafety' import { Gitee } from './Gitee' import { GitHub } from './GitHub' @@ -8,51 +7,23 @@ const platformsMap: Record<'GitHub' | 'Gitee', { platform: Platform; hosts: stri Gitee: { platform: Gitee, hosts: ['gitee.com'] }, } -const CustomDomainsStorageKey = 'CUSTOM_DOMAINS' -async function loadCustomDomains() { - const config = await storageHelper.get([CustomDomainsStorageKey]) - if (config) { - const { [CustomDomainsStorageKey]: customDomains } = config - type CustomDomains = Record // domain -> Platform - if (customDomains) { - Object.keys(customDomains as CustomDomains).forEach(domain => { - if (customDomains[domain] in platformsMap) { - platformsMap[customDomains[domain] as keyof typeof platformsMap].hosts.push(domain) - } - }) - } - } -} -loadCustomDomains() - -async function resolvePlatform(): Promise { +function resolvePlatform() { for (const { hosts, platform } of Object.values(platformsMap)) { - if (hosts.some(host => host === window.location.host)) { + if (hosts.some(host => host === window.location.host || platform.resolveMeta())) { return platform } } return dummyPlatformForTypeSafety } -let $platform: Platform = dummyPlatformForTypeSafety - -export const resolvePlatformP = resolvePlatform() -resolvePlatformP.then(platform => ($platform = platform)) - -export async function getPlatformName() { - await resolvePlatformP +export function getPlatformName() { const keys = Object.keys(platformsMap) as (keyof typeof platformsMap)[] for (const key of keys) { - const { platform } = platformsMap[key] - if (platform === $platform) return key + if (platform === platformsMap[key].platform) return key } } -export const platform: Platform = new Proxy(dummyPlatformForTypeSafety, { - get(target, key: keyof Platform) { - return $platform[key] - }, -}) +export const platform = resolvePlatform() export const errors = { SERVER_FAULT: 'Server Fault',