dow-prod-tracker/src/hooks/use-comments.ts
Leivur R. Djurhuus 2128e79c1a Add revision tracking and threaded comments for pipeline stages
Services, API routes, hooks, and UI components for:
- Revision rounds with submit/review/approve/request-changes flow
- Threaded comments with replies on each stage
- Stage detail sheet accessible from deliverable detail page

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-28 21:40:12 -06:00

51 lines
1.4 KiB
TypeScript

"use client";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import type { CreateCommentInput } from "@/lib/validators/comment";
async function fetchJson<T>(url: string, init?: RequestInit): Promise<T> {
const res = await fetch(url, init);
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error || `Request failed: ${res.status}`);
}
return res.json();
}
export function useComments(stageId: string) {
return useQuery({
queryKey: ["comments", stageId],
queryFn: () => fetchJson(`/api/stages/${stageId}/comments`),
enabled: !!stageId,
});
}
export function useCreateComment(stageId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (data: CreateCommentInput) =>
fetchJson(`/api/stages/${stageId}/comments`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["comments", stageId] });
},
});
}
export function useDeleteComment(stageId: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: (commentId: string) =>
fetchJson(`/api/stages/${stageId}/comments/${commentId}`, {
method: "DELETE",
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["comments", stageId] });
},
});
}