forked from tilt-dev/tilt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OverviewTableDisplayOptions.tsx
122 lines (111 loc) · 3.58 KB
/
OverviewTableDisplayOptions.tsx
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
import { FormControlLabel } from "@material-ui/core"
import React, { useCallback, useMemo } from "react"
import styled from "styled-components"
import { AnalyticsType } from "./analytics"
import { Flag, useFeatures } from "./feature"
import { InstrumentedCheckbox } from "./instrumentedComponents"
import { getResourceLabels, TILTFILE_LABEL, UNLABELED_LABEL } from "./labels"
import { CollapseButton, ExpandButton } from "./resourceListOptionsButtons"
import { useResourceListOptions } from "./ResourceListOptionsContext"
import { resourceIsDisabled } from "./ResourceStatus"
import { Color, Font, FontSize } from "./style-helpers"
import { ResourceName, UIResource } from "./types"
const DisplayOptions = styled.div`
margin-left: auto;
font-family: ${Font.monospace};
font-size: ${FontSize.smallest};
`
const DisplayOptionCheckbox = styled(InstrumentedCheckbox)`
&.MuiCheckbox-root,
&.Mui-checked {
color: ${Color.gray60};
}
`
// Create a list of all the groups from the list of resources.
//
// Sadly, this logic is duplicated across table and sidebar,
// but there's no easy way to consolidate it right now.
function toGroups(
resources: UIResource[],
hideDisabledResources: boolean
): string[] {
let hasUnlabeled = false
let hasTiltfile = false
let hasLabels: { [key: string]: boolean } = {}
resources.forEach((r) => {
const resourceDisabled = resourceIsDisabled(r)
if (hideDisabledResources && resourceDisabled) {
return
}
const labels = getResourceLabels(r)
const isTiltfile = r.metadata?.name === ResourceName.tiltfile
if (labels.length) {
labels.forEach((label) => {
hasLabels[label] = true
})
} else if (isTiltfile) {
hasTiltfile = true
} else {
hasUnlabeled = true
}
})
let groups = Object.keys(hasLabels)
if (groups.length) {
if (hasTiltfile) {
groups.push(TILTFILE_LABEL)
}
if (hasUnlabeled) {
groups.push(UNLABELED_LABEL)
}
}
return groups
}
let analyticsTags = { type: AnalyticsType.Grid }
export function OverviewTableDisplayOptions(props: {
resources?: UIResource[]
}) {
const features = useFeatures()
const { options, setOptions } = useResourceListOptions()
let toggleDisabledResources = useCallback(() => {
setOptions({
showDisabledResources: !options.showDisabledResources,
})
}, [options.showDisabledResources])
const labelsEnabled = features.isEnabled(Flag.Labels)
let resources = props.resources || []
const hideDisabledResources = !options.showDisabledResources
// TODO(nick): Enable/disable the expand/collapse button based
// on whether the groups are shown and the current group state.
let groups = useMemo(
() => toGroups(resources, hideDisabledResources),
[resources, hideDisabledResources]
)
const resourceFilterApplied = options.resourceNameFilter.length > 0
const displayResourceGroups =
labelsEnabled && groups.length && !resourceFilterApplied
return (
<DisplayOptions>
<FormControlLabel
control={
<DisplayOptionCheckbox
analyticsName="ui.web.disabledResourcesToggle"
analyticsTags={analyticsTags}
size="small"
checked={options.showDisabledResources}
onClick={toggleDisabledResources}
/>
}
label="Show disabled resources"
/>
<ExpandButton
disabled={!displayResourceGroups}
analyticsType={AnalyticsType.Grid}
/>
<CollapseButton
groups={groups}
disabled={!displayResourceGroups}
analyticsType={AnalyticsType.Grid}
/>
</DisplayOptions>
)
}