|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useState } from 'react' |
| 4 | +import { chipVariants, cn, OverflowText } from '@sim/emcn' |
| 5 | +import { formatRelativeTime } from '@sim/utils/formatting' |
| 6 | +import Link from 'next/link' |
| 7 | +import { useQueryStates } from 'nuqs' |
| 8 | +import { organizationRoutes } from '@/lib/navigation/paths' |
| 9 | +import { ProjectRow } from '@/app/o/[organizationId]/components/organization-sidebar/components/projects-section/project-row' |
| 10 | +import { |
| 11 | + type ProjectDragProps, |
| 12 | + useProjectOrder, |
| 13 | +} from '@/app/o/[organizationId]/components/organization-sidebar/components/projects-section/use-project-order' |
| 14 | +import { type Project, useProjects } from '@/app/o/[organizationId]/p/hooks/use-projects' |
| 15 | +import { projectParsers } from '@/app/o/[organizationId]/p/search-params' |
| 16 | +import { SidebarSection } from '@/app/workspace/[workspaceId]/w/components/sidebar/components' |
| 17 | +import { SIDEBAR_ITEM_GAP_CLASS } from '@/app/workspace/[workspaceId]/w/components/sidebar/constants' |
| 18 | +import { useMothershipChats } from '@/hooks/queries/mothership-chats' |
| 19 | + |
| 20 | +/** Chats listed under an open project before "Show more". */ |
| 21 | +const CHAT_PREVIEW = 3 |
| 22 | + |
| 23 | +interface ProjectsSectionProps { |
| 24 | + organizationId: string |
| 25 | + isCollapsed: boolean |
| 26 | + pathname: string | null |
| 27 | +} |
| 28 | + |
| 29 | +/** The organization's projects, one per fork lineage, each with its chats underneath. */ |
| 30 | +export function ProjectsSection({ organizationId, isCollapsed, pathname }: ProjectsSectionProps) { |
| 31 | + const { roots } = useProjects(organizationId) |
| 32 | + const { projects, dragProps, isAnyDragActive } = useProjectOrder(roots) |
| 33 | + const [{ chat: openChat }] = useQueryStates(projectParsers) |
| 34 | + return ( |
| 35 | + <SidebarSection title='Projects' railCollapsed={isCollapsed} className='shrink-0'> |
| 36 | + <div className={cn(SIDEBAR_ITEM_GAP_CLASS, 'flex flex-col px-2')}> |
| 37 | + {projects.map((project) => ( |
| 38 | + <ProjectTree |
| 39 | + key={project.id} |
| 40 | + organizationId={organizationId} |
| 41 | + project={project} |
| 42 | + pathname={pathname} |
| 43 | + openChat={openChat} |
| 44 | + railCollapsed={isCollapsed} |
| 45 | + drag={dragProps(project)} |
| 46 | + isAnyDragActive={isAnyDragActive} |
| 47 | + /> |
| 48 | + ))} |
| 49 | + </div> |
| 50 | + </SidebarSection> |
| 51 | + ) |
| 52 | +} |
| 53 | + |
| 54 | +interface ProjectTreeProps { |
| 55 | + organizationId: string |
| 56 | + project: Project |
| 57 | + pathname: string | null |
| 58 | + openChat: string |
| 59 | + railCollapsed: boolean |
| 60 | + drag: ProjectDragProps |
| 61 | + isAnyDragActive: boolean |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * A project row with its chats listed underneath. A project spans every environment in its |
| 66 | + * lineage; the chats shown belong to the environment you are in. |
| 67 | + */ |
| 68 | +function ProjectTree({ |
| 69 | + organizationId, |
| 70 | + project, |
| 71 | + pathname, |
| 72 | + openChat, |
| 73 | + railCollapsed, |
| 74 | + drag, |
| 75 | + isAnyDragActive, |
| 76 | +}: ProjectTreeProps) { |
| 77 | + const routes = organizationRoutes(organizationId) |
| 78 | + const [showAll, setShowAll] = useState(false) |
| 79 | + const activeId = |
| 80 | + project.environments.find((environment) => { |
| 81 | + const base = `${routes.root}/p/${environment.workspaceId}` |
| 82 | + return pathname === base || pathname?.startsWith(`${base}/`) |
| 83 | + })?.workspaceId ?? null |
| 84 | + const inProject = activeId !== null |
| 85 | + /** null follows navigation (open while you're in the project); a click pins it open or shut. */ |
| 86 | + const [override, setOverride] = useState<boolean | null>(null) |
| 87 | + const expanded = override ?? inProject |
| 88 | + const { data: chats = [] } = useMothershipChats(activeId ?? project.id) |
| 89 | + const visible = showAll ? chats : chats.slice(0, CHAT_PREVIEW) |
| 90 | + const panelHref = (chatId: string) => |
| 91 | + `${inProject && pathname ? pathname : routes.project(project.id)}?chat=${chatId}` |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className='flex flex-col gap-[1px]'> |
| 95 | + <ProjectRow |
| 96 | + project={project} |
| 97 | + href={routes.project(project.id)} |
| 98 | + newChatHref={panelHref('new')} |
| 99 | + active={inProject} |
| 100 | + expanded={chats.length > 0 ? expanded : undefined} |
| 101 | + railCollapsed={railCollapsed} |
| 102 | + drag={drag} |
| 103 | + isAnyDragActive={isAnyDragActive} |
| 104 | + onToggleExpand={() => setOverride(inProject ? !expanded : true)} |
| 105 | + /> |
| 106 | + {!railCollapsed && expanded && chats.length > 0 && ( |
| 107 | + <div className='flex flex-col gap-[1px] pl-[22px]'> |
| 108 | + {visible.map((chat) => ( |
| 109 | + <Link |
| 110 | + key={chat.id} |
| 111 | + href={panelHref(chat.id)} |
| 112 | + className={cn( |
| 113 | + chipVariants({ active: openChat === chat.id && inProject, fullWidth: true }), |
| 114 | + 'h-[26px] gap-2' |
| 115 | + )} |
| 116 | + > |
| 117 | + <OverflowText |
| 118 | + label={chat.name} |
| 119 | + className='sidebar-collapse-hide flex-1 text-[var(--text-body)] text-small' |
| 120 | + focusTarget='nearest-interactive' |
| 121 | + /> |
| 122 | + {chat.isActive ? ( |
| 123 | + <span |
| 124 | + role='img' |
| 125 | + aria-label='Running' |
| 126 | + className='inline-flex size-[14px] shrink-0 items-center justify-center' |
| 127 | + > |
| 128 | + <span className='size-[7px] rounded-full bg-[var(--caution)]' /> |
| 129 | + </span> |
| 130 | + ) : ( |
| 131 | + <span className='shrink-0 text-[var(--text-muted)] text-caption'> |
| 132 | + {formatRelativeTime(chat.updatedAt.toISOString()) |
| 133 | + .replace(' ago', '') |
| 134 | + .replace('just now', 'now')} |
| 135 | + </span> |
| 136 | + )} |
| 137 | + </Link> |
| 138 | + ))} |
| 139 | + {chats.length > CHAT_PREVIEW && ( |
| 140 | + <button |
| 141 | + type='button' |
| 142 | + onClick={() => setShowAll((prev) => !prev)} |
| 143 | + className={cn( |
| 144 | + chipVariants({ fullWidth: true }), |
| 145 | + 'h-[26px] text-[var(--text-muted)] text-small' |
| 146 | + )} |
| 147 | + > |
| 148 | + {showAll ? 'Show less' : 'Show more'} |
| 149 | + </button> |
| 150 | + )} |
| 151 | + </div> |
| 152 | + )} |
| 153 | + </div> |
| 154 | + ) |
| 155 | +} |
0 commit comments