lxbme commented on code in PR #3441:
URL: https://github.com/apache/apisix-dashboard/pull/3441#discussion_r3642440072


##########
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:
   Fixed in ae96880f. `classifyRouteId` now strips *all* trailing params before 
reading the action from the tail, so `/secrets/detail/$manager/$id` resolves to 
the secrets detail page regardless of how many params trail it.
   
   Confirmed the old logic was the culprit by running it against that pattern 
in isolation: it returned `null` (hence the generic title), while the 
single-param form still worked.
   
   The classifier is now exported and unit-tested against every pattern the 
generated route tree produces — the two-param secret detail, nested 
`services/detail/$id/routes/*` and `.../stream_routes/*`, nested credentials, 
the parent detail pages, and the null fallbacks — so a new route shape can't 
silently fall back to the app title. The e2e also asserts the real 
`/secrets/detail/$manager/$id` page title.
   



-- 
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]

Reply via email to