aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/src/graph/VisibilityPanel.tsx
blob: 8474b7be7014b580bbfbd2b6de3cbe3d6e7bb7bd (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * SPDX-FileCopyrightText: 2023-2024 The Refinery Authors <https://refinery.tools/>
 *
 * SPDX-License-Identifier: EPL-2.0
 */

import ChevronLeftIcon from '@mui/icons-material/ChevronLeft';
import FilterListIcon from '@mui/icons-material/FilterList';
import LabelIcon from '@mui/icons-material/Label';
import LabelOutlinedIcon from '@mui/icons-material/LabelOutlined';
import SentimentVeryDissatisfiedIcon from '@mui/icons-material/SentimentVeryDissatisfied';
import TuneIcon from '@mui/icons-material/Tune';
import VisibilityOffIcon from '@mui/icons-material/VisibilityOff';
import Badge from '@mui/material/Badge';
import Button from '@mui/material/Button';
import Checkbox from '@mui/material/Checkbox';
import FormControlLabel from '@mui/material/FormControlLabel';
import Switch from '@mui/material/Switch';
import { styled } from '@mui/material/styles';
import { observer } from 'mobx-react-lite';
import { useCallback } from 'react';

import type GraphStore from './GraphStore';
import { isVisibilityAllowed } from './GraphStore';
import RelationName from './RelationName';
import SlideInPanel from './SlideInPanel';

const VisibilityDialogScroll = styled('div', {
  name: 'VisibilityDialog-Scroll',
  shouldForwardProp: (propName) => propName !== 'dialog',
})<{ dialog: boolean }>(({ theme, dialog }) => {
  const overlayOpacity = dialog ? 0.16 : 0.09;
  return {
    display: 'flex',
    flexDirection: 'column',
    height: 'auto',
    overflowX: 'hidden',
    overflowY: 'auto',
    margin: `0 ${theme.spacing(2)}`,
    '& table': {
      // We use flexbox instead of `display: table` to get proper text-overflow
      // behavior for overly long relation names.
      display: 'flex',
      flexDirection: 'column',
    },
    '& thead, & tbody': {
      display: 'flex',
      flexDirection: 'column',
    },
    '& thead': {
      position: 'sticky',
      top: 0,
      zIndex: 999,
      backgroundColor: theme.palette.background.paper,
      ...(theme.palette.mode === 'dark'
        ? {
            // In dark mode, MUI Paper gets a lighter overlay.
            backgroundImage: `linear-gradient(
                rgba(255, 255, 255, ${overlayOpacity}),
                rgba(255, 255, 255, ${overlayOpacity})
              )`,
          }
        : {}),
      '& tr': {
        height: '44px',
      },
    },
    '& tr': {
      display: 'flex',
      flexDirection: 'row',
      maxWidth: '100%',
    },
    '& tbody tr': {
      transition: theme.transitions.create('background', {
        duration: theme.transitions.duration.shortest,
      }),
      '&:hover': {
        background: theme.palette.action.hover,
        '@media (hover: none)': {
          background: 'transparent',
        },
      },
    },
    '& th, & td': {
      display: 'flex',
      flexDirection: 'row',
      alignItems: 'center',
      justifyContent: 'center',
      // Set width in advance, since we can't rely on `display: table-cell`.
      width: '44px',
    },
    '& th:nth-of-type(3), & td:nth-of-type(3)': {
      justifyContent: 'start',
      paddingLeft: theme.spacing(1),
      paddingRight: theme.spacing(2),
      // Only let the last column grow or shrink.
      flexGrow: 1,
      flexShrink: 1,
      // Compute the maximum available space in advance to let the text overflow.
      maxWidth: 'calc(100% - 88px)',
      width: 'min-content',
    },
    '& td:nth-of-type(3)': {
      cursor: 'pointer',
      userSelect: 'none',
      WebkitTapHighlightColor: 'transparent',
    },

    '& thead th, .VisibilityDialog-custom tr:last-child td': {
      borderBottom: `1px solid ${theme.palette.divider}`,
    },
    // Hack to apply `text-overflow`.
    '.VisibilityDialog-nowrap': {
      maxWidth: '100%',
      overflow: 'hidden',
      wordWrap: 'nowrap',
      textOverflow: 'ellipsis',
    },
    '.VisibilityDialog-empty': {
      display: 'flex',
      flexDirection: 'column',
      alignItems: 'center',
      color: theme.palette.text.secondary,
    },
    '.VisibilityDialog-emptyIcon': {
      fontSize: '6rem',
      marginBottom: theme.spacing(1),
    },
  };
});

function VisibilityPanel({
  graph,
  dialog,
}: {
  graph: GraphStore;
  dialog: boolean;
}): JSX.Element {
  const builtinRows: JSX.Element[] = [];
  const rows: JSX.Element[] = [];
  graph.relationMetadata.forEach((metadata, name) => {
    if (!isVisibilityAllowed(metadata, 'must')) {
      return;
    }
    const visibility = graph.getVisibility(name);
    const row = (
      <tr key={metadata.name}>
        <td>
          <Checkbox
            checked={visibility !== 'none'}
            aria-label={`Show true and error values of ${metadata.simpleName}`}
            onClick={() =>
              graph.setVisibility(name, visibility === 'none' ? 'must' : 'none')
            }
          />
        </td>
        <td>
          <Checkbox
            checked={visibility === 'all'}
            disabled={!isVisibilityAllowed(metadata, 'all')}
            aria-label={`Show all values of ${metadata.simpleName}`}
            onClick={() =>
              graph.setVisibility(name, visibility === 'all' ? 'must' : 'all')
            }
          />
        </td>
        <td
          onClick={() => graph.cycleVisibility(name)}
          aria-label="Toggle visiblity"
        >
          <div className="VisibilityDialog-nowrap">
            <RelationName metadata={metadata} abbreviate={graph.abbreviate} />
          </div>
        </td>
      </tr>
    );
    if (name.startsWith('builtin::')) {
      builtinRows.push(row);
    } else {
      rows.push(row);
    }
  });

  const hasRows = rows.length > 0 || builtinRows.length > 0;

  const hideBadge = graph.visibility.size === 0;
  const icon = useCallback(
    (show: boolean) => (
      <Badge color="primary" variant="dot" invisible={hideBadge}>
        {show && !dialog ? <ChevronLeftIcon /> : <TuneIcon />}
      </Badge>
    ),
    [dialog, hideBadge],
  );

  return (
    <SlideInPanel
      anchor="left"
      dialog={dialog}
      title="Customize view"
      icon={icon}
      iconLabel="Filter panel"
      buttons={
        <>
          <Button
            color="inherit"
            onClick={() => graph.hideAll()}
            startIcon={<VisibilityOffIcon />}
          >
            Hide all
          </Button>
          <Button
            color="inherit"
            onClick={() => graph.resetFilter()}
            startIcon={<FilterListIcon />}
          >
            Reset filter
          </Button>
        </>
      }
    >
      <FormControlLabel
        control={
          <Switch
            checked={!graph.abbreviate}
            onClick={() => graph.toggleAbbrevaite()}
          />
        }
        label="Fully qualified names"
      />
      <FormControlLabel
        control={
          <Switch checked={graph.scopes} onClick={() => graph.toggleScopes()} />
        }
        label="Object scopes"
      />
      <VisibilityDialogScroll dialog={dialog}>
        {hasRows ? (
          <table cellSpacing={0}>
            <thead>
              <tr>
                <th aria-label="Show true and error values">
                  <LabelIcon />
                </th>
                <th aria-label="Show unknown values">
                  <LabelOutlinedIcon />
                </th>
                <th>Symbol</th>
              </tr>
            </thead>
            <tbody className="VisibilityDialog-custom">{...rows}</tbody>
            <tbody className="VisibilityDialog-builtin">{...builtinRows}</tbody>
          </table>
        ) : (
          <div className="VisibilityDialog-empty">
            <SentimentVeryDissatisfiedIcon
              className="VisibilityDialog-emptyIcon"
              fontSize="inherit"
            />
            <div>Partial model is empty</div>
          </div>
        )}
      </VisibilityDialogScroll>
    </SlideInPanel>
  );
}

export default observer(VisibilityPanel);