Skip to content

Commit

Permalink
refactor: move HomeScreen to React component
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmaddock committed Mar 7, 2021
1 parent 8793c7b commit ead85f8
Show file tree
Hide file tree
Showing 12 changed files with 456 additions and 339 deletions.
214 changes: 0 additions & 214 deletions packages/metastream-app/src/assets/homescreen.html

This file was deleted.

10 changes: 10 additions & 0 deletions packages/metastream-app/src/assets/webview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Metastream Webview</title>
<link rel="icon" type="image/png" href="/images/icon-192.png" sizes="192x192" />
<link rel="icon" type="image/png" href="/images/icon-512.png" sizes="512x512" />
</head>
<body></body>
</html>
47 changes: 6 additions & 41 deletions packages/metastream-app/src/components/Popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { HighlightButton } from './common/button'
import { t } from 'locale'
import { Trans } from 'react-i18next'
import { isFirefox } from 'utils/browser'
import { createPortal } from 'react-dom'
import { Remote } from './remote'
import { dispatchExtensionMessage } from 'utils/extension'
import { ASSETS_PATH } from 'utils/appUrl'
import { Portal } from './Portal'

const POPUP_WIDTH = 336 // px

Expand Down Expand Up @@ -111,7 +111,6 @@ export class PopupWindow extends Component<Props, State> {
}

private intervalId?: number
private stylesheetObserver?: MutationObserver

componentWillUnmount() {
this.closeWindows()
Expand Down Expand Up @@ -140,11 +139,6 @@ export class PopupWindow extends Component<Props, State> {
PopupWindow.mediaWindowRef = null
}

if (this.stylesheetObserver) {
this.stylesheetObserver.disconnect()
this.stylesheetObserver = undefined
}

clearInterval(this.intervalId)
this.intervalId = undefined

Expand Down Expand Up @@ -186,39 +180,6 @@ export class PopupWindow extends Component<Props, State> {
remotePopupReady: Boolean(PopupWindow.remoteWindowRef && !PopupWindow.remoteWindowRef.closed),
mediaPopupReady: Boolean(PopupWindow.mediaWindowRef && !PopupWindow.mediaWindowRef.closed)
})

if (this.stylesheetObserver) {
this.stylesheetObserver.disconnect()
}

this.stylesheetObserver = new MutationObserver(this.onHeadMutation)
this.stylesheetObserver.observe(document.head, { childList: true })

this.copyStyleSheets()
}

private onHeadMutation: MutationCallback = list => {
const shouldCopyStyles = list.some(record => record.type === 'childList')
if (shouldCopyStyles) {
this.copyStyleSheets()
}
}

private copyStyleSheets() {
const remoteDocument = PopupWindow.remoteWindowRef && PopupWindow.remoteWindowRef.document
if (remoteDocument) {
// remove existing stylesheets
Array.from(remoteDocument.styleSheets).forEach(stylesheet => {
if (stylesheet.ownerNode) stylesheet.ownerNode.remove()
})

// add all stylesheets from main document
Array.from(document.styleSheets).forEach(stylesheet => {
if (stylesheet.ownerNode) {
remoteDocument.head.appendChild(stylesheet.ownerNode.cloneNode(true))
}
})
}
}

private openWindows = () => {
Expand Down Expand Up @@ -322,7 +283,11 @@ export class PopupWindow extends Component<Props, State> {
const root = remoteDocument.getElementById('root')
if (!root) return

return createPortal(<Remote />, root)
return (
<Portal container={root}>
<Remote />
</Portal>
)
}

private renderPopupIcon() {
Expand Down
56 changes: 56 additions & 0 deletions packages/metastream-app/src/components/Portal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import React, { useEffect, useState } from 'react'
import { createPortal } from 'react-dom'

interface Props {
container: HTMLElement
children: React.ReactNode
}

export const Portal = ({ children, container }: Props) => {
const [isReady, setIsReady] = useState(false)

const copyStyleSheets = () => {
const remoteDocument = container.ownerDocument
if (remoteDocument) {
// remove existing stylesheets
Array.from(remoteDocument.styleSheets).forEach(stylesheet => {
if (stylesheet.ownerNode) stylesheet.ownerNode.remove()
})

// add all stylesheets from main document
Array.from(document.styleSheets).forEach(stylesheet => {
if (stylesheet.ownerNode) {
remoteDocument.head.appendChild(stylesheet.ownerNode.cloneNode(true))
}
})
}
}

useEffect(function componentDidMount() {
const stylesheetObserver = new MutationObserver(list => {
const shouldCopyStyles = list.some(record => record.type === 'childList')
if (shouldCopyStyles) {
copyStyleSheets()
}
})

stylesheetObserver.observe(document.head, { childList: true })

copyStyleSheets()

// Need to wait a bit for stylesheets to load to prevent flashing content.
let timeoutId = setTimeout(() => {
setIsReady(true)
}, 60)

return function componentWillUnmount() {
stylesheetObserver.disconnect()

if (timeoutId) {
clearTimeout(timeoutId)
}
}
}, [])

return createPortal(isReady ? children : <></>, container)
}
Loading

0 comments on commit ead85f8

Please sign in to comment.