portfolio/app/context/CommentFormProvider.tsx
Ryan Freeman 159cf8b0a8
All checks were successful
Build And Publish / BuildAndPublish (push) Successful in 3m11s
Update nextjs, merge contexts
2025-04-24 17:20:11 +01:00

34 lines
1.2 KiB
TypeScript

import {createContext, ReactNode, RefObject, useContext, useRef, useState} from 'react'
import {Comment} from '@/types';
type CommentFormContext = {
commentFormRef?: RefObject<HTMLTextAreaElement | null>
focusCommentForm: () => void
commentLength: number
setCommentLength: (commentLength: number) => void
commentMaxLength: number
replyTo: Comment | null
setReplyTo: (replyTo: Comment | null) => void
}
export const CommentFormContext = createContext<CommentFormContext | null>(null)
export const useCommentFormContext = () => useContext(CommentFormContext)
export default function CommentFormProvider({children}: { children: ReactNode }) {
const [replyTo, setReplyTo] = useState<Comment | null>(null)
const [commentLength, setCommentLength] = useState<number>(0)
const commentFormRef = useRef<HTMLTextAreaElement>(null)
const commentMaxLength = 300
const focusCommentForm = () => {
commentFormRef.current?.focus()
}
return (
<CommentFormContext.Provider
value={{commentFormRef, focusCommentForm, commentLength, setCommentLength, commentMaxLength, replyTo, setReplyTo}}>
{children}
</CommentFormContext.Provider>
)
}