11/** @jsx jsx */
22import { jsx } from 'theme-ui'
3- import React , { useReducer , useEffect , useRef , useState } from 'react'
3+ import {
4+ useReducer ,
5+ useEffect ,
6+ useRef ,
7+ useState ,
8+ FunctionComponent ,
9+ } from 'react'
410import { render } from 'react-dom'
511import debounce from 'lodash.debounce'
612import merge from 'lodash.merge'
@@ -15,13 +21,24 @@ import {
1521 LineHeights ,
1622 FontSizes ,
1723 Space ,
24+ // @ts -ignore
1825} from '@theme-ui/editor'
26+ import { Theme } from '@theme-ui/css'
27+
28+ interface DevToolsExceptionInfo {
29+ isError : boolean
30+ code : string
31+ description : string
32+ details : any [ ]
33+ isException : boolean
34+ value : string
35+ }
1936
20- const runScript = script =>
37+ const runScript = ( script : string ) =>
2138 new Promise ( ( resolve , reject ) => {
2239 debounce ( window . chrome . devtools . inspectedWindow . eval , 100 ) (
2340 script ,
24- ( result , err ) => {
41+ ( result : object , err : DevToolsExceptionInfo ) => {
2542 if ( err ) {
2643 console . error ( err )
2744 reject ( err )
@@ -31,17 +48,19 @@ const runScript = script =>
3148 )
3249 } )
3350
34- const mergeState = ( state , next ) => merge ( { } , state , next )
51+ type StateWithColorMode = { colorMode ?: string ; theme ?: Theme }
52+ const mergeState = ( state : StateWithColorMode , next : StateWithColorMode ) =>
53+ merge ( { } , state , next )
3554
36- const CopyTheme = ( { theme } ) => {
55+ const CopyTheme = ( { theme } : { theme : Theme } ) => {
3756 const [ copied , setCopied ] = useState ( false )
38- const timer = useRef ( false )
57+ const timer = useRef ( 0 )
3958
4059 const handleClick = ( ) => {
4160 setCopied ( true )
4261 copyToClipboard ( JSON . stringify ( theme ) )
4362 clearInterval ( timer . current )
44- timer . current = setInterval ( ( ) => setCopied ( false ) , 1000 )
63+ timer . current = window . setInterval ( ( ) => setCopied ( false ) , 1000 )
4564 }
4665
4766 return (
@@ -51,33 +70,32 @@ const CopyTheme = ({ theme }) => {
5170
5271const Spacer = ( ) => < div sx = { { my : 2 } } />
5372
54- const App = ( ) => {
73+ const App : FunctionComponent = ( ) => {
5574 const dark = window . chrome . devtools . panels . themeName === 'dark'
5675
57- const [ state , setState ] = useReducer ( mergeState , {
58- theme : null ,
59- colorMode : null ,
60- } )
76+ const [ state , setState ] = useReducer ( mergeState , { } )
6177
6278 const getTheme = ( ) => {
63- runScript ( `window.__THEME_UI__.theme` ) . then ( theme => {
79+ runScript ( `window.__THEME_UI__.theme` ) . then ( resolvedTheme => {
80+ const theme = resolvedTheme as StateWithColorMode [ 'theme' ]
6481 setState ( { theme } )
6582 } )
6683 }
6784
6885 const getColorMode = ( ) => {
69- runScript ( `window.__THEME_UI__.colorMode` ) . then ( colorMode => {
86+ runScript ( `window.__THEME_UI__.colorMode` ) . then ( resolvedColorMode => {
87+ const colorMode = resolvedColorMode as StateWithColorMode [ 'colorMode' ]
7088 setState ( { colorMode } )
7189 } )
7290 }
7391
74- const setTheme = nextTheme => {
92+ const setTheme = ( nextTheme : Theme ) => {
7593 const json = JSON . stringify ( nextTheme )
7694 runScript ( `window.__THEME_UI__.setTheme(${ json } )` )
7795 setState ( { theme : nextTheme } )
7896 }
7997
80- const setColorMode = nextMode => {
98+ const setColorMode = ( nextMode : Theme [ 'initialColorModeName' ] ) => {
8199 setState ( { colorMode : nextMode } )
82100 runScript ( `window.__THEME_UI__.setColorMode('${ nextMode } ')` )
83101 }
@@ -96,7 +114,7 @@ const App = () => {
96114 setColorMode,
97115 }
98116
99- if ( ! context . theme ) return false
117+ if ( ! context . theme ) return null
100118
101119 return (
102120 < Editor
@@ -135,7 +153,7 @@ const App = () => {
135153 < Space />
136154 </ Row >
137155 < Spacer />
138- < CopyTheme theme = { state . theme } />
156+ < CopyTheme theme = { context . theme } />
139157 </ Editor >
140158 )
141159}
0 commit comments