Open History to see the last commit
1import type { ResolvedKeybindingsConfig } from "@t3tools/contracts";
2import { useGT } from "gt-react";
3import { PinIcon, PinOffIcon } from "lucide-react";
4import type { KeyboardEvent } from "react";
5
6import { resolveShortcutCommand } from "../keybindings";
7
8import { cn } from "~/lib/utils";
9import { Button } from "./ui/button";
10
11/** The thread pin shortcut, which pins the highlighted row in a project list. */
12export function isProjectPinShortcut(
13 event: KeyboardEvent,
14 keybindings: ResolvedKeybindingsConfig,
15): boolean {
16 return (
17 resolveShortcutCommand(event.nativeEvent, keybindings, {
18 platform: navigator.platform,
19 context: { modelPickerOpen: false, editableFocus: true },
20 }) === "thread.pin"
21 );
22}
23
24/**
25 * Pin toggle for a project row in a menu or list. A pinned project always shows its pin;
26 * an unpinned one shows it only on the highlighted row. Keyboard users get a shortcut
27 * from the list that hosts it, so the button stays out of the tab order.
28 */
29export function ProjectPinButton(props: {
30 readonly name: string;
31 readonly pinned: boolean;
32 readonly onToggle: () => void;
33 readonly shortcutLabel?: string | null;
34}) {
35 const gt = useGT();
36 const action = props.pinned ? gt`Unpin ${{ name: props.name }}` : gt`Pin ${{ name: props.name }}`;
37 return (
38 <span
39 className={cn(
40 "group/pin ml-auto flex",
41 !props.pinned && "opacity-0 in-data-highlighted:opacity-100 pointer-coarse:opacity-100",
42 )}
43 >
44 <Button
45 size="icon-xs"
46 variant="ghost-muted"
47 tabIndex={-1}
48 aria-hidden="true"
49 title={
50 props.shortcutLabel ? gt`${{ action }} (${{ shortcut: props.shortcutLabel }})` : action
51 }
52 onPointerDown={(event) => event.stopPropagation()}
53 onMouseDown={(event) => event.preventDefault()}
54 onClick={(event) => {
55 event.preventDefault();
56 event.stopPropagation();
57 props.onToggle();
58 }}
59 >
60 {props.pinned ? (
61 <>
62 <PinIcon className="size-3.5 group-hover/pin:hidden" />
63 <PinOffIcon className="hidden size-3.5 group-hover/pin:block" />
64 </>
65 ) : (
66 <PinIcon className="size-3.5" />
67 )}
68 </Button>
69 </span>
70 );
71}