515 lines
20 KiB
TypeScript
515 lines
20 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useRef, useCallback, useEffect } from "react";
|
|
import { MoreHorizontal, Trash2, Loader2 } from "lucide-react";
|
|
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
|
import { toast } from "sonner";
|
|
import { useTRPC } from "@/trpc/client";
|
|
import { useActivePet } from "@/context/ActivePetContext";
|
|
import { Dialog, DialogContent } from "@/components/ui/dialog";
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
|
import { Button } from "@/components/ui/button";
|
|
import { MentionTextarea } from "@/components/ui/mention-textarea";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "@/components/ui/alert-dialog";
|
|
import {
|
|
Carousel,
|
|
CarouselContent,
|
|
CarouselItem,
|
|
CarouselNext,
|
|
CarouselPrevious,
|
|
type CarouselApi,
|
|
} from "@/components/ui/carousel";
|
|
import Link from "next/link";
|
|
import { getAvatarUrl } from "@/lib/avatar-url";
|
|
import { getMediaUrl } from "@/lib/media-url";
|
|
import { parseCaption } from "@/lib/parse-caption";
|
|
import { PawButton } from "@/components/feed/PawButton";
|
|
import { PawBackButton } from "@/components/feed/PawBackButton";
|
|
import { PawRing } from "@/components/stories/PawRing";
|
|
import type { PostCardPost } from "@/components/feed/PostCard";
|
|
import { useTranslations } from "next-intl";
|
|
|
|
interface PostDetailDialogProps {
|
|
post: PostCardPost | null;
|
|
open: boolean;
|
|
onClose: () => void;
|
|
onDeleted?: (postId: string) => void;
|
|
}
|
|
|
|
export function PostDetailDialog({ post, open, onClose, onDeleted }: PostDetailDialogProps) {
|
|
const t = useTranslations("PostCard");
|
|
const tc = useTranslations("Comments");
|
|
const trpc = useTRPC();
|
|
const { activePetId } = useActivePet();
|
|
const queryClient = useQueryClient();
|
|
|
|
const [carouselApi, setCarouselApi] = useState<CarouselApi>();
|
|
const [currentSlide, setCurrentSlide] = useState(1);
|
|
const [isEditingCaption, setIsEditingCaption] = useState(false);
|
|
const [editCaptionValue, setEditCaptionValue] = useState("");
|
|
const [currentCaption, setCurrentCaption] = useState<string | null>(null);
|
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
|
const [body, setBody] = useState("");
|
|
const [pendingBody, setPendingBody] = useState<string | null>(null);
|
|
const commentsEndRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
if (!post) return;
|
|
setCurrentCaption(post.caption);
|
|
setEditCaptionValue(post.caption ?? "");
|
|
setIsEditingCaption(false);
|
|
setBody("");
|
|
setCurrentSlide(1);
|
|
}, [post?.id]);
|
|
|
|
useEffect(() => {
|
|
if (!carouselApi) return;
|
|
setCurrentSlide(carouselApi.selectedScrollSnap() + 1);
|
|
carouselApi.on("select", () => setCurrentSlide(carouselApi.selectedScrollSnap() + 1));
|
|
}, [carouselApi]);
|
|
|
|
const { data: commentsData, isLoading: commentsLoading } = useQuery({
|
|
...trpc.comments.listByPost.queryOptions({ postId: post?.id ?? "", limit: 50 }),
|
|
enabled: !!post?.id && open,
|
|
});
|
|
|
|
const updateCaptionMutation = useMutation(
|
|
trpc.posts.updateCaption.mutationOptions({
|
|
onSuccess: (updated) => {
|
|
setCurrentCaption(updated.caption);
|
|
setIsEditingCaption(false);
|
|
toast.success(t("captionUpdated"));
|
|
},
|
|
onError: () => toast.error(t("captionUpdateError")),
|
|
})
|
|
);
|
|
|
|
const deleteMutation = useMutation(
|
|
trpc.posts.delete.mutationOptions({
|
|
onSuccess: () => {
|
|
setShowDeleteDialog(false);
|
|
toast.success(t("postDeleted"));
|
|
onClose();
|
|
if (post) onDeleted?.(post.id);
|
|
},
|
|
onError: () => toast.error(t("deleteError")),
|
|
})
|
|
);
|
|
|
|
const createCommentMutation = useMutation(
|
|
trpc.comments.create.mutationOptions({
|
|
onSuccess: () => {
|
|
setPendingBody(null);
|
|
if (post?.id) {
|
|
void queryClient.invalidateQueries({
|
|
queryKey: trpc.comments.listByPost.queryOptions({ postId: post.id, limit: 50 }).queryKey,
|
|
});
|
|
}
|
|
setTimeout(() => commentsEndRef.current?.scrollIntoView({ behavior: "smooth" }), 150);
|
|
},
|
|
onError: () => {
|
|
setBody((prev) => prev || (pendingBody ?? ""));
|
|
setPendingBody(null);
|
|
toast.error(tc("postError"));
|
|
},
|
|
})
|
|
);
|
|
|
|
const deleteCommentMutation = useMutation(
|
|
trpc.comments.delete.mutationOptions({
|
|
onSuccess: () => {
|
|
if (post?.id) {
|
|
void queryClient.invalidateQueries({
|
|
queryKey: trpc.comments.listByPost.queryOptions({ postId: post.id, limit: 50 }).queryKey,
|
|
});
|
|
}
|
|
},
|
|
onError: () => toast.error(tc("deleteError")),
|
|
})
|
|
);
|
|
|
|
const handleSubmitComment = useCallback(() => {
|
|
if (!body.trim() || !post || !activePetId) return;
|
|
const trimmed = body.trim();
|
|
setPendingBody(trimmed);
|
|
setBody("");
|
|
createCommentMutation.mutate({ postId: post.id, petId: activePetId, body: trimmed });
|
|
setTimeout(() => commentsEndRef.current?.scrollIntoView({ behavior: "smooth" }), 50);
|
|
}, [body, post, activePetId, createCommentMutation]);
|
|
|
|
const handleCommentKeyDown = useCallback(
|
|
(e: React.KeyboardEvent<HTMLTextAreaElement>) => {
|
|
if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
|
|
e.preventDefault();
|
|
handleSubmitComment();
|
|
}
|
|
},
|
|
[handleSubmitComment]
|
|
);
|
|
|
|
function formatRelativeTime(date: Date | string): string {
|
|
const diffMs = Date.now() - new Date(date).getTime();
|
|
const diffMins = Math.floor(diffMs / 60000);
|
|
const diffHours = Math.floor(diffMins / 60);
|
|
const diffDays = Math.floor(diffHours / 24);
|
|
if (diffMins < 1) return tc("justNow");
|
|
if (diffMins < 60) return `${diffMins}m`;
|
|
if (diffHours < 24) return `${diffHours}h`;
|
|
if (diffDays < 7) return `${diffDays}d`;
|
|
return new Date(date).toLocaleDateString();
|
|
}
|
|
|
|
if (!post) return null;
|
|
|
|
const { pet, images } = post;
|
|
const isCarousel = images.length > 1;
|
|
const isOwnPost = activePetId === pet.id;
|
|
const avatarUrl = getAvatarUrl(pet.avatarKey);
|
|
const comments = commentsData?.comments ?? [];
|
|
|
|
// Extracted so it renders identically in the mobile bar and the desktop right-panel
|
|
const headerInner = (
|
|
<>
|
|
<Link
|
|
href={`/pets/${pet.id}`}
|
|
className="flex items-center gap-3 flex-1 min-w-0 hover:opacity-80 transition-opacity"
|
|
onClick={onClose}
|
|
>
|
|
<PawRing petId={pet.id} size={32}>
|
|
<Avatar className="w-8 h-8">
|
|
{avatarUrl && <AvatarImage src={avatarUrl} alt={pet.name} />}
|
|
<AvatarFallback className="text-xs bg-muted">
|
|
{pet.name[0]?.toUpperCase() ?? "?"}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
</PawRing>
|
|
<div className="flex-1 min-w-0">
|
|
<p className="text-sm font-semibold text-foreground leading-tight truncate">{pet.name}</p>
|
|
<p className="text-xs text-muted-foreground">{formatRelativeTime(post.createdAt)}</p>
|
|
</div>
|
|
</Link>
|
|
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger
|
|
className="flex items-center justify-center w-10 h-10 rounded-full hover:bg-muted transition-colors cursor-pointer"
|
|
aria-label="Post options"
|
|
>
|
|
<MoreHorizontal className="h-5 w-5 text-muted-foreground" aria-hidden="true" />
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
{isOwnPost ? (
|
|
<>
|
|
<DropdownMenuItem
|
|
onClick={() => { setEditCaptionValue(currentCaption ?? ""); setIsEditingCaption(true); }}
|
|
className="cursor-pointer"
|
|
>
|
|
{t("editCaption")}
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem
|
|
onClick={() => setShowDeleteDialog(true)}
|
|
className="cursor-pointer text-destructive focus:text-destructive"
|
|
>
|
|
{t("deletePost")}
|
|
</DropdownMenuItem>
|
|
</>
|
|
) : (
|
|
<>
|
|
<DropdownMenuItem className="cursor-pointer">{t("reportPost")}</DropdownMenuItem>
|
|
<DropdownMenuSeparator />
|
|
<DropdownMenuItem className="cursor-pointer text-destructive focus:text-destructive">
|
|
{t("blockPet", { name: pet.name })}
|
|
</DropdownMenuItem>
|
|
</>
|
|
)}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
</>
|
|
);
|
|
|
|
const imageContent = images.length > 0 && (
|
|
isCarousel ? (
|
|
<div className="relative w-full h-full">
|
|
<Carousel setApi={setCarouselApi} className="h-full">
|
|
<CarouselContent className="h-full">
|
|
{images.map((img) => (
|
|
<CarouselItem key={img.id} className="basis-full flex items-center justify-center h-full">
|
|
{/* eslint-disable-next-line @next/next/no-img-element */}
|
|
<img
|
|
src={getMediaUrl(img.storageKey) ?? ""}
|
|
alt={currentCaption ?? `${pet.name}'s post`}
|
|
className="max-h-full max-w-full object-contain"
|
|
/>
|
|
</CarouselItem>
|
|
))}
|
|
</CarouselContent>
|
|
<CarouselPrevious className="left-2" />
|
|
<CarouselNext className="right-2" />
|
|
</Carousel>
|
|
<span className="absolute top-2 right-2 z-10 text-xs text-white bg-black/50 rounded px-1.5 py-0.5 select-none">
|
|
{currentSlide} / {images.length}
|
|
</span>
|
|
<div className="absolute bottom-2 left-0 right-0 z-10 flex justify-center gap-2">
|
|
{images.map((_, i) => (
|
|
<button
|
|
key={i}
|
|
className={`w-1.5 h-1.5 rounded-full transition-colors ${i === currentSlide - 1 ? "bg-white" : "bg-white/40"}`}
|
|
onClick={() => carouselApi?.scrollTo(i)}
|
|
aria-label={`Go to image ${i + 1}`}
|
|
/>
|
|
))}
|
|
</div>
|
|
</div>
|
|
) : (
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
<img
|
|
src={getMediaUrl(images[0].storageKey) ?? ""}
|
|
alt={currentCaption ?? `${pet.name}'s post`}
|
|
className="max-h-full max-w-full object-contain"
|
|
/>
|
|
)
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<Dialog open={open} onOpenChange={(o) => { if (!o) onClose(); }}>
|
|
{/*
|
|
Layout:
|
|
Mobile → flex-col: [header][image 45dvh][right-panel scrollable]
|
|
Desktop → flex-row: [image flex-1 left][right-panel 360px right]
|
|
*/}
|
|
<DialogContent
|
|
showCloseButton={false}
|
|
className="p-0 max-w-[960px] sm:max-w-[960px] w-full h-[90dvh] flex flex-col md:flex-row overflow-hidden rounded-xl gap-0"
|
|
>
|
|
{/* Mobile-only header — sits above the image on small screens */}
|
|
<div className="md:hidden flex items-center gap-3 px-4 py-3 border-b shrink-0">
|
|
{headerInner}
|
|
</div>
|
|
|
|
{/* Image column — fixed 45dvh strip on mobile, flex-1 full-height on desktop */}
|
|
<div className="shrink-0 h-[45dvh] md:h-full md:flex-1 md:min-w-0 bg-black flex items-center justify-center">
|
|
{imageContent}
|
|
</div>
|
|
|
|
{/* Right panel: desktop header + scrollable body + sticky input */}
|
|
<div className="flex flex-col md:w-[360px] md:shrink-0 min-h-0 flex-1 md:flex-none md:border-l">
|
|
{/* Desktop-only header */}
|
|
<div className="hidden md:flex items-center gap-3 px-4 py-3 border-b shrink-0">
|
|
{headerInner}
|
|
</div>
|
|
|
|
{/* Scrollable body */}
|
|
<div className="flex-1 overflow-y-auto min-h-0">
|
|
<div className="px-4 pt-3 pb-2 space-y-2">
|
|
{isEditingCaption ? (
|
|
<div className="space-y-2">
|
|
<MentionTextarea
|
|
value={editCaptionValue}
|
|
onChange={setEditCaptionValue}
|
|
maxLength={500}
|
|
rows={3}
|
|
className="text-sm resize-none"
|
|
/>
|
|
<div className="flex gap-2">
|
|
<Button
|
|
size="sm"
|
|
onClick={() =>
|
|
updateCaptionMutation.mutate({ postId: post.id, caption: editCaptionValue.trim() || null })
|
|
}
|
|
disabled={updateCaptionMutation.isPending}
|
|
className="bg-orange-500 hover:bg-orange-600 text-white text-xs h-8"
|
|
>
|
|
{t("saveCaption")}
|
|
</Button>
|
|
<Button
|
|
size="sm"
|
|
variant="outline"
|
|
onClick={() => setIsEditingCaption(false)}
|
|
className="text-xs h-8"
|
|
>
|
|
{t("cancel")}
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
currentCaption && (
|
|
<p className="text-sm text-foreground">
|
|
{parseCaption(currentCaption).map((seg, i) =>
|
|
seg.type === "hashtag" ? (
|
|
<Link key={i} href={`/hashtag/${seg.tag}`} className="text-orange-500 hover:underline" onClick={onClose}>
|
|
#{seg.tag}
|
|
</Link>
|
|
) : seg.type === "mention" ? (
|
|
<Link key={i} href={`/search?q=${seg.name}`} className="text-blue-500 hover:underline" onClick={onClose}>
|
|
@{seg.name}
|
|
</Link>
|
|
) : (
|
|
<span key={i}>{seg.text}</span>
|
|
)
|
|
)}
|
|
</p>
|
|
)
|
|
)}
|
|
|
|
{activePetId && (
|
|
<div className="flex items-center gap-4 pt-1 pb-2 border-b">
|
|
<PawButton
|
|
postId={post.id}
|
|
petId={activePetId}
|
|
initialCount={post._count?.reactions ?? 0}
|
|
initialReacted={false}
|
|
/>
|
|
<PawBackButton postId={post.id} petId={activePetId} />
|
|
{comments.length > 0 && (
|
|
<span className="text-xs text-muted-foreground ml-auto">
|
|
{comments.length === 1 ? tc("countSingular") : tc("countPlural", { count: comments.length })}
|
|
</span>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
|
|
<div className="px-4 pb-3 space-y-3">
|
|
{commentsLoading && (
|
|
<div className="flex justify-center py-4">
|
|
<Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
|
|
</div>
|
|
)}
|
|
{!commentsLoading && comments.length === 0 && (
|
|
<p className="text-sm text-muted-foreground text-center py-3">{tc("empty")}</p>
|
|
)}
|
|
{comments.map((comment) => {
|
|
const commentAvatarUrl = getAvatarUrl(comment.pet.avatarKey);
|
|
const isOwnComment = comment.pet.id === activePetId;
|
|
return (
|
|
<div key={comment.id} className="flex gap-2 group">
|
|
<Avatar className="h-7 w-7 shrink-0 mt-0.5">
|
|
{commentAvatarUrl && <AvatarImage src={commentAvatarUrl} alt={comment.pet.name} />}
|
|
<AvatarFallback className="text-xs bg-muted">
|
|
{comment.pet.name[0]?.toUpperCase()}
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<div className="flex-1 min-w-0">
|
|
<div className="flex items-baseline gap-1.5">
|
|
<Link
|
|
href={`/pets/${comment.pet.id}`}
|
|
className="text-xs font-semibold hover:underline"
|
|
onClick={onClose}
|
|
>
|
|
{comment.pet.name}
|
|
</Link>
|
|
<span className="text-xs text-muted-foreground">
|
|
{formatRelativeTime(comment.createdAt)}
|
|
</span>
|
|
</div>
|
|
<p className="text-sm mt-0.5 break-words">
|
|
{parseCaption(comment.body).map((seg, i) =>
|
|
seg.type === "hashtag" ? (
|
|
<Link key={i} href={`/hashtag/${seg.tag}`} className="text-orange-500 hover:underline">#{seg.tag}</Link>
|
|
) : seg.type === "mention" ? (
|
|
<Link key={i} href={`/search?q=${seg.name}`} className="text-blue-500 hover:underline">@{seg.name}</Link>
|
|
) : (
|
|
<span key={i}>{seg.text}</span>
|
|
)
|
|
)}
|
|
</p>
|
|
</div>
|
|
{isOwnComment && activePetId && (
|
|
<button
|
|
type="button"
|
|
aria-label="Delete comment"
|
|
onClick={() => deleteCommentMutation.mutate({ commentId: comment.id, petId: activePetId })}
|
|
className="opacity-0 group-hover:opacity-100 transition-opacity text-muted-foreground hover:text-destructive shrink-0 self-start mt-0.5"
|
|
>
|
|
<Trash2 className="h-3.5 w-3.5" />
|
|
</button>
|
|
)}
|
|
</div>
|
|
);
|
|
})}
|
|
{pendingBody && (
|
|
<div className="flex gap-2 opacity-60">
|
|
<Avatar className="h-7 w-7 shrink-0 mt-0.5">
|
|
<AvatarFallback className="text-xs bg-orange-100 text-orange-600">
|
|
✓
|
|
</AvatarFallback>
|
|
</Avatar>
|
|
<div className="flex-1 min-w-0">
|
|
<span className="text-xs text-muted-foreground">{tc("sending")}</span>
|
|
<p className="text-sm mt-0.5 break-words">{pendingBody}</p>
|
|
</div>
|
|
<Loader2 className="h-3.5 w-3.5 animate-spin text-muted-foreground shrink-0 self-start mt-1" />
|
|
</div>
|
|
)}
|
|
<div ref={commentsEndRef} />
|
|
</div>
|
|
</div>
|
|
|
|
{/* Sticky comment input */}
|
|
{activePetId && (
|
|
<div className="flex gap-2 px-4 py-3 border-t bg-background shrink-0">
|
|
<MentionTextarea
|
|
rows={1}
|
|
maxLength={500}
|
|
placeholder={tc("placeholder")}
|
|
value={body}
|
|
onChange={setBody}
|
|
onKeyDown={handleCommentKeyDown}
|
|
className="flex-1 resize-none min-h-[40px] max-h-[120px] text-sm"
|
|
/>
|
|
<Button
|
|
size="sm"
|
|
disabled={!body.trim() || createCommentMutation.isPending}
|
|
onClick={handleSubmitComment}
|
|
className="bg-orange-500 hover:bg-orange-600 text-white self-end"
|
|
>
|
|
{createCommentMutation.isPending ? (
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
) : (
|
|
tc("post")
|
|
)}
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</div>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
<AlertDialog open={showDeleteDialog} onOpenChange={setShowDeleteDialog}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>{t("deleteTitle")}</AlertDialogTitle>
|
|
<AlertDialogDescription>{t("deleteDesc")}</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>{t("keepPost")}</AlertDialogCancel>
|
|
<AlertDialogAction
|
|
onClick={() => deleteMutation.mutate({ postId: post.id })}
|
|
disabled={deleteMutation.isPending}
|
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
|
>
|
|
{t("yesDelete")}
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</>
|
|
);
|
|
}
|