|
| 1 | +import { ArrowSquareOutIcon, LinkIcon } from "@phosphor-icons/react"; |
| 2 | +import { |
| 3 | + Button, |
| 4 | + DropdownMenu, |
| 5 | + DropdownMenuContent, |
| 6 | + DropdownMenuGroup, |
| 7 | + DropdownMenuItem, |
| 8 | + DropdownMenuLabel, |
| 9 | + DropdownMenuSeparator, |
| 10 | + DropdownMenuSub, |
| 11 | + DropdownMenuSubContent, |
| 12 | + DropdownMenuSubTrigger, |
| 13 | + DropdownMenuTrigger, |
| 14 | +} from "@posthog/quill"; |
| 15 | +import type { Task } from "@posthog/shared/domain-types"; |
| 16 | +import { TaskTabIcon } from "@posthog/ui/features/browser-tabs/TaskTabIcon"; |
| 17 | +import { useChannelFeed } from "@posthog/ui/features/canvas/hooks/useChannelFeed"; |
| 18 | +import { useChannels } from "@posthog/ui/features/canvas/hooks/useChannels"; |
| 19 | +import { useDashboards } from "@posthog/ui/features/canvas/hooks/useDashboards"; |
| 20 | +import { useBackendChannel } from "@posthog/ui/features/canvas/hooks/useTaskChannels"; |
| 21 | +import { useTasks } from "@posthog/ui/features/tasks/useTasks"; |
| 22 | +import { File, Shapes, SquircleDashed } from "lucide-react"; |
| 23 | + |
| 24 | +// A menu for inserting an in-app reference (canvas / channel / task / this |
| 25 | +// channel's CONTEXT.md) as a deep link. Canvas / Channels / Tasks are submenus; |
| 26 | +// CONTEXT.md is a direct item. |
| 27 | +export function ReferencePicker({ |
| 28 | + channelId, |
| 29 | + onInsert, |
| 30 | +}: { |
| 31 | + channelId: string; |
| 32 | + onInsert: (label: string, href: string) => void; |
| 33 | +}) { |
| 34 | + const { channels } = useChannels(); |
| 35 | + const channelName = channels.find((c) => c.id === channelId)?.name; |
| 36 | + const { channel: backendChannel } = useBackendChannel(channelName); |
| 37 | + const { dashboards } = useDashboards(channelId); |
| 38 | + const { tasks: channelTasks } = useChannelFeed(backendChannel?.id); |
| 39 | + const { data: allTasks = [] } = useTasks(); |
| 40 | + |
| 41 | + // Every task is referenceable: this channel's tasks first, then the rest. |
| 42 | + const channelTaskIds = new Set(channelTasks.map((t) => t.id)); |
| 43 | + const otherTasks = allTasks.filter((t) => !channelTaskIds.has(t.id)); |
| 44 | + |
| 45 | + const taskItem = (t: Task) => ( |
| 46 | + <DropdownMenuItem |
| 47 | + key={t.id} |
| 48 | + onClick={() => |
| 49 | + onInsert( |
| 50 | + t.title || "Untitled task", |
| 51 | + `/website/${channelId}/tasks/${t.id}`, |
| 52 | + ) |
| 53 | + } |
| 54 | + > |
| 55 | + <TaskTabIcon task={t} size={14} /> |
| 56 | + <span className="truncate">{t.title || "Untitled task"}</span> |
| 57 | + </DropdownMenuItem> |
| 58 | + ); |
| 59 | + |
| 60 | + return ( |
| 61 | + <DropdownMenu> |
| 62 | + <DropdownMenuTrigger |
| 63 | + render={ |
| 64 | + <Button variant="outline" size="xs"> |
| 65 | + <LinkIcon size={13} /> |
| 66 | + Insert reference |
| 67 | + </Button> |
| 68 | + } |
| 69 | + /> |
| 70 | + <DropdownMenuContent align="end" className="w-56"> |
| 71 | + <DropdownMenuSub> |
| 72 | + <DropdownMenuSubTrigger> |
| 73 | + <Shapes size={14} className="text-muted-foreground" /> |
| 74 | + Canvas |
| 75 | + </DropdownMenuSubTrigger> |
| 76 | + <DropdownMenuSubContent side="right" sideOffset={4} className="w-56"> |
| 77 | + {dashboards.length === 0 ? ( |
| 78 | + <DropdownMenuItem disabled>No canvases yet</DropdownMenuItem> |
| 79 | + ) : ( |
| 80 | + dashboards.map((d) => ( |
| 81 | + <DropdownMenuItem |
| 82 | + key={d.id} |
| 83 | + onClick={() => |
| 84 | + onInsert(d.name, `/website/${channelId}/dashboards/${d.id}`) |
| 85 | + } |
| 86 | + > |
| 87 | + <Shapes size={14} className="text-muted-foreground" /> |
| 88 | + {d.name} |
| 89 | + </DropdownMenuItem> |
| 90 | + )) |
| 91 | + )} |
| 92 | + </DropdownMenuSubContent> |
| 93 | + </DropdownMenuSub> |
| 94 | + |
| 95 | + <DropdownMenuSub> |
| 96 | + <DropdownMenuSubTrigger> |
| 97 | + <SquircleDashed size={14} className="text-muted-foreground" /> |
| 98 | + Channels |
| 99 | + </DropdownMenuSubTrigger> |
| 100 | + <DropdownMenuSubContent side="right" sideOffset={4} className="w-56"> |
| 101 | + {channels.map((c) => ( |
| 102 | + <DropdownMenuItem |
| 103 | + key={c.id} |
| 104 | + onClick={() => onInsert(`#${c.name}`, `/website/${c.id}`)} |
| 105 | + > |
| 106 | + <SquircleDashed size={14} className="text-muted-foreground" /> |
| 107 | + {c.name} |
| 108 | + </DropdownMenuItem> |
| 109 | + ))} |
| 110 | + </DropdownMenuSubContent> |
| 111 | + </DropdownMenuSub> |
| 112 | + |
| 113 | + <DropdownMenuSub> |
| 114 | + <DropdownMenuSubTrigger> |
| 115 | + <ArrowSquareOutIcon size={14} className="text-muted-foreground" /> |
| 116 | + Tasks |
| 117 | + </DropdownMenuSubTrigger> |
| 118 | + <DropdownMenuSubContent side="right" sideOffset={4} className="w-64"> |
| 119 | + {channelTasks.length === 0 && otherTasks.length === 0 ? ( |
| 120 | + <DropdownMenuItem disabled>No tasks yet</DropdownMenuItem> |
| 121 | + ) : ( |
| 122 | + <> |
| 123 | + {channelTasks.length > 0 && ( |
| 124 | + <DropdownMenuGroup> |
| 125 | + <DropdownMenuLabel>This channel</DropdownMenuLabel> |
| 126 | + {channelTasks.map(taskItem)} |
| 127 | + </DropdownMenuGroup> |
| 128 | + )} |
| 129 | + {otherTasks.length > 0 && ( |
| 130 | + <DropdownMenuGroup> |
| 131 | + {channelTasks.length > 0 && <DropdownMenuSeparator />} |
| 132 | + <DropdownMenuLabel>Other tasks</DropdownMenuLabel> |
| 133 | + {otherTasks.map(taskItem)} |
| 134 | + </DropdownMenuGroup> |
| 135 | + )} |
| 136 | + </> |
| 137 | + )} |
| 138 | + </DropdownMenuSubContent> |
| 139 | + </DropdownMenuSub> |
| 140 | + |
| 141 | + <DropdownMenuItem |
| 142 | + onClick={() => |
| 143 | + onInsert("CONTEXT.md", `/website/${channelId}/context`) |
| 144 | + } |
| 145 | + > |
| 146 | + <File size={14} className="text-muted-foreground" /> |
| 147 | + This channel's CONTEXT.md |
| 148 | + </DropdownMenuItem> |
| 149 | + </DropdownMenuContent> |
| 150 | + </DropdownMenu> |
| 151 | + ); |
| 152 | +} |
0 commit comments