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

DraftHeroHeadline.tsx

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