Path: blob/trunk/javascript/grid-ui/src/components/RunningSessions/ColumnSelector.tsx
2887 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617import React, { useState, useEffect } from 'react'18import {19Box,20Button,21Checkbox,22Dialog,23DialogActions,24DialogContent,25DialogTitle,26FormControlLabel,27FormGroup,28IconButton,29Tooltip,30Typography31} from '@mui/material'32import { ViewColumn as ViewColumnIcon } from '@mui/icons-material'3334interface ColumnSelectorProps {35sessions: any[]36selectedColumns: string[]37onColumnSelectionChange: (columns: string[]) => void38}3940const ColumnSelector: React.FC<ColumnSelectorProps> = ({41sessions,42selectedColumns,43onColumnSelectionChange44}) => {45const [open, setOpen] = useState(false)46const [availableColumns, setAvailableColumns] = useState<string[]>([])47const [localSelectedColumns, setLocalSelectedColumns] = useState<string[]>(selectedColumns)4849useEffect(() => {50setLocalSelectedColumns(selectedColumns)51}, [selectedColumns])5253useEffect(() => {54let allKeys = new Set<string>()55try {56const savedKeys = localStorage.getItem('selenium-grid-all-capability-keys')57if (savedKeys) {58const parsedKeys = JSON.parse(savedKeys)59parsedKeys.forEach((key: string) => allKeys.add(key))60}61} catch (e) {62console.error('Error loading saved capability keys:', e)63}6465sessions.forEach(session => {66try {67const capabilities = JSON.parse(session.capabilities)68Object.keys(capabilities).forEach(key => {69if (70typeof capabilities[key] !== 'object' &&71!key.startsWith('goog:') &&72!key.startsWith('moz:') &&73key !== 'alwaysMatch' &&74key !== 'firstMatch'75) {76allKeys.add(key)77}78})79} catch (e) {80console.error('Error parsing capabilities:', e)81}82})8384const keysArray = Array.from(allKeys).sort()85localStorage.setItem('selenium-grid-all-capability-keys', JSON.stringify(keysArray))8687setAvailableColumns(keysArray)88}, [sessions])8990const handleToggle = (column: string) => {91setLocalSelectedColumns(prev => {92if (prev.includes(column)) {93return prev.filter(col => col !== column)94} else {95return [...prev, column]96}97})98}99100const handleClose = () => {101setOpen(false)102}103104const handleSave = () => {105onColumnSelectionChange(localSelectedColumns)106setOpen(false)107}108109const handleSelectAll = (checked: boolean) => {110if (checked) {111setLocalSelectedColumns([...availableColumns])112} else {113setLocalSelectedColumns([])114}115}116117return (118<Box>119<Tooltip title="Select columns to display" arrow placement="top">120<IconButton121aria-label="select columns"122onClick={() => setOpen(true)}123>124<ViewColumnIcon />125</IconButton>126</Tooltip>127128<Dialog129open={open}130onClose={handleClose}131maxWidth="sm"132fullWidth133>134<DialogTitle>135Select Columns to Display136</DialogTitle>137<DialogContent dividers>138<Typography variant="body2" gutterBottom>139Select capability fields to display as additional columns:140</Typography>141<FormGroup>142<FormControlLabel143control={144<Checkbox145checked={localSelectedColumns.length === availableColumns.length && availableColumns.length > 0}146indeterminate={localSelectedColumns.length > 0 && localSelectedColumns.length < availableColumns.length}147onChange={(e) => handleSelectAll(e.target.checked)}148/>149}150label={<Typography fontWeight="bold">Select All / Unselect All</Typography>}151/>152{availableColumns.map(column => (153<FormControlLabel154key={column}155control={156<Checkbox157checked={localSelectedColumns.includes(column)}158onChange={() => handleToggle(column)}159/>160}161label={column}162/>163))}164</FormGroup>165</DialogContent>166<DialogActions>167<Button onClick={handleClose}>Cancel</Button>168<Button onClick={handleSave} variant="contained" color="primary">169Apply170</Button>171</DialogActions>172</Dialog>173</Box>174)175}176177export default ColumnSelector178179180