fix(webui): keep permission hints outside locked-thread drafts

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-26 15:23:50 +08:00
1 parent 2b7d686117
commit 0bd8456076
4 files changed
+86 -22

No files matched your search

@@ -1,12 +1,12 @@
import { FullAccessIcon, RestrictedAccessIcon, WorkspaceIcon } from "@/components/icons/product-icons";
import type { HTMLAttributes, ReactElement } from "react";
import type { HTMLAttributes, ReactElement, RefObject } from "react";
import { useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
import { ArrowLeft, ArrowRight, Check, ChevronDown, ChevronRight, FolderSearch, History, Search, Star } from "lucide-react";
import { useTranslation } from "react-i18next";
import { ToggleButton } from "@/components/ui/toggle-button";
import { Button } from "@/components/ui/button";
import { floatingItemClassName, floatingItemFocusClassName } from "@/components/ui/floating-surface";
import { HoverHint } from "@/components/ui/tooltip";
import { HoverHint, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { Input, SearchInput } from "@/components/ui/input";
import { Dialog, DialogContent, DialogTrigger, DialogTitle, DialogDescription } from "@/components/ui/dialog";
@@ -684,14 +684,21 @@ export function WorkspaceAccessMenu({
scope,
disabled,
onChange,
hintBoundaryRef,
}: {
scope: WorkspaceScopePayload;
disabled?: boolean;
onChange?: (scope: WorkspaceScopePayload) => void;
hintBoundaryRef: RefObject<HTMLElement>;
}) {
const { t } = useTranslation();
const coarsePointer = useMediaQuery("(pointer: coarse)");
const [open, setOpen] = useState(false);
const [hintOpen, setHintOpen] = useState(false);
const [hint, setHint] = useState<HTMLDivElement | null>(null);
const [hintPosition, setHintPosition] = useState<{ side: "top" | "bottom"; sideOffset: number }>({ side: "bottom", sideOffset: 8 });
const triggerRef = useRef<HTMLButtonElement>(null);
const hintEnabled = !open && !coarsePointer;
const restoreTriggerFocus = useRef(true);
const descriptionId = useId();
const isFull = scope.access_mode === "full";
@@ -701,25 +708,53 @@ export function WorkspaceAccessMenu({
const AccessIcon = isFull ? FullAccessIcon : RestrictedAccessIcon;
useEffect(() => { if (disabled || !onChange) setOpen(false); }, [disabled, onChange]);
useEffect(() => { if (!hintEnabled) setHintOpen(false); }, [hintEnabled]);
useLayoutEffect(() => {
const trigger = triggerRef.current;
const boundary = hintBoundaryRef.current;
if (!hint || !trigger || !boundary) return;
const placeHint = () => {
const triggerBounds = trigger.getBoundingClientRect();
const belowFits = triggerBounds.bottom + 8 + hint.offsetHeight <= window.innerHeight;
// A locked thread has no project row below the toolbar. Flip above the
// whole composer, not above the permission button into the draft.
const next = belowFits
? { side: "bottom" as const, sideOffset: 8 }
: { side: "top" as const, sideOffset: triggerBounds.top - boundary.getBoundingClientRect().top + 8 };
setHintPosition(previous => previous.side === next.side && previous.sideOffset === next.sideOffset ? previous : next);
};
placeHint();
const observer = new ResizeObserver(placeHint);
observer.observe(hint);
observer.observe(boundary);
window.addEventListener("resize", placeHint);
return () => { observer.disconnect(); window.removeEventListener("resize", placeHint); };
}, [hint, hintBoundaryRef]);
return (
<DropdownMenu open={open} onOpenChange={setOpen}>
<HoverHint enabled={!open && !coarsePointer} content={description} side="bottom" sideOffset={8}>
<DropdownMenuTrigger asChild disabled={disabled || !onChange}>
<button type="button" aria-label={accessAriaLabel} aria-describedby={descriptionId}
onPointerDown={() => { restoreTriggerFocus.current = false; }}
onKeyDown={() => { restoreTriggerFocus.current = true; }}
className={cn(
"thread-composer-access touch-target inline-flex h-8 min-w-0 max-w-full shrink items-center gap-1.5 rounded-md px-2 text-[12px] font-medium outline-none transition-colors motion-reduce:transition-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-55",
isFull ? "text-orange-600 hover:text-orange-700 dark:text-orange-300 dark:hover:text-orange-200" : "text-muted-foreground hover:text-foreground",
)}>
<AccessIcon className="h-3.5 w-3.5 shrink-0" />
<span className="min-w-0 truncate">{label}</span>
<ChevronDown className="h-3 w-3 shrink-0" aria-hidden />
<span id={descriptionId} className="sr-only">{description}</span>
</button>
</DropdownMenuTrigger>
</HoverHint>
<TooltipProvider>
<Tooltip open={hintEnabled && hintOpen} onOpenChange={next => setHintOpen(hintEnabled && next)}>
<TooltipTrigger asChild>
<DropdownMenuTrigger asChild disabled={disabled || !onChange}>
<button ref={triggerRef} type="button" aria-label={accessAriaLabel} aria-describedby={descriptionId}
onPointerDown={() => { restoreTriggerFocus.current = false; }}
onKeyDown={() => { restoreTriggerFocus.current = true; }}
className={cn(
"thread-composer-access touch-target inline-flex h-8 min-w-0 max-w-full shrink items-center gap-1.5 rounded-md px-2 text-[12px] font-medium outline-none transition-colors motion-reduce:transition-none focus-visible:ring-2 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-55",
isFull ? "text-orange-600 hover:text-orange-700 dark:text-orange-300 dark:hover:text-orange-200" : "text-muted-foreground hover:text-foreground",
)}>
<AccessIcon className="h-3.5 w-3.5 shrink-0" />
<span className="min-w-0 truncate">{label}</span>
<ChevronDown className="h-3 w-3 shrink-0" aria-hidden />
<span id={descriptionId} className="sr-only">{description}</span>
</button>
</DropdownMenuTrigger>
</TooltipTrigger>
<TooltipContent ref={setHint} side={hintPosition.side} sideOffset={hintPosition.sideOffset}
className="max-w-[min(28rem,calc(100vw-2rem))] whitespace-pre-wrap break-words">{description}</TooltipContent>
</Tooltip>
</TooltipProvider>
<DropdownMenuContent side="top" align="start" sideOffset={8} aria-label={t("thread.composer.workspace.accessAria")}
onPointerDownCapture={() => { restoreTriggerFocus.current = false; }}
onKeyDownCapture={() => { restoreTriggerFocus.current = true; }}