Copilot commented on code in PR #2792:
URL: https://github.com/apache/sedona/pull/2792#discussion_r2999638015


##########
docs-overrides/assets/javascripts/components/search-tags.js:
##########
@@ -0,0 +1,60 @@
+const PLATFORM_RULES = [
+  {pattern: /\/api\/flink\//, label: 'SedonaFlink', cls: 'flink'},
+  {pattern: /\/api\/snowflake\//, label: 'SedonaSnow', cls: 'snow'},
+  {pattern: /\/setup\/flink\//, label: 'SedonaFlink', cls: 'flink'},
+  {pattern: /\/setup\/snowflake\//, label: 'SedonaSnow', cls: 'snow'},
+  {pattern: /\/tutorial\/flink\//, label: 'SedonaFlink', cls: 'flink'},
+  {pattern: /\/tutorial\/snowflake\//, label: 'SedonaSnow', cls: 'snow'},
+  {pattern: /sedonaflink\/?$/, label: 'SedonaFlink', cls: 'flink'},
+  {pattern: /sedonasnow\/?$/, label: 'SedonaSnow', cls: 'snow'},
+  {pattern: /\/api\/sql\//, label: 'SedonaSpark', cls: 'spark'},
+  {pattern: /\/api\/stats\//, label: 'SedonaSpark', cls: 'spark'},
+  {pattern: /\/api\/viz\//, label: 'SedonaSpark', cls: 'spark'},
+  {pattern: /\/setup\/(?!flink|snowflake)/, label: 'SedonaSpark', cls: 
'spark'},
+  {
+    pattern: /\/tutorial\/(?!flink|snowflake)/,
+    label: 'SedonaSpark',
+    cls: 'spark',
+  },
+  {pattern: /sedonaspark\/?$/, label: 'SedonaSpark', cls: 'spark'},
+];
+
+function getPlatform(href) {
+  for (const rule of PLATFORM_RULES) {
+    if (rule.pattern.test(href)) {
+      return rule;
+    }
+  }
+  return null;
+}
+
+function tagResult(item) {
+  if (item.querySelector('.search-tag')) return;
+
+  const link = item.querySelector('a');
+  if (!link) return;
+
+  const platform = getPlatform(link.getAttribute('href') || '');
+  if (!platform) return;
+
+  const tag = document.createElement('span');
+  tag.className = `search-tag search-tag--${platform.cls}`;
+  tag.textContent = platform.label;
+
+  const title = link.querySelector('h1, h2');
+  if (title) {
+    title.appendChild(tag);
+  }
+}
+
+function tagAllResults() {
+  document.querySelectorAll('.md-search-result__item').forEach(tagResult);
+}
+
+export const searchTags = () => {
+  const resultList = document.querySelector('.md-search-result__list');
+  if (!resultList) return;
+
+  const observer = new MutationObserver(tagAllResults);

Review Comment:
   The MutationObserver is watching the entire result list subtree and its 
callback runs `tagAllResults()`, which appends `.search-tag` elements into that 
same subtree. Those DOM inserts will themselves trigger additional mutation 
events, causing redundant full rescans of all results (can become noticeably 
expensive while the user types). Consider processing only 
`MutationRecord.addedNodes` (or scoping to the mutated nodes), and/or 
temporarily disconnecting/debouncing the observer while tagging to avoid 
observer feedback loops.
   ```suggestion
   function handleSearchResultMutations(mutationsList) {
     for (const mutation of mutationsList) {
       for (const node of mutation.addedNodes) {
         if (!(node instanceof HTMLElement)) continue;
   
         if (node.matches('.md-search-result__item')) {
           tagResult(node);
           continue;
         }
   
         const items = node.querySelectorAll
           ? node.querySelectorAll('.md-search-result__item')
           : [];
   
         items.forEach(tagResult);
       }
     }
   }
   
   export const searchTags = () => {
     const resultList = document.querySelector('.md-search-result__list');
     if (!resultList) return;
   
     // Tag any existing results initially.
     tagAllResults();
   
     const observer = new MutationObserver(handleSearchResultMutations);
   ```



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