⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
feat: comments ui proposal (#262)
devjiel741bbc41f617
diff --git a/apps/api/src/activity/controllers/delete-comment.ts b/apps/api/src/activity/controllers/delete-comment.tsnew file mode 100644index 000000000..a1e3d1c2e--- /dev/null+++ b/apps/api/src/activity/controllers/delete-comment.ts@@ -0,0 +1,13 @@+import { and, eq } from "drizzle-orm";+import db from "../../database";+import { activityTable } from "../../database/schema";++async function deleteComment(userEmail: string, id: string) {+ await db+ .delete(activityTable)+ .where(+ and(eq(activityTable.id, id), eq(activityTable.userEmail, userEmail)),+ );+}++export default deleteComment;diff --git a/apps/api/src/activity/controllers/update-comment.ts b/apps/api/src/activity/controllers/update-comment.tsnew file mode 100644index 000000000..ba2ac3e0a--- /dev/null+++ b/apps/api/src/activity/controllers/update-comment.ts@@ -0,0 +1,14 @@+import { and, eq } from "drizzle-orm";+import db from "../../database";+import { activityTable } from "../../database/schema";++async function updateComment(userEmail: string, id: string, content: string) {+ return await db+ .update(activityTable)+ .set({ content })+ .where(+ and(eq(activityTable.id, id), eq(activityTable.userEmail, userEmail)),+ );+}++export default updateComment;diff --git a/apps/api/src/activity/index.ts b/apps/api/src/activity/index.tsindex 9c5172468..4779206bd 100644--- a/apps/api/src/activity/index.ts+++ b/apps/api/src/activity/index.ts@@ -4,8 +4,9 @@ import { z } from "zod";import { subscribeToEvent } from "../events";import createActivity from "./controllers/create-activity";import createComment from "./controllers/create-comment";+import deleteComment from "./controllers/delete-comment";import getActivitiesFromTaskId from "./controllers/get-activities";-+import updateComment from "./controllers/update-comment";const activity = new Hono().get("/:taskId",@@ -54,6 +55,41 @@ const activity = new Hono()return c.json(activity);},+ )+ .put(+ "/comment",+ zValidator(+ "json",+ z.object({+ id: z.string(),+ content: z.string(),+ userEmail: z.string(),+ }),+ ),+ async (c) => {+ const { id, content, userEmail } = c.req.valid("json");++ const activity = await updateComment(userEmail, id, content);++ return c.json(activity);+ },+ )+ .delete(+ "/comment",+ zValidator(+ "json",+ z.object({+ id: z.string(),+ userEmail: z.string(),+ }),+ ),+ async (c) => {+ const { id, userEmail } = c.req.valid("json");++ await deleteComment(userEmail, id);++ return c.json({ message: "Comment deleted" });+ },);subscribeToEvent(diff --git a/apps/web/src/components/task/task-activities.tsx b/apps/web/src/components/task/task-activities.tsxindex ad8c4c6f7..756fef6ac 100644--- a/apps/web/src/components/task/task-activities.tsx+++ b/apps/web/src/components/task/task-activities.tsx@@ -1,18 +1,34 @@import { Avatar, AvatarFallback } from "@/components/ui/avatar";+import useDeleteComment from "@/hooks/mutations/comment/use-delete-comment";import useGetActivitiesByTaskId from "@/hooks/queries/activity/use-get-activities-by-task-id";import { cn } from "@/lib/cn";import { Route } from "@/routes/dashboard/workspace/$workspaceId/project/$projectId/task/$taskId";import { formatDistanceToNow } from "date-fns";import { History, MessageSquare, Pencil, PlusCircle } from "lucide-react";+import { useState } from "react";+import useAuth from "../providers/auth-provider/hooks/use-auth";+import TaskComment from "./task-comment";function TaskActivities() {const { taskId } = Route.useParams();+ const { user } = useAuth();const { data: activities } = useGetActivitiesByTaskId(taskId);+ const { mutateAsync: deleteComment } = useDeleteComment(taskId);+ const [editingCommentId, setEditingCommentId] = useState<string | null>(null);++ const handleDeleteComment = (activityId: string) => {+ if (user?.email) {+ deleteComment({+ id: activityId,+ userEmail: user.email,+ });+ }+ };return (- <div className="space-y-3">+ <div className="flex flex-col gap-4">{activities?.map((activity) => (- <div key={activity.id} className="flex items-start gap-3">+ <div key={activity.id} className="flex items-start gap-4"><div className="relative"><Avatar className="w-8 h-8"><AvatarFallback>{activity.userEmail.charAt(0)}</AvatarFallback>@@ -38,22 +54,69 @@ function TaskActivities() {)}</div></div>- <div className="flex-1">+ <div className="flex-1 flex flex-col gap-2"><div className="flex items-center gap-2">- <span className="text-sm font-medium text-zinc-900 dark:text-zinc-100">+ <span className="text-sm font-medium text-zinc-900 dark:text-zinc-400">{activity.userEmail}</span><span className="text-xs text-zinc-500 dark:text-zinc-400">{formatDistanceToNow(activity.createdAt)} ago</span></div>- <div className="mt-1.5">- <div className="text-sm text-zinc-600 dark:text-zinc-400">+ <div className="flex flex-col gap-1">+ <div className="text-sm text-zinc-600 dark:text-zinc-100">{activity.type === "create" && activity.content}{activity.type === "update" && activity.content}{activity.type === "status" && activity.content}- {activity.type === "comment" && activity.content}+ {activity.type === "comment" &&+ (editingCommentId === activity.id ? (+ <TaskComment+ initialComment={activity.content || ""}+ commentId={activity.id || null}+ onSubmit={() => setEditingCommentId(null)}+ />+ ) : (+ <span className="text-zinc-600 dark:text-zinc-100">+ {activity.content}+ </span>+ ))}</div>+ {activity.type === "comment" && editingCommentId === null && (+ <div className="flex flex-row gap-3">+ <button+ type="button"+ className="text-xs underline text-zinc-500 dark:text-zinc-400"+ onClick={() => setEditingCommentId(activity.id ?? null)}+ >+ Edit+ </button>+ <button+ className="text-xs underline text-zinc-500 dark:text-zinc-400"+ tabIndex={0}+ type="button"+ onClick={() => handleDeleteComment(activity.id)}+ onKeyDown={(e) => {+ if (e.key === "Enter" || e.key === " ") {+ handleDeleteComment(activity.id);+ }+ }}+ >+ Delete+ </button>+ </div>+ )}+ {activity.type === "comment" &&+ editingCommentId === activity.id && (+ <div className="flex flex-row gap-3">+ <button+ type="button"+ className="text-xs underline text-zinc-500 dark:text-zinc-400"+ onClick={() => setEditingCommentId(null)}+ >+ Cancel+ </button>+ </div>+ )}</div></div></div>diff --git a/apps/web/src/components/task/task-comment.tsx b/apps/web/src/components/task/task-comment.tsxindex 55226f1a7..0bde8325f 100644--- a/apps/web/src/components/task/task-comment.tsx+++ b/apps/web/src/components/task/task-comment.tsx@@ -1,14 +1,8 @@import useAuth from "@/components/providers/auth-provider/hooks/use-auth";-import { Avatar, AvatarFallback } from "@/components/ui/avatar";import { Button } from "@/components/ui/button";-import {- Form,- FormControl,- FormField,- FormItem,- FormLabel,-} from "@/components/ui/form";-import useCreateActivity from "@/hooks/mutations/activity/use-create-activity";+import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";+import useCreateComment from "@/hooks/mutations/comment/use-create-comment";+import useUpdateComment from "@/hooks/mutations/comment/use-update-comment";import { Route } from "@/routes/dashboard/workspace/$workspaceId/project/$projectId/task/$taskId";import { zodResolver } from "@hookform/resolvers/zod";import { useQueryClient } from "@tanstack/react-query";@@ -17,15 +11,23 @@ import { useEffect } from "react";import { useForm } from "react-hook-form";import { toast } from "sonner";import { z } from "zod";-const commentSchema = z.object({comment: z.string().min(1),});-function TaskComment() {+function TaskComment({+ initialComment = "",+ commentId = null,+ onSubmit,+}: {+ initialComment?: string;+ commentId?: string | null;+ onSubmit?: () => void;+}) {const { taskId } = Route.useParams();const { user } = useAuth();- const { mutateAsync: createComment } = useCreateActivity();+ const { mutateAsync: createComment } = useCreateComment();+ const { mutateAsync: updateComment } = useUpdateComment();const queryClient = useQueryClient();const form = useForm<z.infer<typeof commentSchema>>({@@ -36,23 +38,37 @@ function TaskComment() {shouldUnregister: true,});- async function onSubmit(data: z.infer<typeof commentSchema>) {+ async function handleSubmit(data: z.infer<typeof commentSchema>) {if (!user?.email) {return;}try {- await createComment({- taskId: taskId,- content: data.comment,- userEmail: user?.email,- });+ if (commentId) {+ await updateComment({+ id: commentId,+ userEmail: user.email,+ content: data.comment,+ });+ onSubmit?.();+ } else {+ await createComment({+ taskId: taskId,+ content: data.comment,+ userEmail: user?.email,+ });+ onSubmit?.();+ }await queryClient.invalidateQueries({queryKey: ["activities", taskId],});- toast.success("Comment added successfully");+ toast.success(+ commentId+ ? "Comment updated successfully"+ : "Comment added successfully",+ );form.reset();} catch (error) {toast.error(@@ -67,27 +83,30 @@ function TaskComment() {};}, [form]);+ useEffect(() => {+ form.setValue("comment", initialComment);+ }, [initialComment, form]);+return (<div className="flex items-start gap-3">- <Avatar className="w-8 h-8">- <AvatarFallback>{user?.name.charAt(0)}</AvatarFallback>- </Avatar><div className="flex-1"><Form {...form}>- <form onSubmit={form.handleSubmit(onSubmit)}>+ <form onSubmit={form.handleSubmit(handleSubmit)}><FormFieldcontrol={form.control}name="comment"render={({ field }) => (<FormItem>- <FormLabel>Comment</FormLabel>- <FormControl>- <textarea- placeholder="Add a comment..."- {...field}- className="w-full rounded-xl border border-zinc-200/50 dark:border-zinc-800/50 bg-white dark:bg-zinc-900 shadow-sm px-4 py-3 text-sm text-zinc-900 dark:text-zinc-100 placeholder:text-zinc-500 dark:placeholder:text-zinc-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 dark:focus:ring-indigo-400 min-h-[100px]"- />- </FormControl>+ <div className="flex flex-col gap-4">+ <FormControl>+ <textarea+ placeholder="Add a comment..."+ {...field}+ value={field.value}+ className="w-full rounded-xl border border-zinc-200/50 dark:border-zinc-800/50 bg-white dark:bg-zinc-900 shadow-sm px-4 py-3 text-sm text-zinc-900 dark:text-zinc-100 placeholder:text-zinc-500 dark:placeholder:text-zinc-400 focus:outline-none focus:ring-2 focus:ring-indigo-500 dark:focus:ring-indigo-400 min-h-[100px]"+ />+ </FormControl>+ </div></FormItem>)}/>@@ -97,7 +116,7 @@ function TaskComment() {className="bg-indigo-600/10 text-indigo-600 hover:bg-indigo-600/20 dark:bg-indigo-400/10 dark:text-indigo-400 dark:hover:bg-indigo-400/20"><MessageSquare className="w-4 h-4 mr-2" />- Comment+ {commentId ? "Update" : "Comment"}</Button></div></form>diff --git a/apps/web/src/fetchers/comment/create-comment.ts b/apps/web/src/fetchers/comment/create-comment.tsnew file mode 100644index 000000000..b57d2ff9a--- /dev/null+++ b/apps/web/src/fetchers/comment/create-comment.ts@@ -0,0 +1,31 @@+import { client } from "@kaneo/libs";+import type { InferRequestType } from "hono/client";++export type CreateCommentRequest = InferRequestType<+ (typeof client)["activity"]["comment"]["$post"]+>["json"];++async function createComment({+ taskId,+ content,+ userEmail,+}: CreateCommentRequest) {+ const response = await client.activity.comment.$post({+ json: {+ taskId,+ content,+ userEmail,+ },+ });++ if (!response.ok) {+ const error = await response.text();+ throw new Error(error);+ }++ const data = await response.json();++ return data;+}++export default createComment;diff --git a/apps/web/src/fetchers/comment/delete-comment.ts b/apps/web/src/fetchers/comment/delete-comment.tsnew file mode 100644index 000000000..3607c50b7--- /dev/null+++ b/apps/web/src/fetchers/comment/delete-comment.ts@@ -0,0 +1,23 @@+import { client } from "@kaneo/libs";+import type { InferRequestType } from "hono/client";++export type DeleteCommentRequest = InferRequestType<+ (typeof client)["activity"]["comment"]["$delete"]+>["json"];++async function deleteComment({ id, userEmail }: DeleteCommentRequest) {+ const response = await client.activity.comment.$delete({+ json: { id, userEmail },+ });++ if (!response.ok) {+ const error = await response.text();+ throw new Error(error);+ }++ const data = await response.json();++ return data;+}++export default deleteComment;diff --git a/apps/web/src/fetchers/comment/update-comment.ts b/apps/web/src/fetchers/comment/update-comment.tsnew file mode 100644index 000000000..b6414f789--- /dev/null+++ b/apps/web/src/fetchers/comment/update-comment.ts@@ -0,0 +1,27 @@+import { client } from "@kaneo/libs";+import type { InferRequestType } from "hono/client";++export type UpdateCommentRequest = InferRequestType<+ (typeof client)["activity"]["comment"]["$put"]+>["json"];++async function updateComment({ id, content, userEmail }: UpdateCommentRequest) {+ const response = await client.activity.comment.$put({+ json: {+ id,+ content,+ userEmail,+ },+ });++ if (!response.ok) {+ const error = await response.text();+ throw new Error(error);+ }++ const data = await response.json();++ return data;+}++export default updateComment;diff --git a/apps/web/src/hooks/mutations/activity/use-create-activity.ts b/apps/web/src/hooks/mutations/activity/use-create-activity.tsdeleted file mode 100644index cac69f608..000000000--- a/apps/web/src/hooks/mutations/activity/use-create-activity.ts+++ /dev/null@@ -1,10 +0,0 @@-import createActivity from "@/fetchers/activity/create-activity";-import { useMutation } from "@tanstack/react-query";--function useCreateActivity() {- return useMutation({- mutationFn: createActivity,- });-}--export default useCreateActivity;diff --git a/apps/web/src/hooks/mutations/comment/use-create-comment.ts b/apps/web/src/hooks/mutations/comment/use-create-comment.tsnew file mode 100644index 000000000..7c0781b3b--- /dev/null+++ b/apps/web/src/hooks/mutations/comment/use-create-comment.ts@@ -0,0 +1,10 @@+import createComment from "@/fetchers/comment/create-comment";+import { useMutation } from "@tanstack/react-query";++function useCreateComment() {+ return useMutation({+ mutationFn: createComment,+ });+}++export default useCreateComment;diff --git a/apps/web/src/hooks/mutations/comment/use-delete-comment.ts b/apps/web/src/hooks/mutations/comment/use-delete-comment.tsnew file mode 100644index 000000000..3b2249d75--- /dev/null+++ b/apps/web/src/hooks/mutations/comment/use-delete-comment.ts@@ -0,0 +1,15 @@+import deleteComment from "@/fetchers/comment/delete-comment";+import { useMutation, useQueryClient } from "@tanstack/react-query";++function useDeleteComment(taskId: string) {+ const queryClient = useQueryClient();++ return useMutation({+ mutationFn: deleteComment,+ onSuccess: () => {+ queryClient.invalidateQueries({ queryKey: ["activities", taskId] });+ },+ });+}++export default useDeleteComment;diff --git a/apps/web/src/hooks/mutations/comment/use-update-comment.ts b/apps/web/src/hooks/mutations/comment/use-update-comment.tsnew file mode 100644index 000000000..b1ae3dc0b--- /dev/null+++ b/apps/web/src/hooks/mutations/comment/use-update-comment.ts@@ -0,0 +1,10 @@+import updateComment from "@/fetchers/comment/update-comment";+import { useMutation } from "@tanstack/react-query";++function useUpdateComment() {+ return useMutation({+ mutationFn: updateComment,+ });+}++export default useUpdateComment;diff --git a/apps/web/src/routes/dashboard/workspace/$workspaceId/project/$projectId/task/$taskId.tsx b/apps/web/src/routes/dashboard/workspace/$workspaceId/project/$projectId/task/$taskId.tsxindex 4a77ec24e..a0ce108a0 100644--- a/apps/web/src/routes/dashboard/workspace/$workspaceId/project/$projectId/task/$taskId.tsx+++ b/apps/web/src/routes/dashboard/workspace/$workspaceId/project/$projectId/task/$taskId.tsx@@ -104,7 +104,7 @@ function TaskEditPage() {</div></div>- <div className="space-y-6">+ <div className="space-y-6 pb-8"><TaskComment /><TaskActivities /></div>