Skip to content

Commit

Permalink
feat(editor): use fp-ts Option to handle initial editor state
Browse files Browse the repository at this point in the history
Use fp-ts Option to safely handle the initial editor state.
This avoids potential errors when accessing nested properties of
the content object. The code now uses `pipe` and Option methods
like `fromNullable`, `map`, and `getOrElse` to handle cases where
`content` or `content.editorState` might be null or undefined.
This makes the code more robust and easier to reason about.
  • Loading branch information
ktun95 committed Feb 14, 2025
1 parent 5d082dc commit b350ec5
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/editor/src/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import { LinkPlugin } from "@lexical/react/LexicalLinkPlugin"
import { HistoryPlugin } from "@lexical/react/LexicalHistoryPlugin"
import LexicalErrorBoundary from "@lexical/react/LexicalErrorBoundary"
import { Grid } from "@material-ui/core"
import { pipe } from "fp-ts/function"
import {
getOrElse as OgetOrElse,
fromNullable as OfromNullable,
map as Omap,
} from "fp-ts/Option"
import { ImagePlugin } from "@dictybase/image-plugin"
import { WidthTablePlugin } from "@dictybase/width-table-plugin"
import { FlexLayoutPlugin } from "@dictybase/flex-layout-plugin"
Expand Down Expand Up @@ -40,7 +46,12 @@ const Editor = ({
plugins,
}: EditorProperties) => {
// eslint-disable-next-line unicorn/no-null
const initialEditorState = content?.editorState || initialStateString || null
const initialEditorState = pipe(
content,
OfromNullable,
Omap(({ editorState }) => editorState),
OgetOrElse(() => initialStateString as InitialEditorStateType),
)
const placeholderClasses = useEditorPlaceholderStyles()
// const persistencePluginStyles = usePersistencePluginStyles()
const editorAreaClasses = useEditorAreaStyles({ editable })
Expand Down

0 comments on commit b350ec5

Please sign in to comment.