fix(web): restore comment typing and portal combobox menus

This commit is contained in:
2026-03-30 13:32:51 +02:00
parent 5b60cf5553
commit 9268a38df4
3 changed files with 55 additions and 39 deletions
@@ -4,7 +4,7 @@ import { useCallback, useEffect, useRef, useState } from "react";
import { trpc } from "~/lib/trpc/client.js"; import { trpc } from "~/lib/trpc/client.js";
interface CommentInputProps { interface CommentInputProps {
entityType: string; entityType: "estimate";
entityId: string; entityId: string;
parentId?: string; parentId?: string;
onSubmit: (body: string) => void; onSubmit: (body: string) => void;
@@ -31,8 +31,10 @@ interface CommentItem {
replies: CommentReply[]; replies: CommentReply[];
} }
type CommentEntityType = "estimate";
interface CommentThreadProps { interface CommentThreadProps {
entityType: string; entityType: CommentEntityType;
entityId: string; entityId: string;
} }
@@ -116,7 +118,7 @@ function SingleComment({
isReply = false, isReply = false,
}: { }: {
comment: CommentItem | CommentReply; comment: CommentItem | CommentReply;
entityType: string; entityType: CommentEntityType;
entityId: string; entityId: string;
isReply?: boolean; isReply?: boolean;
}) { }) {
+50 -36
View File
@@ -1,7 +1,9 @@
"use client"; "use client";
import { useState, useRef, useEffect, useMemo, type ReactNode } from "react"; import { createPortal } from "react-dom";
import { useState, useRef, useMemo, useCallback, type ReactNode } from "react";
import { useDebounce } from "~/hooks/useDebounce.js"; import { useDebounce } from "~/hooks/useDebounce.js";
import { useAnchoredOverlay } from "~/hooks/useAnchoredOverlay.js";
interface EntityComboboxProps<T extends { id: string }> { interface EntityComboboxProps<T extends { id: string }> {
value: string | null; value: string | null;
@@ -35,11 +37,22 @@ export function EntityCombobox<T extends { id: string }>({
const debouncedSearch = useDebounce(search, 300); const debouncedSearch = useDebounce(search, 300);
const containerRef = useRef<HTMLDivElement>(null); const containerRef = useRef<HTMLDivElement>(null);
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
const closeDropdown = useCallback(() => {
setOpen(false);
setSearch("");
}, []);
const { data: searchItems } = useSearchQuery(debouncedSearch, open); const { data: searchItems } = useSearchQuery(debouncedSearch, open);
const items = searchItems ?? []; const items = searchItems ?? [];
const { data: selectedItems } = useSelectedQuery(value, !!value && !open); const { data: selectedItems } = useSelectedQuery(value, !!value && !open);
const { panelRef, position } = useAnchoredOverlay<HTMLDivElement>({
open,
onClose: closeDropdown,
align: "start",
matchTriggerWidth: true,
triggerRef: containerRef,
});
const selectedLabel = useMemo(() => { const selectedLabel = useMemo(() => {
if (!value) return ""; if (!value) return "";
@@ -50,18 +63,6 @@ export function EntityCombobox<T extends { id: string }>({
return value; return value;
}, [value, items, selectedItems, getLabel]); }, [value, items, selectedItems, getLabel]);
useEffect(() => {
if (!open) return;
function handleClick(e: MouseEvent) {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
setOpen(false);
setSearch("");
}
}
document.addEventListener("mousedown", handleClick);
return () => document.removeEventListener("mousedown", handleClick);
}, [open]);
function handleFocus() { function handleFocus() {
if (disabled) return; if (disabled) return;
setOpen(true); setOpen(true);
@@ -107,29 +108,42 @@ export function EntityCombobox<T extends { id: string }>({
</div> </div>
{open && ( {open && (
<div className="absolute left-0 right-0 top-full mt-1 z-[60] bg-white dark:bg-gray-800 border border-gray-200 dark:border-gray-600 rounded-xl shadow-xl overflow-hidden"> typeof document !== "undefined"
<ul className="max-h-52 overflow-y-auto py-1"> ? createPortal(
{items.length === 0 ? ( <div
<li className="px-3 py-2 text-sm text-gray-400 dark:text-gray-500">No results</li> ref={panelRef}
) : ( className="fixed z-[9998] overflow-hidden rounded-xl border border-gray-200 bg-white shadow-xl dark:border-gray-600 dark:bg-gray-800"
items.map((item) => ( style={{
<li key={item.id}> top: position.top,
<button left: position.left,
type="button" width: position.minWidth,
onMouseDown={() => select(item.id)} }}
className={`w-full text-left px-3 py-1.5 text-sm hover:bg-brand-50 dark:hover:bg-brand-950/40 ${ >
item.id === value <ul className="max-h-52 overflow-y-auto py-1">
? "bg-brand-50 dark:bg-brand-950/40 text-brand-700 dark:text-brand-300 font-medium" {items.length === 0 ? (
: "text-gray-700 dark:text-gray-200" <li className="px-3 py-2 text-sm text-gray-400 dark:text-gray-500">No results</li>
}`} ) : (
> items.map((item) => (
{renderItem ? renderItem(item, item.id === value) : getLabel(item)} <li key={item.id}>
</button> <button
</li> type="button"
)) onMouseDown={() => select(item.id)}
)} className={`w-full text-left px-3 py-1.5 text-sm hover:bg-brand-50 dark:hover:bg-brand-950/40 ${
</ul> item.id === value
</div> ? "bg-brand-50 dark:bg-brand-950/40 text-brand-700 dark:text-brand-300 font-medium"
: "text-gray-700 dark:text-gray-200"
}`}
>
{renderItem ? renderItem(item, item.id === value) : getLabel(item)}
</button>
</li>
))
)}
</ul>
</div>,
document.body,
)
: null
)} )}
</div> </div>
); );