go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/milo/ui/src/test_verdict/pages/test_search/test_search.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 Box from '@mui/material/Box';
    16  import { useParams } from 'react-router-dom';
    17  
    18  import { RecoverableErrorBoundary } from '@/common/components/error_handling';
    19  import { PageMeta } from '@/common/components/page_meta';
    20  import { SearchInput } from '@/common/components/search_input';
    21  import { UiPage } from '@/common/constants/view';
    22  import { useSyncedSearchParams } from '@/generic_libs/hooks/synced_search_params';
    23  
    24  import { DEFAULT_TEST_PROJECT } from '../../../routes/search_loader/search_redirection_loader';
    25  
    26  import { TestList } from './test_list';
    27  
    28  export const TestSearch = () => {
    29    const { project } = useParams();
    30    const [searchParams, setSearchParams] = useSyncedSearchParams();
    31    const searchQuery = searchParams.get('q') || '';
    32  
    33    const handleSearchQueryChange = (newSearchQuery: string) => {
    34      setSearchParams((prev) => {
    35        const next = new URLSearchParams(prev);
    36        if (newSearchQuery === '') {
    37          next.delete('q');
    38        } else {
    39          next.set('q', newSearchQuery);
    40        }
    41        return next;
    42      });
    43    };
    44  
    45    const selectedProject = project || DEFAULT_TEST_PROJECT;
    46  
    47    return (
    48      <Box sx={{ px: 6, py: 2 }}>
    49        <PageMeta
    50          title="Test search"
    51          project={selectedProject}
    52          selectedPage={UiPage.TestHistory}
    53        />
    54        <Box sx={{ mx: 20 }}>
    55          <SearchInput
    56            placeholder="Search tests in the specified project"
    57            onValueChange={handleSearchQueryChange}
    58            value={searchQuery}
    59            initDelayMs={600}
    60          />
    61        </Box>
    62        <Box sx={{ mt: 5 }}>
    63          <TestList searchQuery={searchQuery} project={selectedProject} />
    64        </Box>
    65      </Box>
    66    );
    67  };
    68  
    69  export const element = (
    70    // See the documentation for `<LoginPage />` for why we handle error this way.
    71    <RecoverableErrorBoundary key="test-search">
    72      <TestSearch />
    73    </RecoverableErrorBoundary>
    74  );