|
3 | 3 | // upstream `CommandPalette.tsx` carries only marked hook lines pointing here; |
4 | 4 | // the capability probe, the navigation command, and the action item are built |
5 | 5 | // in this module. |
6 | | -import { CircleDotIcon, ListFilterIcon } from "lucide-react"; |
| 6 | +import { projectFilterProjectKeys } from "@t3tools/client-runtime/state/project-filter"; |
| 7 | +import { CircleDotIcon, ListFilterIcon, SearchIcon } from "lucide-react"; |
| 8 | +import { useMemo, useState } from "react"; |
7 | 9 |
|
8 | | -import { openProjectChooser } from "../projectChooser.fork"; |
9 | | -import { ITEM_ICON_CLASS, type CommandPaletteActionItem } from "./CommandPalette.logic"; |
| 10 | +import { openProjectChooser, useProjectChooserHostValue } from "../projectChooser.fork"; |
| 11 | +import { chatNewMatchesCurrentProject, scopeToProjectKeys } from "../projectFilterScope.fork"; |
| 12 | +import type { ChatThreadActionContext } from "../lib/chatThreadActions"; |
| 13 | +import { |
| 14 | + enumerateCommandPaletteItems, |
| 15 | + ITEM_ICON_CLASS, |
| 16 | + type CommandPaletteActionItem, |
| 17 | + type CommandPaletteGroup, |
| 18 | + type CommandPaletteSubmenuItem, |
| 19 | + type CommandPaletteView, |
| 20 | +} from "./CommandPalette.logic"; |
| 21 | +import type { ScopedProjectRef } from "@t3tools/contracts"; |
10 | 22 |
|
11 | 23 | /** |
12 | 24 | * The palette's Issues navigation entry, or `null` when no reachable |
@@ -60,3 +72,140 @@ export function buildProjectChooserActionItemFork( |
60 | 72 | }, |
61 | 73 | }; |
62 | 74 | } |
| 75 | + |
| 76 | +// Under the window's project filter, thread search, project search, and the new-thread picker |
| 77 | +// show the filter's projects until the user picks "Search all projects", which lasts until the |
| 78 | +// palette closes. Without a chooser (no projects yet, or a route without the sidebar) nothing |
| 79 | +// is scoped. |
| 80 | + |
| 81 | +// The new-thread picker view `CommandPalette.tsx` pushes. |
| 82 | +const NEW_THREAD_IN_VIEW = "projects"; |
| 83 | + |
| 84 | +export interface CommandPaletteProjectScope { |
| 85 | + /** The project keys the palette shows; `null` shows every project. */ |
| 86 | + readonly projectKeys: ReadonlySet<string> | null; |
| 87 | + /** The toggle between the filter and every project; `null` when unscoped. */ |
| 88 | + readonly toggle: CommandPaletteActionItem | null; |
| 89 | +} |
| 90 | + |
| 91 | +export function useCommandPaletteProjectScopeFork(): CommandPaletteProjectScope { |
| 92 | + const chooser = useProjectChooserHostValue(); |
| 93 | + const filter = chooser?.filter ?? null; |
| 94 | + const label = chooser?.label ?? null; |
| 95 | + const [searchAll, setSearchAll] = useState(false); |
| 96 | + const filterKeys = useMemo( |
| 97 | + () => (filter === null ? null : projectFilterProjectKeys(filter)), |
| 98 | + [filter], |
| 99 | + ); |
| 100 | + return useMemo(() => { |
| 101 | + if (filterKeys === null || label === null) return { projectKeys: null, toggle: null }; |
| 102 | + const toggle: CommandPaletteActionItem = searchAll |
| 103 | + ? { |
| 104 | + kind: "action", |
| 105 | + value: "action:search-filtered-projects", |
| 106 | + searchTerms: [], |
| 107 | + title: "Search only this window's projects", |
| 108 | + description: label, |
| 109 | + icon: <ListFilterIcon className={ITEM_ICON_CLASS} />, |
| 110 | + keepOpen: true, |
| 111 | + run: async () => setSearchAll(false), |
| 112 | + } |
| 113 | + : { |
| 114 | + kind: "action", |
| 115 | + value: "action:search-all-projects", |
| 116 | + searchTerms: [], |
| 117 | + title: "Search all projects", |
| 118 | + description: label, |
| 119 | + icon: <SearchIcon className={ITEM_ICON_CLASS} />, |
| 120 | + keepOpen: true, |
| 121 | + run: async () => setSearchAll(true), |
| 122 | + }; |
| 123 | + return { projectKeys: searchAll ? null : filterKeys, toggle }; |
| 124 | + }, [filterKeys, label, searchAll]); |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * Whether "New thread in <current project>" may show the `chat.new` hint: the |
| 129 | + * shortcut must land where the item does. |
| 130 | + */ |
| 131 | +export function useNewThreadHintFork( |
| 132 | + activeDraftThread: ChatThreadActionContext["activeDraftThread"], |
| 133 | + activeThread: ChatThreadActionContext["activeThread"] | null, |
| 134 | + defaultProjectRef: ChatThreadActionContext["defaultProjectRef"], |
| 135 | +): boolean { |
| 136 | + const filter = useProjectChooserHostValue()?.filter ?? null; |
| 137 | + return ( |
| 138 | + filter === null || |
| 139 | + chatNewMatchesCurrentProject(filter, { |
| 140 | + activeDraftThread, |
| 141 | + activeThread: activeThread ?? undefined, |
| 142 | + defaultProjectRef, |
| 143 | + }) |
| 144 | + ); |
| 145 | +} |
| 146 | + |
| 147 | +/** Drops the `chat.new` hint from "New thread in <current project>" when it would mislead. */ |
| 148 | +export function dropNewThreadHintFork( |
| 149 | + items: Array<CommandPaletteActionItem | CommandPaletteSubmenuItem>, |
| 150 | + showHint: boolean, |
| 151 | +): void { |
| 152 | + if (showHint) return; |
| 153 | + const index = items.findIndex((item) => item.value === "action:new-thread"); |
| 154 | + const item = items[index]; |
| 155 | + if (item === undefined || item.kind !== "action") return; |
| 156 | + const { shortcutCommand: _hint, ...withoutHint } = item; |
| 157 | + items[index] = withoutHint; |
| 158 | +} |
| 159 | + |
| 160 | +/** The palette's threads under the scope. */ |
| 161 | +export function scopedPaletteThreadsFork< |
| 162 | + Thread extends { readonly environmentId: string; readonly projectId: string }, |
| 163 | +>(threads: ReadonlyArray<Thread>, projectKeys: ReadonlySet<string> | null) { |
| 164 | + return scopeToProjectKeys(threads, projectKeys, (thread) => [ |
| 165 | + `${thread.environmentId}:${thread.projectId}`, |
| 166 | + ]); |
| 167 | +} |
| 168 | + |
| 169 | +/** |
| 170 | + * The palette's project picker entries under the scope. A scope that leaves no |
| 171 | + * entry (its projects not loaded yet) shows every project, so the picker is |
| 172 | + * never empty. |
| 173 | + */ |
| 174 | +export function scopePaletteProjectEntriesFork< |
| 175 | + Entry extends { readonly group: { readonly memberProjectRefs: ReadonlyArray<ScopedProjectRef> } }, |
| 176 | +>(entries: ReadonlyArray<Entry>, projectKeys: ReadonlySet<string> | null): ReadonlyArray<Entry> { |
| 177 | + const scoped = scopeToProjectKeys(entries, projectKeys, (entry) => |
| 178 | + entry.group.memberProjectRefs.map((ref) => `${ref.environmentId}:${ref.projectId}`), |
| 179 | + ); |
| 180 | + return scoped.length > 0 ? scoped : entries; |
| 181 | +} |
| 182 | + |
| 183 | +/** |
| 184 | + * The new-thread picker view follows the scope after it was pushed: its items |
| 185 | + * become the current picker items, keeping the pushed first item (the current |
| 186 | + * project) first. Other views pass through. |
| 187 | + */ |
| 188 | +export function refreshNewThreadInViewFork( |
| 189 | + groups: ReadonlyArray<CommandPaletteGroup>, |
| 190 | + projectThreadItems: ReadonlyArray<CommandPaletteActionItem>, |
| 191 | +): ReadonlyArray<CommandPaletteGroup> { |
| 192 | + const view = groups[0]; |
| 193 | + if (groups.length !== 1 || view?.value !== NEW_THREAD_IN_VIEW) return groups; |
| 194 | + const lead = view.items[0]?.value; |
| 195 | + const ordered = [ |
| 196 | + ...projectThreadItems.filter((item) => item.value === lead), |
| 197 | + ...projectThreadItems.filter((item) => item.value !== lead), |
| 198 | + ]; |
| 199 | + return [{ ...view, items: enumerateCommandPaletteItems(ordered) }]; |
| 200 | +} |
| 201 | + |
| 202 | +/** Appends the scope toggle below the root list and the new-thread picker. */ |
| 203 | +export function withProjectScopeToggleFork( |
| 204 | + groups: CommandPaletteGroup[], |
| 205 | + scope: CommandPaletteProjectScope, |
| 206 | + currentView: CommandPaletteView | null, |
| 207 | +): CommandPaletteGroup[] { |
| 208 | + if (scope.toggle === null) return groups; |
| 209 | + if (currentView !== null && currentView.groups[0]?.value !== NEW_THREAD_IN_VIEW) return groups; |
| 210 | + return [...groups, { value: "project-scope", label: "Scope", items: [scope.toggle] }]; |
| 211 | +} |
0 commit comments