-
Notifications
You must be signed in to change notification settings - Fork 288
/
SidebarResources.tsx
209 lines (182 loc) · 5.67 KB
/
SidebarResources.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
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
import React from "react"
import styled from "styled-components"
import { OverviewSidebarOptions } from "./OverviewSidebarOptions"
import PathBuilder from "./PathBuilder"
import SidebarItem from "./SidebarItem"
import SidebarItemView, {
SidebarItemAll,
triggerUpdate,
} from "./SidebarItemView"
import SidebarKeyboardShortcuts from "./SidebarKeyboardShortcuts"
import { useSidebarPin } from "./SidebarPin"
import { Color, FontSize, SizeUnit } from "./style-helpers"
import { ResourceView, SidebarOptions } from "./types"
let SidebarResourcesRoot = styled.nav`
flex: 1 0 auto;
&.isOverview {
overflow: auto;
flex-shrink: 1;
}
`
let SidebarList = styled.div``
let SidebarListSectionName = styled.div`
margin-top: ${SizeUnit(0.5)};
margin-left: ${SizeUnit(0.5)};
text-transform: uppercase;
color: ${Color.grayLight};
font-size: ${FontSize.small};
`
const SidebarListSectionItems = styled.ul`
margin-top: ${SizeUnit(0.25)};
list-style: none;
`
export function SidebarListSection(
props: React.PropsWithChildren<{ name: string }>
): JSX.Element {
return (
<div>
<SidebarListSectionName>{props.name}</SidebarListSectionName>
<SidebarListSectionItems>{props.children}</SidebarListSectionItems>
</div>
)
}
type Resource = Proto.webviewResource
type Build = Proto.webviewBuildRecord
type SidebarProps = {
items: SidebarItem[]
selected: string
resourceView: ResourceView
pathBuilder: PathBuilder
}
type SidebarState = SidebarOptions
let defaultOptions: SidebarOptions = {
showResources: true,
showTests: true,
alertsOnTop: false,
}
function PinnedItems(props: SidebarProps) {
let ctx = useSidebarPin()
let pinnedItems = ctx.pinnedResources?.flatMap((r) =>
props.items
.filter((i) => i.name === r)
.map((i) => (
<SidebarItemView
key={"sidebarItemPinned-" + i.name}
item={i}
selected={props.selected === i.name}
pathBuilder={props.pathBuilder}
resourceView={props.resourceView}
/>
))
)
if (!pinnedItems?.length) {
return null
}
return <SidebarListSection name="Pinned">{pinnedItems}</SidebarListSection>
}
function hasAlerts(item: SidebarItem): boolean {
return item.buildAlertCount > 0 || item.runtimeAlertCount > 0
}
function sortByHasAlerts(itemA: SidebarItem, itemB: SidebarItem): number {
return Number(hasAlerts(itemB)) - Number(hasAlerts(itemA))
}
export class SidebarResources extends React.Component<
SidebarProps,
SidebarState
> {
constructor(props: SidebarProps) {
super(props)
this.triggerSelected = this.triggerSelected.bind(this)
this.toggleShowResources = this.toggleShowResources.bind(this)
this.toggleShowTests = this.toggleShowTests.bind(this)
this.toggleAlertsOnTop = this.toggleAlertsOnTop.bind(this)
this.state = defaultOptions
}
triggerSelected(action: string) {
if (this.props.selected) {
triggerUpdate(this.props.selected, action)
}
}
toggleShowResources() {
this.setState((prevState: SidebarOptions) => {
return {
showResources: !prevState.showResources,
}
})
}
toggleShowTests() {
this.setState((prevState: SidebarOptions) => {
return {
showTests: !prevState.showTests,
}
})
}
toggleAlertsOnTop() {
this.setState((prevState: SidebarOptions) => {
return {
alertsOnTop: !prevState.alertsOnTop,
}
})
}
render() {
let pb = this.props.pathBuilder
let totalAlerts = this.props.items // Open Q: do we include alert totals for hidden elems?
.map((i) => i.buildAlertCount + i.runtimeAlertCount)
.reduce((sum, current) => sum + current, 0)
let testsPresent = this.props.items.some((item) => item.isTest)
// TODO: what do we do when we filter out the selected item? Pinned item(s)?
// and what effect does this have on keyboard shortcuts? :(
let filteredItems = this.props.items.filter(
(item) =>
(!item.isTest && this.state.showResources) ||
(item.isTest && this.state.showTests) ||
item.isTiltfile
)
if (this.state.alertsOnTop) {
filteredItems.sort(sortByHasAlerts)
}
let listItems = filteredItems.map((item) => (
<SidebarItemView
key={"sidebarItem-" + item.name}
item={item}
selected={this.props.selected == item.name}
pathBuilder={this.props.pathBuilder}
resourceView={this.props.resourceView}
/>
))
let nothingSelected = !this.props.selected
let isOverviewClass =
this.props.resourceView === ResourceView.OverviewDetail
? "isOverview"
: ""
return (
<SidebarResourcesRoot className={`Sidebar-resources ${isOverviewClass}`}>
<SidebarList>
<SidebarListSection name="">
<SidebarItemAll
nothingSelected={nothingSelected}
totalAlerts={totalAlerts}
/>
</SidebarListSection>
<PinnedItems {...this.props} />
{testsPresent && (
<OverviewSidebarOptions
curState={this.state}
toggleShowResources={this.toggleShowResources}
toggleShowTests={this.toggleShowTests}
toggleAlertsOnTop={this.toggleAlertsOnTop}
/> // TODO: if this vanishes because no tests present, reset it to show everything
)}
<SidebarListSection name="resources">{listItems}</SidebarListSection>
</SidebarList>
<SidebarKeyboardShortcuts
selected={this.props.selected}
items={filteredItems}
onTrigger={this.triggerSelected}
resourceView={this.props.resourceView}
/>
</SidebarResourcesRoot>
)
}
}
export default SidebarResources