chore: don't show convert as-is button when changing to super if text is not json

This commit is contained in:
Aman Harwara
2023-08-05 01:43:55 +05:30
parent 3a0f929261
commit 83b83cd96d

View File

@@ -18,8 +18,18 @@ type Props = {
onComplete: () => void onComplete: () => void
} }
function isValidJson(string: string) {
try {
JSON.parse(string)
} catch (e) {
return false
}
return true
}
export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application, closeDialog, onComplete }) => { export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application, closeDialog, onComplete }) => {
const isSeamlessConvert = note.text.length === 0 const isSeamlessConvert = note.text.length === 0
const canBeConvertedAsIs = isValidJson(note.text)
const [lastValue, setLastValue] = useState({ text: '', previewPlain: '' }) const [lastValue, setLastValue] = useState({ text: '', previewPlain: '' })
const format = const format =
@@ -79,8 +89,8 @@ export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application,
onComplete() onComplete()
}, [closeDialog, application, note, onComplete, performConvert]) }, [closeDialog, application, note, onComplete, performConvert])
const modalActions: ModalAction[] = useMemo( const modalActions = useMemo(
() => [ (): ModalAction[] => [
{ {
label: 'Cancel', label: 'Cancel',
onClick: closeDialog, onClick: closeDialog,
@@ -97,9 +107,10 @@ export const SuperNoteImporter: FunctionComponent<Props> = ({ note, application,
label: 'Convert As-Is', label: 'Convert As-Is',
onClick: convertAsIs, onClick: convertAsIs,
type: 'secondary', type: 'secondary',
hidden: !canBeConvertedAsIs,
}, },
], ],
[closeDialog, confirmConvert, convertAsIs], [canBeConvertedAsIs, closeDialog, confirmConvert, convertAsIs],
) )
if (isSeamlessConvert) { if (isSeamlessConvert) {