Skip to content

Commit 1b2b149

Browse files
committed
chore(chrome): improve types
1 parent 534ef53 commit 1b2b149

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

packages/chrome/src/index.tsx

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,20 @@ import {
2525
} from '@theme-ui/editor'
2626
import { Theme } from '@theme-ui/css'
2727

28-
const runScript = (script: any) =>
28+
interface DevToolsExceptionInfo {
29+
isError: boolean
30+
code: string
31+
description: string
32+
details: any[]
33+
isException: boolean
34+
value: string
35+
}
36+
37+
const runScript = (script: string) =>
2938
new Promise((resolve, reject) => {
3039
debounce(window.chrome.devtools.inspectedWindow.eval, 100)(
3140
script,
32-
(result: any, err: Error) => {
41+
(result: object, err: DevToolsExceptionInfo) => {
3342
if (err) {
3443
console.error(err)
3544
reject(err)
@@ -39,7 +48,9 @@ const runScript = (script: any) =>
3948
)
4049
})
4150

42-
const mergeState = (state: any, next: any) => merge({}, state, next)
51+
type StateWithColorMode = { colorMode?: string; theme?: Theme }
52+
const mergeState = (state: StateWithColorMode, next: StateWithColorMode) =>
53+
merge({}, state, next)
4354

4455
const CopyTheme = ({ theme }: { theme: Theme }) => {
4556
const [copied, setCopied] = useState(false)
@@ -62,19 +73,18 @@ const Spacer = () => <div sx={{ my: 2 }} />
6273
const App: FunctionComponent = () => {
6374
const dark = window.chrome.devtools.panels.themeName === 'dark'
6475

65-
const [state, setState] = useReducer(mergeState, {
66-
theme: null,
67-
colorMode: null,
68-
})
76+
const [state, setState] = useReducer(mergeState, {})
6977

7078
const getTheme = () => {
71-
runScript(`window.__THEME_UI__.theme`).then(theme => {
79+
runScript(`window.__THEME_UI__.theme`).then(resolvedTheme => {
80+
const theme = resolvedTheme as StateWithColorMode['theme']
7281
setState({ theme })
7382
})
7483
}
7584

7685
const getColorMode = () => {
77-
runScript(`window.__THEME_UI__.colorMode`).then(colorMode => {
86+
runScript(`window.__THEME_UI__.colorMode`).then(resolvedColorMode => {
87+
const colorMode = resolvedColorMode as StateWithColorMode['colorMode']
7888
setState({ colorMode })
7989
})
8090
}
@@ -143,7 +153,7 @@ const App: FunctionComponent = () => {
143153
<Space />
144154
</Row>
145155
<Spacer />
146-
<CopyTheme theme={state.theme} />
156+
<CopyTheme theme={context.theme} />
147157
</Editor>
148158
)
149159
}

0 commit comments

Comments
 (0)