aboutsummaryrefslogtreecommitdiffstats
path: root/webui/src/pages/bug/EditHistoryMenu.tsx
blob: da2ed0cd3b8722ff9894515f3a5310abbebc97b3 (plain) (blame)
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
import React from 'react';

import CircularProgress from '@material-ui/core/CircularProgress';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';

import Date from 'src/components/Date';

import { AddCommentFragment } from './MessageCommentFragment.generated';
import { CreateFragment } from './MessageCreateFragment.generated';
import { useMessageEditHistoryQuery } from './MessageEditHistory.generated';

const ITEM_HEIGHT = 48;

type Props = {
  anchor: null | HTMLElement;
  bugId: string;
  commentId: string;
  onClose: () => void;
};
function EditHistoryMenu({ anchor, bugId, commentId, onClose }: Props) {
  const open = Boolean(anchor);

  const { loading, error, data } = useMessageEditHistoryQuery({
    variables: { bugIdPrefix: bugId },
  });
  if (loading) return <CircularProgress />;
  if (error) return <p>Error: {error}</p>;

  const comments = data?.repository?.bug?.timeline.comments as (
    | AddCommentFragment
    | CreateFragment
  )[];
  // NOTE Searching for the changed comment could be dropped if GraphQL get
  // filter by id argument for timelineitems
  const comment = comments.find((elem) => elem.id === commentId);
  const history = comment?.history;

  return (
    <div>
      <Menu
        id="long-menu"
        anchorEl={anchor}
        keepMounted
        open={open}
        onClose={onClose}
        PaperProps={{
          style: {
            maxHeight: ITEM_HEIGHT * 4.5,
            width: '20ch',
          },
        }}
      >
        <MenuItem key={0} disabled>
          Edited {history?.length} times.
        </MenuItem>
        {history?.map((edit, index) => (
          <MenuItem key={index} onClick={onClose}>
            <Date date={edit.date} />
          </MenuItem>
        ))}
      </Menu>
    </div>
  );
}

export default EditHistoryMenu;