Skip to content

Commit 13f7f9e

Browse files
committed
Handle query that's not a function
1 parent c08d424 commit 13f7f9e

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

components/Editor/QueryForm.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,23 @@ interface State {
119119
}
120120

121121
const QueryForm: React.FC<QueryProps> = ({ id, fn, openByDefault, storageKey }) => {
122+
const fnIsAFunction = typeof fn === 'function'
123+
122124
const { addLine } = useConsole()
123125
const [open, setOpen] = useEditorState(`${storageKey}-open`, !!openByDefault)
124126
const [storedValues, setStoredValues] = useEditorState(
125-
`${storageKey}-values`,
126-
JSON.stringify([...new Array(fn.length)].map(() => ''))
127+
fnIsAFunction ? `${storageKey}-values` : null,
128+
JSON.stringify([...new Array(fnIsAFunction ? fn.length : 0)].map(() => ''))
127129
)
128130
const [state, setState] = useState<State>({
129131
status: STATUS.READY,
130132
})
131133

132-
const values = JSON.parse(storedValues)
134+
if (!fnIsAFunction) {
135+
return <div>"{id}" is not a function</div>
136+
}
137+
138+
const values = JSON.parse(storedValues || '{}')
133139
const setValues = (newVals: string[]) => setStoredValues(JSON.stringify(newVals))
134140

135141
const functionNames = functionToParamNames(fn)

hooks/editor-state.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,18 @@ export const EDITOR_TYPES = {
4949
}
5050

5151
export function useEditorState<T = any>(
52-
key: string,
52+
key?: string | null,
5353
defaultState?: T,
5454
storageKey: string = EDITOR_TYPES.EDITOR_STATE
55-
): [T, (val: T) => void] {
56-
const updater = getUpdater(key)
55+
): [T | null, (val: T) => void] {
56+
const updater = getUpdater(key || 'undefined')
5757
updater.register()
5858

59-
const value = getEditorState({ key, storageKey }) || defaultState || null
59+
if (!key) {
60+
return [null, () => null]
61+
}
62+
63+
const value: T | null = getEditorState({ key, storageKey }) || defaultState || null
6064

6165
const setValue = (newVal: T) => {
6266
setEditorState({ key, value: newVal, storageKey })

0 commit comments

Comments
 (0)