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

DraftHeroHeadline.tsx

208 lines7.8 KB

1import type { DraftId } from "~/composerDraftStore";
2import { useComposerDraftStore } from "~/composerDraftStore";
3import 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} from "~/sidebarProjectGrouping";
16import { useProjects, useThreadShells } from "~/state/entities";
17import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments";
18import { sortLogicalProjectsForSidebar } from "../Sidebar.logic";
19import {
20 Menu,
21 MenuItem,
22 MenuPopup,
23 MenuRadioGroup,
24 MenuRadioItem,
25 MenuSeparator,
26 MenuTrigger,
27} from "../ui/menu";
28import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
29​
30interface DraftHeroHeadlineProps {
31 readonly draftId: DraftId | null;
32 readonly activeProjectRef: ScopedProjectRef | null;
33 readonly activeProjectTitle: string | null;
35​
36export function DraftHeroHeadline({
37 draftId,
38 activeProjectRef,
39 activeProjectTitle,
40}: DraftHeroHeadlineProps) {
41 const projects = useProjects();
42 const threads = useThreadShells();
43 const { environments } = useEnvironments();
44 const primaryEnvironmentId = usePrimaryEnvironmentId();
45 const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
46 const projectSortOrder = useClientSettings((settings) => settings.sidebarProjectSortOrder);
47 const setLogicalProjectDraftThreadId = useComposerDraftStore(
48 (store) => store.setLogicalProjectDraftThreadId,
49 );
50 const getComposerDraft = useComposerDraftStore((store) => store.getComposerDraft);
51 const applyStickyState = useComposerDraftStore((store) => store.applyStickyState);
52 const setModelSelection = useComposerDraftStore((store) => store.setModelSelection);
53 const openAddProject = useCallback(() => openCommandPalette({ open: "add-project" }), []);
54​
55 const environmentLabelById = useMemo(
56 () =>
57 new Map(
58 environments.map((environment) => [environment.environmentId, environment.label] as const),
59 ),
60 [environments],
61 );
62 const projectGroups = useMemo(
63 () =>
64 sortLogicalProjectsForSidebar(
65 buildSidebarProjectSnapshots({
66 projects,
67 settings: projectGroupingSettings,
68 primaryEnvironmentId,
69 resolveEnvironmentLabel: (environmentId) =>
70 environmentLabelById.get(environmentId) ?? null,
71 }),
72 threads,
73 projectSortOrder,
74 ),
75 [
76 environmentLabelById,
77 primaryEnvironmentId,
78 projectGroupingSettings,
79 projectSortOrder,
80 projects,
81 threads,
82 ],
83 );
84 const projectPickerEntries = useMemo(
85 () =>
86 buildSidebarProjectPickerEntries({
87 groups: projectGroups,
88 preferredProjectRef: activeProjectRef,
89 }),
90 [activeProjectRef, projectGroups],
91 );
92 const projectEntryByKey = useMemo(
93 () => new Map(projectPickerEntries.map((entry) => [entry.group.projectKey, entry] as const)),
94 [projectPickerEntries],
95 );
96 const activeProjectGroup =
97 activeProjectRef === null
98 ? null
99 : (projectGroups.find((group) =>
100 group.memberProjectRefs.some(
101 (projectRef) => scopedProjectKey(projectRef) === scopedProjectKey(activeProjectRef),
102 ),
103 ) ?? null);
104 const activeProjectKey = activeProjectGroup?.projectKey ?? "";
105 const activeProjectDisplayName = activeProjectGroup?.displayName ?? activeProjectTitle;
106 const hasResolvedProject = activeProjectTitle !== null;
107 const canChooseProject = projectPickerEntries.length > 0;
108 const shouldShowProjectMenu = canChooseProject;
109​
110 const projectSelector = shouldShowProjectMenu ? (
111 <Menu>
112 <Tooltip>
113 <TooltipTrigger
114 render={
115 <MenuTrigger
116 aria-label={hasResolvedProject ? "Change project" : "Choose a project"}
117 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"
118 />
119 }
120 >
121 {activeProjectDisplayName ?? "Choose a project"}
122 </TooltipTrigger>
123 {activeProjectDisplayName ? (
124 <TooltipPopup side="top" className="max-w-80">
125 {activeProjectDisplayName}
126 </TooltipPopup>
127 ) : null}
128 </Tooltip>
129 <MenuPopup align="center" className="max-h-80 min-w-40! w-max max-w-64 overflow-y-auto">
130 <MenuRadioGroup
131 value={activeProjectKey}
132 onValueChange={(value) => {
133 const entry = projectEntryByKey.get(value as string);
134 if (!entry || value === activeProjectKey) {
135 return;
136 }
137 const project = entry.targetProject;
138 if (!draftId) {
139 return;
140 }
141 // Project selection changes the target of the open draft in
142 // place. The prompt stays in the same composer session, so the
143 // sidebar only gets a draft row if the user later navigates away.
144 const currentDraft = getComposerDraft(draftId);
145 setLogicalProjectDraftThreadId(
146 entry.group.projectKey,
147 scopeProjectRef(project.environmentId, project.id),
148 draftId,
149 );
150 if (!hasExplicitComposerModelSelection(currentDraft)) {
151 applyStickyState(draftId);
152 const defaultModelSelection =
153 project.defaultModelSelection ??
154 environments.find(
155 (environment) => environment.environmentId === project.environmentId,
156 )?.serverConfig?.settings.defaultModelSelection;
157 if (defaultModelSelection) {
158 setModelSelection(draftId, defaultModelSelection, {
159 replaceOptions: true,
160 });
161 }
162 }
163 }}
164 >
165 {projectPickerEntries.map(({ group }) => {
166 return (
167 <MenuRadioItem key={group.projectKey} value={group.projectKey} closeOnClick>
168 <Tooltip>
169 <TooltipTrigger render={<span className="block min-w-0 truncate" />}>
170 {group.displayName}
171 </TooltipTrigger>
172 <TooltipPopup side="top" className="max-w-80">
173 {group.displayName}
174 </TooltipPopup>
175 </Tooltip>
176 </MenuRadioItem>
177 );
178 })}
179 </MenuRadioGroup>
180 <MenuSeparator />
181 <MenuItem onClick={openAddProject}>
182 <FolderPlusIcon />
183 New project
184 </MenuItem>
185 </MenuPopup>
186 </Menu>
187 ) : (
188 <button
189 type="button"
190 onClick={openAddProject}
191 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"
192 >
193 {activeProjectTitle ?? "Add a project"}
194 </button>
195 );
196​
197 return (
198 <h1 className="mx-auto w-full max-w-5xl text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl">
199 {hasResolvedProject ? (
200 <>What should we build in {projectSelector}?</>
201 ) : canChooseProject ? (
202 <>{projectSelector} to start</>
203 ) : (
204 <>Add a project to start</>
205 )}
206 </h1>
207 );