github.com/tilt-dev/tilt@v0.36.0/web/src/OverviewSidebarOptions.tsx (about)

     1  import { FormControlLabel } from "@material-ui/core"
     2  import React, { useCallback, useMemo } from "react"
     3  import styled from "styled-components"
     4  import { Flag, useFeatures } from "./feature"
     5  import { InstrumentedCheckbox } from "./instrumentedComponents"
     6  import { TILTFILE_LABEL, UNLABELED_LABEL } from "./labels"
     7  import { CollapseButton, ExpandButton } from "./resourceListOptionsButtons"
     8  import { useResourceListOptions } from "./ResourceListOptionsContext"
     9  import { ResourceNameFilter } from "./ResourceNameFilter"
    10  import SidebarItem from "./SidebarItem"
    11  import { sidebarItemIsDisabled } from "./SidebarItemView"
    12  import {
    13    Color,
    14    Font,
    15    FontSize,
    16    mixinResetListStyle,
    17    SizeUnit,
    18  } from "./style-helpers"
    19  import { ResourceName } from "./types"
    20  import { OverviewSidebarToggle } from "./OverviewSidebarToggle"
    21  
    22  export const OverviewSidebarOptionsRoot = styled.div`
    23    display: flex;
    24    justify-content: space-between;
    25    font-family: ${Font.monospace};
    26    font-size: ${FontSize.smallest};
    27    padding-left: ${SizeUnit(0.5)};
    28    padding-right: ${SizeUnit(0.5)};
    29    color: ${Color.offWhite};
    30    flex-direction: column;
    31  `
    32  
    33  const OverviewSidebarOptionsButtonRow = styled.div`
    34    align-items: center;
    35    display: flex;
    36    flex-direction: row;
    37    justify-content: space-between;
    38    width: 100%;
    39  `
    40  
    41  export const FilterOptionList = styled.ul`
    42    ${mixinResetListStyle};
    43    display: flex;
    44    align-items: center;
    45    user-select: none; /* Prevent unsightly highlighting on the label */
    46  `
    47  
    48  export const SidebarOptionsLabel = styled(FormControlLabel)`
    49    .MuiFormControlLabel-label.MuiTypography-body1 {
    50      line-height: 1;
    51    }
    52  `
    53  
    54  export const CheckboxToggle = styled(InstrumentedCheckbox)`
    55    &.MuiCheckbox-root,
    56    &.Mui-checked {
    57      color: ${Color.gray60};
    58    }
    59  `
    60  
    61  // Create a list of all the groups from the list of resources.
    62  //
    63  // Sadly, this logic is duplicated several times across table and sidebar,
    64  // but there's no easy way to consolidate it right now.
    65  function toGroups(
    66    items: SidebarItem[],
    67    hideDisabledResources: boolean
    68  ): string[] {
    69    let hasUnlabeled = false
    70    let hasTiltfile = false
    71    let hasLabels: { [key: string]: boolean } = {}
    72    items.forEach((item) => {
    73      const isDisabled = sidebarItemIsDisabled(item)
    74      if (hideDisabledResources && isDisabled) {
    75        return
    76      }
    77  
    78      const labels = item.labels
    79      const isTiltfile = item.name === ResourceName.tiltfile
    80      if (labels.length) {
    81        labels.forEach((label) => {
    82          hasLabels[label] = true
    83        })
    84      } else if (isTiltfile) {
    85        hasTiltfile = true
    86      } else {
    87        hasUnlabeled = true
    88      }
    89    })
    90  
    91    let groups = Object.keys(hasLabels)
    92    if (groups.length) {
    93      if (hasTiltfile) {
    94        groups.push(TILTFILE_LABEL)
    95      }
    96      if (hasUnlabeled) {
    97        groups.push(UNLABELED_LABEL)
    98      }
    99    }
   100    return groups
   101  }
   102  
   103  export function OverviewSidebarOptions(props: { items?: SidebarItem[] }) {
   104    const features = useFeatures()
   105    const { options, setOptions } = useResourceListOptions()
   106    let toggleDisabledResources = useCallback(() => {
   107      setOptions({
   108        showDisabledResources: !options.showDisabledResources,
   109      })
   110    }, [options.showDisabledResources])
   111  
   112    const labelsEnabled = features.isEnabled(Flag.Labels)
   113    let items = props.items || []
   114  
   115    const hideDisabledResources = !options.showDisabledResources
   116  
   117    // TODO(nick): Enable/disable the expand/collapse button based
   118    // on whether the groups are shown and the current group state.
   119    let groups = useMemo(
   120      () => toGroups(items, hideDisabledResources),
   121      [items, hideDisabledResources]
   122    )
   123    const resourceFilterApplied = options.resourceNameFilter.length > 0
   124    const displayResourceGroups =
   125      labelsEnabled && groups.length && !resourceFilterApplied
   126  
   127    const disabledResourcesToggle = (
   128      <SidebarOptionsLabel
   129        control={
   130          <CheckboxToggle
   131            size="small"
   132            checked={options.showDisabledResources}
   133            onClick={toggleDisabledResources}
   134          />
   135        }
   136        label="Show disabled resources"
   137      />
   138    )
   139  
   140    return (
   141      <OverviewSidebarOptionsRoot>
   142        <OverviewSidebarOptionsButtonRow>
   143          <ResourceNameFilter />
   144          <OverviewSidebarToggle />
   145        </OverviewSidebarOptionsButtonRow>
   146        <OverviewSidebarOptionsButtonRow>
   147          <SidebarOptionsLabel
   148            control={
   149              <CheckboxToggle
   150                size="small"
   151                checked={options.alertsOnTop}
   152                onClick={(_e) =>
   153                  setOptions({ alertsOnTop: !options.alertsOnTop })
   154                }
   155              />
   156            }
   157            label="Alerts on top"
   158          />
   159          <div>
   160            <ExpandButton disabled={!displayResourceGroups} />
   161            <CollapseButton groups={groups} disabled={!displayResourceGroups} />
   162          </div>
   163        </OverviewSidebarOptionsButtonRow>
   164        {disabledResourcesToggle}
   165      </OverviewSidebarOptionsRoot>
   166    )
   167  }