mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-11 13:24:55 +01:00
## Thinking Path > - Paperclip is the open source app people use to manage AI agents for work. > - The new task dialog lets an operator assign work on a phone. > - The mobile picker used fixed coordinates inside the Radix wrapper. > - iOS Safari already makes fixed coordinates relative to the visual viewport. > - The old code added the visual viewport offset a second time. > - This pull request removes the second offset and gives the mobile wrapper full viewport geometry. > - The benefit is a visible picker and search field when the iOS keyboard opens. ## Linked Issues or Issue Description **What happened?** The assignee and project pickers could move outside the visible screen in the new task dialog on iOS. **What did you expect to happen?** The picker and its search field must stay visible above the bottom edge and the software keyboard. **Steps to reproduce** Open the new task dialog on an iPhone. Select Assignee or Project. The picker can move outside the visual viewport when Safari pans the page. **Paperclip version or commit** The problem exists after the change in PR #13343. **Deployment mode** The problem affects the board UI in local and authenticated modes. Refs #13343 ## What Changed - Use visual viewport local coordinates for the new task dialog. - Give the mobile Radix popper wrapper full viewport geometry. - Render both shared selector components as large, titled mobile modals. - Keep desktop selectors as anchored popovers. - Cover the new-task assignee, new-task project, composer assignee, and generic searchable selector in Storybook. - Add iPhone-sized Storybook states for the assignee and project pickers. - Update viewport tests for the corrected coordinate model. ## Verification - `pnpm --filter @paperclipai/ui exec vitest run src/components/NewIssueDialog.test.tsx src/components/InlineEntitySelector.test.tsx` - `pnpm check:token-gates` - Playwright used the iPhone 14 device profile. All mobile modals were inside the 390 by 664 CSS viewport. - Each mobile modal was `x=16, y=16, width=358, height=632`. - A desktop check kept the anchored popover at `width=320, height=211`. ## Risks - Low risk. The CSS only changes narrow mobile viewports. - The full-screen popper wrapper does not receive pointer events. The picker still receives pointer events. > For core feature work, check [`ROADMAP.md`](ROADMAP.md) first and discuss it in `#dev` before opening the PR. See `CONTRIBUTING.md`. ## Model Used - OpenAI GPT-5.6, Codex agent with tool use and code execution. ## Checklist - [x] I have included a thinking path that traces from project context to this change - [x] I have specified the model used (with version and capability details) - [x] I have checked ROADMAP.md and confirmed this PR does not duplicate planned core work - [x] I have searched GitHub for duplicate or related PRs and linked them above - [x] I have either (a) linked existing issues with `Fixes: #` / `Closes #` / `Refs #` OR (b) described the issue in-PR following the relevant issue template - [x] I have not referenced internal/instance-local Paperclip issues or links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip` URLs) - [x] My branch name describes the change and contains no internal task ID - [x] I have run tests locally and they pass - [x] I have added or updated tests where applicable - [x] I have updated relevant Storybook documentation to reflect my changes - [x] I have considered and documented any risks above - [ ] All Paperclip CI gates are green - [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups - [x] I will address all Greptile and reviewer comments before requesting merge --------- Co-authored-by: Paperclip <noreply@paperclip.ing>
336 lines
12 KiB
TypeScript
336 lines
12 KiB
TypeScript
import { Check, ChevronsUpDown, X } from "lucide-react";
|
|
import { useMemo, useRef, useState, type ReactNode } from "react";
|
|
import { Button } from "@/components/ui/button";
|
|
import {
|
|
Command,
|
|
CommandEmpty,
|
|
CommandGroup,
|
|
CommandInput,
|
|
CommandItem,
|
|
CommandList,
|
|
} from "@/components/ui/command";
|
|
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";
|
|
import { fuzzyTextMatchesQuery, normalizeSearchText, scoreFuzzyTextFields } from "@/lib/searchable-select";
|
|
import { cn } from "@/lib/utils";
|
|
import { useMobileEntityPickerViewportStyle } from "@/hooks/useMobileEntityPickerViewportStyle";
|
|
|
|
export interface SearchableSelectOption<TValue extends string = string> {
|
|
key: string;
|
|
value: TValue;
|
|
label: string;
|
|
title?: string;
|
|
searchText?: string;
|
|
disabled?: boolean;
|
|
}
|
|
|
|
export interface SearchableSelectGroup<TValue extends string = string, TOption extends SearchableSelectOption<TValue> = SearchableSelectOption<TValue>> {
|
|
id: string;
|
|
label?: string;
|
|
options: readonly TOption[];
|
|
}
|
|
|
|
interface SearchableSelectRenderState {
|
|
selected: boolean;
|
|
}
|
|
|
|
export interface SearchableSelectProps<
|
|
TValue extends string = string,
|
|
TOption extends SearchableSelectOption<TValue> = SearchableSelectOption<TValue>,
|
|
> {
|
|
value: TValue | "";
|
|
groups: readonly SearchableSelectGroup<TValue, TOption>[];
|
|
onValueChange: (value: TValue, option: TOption) => void | boolean | { close?: boolean };
|
|
placeholder: string;
|
|
searchPlaceholder?: string;
|
|
emptyMessage?: string;
|
|
loadingMessage?: string;
|
|
loading?: boolean;
|
|
disabled?: boolean;
|
|
className?: string;
|
|
triggerClassName?: string;
|
|
contentClassName?: string;
|
|
align?: "start" | "center" | "end";
|
|
contentWidth?: "trigger" | "auto";
|
|
renderValue?: (option: TOption | null) => ReactNode;
|
|
renderOption?: (option: TOption, state: SearchableSelectRenderState) => ReactNode;
|
|
deriveGroups?: (
|
|
query: string,
|
|
groups: readonly SearchableSelectGroup<TValue, TOption>[],
|
|
) => readonly SearchableSelectGroup<TValue, TOption>[];
|
|
filterOption?: (option: TOption, query: string) => boolean;
|
|
scoreOption?: (option: TOption, query: string) => number | null;
|
|
disablePortal?: boolean;
|
|
/** Heading for the large mobile selector modal. Defaults to the placeholder. */
|
|
mobileTitle?: string;
|
|
/**
|
|
* Optional pinned "creatable" item rendered at the bottom of the list,
|
|
* regardless of the query (used e.g. by the secret picker's
|
|
* `+ Create secret "<query>"…` affordance). `render` receives the live
|
|
* query so the label can echo it; `onSelect` fires with the query and the
|
|
* popover closes.
|
|
*/
|
|
createItem?: {
|
|
render: (query: string) => ReactNode;
|
|
onSelect: (query: string) => void;
|
|
};
|
|
}
|
|
|
|
function defaultFilterOption(option: SearchableSelectOption, query: string) {
|
|
return fuzzyTextMatchesQuery(`${option.label} ${option.searchText ?? ""}`, query);
|
|
}
|
|
|
|
function defaultScoreOption(option: SearchableSelectOption, query: string) {
|
|
return scoreFuzzyTextFields([
|
|
{ text: option.label, weight: 0 },
|
|
{ text: option.searchText, weight: 20 },
|
|
], query);
|
|
}
|
|
|
|
export function SearchableSelect<
|
|
TValue extends string = string,
|
|
TOption extends SearchableSelectOption<TValue> = SearchableSelectOption<TValue>,
|
|
>({
|
|
value,
|
|
groups,
|
|
onValueChange,
|
|
placeholder,
|
|
searchPlaceholder = "Search...",
|
|
emptyMessage = "No options found.",
|
|
loadingMessage = "Loading...",
|
|
loading = false,
|
|
disabled = false,
|
|
className,
|
|
triggerClassName,
|
|
contentClassName,
|
|
align = "start",
|
|
contentWidth = "trigger",
|
|
renderValue,
|
|
renderOption,
|
|
deriveGroups,
|
|
filterOption = defaultFilterOption,
|
|
scoreOption,
|
|
disablePortal,
|
|
mobileTitle,
|
|
createItem,
|
|
}: SearchableSelectProps<TValue, TOption>) {
|
|
const [open, setOpen] = useState(false);
|
|
const [query, setQuery] = useState("");
|
|
const mobileViewportStyle = useMobileEntityPickerViewportStyle();
|
|
const pointerFocusRef = useRef(false);
|
|
const suppressNextTriggerFocusRef = useRef(false);
|
|
|
|
const selectedOption = useMemo(() => {
|
|
for (const group of groups) {
|
|
const option = group.options.find((candidate) => candidate.value === value);
|
|
if (option) return option;
|
|
}
|
|
return null;
|
|
}, [groups, value]);
|
|
|
|
const visibleGroups = useMemo(
|
|
() => (deriveGroups ? deriveGroups(query, groups) : groups),
|
|
[deriveGroups, groups, query],
|
|
);
|
|
|
|
const filteredGroups = useMemo(() => {
|
|
if (loading) return [];
|
|
const normalizedQuery = normalizeSearchText(query);
|
|
return visibleGroups
|
|
.map((group) => {
|
|
const options = group.options
|
|
.map((option, index) => {
|
|
if (!normalizedQuery) return { option, index, score: 0 };
|
|
|
|
if (!filterOption(option, query)) return null;
|
|
|
|
if (scoreOption) {
|
|
const score = scoreOption(option, query);
|
|
return score === null ? null : { option, index, score };
|
|
}
|
|
|
|
return {
|
|
option,
|
|
index,
|
|
score: defaultScoreOption(option, query) ?? Number.MAX_SAFE_INTEGER,
|
|
};
|
|
})
|
|
.filter((entry): entry is { option: TOption; index: number; score: number } => entry !== null);
|
|
|
|
if (normalizedQuery) {
|
|
options.sort((a, b) => a.score - b.score || a.index - b.index);
|
|
}
|
|
|
|
return {
|
|
...group,
|
|
options: options.map((entry) => entry.option),
|
|
};
|
|
})
|
|
.filter((group) => group.options.length > 0);
|
|
}, [filterOption, loading, query, scoreOption, visibleGroups]);
|
|
|
|
const hasOptions = filteredGroups.some((group) => group.options.length > 0);
|
|
|
|
function closePopover({ suppressTriggerFocus = false }: { suppressTriggerFocus?: boolean } = {}) {
|
|
if (suppressTriggerFocus) {
|
|
suppressNextTriggerFocusRef.current = true;
|
|
}
|
|
setOpen(false);
|
|
setQuery("");
|
|
}
|
|
|
|
function selectOption(option: TOption) {
|
|
if (option.disabled) return;
|
|
suppressNextTriggerFocusRef.current = true;
|
|
const result = onValueChange(option.value, option);
|
|
if (result === false || (typeof result === "object" && result?.close === false)) {
|
|
return;
|
|
}
|
|
closePopover();
|
|
}
|
|
|
|
return (
|
|
<Popover
|
|
open={open}
|
|
onOpenChange={(next) => {
|
|
setOpen(next);
|
|
if (!next) setQuery("");
|
|
}}
|
|
>
|
|
<PopoverTrigger asChild>
|
|
<Button
|
|
type="button"
|
|
variant="outline"
|
|
disabled={disabled}
|
|
onPointerDown={() => {
|
|
pointerFocusRef.current = true;
|
|
}}
|
|
onFocus={() => {
|
|
const shouldIgnoreFocus = pointerFocusRef.current || suppressNextTriggerFocusRef.current;
|
|
pointerFocusRef.current = false;
|
|
suppressNextTriggerFocusRef.current = false;
|
|
if (shouldIgnoreFocus) {
|
|
return;
|
|
}
|
|
setOpen(true);
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (event.key === "Escape" && open) {
|
|
event.preventDefault();
|
|
closePopover();
|
|
}
|
|
}}
|
|
aria-expanded={open}
|
|
role="combobox"
|
|
className={cn("w-full justify-between overflow-hidden", className, triggerClassName)}
|
|
>
|
|
<span className={cn("min-w-0 flex-1 truncate text-left", !selectedOption && "text-muted-foreground")}>
|
|
{renderValue ? renderValue(selectedOption) : selectedOption?.label ?? placeholder}
|
|
</span>
|
|
<ChevronsUpDown className="ml-2 size-4 opacity-50" />
|
|
</Button>
|
|
</PopoverTrigger>
|
|
<PopoverContent
|
|
data-mobile-entity-picker=""
|
|
aria-label={mobileTitle ?? placeholder}
|
|
style={mobileViewportStyle}
|
|
align={align}
|
|
collisionPadding={16}
|
|
disablePortal={disablePortal}
|
|
className={cn(
|
|
"p-0",
|
|
contentWidth === "trigger"
|
|
? "w-(--radix-popover-trigger-width) min-w-56 max-w-(--sz-calc-23)"
|
|
: "w-72 max-w-(--sz-calc-23)",
|
|
contentClassName,
|
|
)}
|
|
onKeyDownCapture={(event) => {
|
|
if (event.key === "Escape") {
|
|
event.preventDefault();
|
|
event.stopPropagation();
|
|
closePopover({ suppressTriggerFocus: true });
|
|
}
|
|
}}
|
|
>
|
|
<div data-mobile-entity-picker-header="" className="hidden items-center justify-between border-b border-border px-4 py-3">
|
|
<span className="text-base font-semibold text-foreground">{mobileTitle ?? placeholder}</span>
|
|
<button
|
|
type="button"
|
|
className="inline-flex size-9 items-center justify-center rounded-md text-muted-foreground hover:bg-accent hover:text-foreground"
|
|
aria-label="Close selector"
|
|
onClick={() => closePopover({ suppressTriggerFocus: true })}
|
|
>
|
|
<X className="size-5" />
|
|
</button>
|
|
</div>
|
|
<Command shouldFilter={false}>
|
|
<CommandInput
|
|
value={query}
|
|
onValueChange={setQuery}
|
|
placeholder={searchPlaceholder}
|
|
/>
|
|
<CommandList
|
|
className="overscroll-contain touch-pan-y"
|
|
onWheelCapture={(event) => {
|
|
if (event.currentTarget.scrollHeight > event.currentTarget.clientHeight) {
|
|
event.stopPropagation();
|
|
}
|
|
}}
|
|
>
|
|
{loading ? (
|
|
<div className="px-3 py-6 text-center text-sm text-muted-foreground">{loadingMessage}</div>
|
|
) : (
|
|
<>
|
|
{!hasOptions && !createItem ? <CommandEmpty>{emptyMessage}</CommandEmpty> : null}
|
|
{!hasOptions && createItem ? (
|
|
<div className="px-3 py-3 text-center text-xs text-muted-foreground">{emptyMessage}</div>
|
|
) : null}
|
|
{filteredGroups.map((group) => (
|
|
<CommandGroup key={group.id} heading={group.label}>
|
|
{group.options.map((option) => {
|
|
const selected = option.value === value;
|
|
return (
|
|
<CommandItem
|
|
key={option.key}
|
|
value={option.key}
|
|
disabled={option.disabled}
|
|
onSelect={() => selectOption(option)}
|
|
>
|
|
{renderOption
|
|
? renderOption(option, { selected })
|
|
: <span className="min-w-0 truncate">{option.label}</span>}
|
|
<Check className={cn("ml-auto size-4", selected ? "opacity-100" : "opacity-0")} />
|
|
</CommandItem>
|
|
);
|
|
})}
|
|
</CommandGroup>
|
|
))}
|
|
{createItem ? (
|
|
<CommandGroup className="border-t border-border">
|
|
<CommandItem
|
|
value="__searchable_select_create__"
|
|
onSelect={() => {
|
|
const q = query;
|
|
suppressNextTriggerFocusRef.current = true;
|
|
closePopover();
|
|
// Defer to the next macrotask so this popover fully
|
|
// closes (and Radix returns focus to the trigger)
|
|
// before the consumer opens its own popover. Firing
|
|
// synchronously lets the close's focus-return land as a
|
|
// `focusOutside` on the newly-opened popover, which
|
|
// Radix would immediately dismiss (see PAP-12476).
|
|
window.setTimeout(() => createItem.onSelect(q), 0);
|
|
}}
|
|
>
|
|
{createItem.render(query)}
|
|
</CommandItem>
|
|
</CommandGroup>
|
|
) : null}
|
|
</>
|
|
)}
|
|
</CommandList>
|
|
</Command>
|
|
</PopoverContent>
|
|
</Popover>
|
|
);
|
|
}
|