import { useAdmin } from 'web/hooks/use-admin'
import { Row } from '../layout/row'
import { Content } from '../editor'
import { TextEditor, useTextEditor } from 'web/components/editor'
import { Button } from '../button'
import { Spacer } from '../layout/spacer'
import { Group } from 'common/group'
import { deleteFieldFromGroup, updateGroup } from 'web/lib/firebase/groups'
import PencilIcon from '@heroicons/react/solid/PencilIcon'
import { DocumentRemoveIcon } from '@heroicons/react/solid'
import { createPost } from 'web/lib/firebase/api'
import { Post } from 'common/post'
import { deletePost, updatePost } from 'web/lib/firebase/posts'
import { useState } from 'react'
import { usePost } from 'web/hooks/use-post'
export function GroupAboutPost(props: {
group: Group
isCreator: boolean
post: Post
}) {
const { group, isCreator } = props
const post = usePost(group.aboutPostId) ?? props.post
const isAdmin = useAdmin()
if (group.aboutPostId == null && !isCreator) {
return
No post has been created
}
return (
{isCreator || isAdmin ? (
) : (
)}
)
}
function RichEditGroupAboutPost(props: { group: Group; post: Post }) {
const { group, post } = props
const [editing, setEditing] = useState(false)
const [isSubmitting, setIsSubmitting] = useState(false)
const { editor, upload } = useTextEditor({
defaultValue: post.content,
disabled: isSubmitting,
})
async function savePost() {
if (!editor) return
const newPost = {
title: group.name,
content: editor.getJSON(),
}
if (group.aboutPostId == null) {
const result = await createPost(newPost).catch((e) => {
console.error(e)
return e
})
await updateGroup(group, {
aboutPostId: result.post.id,
})
} else {
await updatePost(post, {
content: newPost.content,
})
}
}
async function deleteGroupAboutPost() {
await deletePost(post)
await deleteFieldFromGroup(group, 'aboutPostId')
}
return editing ? (
<>
>
) : (
<>
{group.aboutPostId == null ? (
No post has been added yet.
) : (
)}
>
)
}