import { useState } from 'react' import { Spacer } from 'web/components/layout/spacer' import { Title } from 'web/components/title' import { TextEditor, useTextEditor } from 'web/components/editor' import { createPost } from 'web/lib/firebase/api' import clsx from 'clsx' import Router from 'next/router' import { MAX_POST_TITLE_LENGTH } from 'common/post' import { postPath } from 'web/lib/firebase/posts' import { Group } from 'common/group' import { ExpandingInput } from './expanding-input' export function CreatePost(props: { group?: Group }) { const [title, setTitle] = useState('') const [subtitle, setSubtitle] = useState('') const [error, setError] = useState('') const [isSubmitting, setIsSubmitting] = useState(false) const { group } = props const { editor, upload } = useTextEditor({ key: `post ${group?.id || ''}`, disabled: isSubmitting, }) const isValid = editor && title.length > 0 && subtitle.length > 0 && editor.isEmpty === false async function savePost(title: string) { if (!editor) return const newPost = { title: title, subtitle: subtitle, content: editor.getJSON(), groupId: group?.id, } const result = await createPost(newPost).catch((e) => { console.log(e) setError('There was an error creating the post, please try again') return e }) if (result.post) { editor.commands.clearContent(true) await Router.push(postPath(result.post.slug)) } } return (