fix(comment): align mention audience with entity visibility

This commit is contained in:
2026-03-30 18:50:36 +02:00
parent 34067f1576
commit dd71e8f80b
7 changed files with 616 additions and 97 deletions
@@ -1,12 +1,13 @@
"use client";
import type { CommentEntityType } from "@capakraken/shared";
import { createPortal } from "react-dom";
import { useCallback, useEffect, useRef, useState } from "react";
import { trpc } from "~/lib/trpc/client.js";
import { useAnchoredOverlay } from "~/hooks/useAnchoredOverlay.js";
interface CommentInputProps {
entityType: "estimate";
entityType: CommentEntityType;
entityId: string;
parentId?: string;
onSubmit: (body: string) => void;
@@ -23,8 +24,8 @@ interface MentionCandidate {
}
export function CommentInput({
entityType: _entityType,
entityId: _entityId,
entityType,
entityId,
parentId: _parentId,
onSubmit,
onCancel,
@@ -38,25 +39,17 @@ export function CommentInput({
const [cursorPosition, setCursorPosition] = useState(0);
const textareaRef = useRef<HTMLTextAreaElement>(null);
// Fetch users for mention autocomplete (only when needed)
const usersQuery = trpc.user.listAssignable.useQuery(undefined, {
const usersQuery = trpc.comment.listMentionCandidates.useQuery({
entityType,
entityId,
...(mentionQuery && mentionQuery.length > 0 ? { query: mentionQuery } : {}),
}, {
enabled: mentionQuery !== null,
staleTime: 60_000,
});
const users = usersQuery.data ?? [];
// Filter users based on mention query
const filteredUsers: MentionCandidate[] =
mentionQuery !== null
? users.filter((u) => {
const q = mentionQuery.toLowerCase();
return (
(u.name?.toLowerCase().includes(q) ?? false) ||
u.email.toLowerCase().includes(q)
);
}).slice(0, 8)
: [];
mentionQuery !== null ? (usersQuery.data ?? []).slice(0, 8) : [];
const mentionOpen = mentionQuery !== null && filteredUsers.length > 0;
const { panelRef, position } = useAnchoredOverlay<HTMLTextAreaElement>({
open: mentionOpen,