starling-devex 0.1.2

Starling: a local dev orchestrator with a central daemon, shared named-URL proxy, and a k9s-style TUI (a Rust port of Tilt + portless)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
import React, { ChangeEvent, useCallback, useMemo, useState } from "react"
import { CellProps, Column, HeaderProps, Row } from "react-table"
import TimeAgo from "react-timeago"
import styled from "styled-components"
import { ApiButton, ApiIcon, ButtonSet } from "./ApiButton"
import { ReactComponent as CheckmarkSvg } from "./assets/svg/checkmark.svg"
import { ReactComponent as CopySvg } from "./assets/svg/copy.svg"
import { ReactComponent as LinkSvg } from "./assets/svg/link.svg"
import { ReactComponent as StarSvg } from "./assets/svg/star.svg"
import { linkToTiltDocs, TiltDocsPage } from "./constants"
import { Hold } from "./Hold"
import {
  InstrumentedButton,
  InstrumentedCheckbox,
} from "./instrumentedComponents"
import { displayURL, resolveURL } from "./links"
import { OverviewButtonMixin } from "./OverviewButton"
import { OverviewTableBuildButton } from "./OverviewTableBuildButton"
import OverviewTableStarResourceButton from "./OverviewTableStarResourceButton"
import OverviewTableStatus from "./OverviewTableStatus"
import OverviewTableTriggerModeToggle from "./OverviewTableTriggerModeToggle"
import { useResourceNav } from "./ResourceNav"
import { useResourceSelection } from "./ResourceSelectionContext"
import { disabledResourceStyleMixin } from "./ResourceStatus"
import { useStarredResources } from "./StarredResourcesContext"
import {
  Color,
  FontSize,
  mixinResetButtonStyle,
  SizeUnit,
} from "./style-helpers"
import { timeAgoFormatter } from "./timeFormatters"
import TiltTooltip, { TiltInfoTooltip } from "./Tooltip"
import { startBuild } from "./trigger"
import { ResourceStatus, TriggerMode, UIButton, UILink } from "./types"

/**
 * Types
 */
type OverviewTableBuildButtonStatus = {
  isBuilding: boolean
  hasBuilt: boolean
  hasPendingChanges: boolean
  isQueued: boolean
}

type OverviewTableResourceStatus = {
  buildStatus: ResourceStatus
  buildAlertCount: number
  lastBuildDur: moment.Duration | null
  runtimeStatus: ResourceStatus
  runtimeAlertCount: number
  hold?: Hold | null
}

export type RowValues = {
  lastDeployTime: string
  trigger: OverviewTableBuildButtonStatus
  name: string
  resourceTypeLabel: string
  statusLine: OverviewTableResourceStatus
  podId: string
  endpoints: UILink[]
  mode: TriggerMode
  buttons: ButtonSet
  selectable: boolean
}

/**
 * Styles
 */

export const SelectionCheckbox = styled(InstrumentedCheckbox)`
  &.MuiCheckbox-root,
  &.Mui-checked {
    color: ${Color.gray60};
  }

  &.Mui-disabled {
    opacity: 0.25;
    cursor: not-allowed;
  }
`

const TableHeaderStarIcon = styled(StarSvg)`
  fill: ${Color.gray70};
  height: 13px;
  width: 13px;
`

export const Name = styled.button`
  ${mixinResetButtonStyle};
  color: ${Color.offWhite};
  font-size: ${FontSize.small};
  padding-top: ${SizeUnit(1 / 3)};
  padding-bottom: ${SizeUnit(1 / 3)};
  text-align: left;
  cursor: pointer;

  &:hover {
    text-decoration: underline;
    text-underline-position: under;
  }

  &.has-error {
    color: ${Color.red};
  }

  &.isDisabled {
    ${disabledResourceStyleMixin}
    color: ${Color.gray60};
  }
`

const Endpoint = styled.a`
  display: flex;
  align-items: center;
  max-width: 150px;
`
const DetailText = styled.div`
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
`

const StyledLinkSvg = styled(LinkSvg)`
  fill: ${Color.gray50};
  flex-shrink: 0;
  margin-right: ${SizeUnit(0.2)};
`

const PodId = styled.div`
  display: flex;
  align-items: center;
`
const PodIdInput = styled.input`
  background-color: transparent;
  color: ${Color.gray60};
  font-family: inherit;
  font-size: inherit;
  border: 1px solid ${Color.gray10};
  border-radius: 2px;
  padding: ${SizeUnit(0.1)} ${SizeUnit(0.2)};
  width: 100px;
  text-overflow: ellipsis;
  overflow: auto;

  &::selection {
    background-color: ${Color.gray30};
  }
`
const PodIdCopy = styled(InstrumentedButton)`
  ${mixinResetButtonStyle};
  padding-top: ${SizeUnit(0.5)};
  padding: ${SizeUnit(0.25)};
  flex-shrink: 0;

  svg {
    fill: ${Color.gray60};
  }
`
const CustomActionButton = styled(ApiButton)`
  button {
    ${OverviewButtonMixin};
  }
`
const WidgetCell = styled.span`
  display: flex;
  flex-wrap: wrap;
  max-width: ${SizeUnit(8)};

  .MuiButtonGroup-root {
    margin-bottom: ${SizeUnit(0.125)};
    margin-right: ${SizeUnit(0.125)};
    margin-top: ${SizeUnit(0.125)};
  }
`

/**
 * Table data helpers
 */

export function rowIsDisabled(row: Row<RowValues>): boolean {
  // If a resource is disabled, both runtime and build statuses should
  // be `disabled` and it won't matter which one we look at
  return row.original.statusLine.runtimeStatus === ResourceStatus.Disabled
}

async function copyTextToClipboard(text: string, cb: () => void) {
  await navigator.clipboard.writeText(text)
  cb()
}

function statusSortKey(row: RowValues): string {
  const status = row.statusLine
  let order
  if (
    status.buildStatus == ResourceStatus.Unhealthy ||
    status.runtimeStatus === ResourceStatus.Unhealthy
  ) {
    order = 0
  } else if (status.buildAlertCount || status.runtimeAlertCount) {
    order = 1
  } else if (
    status.runtimeStatus === ResourceStatus.Disabled ||
    status.buildStatus === ResourceStatus.Disabled
  ) {
    // Disabled resources should appear last
    order = 3
  } else {
    order = 2
  }
  // add name after order just to keep things stable when orders are equal
  return `${order}${row.name}`
}

/**
 * Header components
 */
export function ResourceSelectionHeader({
  rows,
  column,
}: HeaderProps<RowValues>) {
  const { selected, isSelected, select, deselect } = useResourceSelection()

  const selectableResourcesInTable = useMemo(() => {
    const resources: string[] = []
    rows.forEach(({ original }) => {
      if (original.selectable) {
        resources.push(original.name)
      }
    })

    return resources
  }, [rows])

  function getSelectionState(resourcesInTable: string[]): {
    indeterminate: boolean
    checked: boolean
  } {
    let anySelected = false
    let anyUnselected = false
    for (let i = 0; i < resourcesInTable.length; i++) {
      if (isSelected(resourcesInTable[i])) {
        anySelected = true
      } else {
        anyUnselected = true
      }

      if (anySelected && anyUnselected) {
        break
      }
    }

    return {
      indeterminate: anySelected && anyUnselected,
      checked: !anyUnselected,
    }
  }

  const { indeterminate, checked } = useMemo(
    () => getSelectionState(selectableResourcesInTable),
    [selectableResourcesInTable, selected]
  )

  // If no resources in the table are selectable, don't render
  if (selectableResourcesInTable.length === 0) {
    return null
  }

  const onChange = (_e: ChangeEvent<HTMLInputElement>) => {
    if (!checked) {
      select(...selectableResourcesInTable)
    } else {
      deselect(...selectableResourcesInTable)
    }
  }

  return (
    <SelectionCheckbox
      aria-label="Resource group selection"
      checked={checked}
      aria-checked={checked}
      indeterminate={indeterminate}
      onChange={onChange}
      size="small"
    />
  )
}

/**
 * Column components
 */
export function TableStarColumn({ row }: CellProps<RowValues>) {
  let ctx = useStarredResources()
  return (
    <OverviewTableStarResourceButton resourceName={row.values.name} ctx={ctx} />
  )
}

export function TableUpdateColumn({ row }: CellProps<RowValues>) {
  if (!row.values.lastDeployTime) {
    return null
  }
  return (
    <TimeAgo date={row.values.lastDeployTime} formatter={timeAgoFormatter} />
  )
}

export function TableSelectionColumn({ row }: CellProps<RowValues>) {
  const selections = useResourceSelection()
  const resourceName = row.original.name
  const checked = selections.isSelected(resourceName)

  const onChange = useCallback(
    (_e: ChangeEvent<HTMLInputElement>) => {
      if (!checked) {
        selections.select(resourceName)
      } else {
        selections.deselect(resourceName)
      }
    },
    [checked, selections]
  )

  let disabled = !row.original.selectable
  let label = row.original.selectable
    ? "Select resource"
    : "Cannot select resource"

  return (
    <SelectionCheckbox
      checked={checked}
      aria-checked={checked}
      onChange={onChange}
      size="small"
      disabled={disabled}
      aria-label={label}
    />
  )
}

let TableBuildButtonColumnRoot = styled.div`
  display: flex;
  align-items: center;
`

export function TableBuildButtonColumn({ row }: CellProps<RowValues>) {
  // If resource is disabled, don't display build button
  if (rowIsDisabled(row)) {
    return null
  }

  const trigger = row.original.trigger
  let onStartBuild = useCallback(
    () => startBuild(row.values.name),
    [row.values.name]
  )
  return (
    <TableBuildButtonColumnRoot>
      <OverviewTableBuildButton
        hasPendingChanges={trigger.hasPendingChanges}
        hasBuilt={trigger.hasBuilt}
        isBuilding={trigger.isBuilding}
        triggerMode={row.values.mode}
        isQueued={trigger.isQueued}
        onStartBuild={onStartBuild}
        stopBuildButton={row.original.buttons.stopBuild}
      />
    </TableBuildButtonColumnRoot>
  )
}

export function TableNameColumn({ row }: CellProps<RowValues>) {
  let nav = useResourceNav()
  let hasError =
    row.original.statusLine.buildStatus === ResourceStatus.Unhealthy ||
    row.original.statusLine.runtimeStatus === ResourceStatus.Unhealthy
  const errorClass = hasError ? "has-error" : ""
  const disabledClass = rowIsDisabled(row) ? "isDisabled" : ""
  return (
    <Name
      className={`${errorClass} ${disabledClass}`}
      onClick={(e) => nav.openResource(row.values.name)}
    >
      {row.values.name}
    </Name>
  )
}

let TableStatusColumnRoot = styled.div`
  display: flex;
  flex-direction: column;
  align-items: start;
  justify-content: space-around;
  min-height: 4em;
`

export function TableStatusColumn({ row }: CellProps<RowValues>) {
  const status = row.original.statusLine
  const runtimeStatus = (
    <OverviewTableStatus
      status={status.runtimeStatus}
      resourceName={row.values.name}
    />
  )

  // If a resource is disabled, only one status needs to be displayed
  if (rowIsDisabled(row)) {
    return <TableStatusColumnRoot>{runtimeStatus}</TableStatusColumnRoot>
  }

  return (
    <TableStatusColumnRoot>
      <OverviewTableStatus
        status={status.buildStatus}
        lastBuildDur={status.lastBuildDur}
        isBuild={true}
        resourceName={row.values.name}
        hold={status.hold}
      />
      {runtimeStatus}
    </TableStatusColumnRoot>
  )
}

export function TablePodIDColumn({ row }: CellProps<RowValues>) {
  let [showCopySuccess, setShowCopySuccess] = useState(false)

  let copyClick = () => {
    copyTextToClipboard(row.values.podId, () => {
      setShowCopySuccess(true)

      setTimeout(() => {
        setShowCopySuccess(false)
      }, 3000)
    })
  }

  // If resource is disabled, don't display pod information
  if (rowIsDisabled(row)) {
    return null
  }

  let icon = showCopySuccess ? (
    <CheckmarkSvg width="15" height="15" />
  ) : (
    <CopySvg width="15" height="15" />
  )

  function selectPodIdInput() {
    const input = document.getElementById(
      `pod-${row.values.podId}`
    ) as HTMLInputElement
    input && input.select()
  }

  if (!row.values.podId) return null
  return (
    <PodId>
      <PodIdInput
        id={`pod-${row.values.podId}`}
        value={row.values.podId}
        readOnly={true}
        onClick={() => selectPodIdInput()}
      />
      <PodIdCopy onClick={copyClick} title="Copy Pod ID">
        {icon}
      </PodIdCopy>
    </PodId>
  )
}

export function TableEndpointColumn({ row }: CellProps<RowValues>) {
  // If a resource is disabled, don't display any endpoints
  if (rowIsDisabled(row)) {
    return null
  }

  let endpoints = row.original.endpoints.map((ep: any) => {
    let url = resolveURL(ep.url || "")
    return (
      <Endpoint
        href={url}
        // We use ep.url as the target, so that clicking the link re-uses the tab.
        target={url}
        key={url}
      >
        <StyledLinkSvg />
        <DetailText title={ep.name || displayURL(url)}>
          {ep.name || displayURL(url)}
        </DetailText>
      </Endpoint>
    )
  })
  return <>{endpoints}</>
}

export function TableTriggerModeColumn({ row }: CellProps<RowValues>) {
  let isTiltfile = row.values.name == "(Tiltfile)"
  const isDisabled = rowIsDisabled(row)

  if (isTiltfile || isDisabled) return null
  return (
    <OverviewTableTriggerModeToggle
      resourceName={row.values.name}
      triggerMode={row.values.mode}
    />
  )
}

export function TableWidgetsColumn({ row }: CellProps<RowValues>) {
  // If a resource is disabled, don't display any buttons
  if (rowIsDisabled(row)) {
    return null
  }

  const buttons = row.original.buttons.default.map((b: UIButton) => {
    let content = (
      <CustomActionButton key={b.metadata?.name} uiButton={b}>
        <ApiIcon
          iconName={b.spec?.iconName || "smart_button"}
          iconSVG={b.spec?.iconSVG}
        />
      </CustomActionButton>
    )

    if (b.spec?.text) {
      content = (
        <TiltTooltip title={b.spec.text}>
          <span>{content}</span>
        </TiltTooltip>
      )
    }

    return (
      <React.Fragment key={b.metadata?.name || ""}>{content}</React.Fragment>
    )
  })
  return <WidgetCell>{buttons}</WidgetCell>
}

/**
 * Column tooltips
 */
const modeColumn: Column<RowValues> = {
  Header: "Mode",
  id: "mode",
  accessor: "mode",
  Cell: TableTriggerModeColumn,
  width: "auto",
}

const widgetsColumn: Column<RowValues> = {
  Header: "Widgets",
  id: "widgets",
  accessor: (row: any) => row.buttons.default.length,
  Cell: TableWidgetsColumn,
  width: "auto",
}

const columnNameToInfoTooltip: {
  [key: string]: NonNullable<React.ReactNode>
} = {
  [modeColumn.id as string]: (
    <>
      Trigger mode can be toggled through the UI. To set it persistently, see{" "}
      <a
        href={linkToTiltDocs(TiltDocsPage.TriggerMode)}
        target="_blank"
        rel="noopener noreferrer"
      >
        Tiltfile docs
      </a>
      .
    </>
  ),
  [widgetsColumn.id as string]: (
    <>
      Buttons can be added to resources to easily perform custom actions. See{" "}
      <a
        href={linkToTiltDocs(TiltDocsPage.CustomButtons)}
        target="_blank"
        rel="noopener noreferrer"
      >
        buttons docs
      </a>
      .
    </>
  ),
}

export function ResourceTableHeaderTip(props: { id?: string }) {
  if (!props.id) {
    return null
  }

  const tooltipContent = columnNameToInfoTooltip[props.id]
  if (!tooltipContent) {
    return null
  }

  return (
    <TiltInfoTooltip
      title={tooltipContent}
      dismissId={`table-header-${props.id}`}
    />
  )
}

// https://react-table.tanstack.com/docs/api/useTable#column-options
// The docs on this are not very clear!
// `accessor` should return a primitive, and that primitive is used for sorting and filtering
// the Cell function can get whatever it needs to render via row.original
// best evidence I've (Matt) found: https://github.com/tannerlinsley/react-table/discussions/2429#discussioncomment-25582
//   (from the author)
export const COLUMNS: Column<RowValues>[] = [
  {
    Header: (props) => <ResourceSelectionHeader {...props} />,
    id: "selection",
    disableSortBy: true,
    width: "70px",
    Cell: TableSelectionColumn,
  },
  {
    Header: () => <TableHeaderStarIcon title="Starred" />,
    id: "starred",
    disableSortBy: true,
    width: "40px",
    Cell: TableStarColumn,
  },
  {
    Header: "Updated",
    accessor: "lastDeployTime",
    width: "100px",
    Cell: TableUpdateColumn,
  },
  {
    Header: "Trigger",
    accessor: "trigger",
    disableSortBy: true,
    Cell: TableBuildButtonColumn,
    width: "80px",
  },
  {
    Header: "Resource Name",
    accessor: "name",
    Cell: TableNameColumn,
    width: "400px",
  },
  {
    Header: "Type",
    accessor: "resourceTypeLabel",
    width: "auto",
  },
  {
    Header: "Status",
    accessor: (row) => statusSortKey(row),
    Cell: TableStatusColumn,
    width: "auto",
  },
  {
    Header: "Pod ID",
    accessor: "podId",
    width: "auto",
    Cell: TablePodIDColumn,
  },
  widgetsColumn,
  {
    Header: "Endpoints",
    id: "endpoints",
    accessor: (row) => row.endpoints.length,
    sortType: "basic",
    Cell: TableEndpointColumn,
    width: "auto",
  },
  modeColumn,
]