go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/build/components/build_table/build_table_row.tsx (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  import { TableRow } from '@mui/material';
    16  import { ReactNode, useEffect, useState } from 'react';
    17  
    18  import { OutputBuild } from '@/build/types';
    19  
    20  import {
    21    BuildProvider,
    22    RowExpandedProvider,
    23    SetRowExpandedProvider,
    24    useDefaultExpanded,
    25  } from './context';
    26  
    27  export interface BuildTableRowProps {
    28    readonly build: OutputBuild;
    29    readonly children: ReactNode;
    30  }
    31  
    32  export function BuildTableRow({ build, children }: BuildTableRowProps) {
    33    const defaultExpanded = useDefaultExpanded();
    34    const [expanded, setExpanded] = useState(() => defaultExpanded);
    35    useEffect(() => {
    36      setExpanded(defaultExpanded);
    37    }, [defaultExpanded]);
    38  
    39    return (
    40      <TableRow
    41        // Set a CSS class to support CSS-only toggle solution.
    42        className={
    43          expanded ? 'BuildTableRow-expanded' : 'BuildTableRow-collapsed'
    44        }
    45        sx={{
    46          '& > td': {
    47            // Use `vertical-align: baseline` so the cell content (including the
    48            // expand button) won't shift downwards when the row is expanded.
    49            verticalAlign: 'baseline',
    50            whiteSpace: 'nowrap',
    51          },
    52        }}
    53      >
    54        <SetRowExpandedProvider value={setExpanded}>
    55          {/* Provide the expanded state via context in case a CSS-only toggle
    56           ** solution can't be achieved. */}
    57          <RowExpandedProvider value={expanded}>
    58            {/* Pass build to cells via context so composing a row require less
    59             ** boilerplate. */}
    60            <BuildProvider value={build}>{children}</BuildProvider>
    61          </RowExpandedProvider>
    62        </SetRowExpandedProvider>
    63      </TableRow>
    64    );
    65  }