File size: 4,024 Bytes
af1bee9
fb43301
af1bee9
 
 
fb43301
af1bee9
 
 
ea59f21
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { Button, Popover, Switch } from '@mantine/core';
import { IconBell } from '@/components/icons';
import { useState } from 'react';
import { Link } from 'react-router-dom';
import { CoworkerAvatar } from '../components/CoworkerAvatar';
import { COWORKERS } from '../domain/coworkers';
import { toPath, useBrowserPref, useNotifications, whenWords, type Notice } from './useNotifications';
import './notify.css';

/** The coworker's face, or the Astra mark for the app itself (and anyone not on the team). */
function Face({ name }: { name: string }) {
  const known = COWORKERS.some((c) => c.name === name);
  if (!known) return <img className="pw-notify-mark" src="/favicon-graphite.svg" alt="" width={28} height={28} />;
  return <CoworkerAvatar name={name} size={28} />;
}

function Item({ n, unread, onPick }: { n: Notice; unread: boolean; onPick: () => void }) {
  const { path, external } = toPath(n.link);
  const inner = (
    <>

      <span className="pw-notify-face" aria-hidden><Face name={n.coworker} /></span>

      <span className="pw-notify-text">

        <span className="pw-notify-title">{unread && <span className="pw-notify-dot" aria-hidden />}{n.title}</span>

        {n.body && <span className="pw-notify-body">{n.body}</span>}

        <span className="pw-notify-meta">{n.coworker} · {whenWords(n.created_at)}{unread && <span className="fpt-sr-only">, unread</span>}</span>

      </span>

    </>
  );
  return (
    <li className="pw-notify-item" data-unread={unread || undefined}>

      {external

        ? <a href={path} target="_blank" rel="noopener noreferrer" onClick={onPick}>{inner}</a>

        : <Link to={path} onClick={onPick}>{inner}</Link>}

    </li>
  );
}

/** The bell in the top bar: what the coworkers want you to know, newest first, with the unread count. */
export function NotificationBell() {
  const [open, setOpen] = useState(false);
  const { items, unread, lastSeen, markAllSeen, error } = useNotifications();
  const browser = useBrowserPref();
  const count = unread.length;
  const shown = items.slice(0, 15);
  return (
    <Popover opened={open} onChange={setOpen} position="bottom-end" width={360} shadow="lg" withinPortal trapFocus returnFocus>

      <Popover.Target>

        <button type="button" className="fpt-iconbtn pw-bell" data-testid="notify-bell" onClick={() => setOpen((v) => !v)}

          aria-label={`Notifications, ${count} unread`}>

          <IconBell size={17} stroke={1.7} aria-hidden />

          {count > 0 && <span className="pw-bell-count" aria-hidden>{count > 9 ? '9+' : count}</span>}

        </button>

      </Popover.Target>

      <Popover.Dropdown className="pw-notify" aria-label="Notifications">

        <div className="pw-notify-head">

          <h2 className="pw-notify-h">Notifications</h2>

          <Button variant="subtle" size="compact-xs" onClick={markAllSeen} disabled={count === 0}>Mark all as read</Button>

        </div>

        {error ? <p className="pw-notify-empty">Notifications could not be loaded. They will try again shortly.</p>

          : shown.length === 0 ? <p className="pw-notify-empty" data-testid="notify-empty">Nothing new. You'll see it here when a coworker needs you.</p>

            : (

              <ul className="pw-notify-list" aria-label="Latest notifications">

                {shown.map((n) => <Item key={n.id} n={n} unread={n.id > lastSeen} onPick={() => setOpen(false)} />)}

              </ul>

            )}

        {browser.supported && (

          <div className="pw-notify-foot">

            <Switch size="xs" label="Tell me in this browser too" checked={browser.on} disabled={browser.blocked}

              onChange={(e) => void browser.setOn(e.currentTarget.checked)} />

            {browser.blocked && <p className="pw-notify-note">Your browser blocks notifications for this site. Allow them in the site settings to turn this on.</p>}

          </div>

        )}

      </Popover.Dropdown>

    </Popover>
  );
}