Copilot commented on code in PR #3222: URL: https://github.com/apache/apisix-dashboard/pull/3222#discussion_r2911154775
########## src/utils/clientSideFilter.ts: ########## @@ -0,0 +1,166 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { STATUS_ALL } from '@/config/constant'; +import type { APISIXType } from '@/types/schema/apisix'; + +import type { SearchFormValues } from '../components/form/SearchForm'; + +/** + * Client-side filtering utility for routes + * Used as a fallback when backend doesn't support certain filter parameters + */ + +export const filterRoutes = ( + routes: APISIXType['RespRouteItem'][], + filters: SearchFormValues +): APISIXType['RespRouteItem'][] => { + return routes.filter((route) => { + const routeData = route.value; + + // Filter by name + if (filters.name) { + const nameMatch = routeData.name + ?.toLowerCase() + .includes(filters.name.toLowerCase()); + if (!nameMatch) return false; + } + + // Filter by ID + if (filters.id) { + const idMatch = String(routeData.id) + .toLowerCase() + .includes(filters.id.toLowerCase()); + if (!idMatch) return false; + } + + // Filter by host + if (filters.host) { + const host = Array.isArray(routeData.host) + ? routeData.host.join(',') + : routeData.host || ''; + const hosts = Array.isArray((routeData as unknown as Record<string, string[]>).hosts) + ? (routeData as unknown as Record<string, string[]>).hosts.join(',') + : ''; + const combinedHost = `${host} ${hosts}`.toLowerCase(); Review Comment: The host/hosts handling uses `as unknown as Record<string, string[]>` casts even though `hosts` is part of the APISIX route schema. Prefer accessing `routeData.hosts` directly (and similarly `routeData.uris`) to avoid unsafe casts and make the filter logic easier to maintain. ########## src/utils/clientSideFilter.ts: ########## @@ -0,0 +1,166 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { STATUS_ALL } from '@/config/constant'; +import type { APISIXType } from '@/types/schema/apisix'; + +import type { SearchFormValues } from '../components/form/SearchForm'; Review Comment: `clientSideFilter` imports `SearchFormValues` from a React component file, which creates an undesirable dependency from utils -> components (and makes reuse/testing harder). Consider moving `SearchFormValues` to a shared types module (e.g. `src/types/search.ts`) and importing it from there (or define a `RouteFilters` type in `src/utils/clientSideFilter.ts` to keep this utility UI-agnostic). ########## src/utils/clientSideFilter.ts: ########## @@ -0,0 +1,166 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { STATUS_ALL } from '@/config/constant'; +import type { APISIXType } from '@/types/schema/apisix'; + +import type { SearchFormValues } from '../components/form/SearchForm'; + +/** + * Client-side filtering utility for routes + * Used as a fallback when backend doesn't support certain filter parameters + */ + +export const filterRoutes = ( + routes: APISIXType['RespRouteItem'][], + filters: SearchFormValues +): APISIXType['RespRouteItem'][] => { + return routes.filter((route) => { + const routeData = route.value; + + // Filter by name + if (filters.name) { + const nameMatch = routeData.name + ?.toLowerCase() + .includes(filters.name.toLowerCase()); + if (!nameMatch) return false; + } + + // Filter by ID + if (filters.id) { + const idMatch = String(routeData.id) + .toLowerCase() + .includes(filters.id.toLowerCase()); + if (!idMatch) return false; + } + + // Filter by host + if (filters.host) { + const host = Array.isArray(routeData.host) + ? routeData.host.join(',') + : routeData.host || ''; + const hosts = Array.isArray((routeData as unknown as Record<string, string[]>).hosts) + ? (routeData as unknown as Record<string, string[]>).hosts.join(',') + : ''; + const combinedHost = `${host} ${hosts}`.toLowerCase(); + const hostMatch = combinedHost.includes(filters.host.toLowerCase()); + if (!hostMatch) return false; + } + + // Filter by path/URI + if (filters.path) { + const uri = Array.isArray(routeData.uri) + ? routeData.uri.join(',') + : routeData.uri || ''; + const uris = Array.isArray(routeData.uris) + ? routeData.uris.join(',') + : ''; + const combinedPath = `${uri} ${uris}`.toLowerCase(); + const pathMatch = combinedPath.includes(filters.path.toLowerCase()); + if (!pathMatch) return false; + } + + // Filter by description + // Note: Routes without a description field are excluded when description filter is active + if (filters.description) { + const descMatch = (routeData.desc || '') + .toLowerCase() + .includes(filters.description.toLowerCase()); + if (!descMatch) return false; + } + + // Filter by plugin: treat the filter text as a substring across all plugin names + // Note: Routes without any plugins are excluded when plugin filter is active + if (filters.plugin) { + if (!routeData.plugins) return false; + const pluginNames = Object.keys(routeData.plugins).join(',').toLowerCase(); + const pluginMatch = pluginNames.includes(filters.plugin.toLowerCase()); + if (!pluginMatch) return false; + } + + // Filter by labels: match provided label key:value tokens against route label pairs + // Note: Routes without any labels are excluded when labels filter is active + if (filters.labels) { + const labelFilters = Array.isArray(filters.labels) + ? filters.labels + : [filters.labels]; + if (labelFilters.length > 0) { + if (!routeData.labels) return false; + + const routeLabels = Object.keys(routeData.labels).map((key) => + `${key}:${routeData.labels![key]}`.toLowerCase() + ); + const hasMatchingLabel = labelFilters.some((filterLabel) => + routeLabels.some((routeLabel) => + routeLabel.includes(filterLabel.toLowerCase()) + ) + ); + if (!hasMatchingLabel) return false; + } + } + + // Filter by version + if (filters.version) { + if (!routeData.labels?.version) return false; + const versionMatch = routeData.labels.version === filters.version; + if (!versionMatch) return false; + } + + // Filter by status + if (filters.status && filters.status !== STATUS_ALL) { + const isPublished = routeData.status === 1; + if (filters.status === 'Published' && !isPublished) return false; + if (filters.status === 'UnPublished' && isPublished) return false; + } Review Comment: Route `status` is optional in the APISIX schema; treating `undefined` as “unpublished” means filtering for Published will exclude routes that omit `status` (which are typically enabled by default). Consider defaulting `routeData.status` to `1` when it’s `undefined` before evaluating Published/UnPublished. ########## src/components/form/SearchForm.tsx: ########## @@ -0,0 +1,249 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { AutoComplete, Button, Col, Form, Input, Row, Select, Space } from 'antd'; +import { useEffect, useMemo } from 'react'; +import { useTranslation } from 'react-i18next'; + +import { STATUS_ALL } from '@/config/constant'; + +export type SearchFormValues = { + name?: string; + id?: string; + host?: string; + path?: string; + description?: string; + plugin?: string; + labels?: string[]; + version?: string; + status?: string; +}; + +type Option = { + label: string; + value: string; +}; + +type SearchFormProps = { + onSearch?: (values: SearchFormValues) => void; + onReset?: (values: SearchFormValues) => void; + labelOptions?: Option[]; + versionOptions?: Option[]; + statusOptions?: Option[]; + initialValues?: Partial<SearchFormValues>; +}; + +export const SearchForm = (props: SearchFormProps) => { + const { + onSearch, + onReset, + labelOptions, + versionOptions, + statusOptions, + initialValues, + } = props; + + const { t } = useTranslation('common'); + const [form] = Form.useForm<SearchFormValues>(); + + const defaultStatusOptions = useMemo<Option[]>( + () => [ + { + label: t('form.searchForm.status.all'), + value: STATUS_ALL, + }, + { + label: t('form.searchForm.status.published'), + value: 'Published', + }, + { + label: t('form.searchForm.status.unpublished'), + value: 'UnPublished', + }, Review Comment: The status filter terminology here (“Published/UnPublished”) is inconsistent with the rest of the UI, which labels `status` values as “Enabled/Disabled” (see `form.basic.statusOption.*` in `common.json`). Consider reusing the existing status labels (and aligning `STATUS_ALL` accordingly) to avoid confusing users. ########## src/types/schema/pageSearch.ts: ########## @@ -29,8 +29,23 @@ export const pageSearchSchema = z .optional() .default(10) .transform((val) => (val ? Number(val) : 10)), + // Common search filter fields used by routes and other pages. + // Keeping these optional preserves backward compatibility while + // ensuring URL params are normalized into consistent shapes. name: z.string().optional(), - label: z.string().optional(), + version: z.string().optional(), + labels: z + .preprocess((val) => { + if (val === undefined || val === null) { + return undefined; + } + if (Array.isArray(val)) { + return val; + } + return [val]; + }, z.array(z.string()).optional()) + .optional(), + status: z.string().optional(), Review Comment: `labels` is declared as `z.array(z.string()).optional()` and then wrapped with an additional `.optional()`. This is redundant; keeping a single optional layer will make the schema easier to read. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
