'use client'; import { useUser } from '@gitroom/frontend/components/layout/user.context'; import { Button } from '@gitroom/react/form/button'; import { FC, useCallback, useMemo, useState } from 'react'; import { useFetch } from '@gitroom/helpers/utils/custom.fetch'; import useSWR from 'swr'; import { FieldValues, SubmitHandler, useForm } from 'react-hook-form'; import { useT } from '@gitroom/react/translation/get.transation.service.client'; export const RenderComponents: FC<{ postId: string; }> = (props) => { const { postId } = props; const fetch = useFetch(); const comments = useCallback(async () => { return (await fetch(`/public/posts/${postId}/comments`)).json(); }, [postId]); const { data, mutate, isLoading } = useSWR('comments', comments); const mapUsers = useMemo(() => { return (data?.comments || []).reduce( (all: any, current: any) => { all.users[current.userId] = all.users[current.userId] || all.counter++; return all; }, { users: {}, counter: 1, } ).users; }, [data]); const { handleSubmit, register, setValue } = useForm(); const submit: SubmitHandler = useCallback( async (e) => { setValue('comment', ''); await fetch(`/posts/${postId}/comments`, { method: 'POST', body: JSON.stringify(e), }); mutate(); }, [postId, mutate] ); const t = useT(); if (isLoading) { return <>; } return ( <>