victor HF Staff commited on
Commit
aedf456
·
1 Parent(s): f68f600

feat: Implement sorting for the request inspector table

Browse files
Files changed (1) hide show
  1. index.html +61 -1
index.html CHANGED
@@ -586,6 +586,24 @@
586
  }
587
  });
588
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
589
 
590
  // --- Fetching Logic ---
591
  function fetchAllRows(offset = 0) {
@@ -919,11 +937,53 @@
919
  });
920
  }
921
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
922
  // --- Request Inspector Table ---
923
  function createRequestTable(rows) {
924
  const tableHead = document.querySelector('#requestTable thead');
925
  const tableBody = document.querySelector('#requestTable tbody');
926
- tableHead.innerHTML = ''; tableBody.innerHTML = ''; // Clear
 
927
 
928
  document.getElementById('request-table-title').textContent = `Request Inspector (${rows.length} requests shown)`;
929
 
 
586
  }
587
  });
588
 
589
+ // Sorting listener for Request Inspector Table Header
590
+ document.querySelector('#requestTable thead').addEventListener('click', (event) => {
591
+ const header = event.target.closest('th.sortable-header');
592
+ if (!header) return; // Clicked outside a sortable header
593
+
594
+ const sortKey = header.dataset.sortKey;
595
+
596
+ // Determine new sort direction
597
+ if (sortKey === currentSortKey) {
598
+ currentSortDirection = currentSortDirection === 'asc' ? 'desc' : 'asc';
599
+ } else {
600
+ currentSortKey = sortKey;
601
+ currentSortDirection = 'asc';
602
+ }
603
+
604
+ sortTable(currentSortKey, currentSortDirection);
605
+ });
606
+
607
 
608
  // --- Fetching Logic ---
609
  function fetchAllRows(offset = 0) {
 
937
  });
938
  }
939
 
940
+ // --- Sorting Function ---
941
+ function sortTable(key, direction) {
942
+ currentFilteredRows.sort((a, b) => {
943
+ let valA = a[key];
944
+ let valB = b[key];
945
+
946
+ // Handle specific cases
947
+ if (key === 'error_message') { // Sort by presence (Yes/No)
948
+ valA = valA ? 1 : 0; // Error present = 1, No error = 0
949
+ valB = valB ? 1 : 0;
950
+ } else if (key === 'model_id') { // Sort by shortened model name
951
+ valA = valA ? valA.split('/').pop() : '';
952
+ valB = valB ? valB.split('/').pop() : '';
953
+ }
954
+
955
+ // Handle null/undefined values (treat them as lowest or highest depending on direction)
956
+ const nullVal = direction === 'asc' ? Infinity : -Infinity;
957
+ valA = (valA === null || valA === undefined) ? nullVal : valA;
958
+ valB = (valB === null || valB === undefined) ? nullVal : valB;
959
+
960
+ // Comparison logic
961
+ if (typeof valA === 'string' && typeof valB === 'string') {
962
+ return direction === 'asc' ? valA.localeCompare(valB) : valB.localeCompare(valA);
963
+ } else {
964
+ return direction === 'asc' ? valA - valB : valB - valA;
965
+ }
966
+ });
967
+ createRequestTable(currentFilteredRows); // Re-render the table body
968
+ updateSortIndicators(); // Update visual indicators
969
+ }
970
+
971
+ function updateSortIndicators() {
972
+ document.querySelectorAll('#requestTable th.sortable-header').forEach(th => {
973
+ th.classList.remove('sort-asc', 'sort-desc');
974
+ if (th.dataset.sortKey === currentSortKey) {
975
+ th.classList.add(currentSortDirection === 'asc' ? 'sort-asc' : 'sort-desc');
976
+ }
977
+ });
978
+ }
979
+
980
+
981
  // --- Request Inspector Table ---
982
  function createRequestTable(rows) {
983
  const tableHead = document.querySelector('#requestTable thead');
984
  const tableBody = document.querySelector('#requestTable tbody');
985
+ // Clear only the body, keep the header for event listeners
986
+ tableBody.innerHTML = '';
987
 
988
  document.getElementById('request-table-title').textContent = `Request Inspector (${rows.length} requests shown)`;
989