Skip to content

Commit

Permalink
fix: sringify errors cases in key generator helper (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele authored Jul 22, 2022
1 parent a7bcefd commit dd381ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 35 deletions.
6 changes: 5 additions & 1 deletion helpers/generateKeyFromValue.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import sha1 from 'sha1'

export function generateKeyFromValue(value: any): string {
return sha1(JSON.stringify(value))
try {
return sha1(JSON.stringify(value))
} catch (err) {
return String(value)
}
}
38 changes: 4 additions & 34 deletions stories/fields/MarkdownEditor.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react'

import { MarkdownEditor } from '../..'
// @ts-ignore
import exampleMarkdown from '../../public/documents/example.md'

import type { MarkdownEditorProps } from '../..'

Expand All @@ -11,7 +13,7 @@ export default {
argTypes: {},

args: {
defaultValue: ``,
defaultValue: exampleMarkdown,
error: '',
helper: '',
isDisabled: false,
Expand All @@ -21,36 +23,4 @@ export default {
},
}

export const _MarkdownEditor = (props: MarkdownEditorProps) => {
const $markdownSource = React.useRef<string>()
const [isLoading, setIsLoading] = React.useState(true)

const controlledProps = React.useMemo(() => {
if (isLoading) {
return {
...props,
isDisabled: true,
placeholder: 'Loading example…',
}
}

return {
...props,
defaultValue: $markdownSource.current,
}
}, [isLoading])

const loadExample = React.useCallback(async () => {
const response = await fetch('/documents/example.md')
const body = await response.text()

$markdownSource.current = body
setIsLoading(false)
}, [])

React.useEffect(() => {
loadExample()
}, [])

return <MarkdownEditor {...controlledProps} />
}
export const _MarkdownEditor = (props: MarkdownEditorProps) => <MarkdownEditor {...props} />

0 comments on commit dd381ab

Please sign in to comment.