import { FC, useCallback, useEffect, useState } from 'react';
import dayjs from 'dayjs';
import { TopTitle } from '@gitroom/frontend/components/launches/helpers/top.title.component';
import { useModals } from '@mantine/modals';
import { Textarea } from '@gitroom/react/form/textarea';
import { Button } from '@gitroom/react/form/button';
import clsx from 'clsx';
import { useUser } from '@gitroom/frontend/components/layout/user.context';
import { Input } from '@gitroom/react/form/input';
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
import { deleteDialog } from '@gitroom/react/helpers/delete.dialog';
import interClass from '@gitroom/react/helpers/inter.font';
export const CommentBox: FC<{
value?: string;
type: 'textarea' | 'input';
onChange: (comment: string) => void;
}> = (props) => {
const { value, onChange, type } = props;
const Component = type === 'textarea' ? Textarea : Input;
const [newComment, setNewComment] = useState(value || '');
const newCommentFunc = useCallback(
(event: { target: { value: string } }) => {
setNewComment(event.target.value);
},
[newComment]
);
const changeIt = useCallback(() => {
onChange(newComment);
setNewComment('');
}, [newComment]);
return (
);
};
interface Comments {
id: string;
content: string;
user: { email: string; id: string };
childrenComment: Comments[];
}
export const EditableCommentComponent: FC<{
comment: Comments;
onEdit: (content: string) => void;
onDelete: () => void;
}> = (props) => {
const { comment, onEdit, onDelete } = props;
const [commentContent, setCommentContent] = useState(comment.content);
const [editMode, setEditMode] = useState(false);
const user = useUser();
const updateComment = useCallback((commentValue: string) => {
if (commentValue !== comment.content) {
setCommentContent(commentValue);
onEdit(commentValue);
}
setEditMode(false);
}, []);
const deleteCommentFunction = useCallback(async () => {
if (
await deleteDialog(
'Are you sure you want to delete this comment?',
'Yes, Delete'
)
) {
onDelete();
}
}, []);
if (editMode) {
return (
);
}
return (
{commentContent}
{user?.id === comment.user.id && (
<>
>
)}
);
};
export const CommentComponent: FC<{ date: dayjs.Dayjs }> = (props) => {
const { date } = props;
const { closeAll } = useModals();
const [commentsList, setCommentsList] = useState([]);
const user = useUser();
const fetch = useFetch();
const load = useCallback(async () => {
const data = await (
await fetch(`/comments/${date.utc().format('YYYY-MM-DDTHH:mm:00')}`)
).json();
setCommentsList(data);
}, []);
useEffect(() => {
load();
}, []);
const editComment = useCallback(
(comment: Comments) => async (content: string) => {
fetch(`/comments/${comment.id}`, {
method: 'PUT',
body: JSON.stringify({
content,
date: date.utc().format('YYYY-MM-DDTHH:mm:00'),
}),
});
},
[]
);
const addComment = useCallback(
async (content: string) => {
const { id } = await (
await fetch('/comments', {
method: 'POST',
body: JSON.stringify({
content,
date: date.utc().format('YYYY-MM-DDTHH:mm:00'),
}),
})
).json();
setCommentsList((list) => [
{
id,
user: { email: user?.email!, id: user?.id! },
content,
childrenComment: [],
},
...list,
]);
},
[commentsList, setCommentsList]
);
const deleteComment = useCallback(
(comment: Comments) => async () => {
await fetch(`/comments/${comment.id}`, {
method: 'DELETE',
});
setCommentsList((list) => list.filter((item) => item.id !== comment.id));
},
[commentsList, setCommentsList]
);
const deleteChildrenComment = useCallback(
(parent: Comments, children: Comments) => async () => {
await fetch(`/comments/${children.id}`, {
method: 'DELETE',
});
setCommentsList((list) =>
list.map((item) => {
if (item.id === parent.id) {
return {
...item,
childrenComment: item.childrenComment.filter(
(child) => child.id !== children.id
),
};
}
return item;
})
);
},
[commentsList, setCommentsList]
);
const addChildrenComment = useCallback(
(comment: Comments) => async (content: string) => {
const { id } = await (
await fetch(`/comments/${comment.id}`, {
method: 'POST',
body: JSON.stringify({
content,
date: date.utc().format('YYYY-MM-DDTHH:mm:00'),
}),
})
).json();
setCommentsList((list) =>
list.map((item) => {
if (item.id === comment.id) {
return {
...item,
childrenComment: [
...item.childrenComment,
{
id,
user: { email: user?.email!, id: user?.id! },
content,
childrenComment: [],
},
],
};
}
return item;
})
);
},
[commentsList]
);
const extractNameFromEmailAndCapitalize = useCallback((email: string) => {
return (
email.split('@')[0].charAt(0).toUpperCase() + email.split('@')[0].slice(1)
);
}, []);
return (
{commentsList.map((comment, index) => (
<>
{comment.user.email[0].toUpperCase()}
{extractNameFromEmailAndCapitalize(comment.user.email)}
{comment?.childrenComment?.map((childComment, index2) => (
{childComment.user.email[0].toUpperCase()}
{extractNameFromEmailAndCapitalize(
childComment.user.email
)}
))}
>
))}
);
};