File size: 3,451 Bytes
af1bee9
fb43301
af1bee9
 
 
589f639
af1bee9
 
 
 
 
 
 
 
 
 
 
 
0d811c3
af1bee9
 
0d811c3
af1bee9
 
 
 
 
 
589f639
af1bee9
 
 
 
 
 
 
 
 
 
589f639
 
 
af1bee9
 
589f639
 
 
 
 
af1bee9
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { Button } from '@mantine/core';
import { IconChevronLeft, IconMenu2, IconSearch, IconSparkles } from '@/components/icons';
import type { ReactNode } from 'react';
import { Link } from 'react-router-dom';
import { NotificationBell } from '../../notify/NotificationBell';
import { EnvBadge, envKind, useChosenEnv } from './EnvSwitcher';
import { useShell } from './prefs';

export const isMac = () => typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent);

/** The toolbar of every page: sidebar toggle, back link, title and one-line caption on the left; a state chip, the page's

 * actions and the search field (it opens the command palette, Ctrl K) on the right. Translucent, content scrolls under it.

 * When the sidebar is folded to its icons, the practice-environment cue is repeated here so it never disappears. */
export function TopBar({ title, sub, middle, actions, titleTestId, honesty = true, back, plainTitle = false }: {

  title: ReactNode; sub?: ReactNode; middle?: ReactNode; actions?: ReactNode; titleTestId?: string; honesty?: boolean; back?: { label: string; to: string };

  /** The page has its own large heading: the toolbar shows the title as plain text, not a second heading. */

  plainTitle?: boolean;

}) {
  const { size, sidebarExpanded, openNav, openPalette } = useShell();
  const { pod } = useChosenEnv();
  const drawer = size === 'tablet' || size === 'phone';
  const cue = honesty && drawer && !sidebarExpanded && pod && envKind(pod.kind) === 'mock'
    ? <span className="fpt-topbar-cue"><EnvBadge kind="mock" /></span> : null;
  const phone = size === 'phone';
  return (
    <header className="fpt-topbar pw-topbar" data-size={size}>

      {drawer

        ? <button type="button" className="fpt-iconbtn fpt-topbar-menu" aria-label="Open the menu" onClick={openNav}><IconMenu2 size={17} /></button>

        : null}

      {back && <Link to={back.to} className="fpt-back" aria-label={`Back to ${back.label}`}><IconChevronLeft size={16} stroke={2} aria-hidden />{back.label}</Link>}

      <div className="fpt-topbar-left">

        <div className="fpt-topbar-row1">

          {plainTitle ? <div className="fpt-title" data-testid={titleTestId}>{title}</div> : <h1 className="fpt-title" data-testid={titleTestId}>{title}</h1>}

          {middle && <div className="fpt-topbar-mid">{middle}</div>}

        </div>

        {(sub || cue) && <div className="fpt-topbar-sub">{sub}{sub && cue ? <span aria-hidden className="fpt-topbar-dotsep">·</span> : null}{cue}</div>}

      </div>

      <div className="fpt-topbar-actions">

        {actions}

        {/* wide screens: notifications, search and Ask live in the top navigation */}

        {drawer && <NotificationBell />}

        {drawer && !phone && (

          <Button component={Link} to="/assistant" variant="default" size="xs" className="pw-askai" aria-label="Ask AI" title="Ask AI" leftSection={<IconSparkles size={14} aria-hidden />}>Ask AI</Button>

        )}

        {drawer && (

          <button type="button" className="fpt-search" onClick={openPalette} aria-label={`Search or ask ${isMac() ? '⌘K' : 'Ctrl K'}`} title="Search, go to a page or ask a question">

            <IconSearch size={16} stroke={1.8} aria-hidden /><span>Search or ask</span>{' '}<kbd>{isMac() ? '⌘K' : 'Ctrl K'}</kbd>

          </button>

        )}

      </div>

    </header>
  );
}