LiteSun commented on code in PR #3441: URL: https://github.com/apache/apisix-dashboard/pull/3441#discussion_r3636649228
########## src/hooks/useDocumentTitle.ts: ########## @@ -0,0 +1,104 @@ +/** + * 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 { useRouterState } from '@tanstack/react-router'; +import { useEffect } from 'react'; +import { useTranslation } from 'react-i18next'; + +import type { Resources } from '@/config/i18n'; +import { navRoutes } from '@/config/navRoutes'; + +// matches the static <title> in index.html (the default before any route +// resolves and the suffix on every page) +const APP_NAME = 'Apache APISIX Dashboard'; + +type SourceLabel = keyof Resources['en']['common']['sources']; + +// path segment -> sources i18n label. Built from the nav table (the single +// source of truth), plus `credentials`, which is only ever a nested +// resource (never a top-level nav entry). +const segmentToLabel: Record<string, SourceLabel> = { + ...Object.fromEntries( + navRoutes.map((r) => [r.to.replace(/^\//, ''), r.label]) + ), + credentials: 'credentials', +}; + +const isParam = (seg: string) => seg.startsWith('$'); + +/** + * Classify the DEEPEST matched route by its pattern (e.g. + * `/services/detail/$id/routes/add`). The action is read from the TAIL of + * the pattern so a `detail/$id` in the middle (navigation context) does + * not misclassify a nested add/detail page as the parent's detail (#3441 + * review): a nested `…/routes/add` is "Add Route", not "Service Detail". + */ +const classify = ( + routeId: string +): { label: SourceLabel; action: 'add' | 'detail' | 'list' } | null => { + const segs = routeId.split('/').filter(Boolean); + if (segs.length === 0) return null; + + const last = segs[segs.length - 1]; + let action: 'add' | 'detail' | 'list'; + let resourceIdx: number; + + if (last === 'add') { + action = 'add'; + resourceIdx = segs.length - 2; + } else if (isParam(last) && segs[segs.length - 2] === 'detail') { Review Comment: The new classifier still misses the Secret detail route. `/secrets/detail/$manager/$id` ends with a parameter, but the segment immediately before it is `$manager`, not `detail`, so this condition falls through to the list branch. That branch selects `$manager` as the resource, `segmentToLabel` has no match, and `classify()` returns `null`; the page therefore falls back to the generic application title. Please handle detail routes with multiple trailing parameters and add a regression case for this route. -- 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]
