Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DraftHeroHeadline.tsx

258 lines9.9 KB

1import type { DraftId } from "~/composerDraftStore";
2import { useComposerDraftStore } from "~/composerDraftStore";
3import { resolveEnvironmentMachineKind, type ScopedProjectRef } from "@t3tools/contracts";
4import { scopedProjectKey, scopeProjectRef } from "@t3tools/client-runtime/environment";
5import { FolderPlusIcon } from "lucide-react";
6import { useCallback, useMemo } from "react";
7​
8import { openCommandPalette } from "~/commandPaletteBus";
9import { useClientSettings } from "~/hooks/useSettings";
10import { hasExplicitComposerModelSelection } from "~/lib/chatThreadActions";
11import { selectProjectGroupingSettings } from "~/logicalProject";
12import {
13 buildSidebarProjectPickerEntries,
14 buildSidebarProjectSnapshots,
15 projectGroupsSpanEnvironments,
16} from "~/sidebarProjectGrouping";
17import { useProjects, useThreadShells } from "~/state/entities";
18import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments";
19import { ProjectEnvironmentBadge } from "../ProjectEnvironmentBadge";
20import { ProjectFavicon } from "../ProjectFavicon";
21import { sortLogicalProjectsForSidebar } from "../Sidebar.logic";
22import {
23 Menu,
24 MenuItem,
25 MenuPopup,
26 MenuRadioGroup,
27 MenuRadioItem,
28 MenuSeparator,
29 MenuTrigger,
30} from "../ui/menu";
31import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
32import { InlineButton } from "../ui/button";
33import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
34​
35interface DraftHeroHeadlineProps {
36 readonly draftId: DraftId | null;
37 readonly activeProjectRef: ScopedProjectRef | null;
38 readonly activeProjectTitle: string | null;
40​
41export function DraftHeroHeadline({
42 draftId,
43 activeProjectRef,
44 activeProjectTitle,
45}: DraftHeroHeadlineProps) {
46 const projects = useProjects();
47 const threads = useThreadShells();
48 const { environments } = useEnvironments();
49 const primaryEnvironmentId = usePrimaryEnvironmentId();
50 const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
51 const projectSortOrder = useClientSettings((settings) => settings.sidebarProjectSortOrder);
52 const setLogicalProjectDraftThreadId = useComposerDraftStore(
53 (store) => store.setLogicalProjectDraftThreadId,
54 );
55 const getComposerDraft = useComposerDraftStore((store) => store.getComposerDraft);
56 const applyStickyState = useComposerDraftStore((store) => store.applyStickyState);
57 const setModelSelection = useComposerDraftStore((store) => store.setModelSelection);
58 const openAddProject = useCallback(() => openCommandPalette({ open: "add-project" }), []);
59​
60 const environmentLabelById = useMemo(
61 () =>
62 new Map(
63 environments.map((environment) => [environment.environmentId, environment.label] as const),
64 ),
65 [environments],
66 );
67 const projectGroups = useMemo(
68 () =>
69 sortLogicalProjectsForSidebar(
70 buildSidebarProjectSnapshots({
71 projects,
72 settings: projectGroupingSettings,
73 primaryEnvironmentId,
74 resolveEnvironmentLabel: (environmentId) =>
75 environmentLabelById.get(environmentId) ?? null,
76 }),
77 threads,
78 projectSortOrder,
79 ),
80 [
81 environmentLabelById,
82 primaryEnvironmentId,
83 projectGroupingSettings,
84 projectSortOrder,
85 projects,
86 threads,
87 ],
88 );
89 // Same-named projects on two machines are only told apart by where they
90 // live, so rows on another machine carry its icon once the catalog spans
91 // more than one environment; a single-machine catalog stays as it was.
92 const showProjectEnvironments = useMemo(
93 () => projectGroupsSpanEnvironments(projectGroups),
94 [projectGroups],
95 );
96 const environmentMachineById = useMemo(
97 () =>
98 new Map(
99 environments.map(
100 (environment) =>
101 [
102 environment.environmentId,
103 resolveEnvironmentMachineKind(environment.serverConfig),
104 ] as const,
105 ),
106 ),
107 [environments],
108 );
109 const projectPickerEntries = useMemo(
110 () =>
111 buildSidebarProjectPickerEntries({
112 groups: projectGroups,
113 preferredProjectRef: activeProjectRef,
114 }),
115 [activeProjectRef, projectGroups],
116 );
117 const projectEntryByKey = useMemo(
118 () => new Map(projectPickerEntries.map((entry) => [entry.group.projectKey, entry] as const)),
119 [projectPickerEntries],
120 );
121 const activeProjectGroup =
122 activeProjectRef === null
123 ? null
124 : (projectGroups.find((group) =>
125 group.memberProjectRefs.some(
126 (projectRef) => scopedProjectKey(projectRef) === scopedProjectKey(activeProjectRef),
127 ),
128 ) ?? null);
129 const activeProjectKey = activeProjectGroup?.projectKey ?? "";
130 const activeProjectDisplayName = activeProjectGroup?.displayName ?? activeProjectTitle;
131 const hasResolvedProject = activeProjectTitle !== null;
132 const canChooseProject = projectPickerEntries.length > 0;
133 const shouldShowProjectMenu = canChooseProject;
134​
135 const projectSelector = shouldShowProjectMenu ? (
136 <Menu>
137 <Tooltip>
138 <TooltipTrigger
139 render={
140 // The trigger's accessible name comes from its visible text (the
141 // project title) so the hero sentence reads naturally: an
142 // aria-label here would replace the title with an action phrase
143 // mid-sentence and baffle screen-reader users.
144 <MenuTrigger
145 render={<InlineButton tone="picker" />}
146 className="pointer-events-auto max-w-64 align-baseline"
147 />
148 }
149 >
150 <span className="min-w-0 truncate">{activeProjectDisplayName ?? "Choose a project"}</span>
151 </TooltipTrigger>
152 {activeProjectDisplayName ? (
153 <TooltipPopup side="top">{activeProjectDisplayName}</TooltipPopup>
154 ) : null}
155 </Tooltip>
156 <MenuPopup align="center" className="max-h-80 overflow-y-auto">
157 <MenuRadioGroup
158 value={activeProjectKey}
159 onValueChange={(value) => {
160 const entry = projectEntryByKey.get(value as string);
161 if (!entry || value === activeProjectKey) {
162 return;
163 }
164 const project = entry.targetProject;
165 if (!draftId) {
166 return;
167 }
168 // Project selection changes the target of the open draft in
169 // place. The prompt stays in the same composer session, so the
170 // sidebar only gets a draft row if the user later navigates away.
171 const currentDraft = getComposerDraft(draftId);
172 setLogicalProjectDraftThreadId(
173 entry.group.projectKey,
174 scopeProjectRef(project.environmentId, project.id),
175 draftId,
176 );
177 if (!hasExplicitComposerModelSelection(currentDraft)) {
178 applyStickyState(draftId);
179 const environmentSettings = environments.find(
180 (environment) => environment.environmentId === project.environmentId,
181 )?.serverConfig?.settings;
182 const defaultModelSelection = environmentSettings
183 ? resolveProjectSettings(environmentSettings, project.id, project).settings
184 .defaultModelSelection
185 : project.defaultModelSelection;
186 if (defaultModelSelection) {
187 setModelSelection(draftId, defaultModelSelection, {
188 replaceOptions: true,
189 });
190 }
191 }
192 }}
193 >
194 {projectPickerEntries.map(({ group }) => {
195 return (
196 <MenuRadioItem key={group.projectKey} value={group.projectKey} closeOnClick>
197 <span className="flex min-w-0 items-center gap-2">
198 <ProjectFavicon project={group} className="size-4 shrink-0" />
199 <Tooltip>
200 <TooltipTrigger render={<span className="block min-w-0 truncate" />}>
201 {group.displayName}
202 </TooltipTrigger>
203 <TooltipPopup side="top">{group.displayName}</TooltipPopup>
204 </Tooltip>
205 {showProjectEnvironments ? (
206 <ProjectEnvironmentBadge
207 group={group}
208 primaryEnvironmentId={primaryEnvironmentId}
209 machineByEnvironmentId={environmentMachineById}
210 />
211 ) : null}
212 </span>
213 </MenuRadioItem>
214 );
215 })}
216 </MenuRadioGroup>
217 <MenuSeparator />
218 <MenuItem onClick={openAddProject}>
219 <FolderPlusIcon />
220 New project
221 </MenuItem>
222 </MenuPopup>
223 </Menu>
224 ) : (
225 <button
226 type="button"
227 onClick={openAddProject}
228 className="pointer-events-auto inline cursor-pointer border-muted-foreground/35 border-b border-dotted text-muted-foreground/60 transition-colors hover:border-muted-foreground/60 hover:text-muted-foreground/80 focus-visible:rounded-sm focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-ring"
229 >
230 {activeProjectTitle ?? "Add a project"}
231 </button>
232 );
233​
234 // The composer hero is a sentence, so the heading's accessible name must be
235 // a complete sentence too. The project picker is a control rendered inline
236 // in the h1; without an explicit label its widget state bleeds into the
237 // announced phrase.
238 const headingLabel = hasResolvedProject
239 ? `What should we build in ${activeProjectDisplayName}?`
240 : canChooseProject
241 ? `${activeProjectDisplayName ?? "Choose a project"} to start`
242 : "Add a project to start";
243​
244 return (
245 <h1
246 aria-label={headingLabel}
247 className="mx-auto w-full max-w-5xl text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl"
248 >
249 {hasResolvedProject ? (
250 <>What should we build in {projectSelector}?</>
251 ) : canChooseProject ? (
252 <>{projectSelector} to start</>
253 ) : (
254 <>Add a project to start</>
255 )}
256 </h1>
257 );