LayoutHeader.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. import { useParams } from 'common'
  2. import dayjs from 'dayjs'
  3. import { AnimatePresence, motion } from 'framer-motion'
  4. import { ChevronLeft } from 'lucide-react'
  5. import Link from 'next/link'
  6. import { useRouter } from 'next/router'
  7. import { ReactNode, useMemo } from 'react'
  8. import { Badge, cn } from 'ui'
  9. import { CommandMenuTriggerInput } from 'ui-patterns'
  10. import { BreadcrumbsView } from './BreadcrumbsView'
  11. import { FeedbackDropdown } from './FeedbackDropdown/FeedbackDropdown'
  12. import { HomeIcon } from './HomeIcon'
  13. import { LocalVersionPopover } from './LocalVersionPopover'
  14. import { MergeRequestButton } from './MergeRequestButton'
  15. import { ConnectButton } from '@/components/interfaces/ConnectButton/ConnectButton'
  16. import { ConnectSheet } from '@/components/interfaces/ConnectSheet/ConnectSheet'
  17. import { LocalDropdown } from '@/components/interfaces/LocalDropdown'
  18. import { UserDropdown } from '@/components/interfaces/UserDropdown'
  19. import { AdvisorButton } from '@/components/layouts/AppLayout/AdvisorButton'
  20. import { AssistantButton } from '@/components/layouts/AppLayout/AssistantButton'
  21. import { BranchDropdown } from '@/components/layouts/AppLayout/BranchDropdown'
  22. import { InlineEditorButton } from '@/components/layouts/AppLayout/InlineEditorButton'
  23. import { OrganizationDropdown } from '@/components/layouts/AppLayout/OrganizationDropdown'
  24. import { ProjectDropdown } from '@/components/layouts/AppLayout/ProjectDropdown'
  25. import { HelpButton } from '@/components/ui/HelpPanel/HelpButton'
  26. import { getResourcesExceededLimitsOrg } from '@/components/ui/OveragesBanner/OveragesBanner.utils'
  27. import { useOrgUsageQuery } from '@/data/usage/org-usage-query'
  28. import { useSelectedOrganizationQuery } from '@/hooks/misc/useSelectedOrganization'
  29. import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
  30. import { IS_PLATFORM } from '@/lib/constants'
  31. import { useTrack } from '@/lib/telemetry/track'
  32. import { SHORTCUT_IDS } from '@/state/shortcuts/registry'
  33. import { useIsShortcutEnabled } from '@/state/shortcuts/useIsShortcutEnabled'
  34. const LayoutHeaderDivider = ({ className, ...props }: React.HTMLProps<HTMLSpanElement>) => (
  35. <span className={cn('text-border-stronger pr-2', className)} {...props}>
  36. <svg
  37. viewBox="0 0 24 24"
  38. width="16"
  39. height="16"
  40. stroke="currentColor"
  41. strokeWidth="1"
  42. strokeLinecap="round"
  43. strokeLinejoin="round"
  44. fill="none"
  45. shapeRendering="geometricPrecision"
  46. aria-hidden={true}
  47. >
  48. <path d="M16 3.549L7.12 20.600" />
  49. </svg>
  50. </span>
  51. )
  52. interface LayoutHeaderProps {
  53. customHeaderComponents?: ReactNode
  54. breadcrumbs?: unknown[]
  55. headerTitle?: string
  56. backToDashboardURL?: string
  57. }
  58. export const LayoutHeader = ({
  59. customHeaderComponents,
  60. breadcrumbs = [],
  61. headerTitle,
  62. backToDashboardURL,
  63. }: LayoutHeaderProps) => {
  64. const router = useRouter()
  65. const { ref: projectRef, slug } = useParams()
  66. const { data: selectedProject } = useSelectedProjectQuery()
  67. const { data: selectedOrganization } = useSelectedOrganizationQuery()
  68. const track = useTrack()
  69. const commandMenuEnabled = useIsShortcutEnabled(SHORTCUT_IDS.COMMAND_MENU_OPEN)
  70. const isAccountPage = router.pathname.startsWith('/account')
  71. // We only want to query the org usage and check for possible over-ages for plans without usage billing enabled (free or pro with spend cap)
  72. const { data: orgUsage } = useOrgUsageQuery(
  73. { orgSlug: selectedOrganization?.slug },
  74. { enabled: selectedOrganization?.usage_billing_enabled === false }
  75. )
  76. const exceedingLimits = useMemo(() => {
  77. if (orgUsage) {
  78. return getResourcesExceededLimitsOrg(orgUsage?.usages || []).length > 0
  79. } else {
  80. return false
  81. }
  82. }, [orgUsage])
  83. const isNewProject =
  84. selectedProject?.inserted_at !== undefined &&
  85. dayjs(selectedProject.inserted_at).isAfter(dayjs().subtract(5, 'day'))
  86. const connectButtonType = isNewProject ? 'primary' : 'default'
  87. // show org selection if we are on a project page or on a explicit org route
  88. const showOrgSelection = slug || (selectedOrganization && projectRef)
  89. return (
  90. <>
  91. <header className="hidden md:flex h-11 md:h-12 items-center shrink-0 border-b">
  92. {backToDashboardURL && isAccountPage && (
  93. <div className="flex items-center justify-center border-r flex-0 md:hidden h-full aspect-square">
  94. <Link
  95. href={backToDashboardURL}
  96. onClick={() => track('header_back_to_dashboard_clicked')}
  97. className="flex items-center justify-center border-none bg-transparent! rounded-md min-w-[30px] w-[30px] h-[30px] text-foreground-lighter hover:text-foreground transition-colors"
  98. >
  99. <ChevronLeft strokeWidth={1.5} size={16} />
  100. </Link>
  101. </div>
  102. )}
  103. <div
  104. className={cn(
  105. 'flex items-center justify-between h-full pr-3 flex-1 overflow-x-auto gap-x-8 pl-4'
  106. )}
  107. >
  108. <div className="flex md:hidden items-center text-sm not-sr-only">
  109. <AnimatePresence>
  110. {headerTitle && (
  111. <motion.div
  112. className="flex items-center -ml-1"
  113. initial={{ opacity: 0, x: -20 }}
  114. animate={{ opacity: 1, x: 0 }}
  115. exit={{ opacity: 0, x: -20 }}
  116. transition={{
  117. duration: 0.15,
  118. ease: 'easeOut',
  119. }}
  120. >
  121. <LayoutHeaderDivider />
  122. <span className="text-foreground">{headerTitle}</span>
  123. </motion.div>
  124. )}
  125. </AnimatePresence>
  126. </div>
  127. <div className="hidden md:flex items-center text-sm">
  128. <HomeIcon />
  129. <div className="flex items-center md:pl-2">
  130. {showOrgSelection && IS_PLATFORM ? (
  131. <>
  132. <LayoutHeaderDivider className="hidden md:block" />
  133. <OrganizationDropdown />
  134. </>
  135. ) : null}
  136. <AnimatePresence>
  137. {projectRef && (
  138. <motion.div
  139. className="flex items-center"
  140. initial={{ opacity: 0, x: -20 }}
  141. animate={{ opacity: 1, x: 0 }}
  142. exit={{ opacity: 0, x: -20 }}
  143. transition={{
  144. duration: 0.15,
  145. ease: 'easeOut',
  146. }}
  147. >
  148. {IS_PLATFORM && <LayoutHeaderDivider />}
  149. <ProjectDropdown />
  150. {exceedingLimits && (
  151. <div className="ml-2">
  152. <Link
  153. href={`/org/${selectedOrganization?.slug}/usage`}
  154. onClick={() => track('header_exceeding_usage_badge_clicked')}
  155. >
  156. <Badge variant="destructive">Exceeding usage limits</Badge>
  157. </Link>
  158. </div>
  159. )}
  160. {selectedProject && IS_PLATFORM && (
  161. <>
  162. <LayoutHeaderDivider />
  163. <BranchDropdown />
  164. </>
  165. )}
  166. </motion.div>
  167. )}
  168. </AnimatePresence>
  169. </div>
  170. <AnimatePresence>
  171. {headerTitle && (
  172. <motion.div
  173. className="flex items-center"
  174. initial={{ opacity: 0, x: -20 }}
  175. animate={{ opacity: 1, x: 0 }}
  176. exit={{ opacity: 0, x: -20 }}
  177. transition={{
  178. duration: 0.15,
  179. ease: 'easeOut',
  180. }}
  181. >
  182. <LayoutHeaderDivider />
  183. <span className="text-foreground">{headerTitle}</span>
  184. </motion.div>
  185. )}
  186. </AnimatePresence>
  187. <AnimatePresence>
  188. {projectRef && (
  189. <motion.div
  190. className="ml-3 items-center gap-x-2 flex"
  191. initial={{ opacity: 0, x: -20 }}
  192. animate={{ opacity: 1, x: 0 }}
  193. exit={{ opacity: 0, x: -20 }}
  194. transition={{
  195. duration: 0.15,
  196. ease: 'easeOut',
  197. }}
  198. >
  199. {IS_PLATFORM && <MergeRequestButton />}
  200. <ConnectButton buttonType={connectButtonType} />
  201. </motion.div>
  202. )}
  203. </AnimatePresence>
  204. <BreadcrumbsView defaultValue={breadcrumbs} />
  205. </div>
  206. <div className="flex items-center gap-x-2">
  207. {customHeaderComponents && customHeaderComponents}
  208. {IS_PLATFORM ? (
  209. <>
  210. <FeedbackDropdown />
  211. <div className="flex items-center gap-1 md:gap-2">
  212. <CommandMenuTriggerInput
  213. showShortcut={commandMenuEnabled}
  214. placeholder="Search..."
  215. className={cn(
  216. 'hidden md:flex md:min-w-32 xl:min-w-32 rounded-full bg-transparent',
  217. '[&_.command-shortcut]:border-none',
  218. '[&_.command-shortcut]:pr-2',
  219. '[&_.command-shortcut]:bg-transparent',
  220. '[&_.command-shortcut]:text-foreground-lighter',
  221. '[&_.command-shortcut]:shadow-none'
  222. )}
  223. />
  224. <HelpButton />
  225. <AdvisorButton projectRef={projectRef} />
  226. <AnimatePresence initial={false}>
  227. {!!projectRef && (
  228. <>
  229. <InlineEditorButton />
  230. <AssistantButton />
  231. </>
  232. )}
  233. </AnimatePresence>
  234. </div>
  235. <UserDropdown triggerClassName="hidden md:flex" />
  236. </>
  237. ) : (
  238. <>
  239. <LocalVersionPopover />
  240. <div className="flex items-center gap-1 md:gap-2">
  241. <CommandMenuTriggerInput
  242. placeholder="Search..."
  243. className="hidden md:flex md:min-w-32 xl:min-w-32 rounded-full bg-transparent
  244. [&_.command-shortcut]:border-none
  245. [&_.command-shortcut]:pr-2
  246. [&_.command-shortcut]:bg-transparent
  247. [&_.command-shortcut]:text-foreground-lighter
  248. [&_.command-shortcut]:shadow-none
  249. "
  250. />
  251. <HelpButton />
  252. <AdvisorButton projectRef={projectRef} />
  253. <AnimatePresence initial={false}>
  254. {!!projectRef && (
  255. <>
  256. <InlineEditorButton />
  257. <AssistantButton />
  258. </>
  259. )}
  260. </AnimatePresence>
  261. </div>
  262. <LocalDropdown triggerClassName="hidden md:flex" />
  263. </>
  264. )}
  265. </div>
  266. </div>
  267. </header>
  268. <ConnectSheet />
  269. </>
  270. )
  271. }