davanstrien HF staff commited on
Commit
b6c1ca0
·
1 Parent(s): 6e41f47

add share query button

Browse files
Files changed (1) hide show
  1. index.html +142 -17
index.html CHANGED
@@ -162,6 +162,11 @@
162
  const MAX_RESULTS = 100;
163
  let currentPage = 1;
164
 
 
 
 
 
 
165
  // Initialize Lucide icons
166
  lucide.createIcons();
167
 
@@ -177,6 +182,13 @@
177
 
178
  document.getElementById(`${tabId}Content`).classList.remove("hidden");
179
  document.getElementById(`${tabId}Tab`).classList.add("active");
 
 
 
 
 
 
 
180
  }
181
 
182
  // Create result card
@@ -205,6 +217,14 @@
205
  <span class="bg-blue-50 px-2 py-1 rounded text-sm">
206
  ${(result.similarity * 100).toFixed(1)}% match
207
  </span>
 
 
 
 
 
 
 
 
208
  <button
209
  onclick="findSimilarFromResult('${
210
  result.dataset_id
@@ -288,16 +308,33 @@
288
  }
289
  }
290
 
291
- // Search datasets
 
 
 
 
 
 
 
 
 
 
 
 
 
292
  const searchDatasets = _.debounce(async (query, page = 1) => {
293
  if (query.length < MIN_SEARCH_LENGTH) {
294
  document.getElementById("resultsContainer").innerHTML = "";
 
295
  return;
296
  }
297
 
298
  document.getElementById("searchLoader").classList.remove("hidden");
299
  document.getElementById("errorMessage").classList.add("hidden");
300
 
 
 
 
301
  try {
302
  const response = await fetch(
303
  `${API_URL}/search/datasets?query=${encodeURIComponent(query)}&k=${
@@ -382,11 +419,14 @@
382
  findSimilarDatasets();
383
  }
384
 
385
- // Find similar datasets
386
  async function findSimilarDatasets(page = 1) {
387
  const datasetId = document.getElementById("datasetInput").value;
388
  if (!datasetId) return;
389
 
 
 
 
390
  const similarLoader = document.getElementById("similarLoader");
391
  if (similarLoader) {
392
  similarLoader.classList.remove("hidden");
@@ -418,34 +458,64 @@
418
  console.log("Displaying results:", results);
419
  if (results && results.length > 0) {
420
  container.innerHTML = `
421
- <div class="flex justify-between items-center">
422
  <h2 class="text-lg font-semibold">Results</h2>
423
- <span class="text-sm text-gray-500">Found ${
424
- results.length
425
- } results</span>
 
 
 
 
 
 
 
 
 
 
426
  </div>
427
  ${results.map((result) => createResultCard(result)).join("")}
428
  ${
429
  results.length >= RESULTS_PER_PAGE * page &&
430
  RESULTS_PER_PAGE * (page + 1) <= MAX_RESULTS
431
- ? `<button
432
- onclick="loadMore()"
433
- class="w-full mt-4 px-6 py-3 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg transition-colors flex items-center gap-2 justify-center"
434
- >
435
- <i data-lucide="more-horizontal"></i>
436
- Load More Results
437
- </button>`
 
 
 
 
 
 
 
 
 
 
438
  : results.length >= MAX_RESULTS
439
  ? `<div class="text-center mt-4 p-6 bg-blue-50 rounded-lg">
440
- <p class="text-gray-700 mb-3">You've reached the end of our dataset journey! (${MAX_RESULTS} results)</p>
441
- <p class="text-gray-600 mb-4">Can't find what you're looking for? Why not create and share your own dataset?</p>
 
442
  <a href="https://huggingface.co/docs/datasets/upload_dataset"
443
  target="_blank"
444
  class="inline-flex items-center gap-2 text-blue-500 hover:text-blue-700">
445
  <i data-lucide="external-link"></i>
446
  Learn how to share your dataset on Hugging Face
447
  </a>
448
- </div>`
 
 
 
 
 
 
 
 
 
449
  : ""
450
  }
451
  `;
@@ -528,8 +598,52 @@
528
  : "rotate(90deg)";
529
  }
530
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
531
  // Update the event listeners section
532
- document.addEventListener("DOMContentLoaded", () => {
533
  const datasetInput = document.getElementById("datasetInput");
534
  let programmaticFocus = false;
535
 
@@ -563,6 +677,17 @@
563
  }
564
  programmaticFocus = false;
565
  });
 
 
 
 
 
 
 
 
 
 
 
566
  });
567
  </script>
568
  </body>
 
162
  const MAX_RESULTS = 100;
163
  let currentPage = 1;
164
 
165
+ // Add these constants near the top with other configurations
166
+ const URL_PARAMS = new URLSearchParams(window.location.search);
167
+ const INITIAL_SEARCH = URL_PARAMS.get("q");
168
+ const INITIAL_SIMILAR = URL_PARAMS.get("similar");
169
+
170
  // Initialize Lucide icons
171
  lucide.createIcons();
172
 
 
182
 
183
  document.getElementById(`${tabId}Content`).classList.remove("hidden");
184
  document.getElementById(`${tabId}Tab`).classList.add("active");
185
+
186
+ // Clear URL parameters when switching tabs
187
+ if (tabId === "search") {
188
+ updateURL({ similar: null });
189
+ } else if (tabId === "similar") {
190
+ updateURL({ q: null });
191
+ }
192
  }
193
 
194
  // Create result card
 
217
  <span class="bg-blue-50 px-2 py-1 rounded text-sm">
218
  ${(result.similarity * 100).toFixed(1)}% match
219
  </span>
220
+ <button
221
+ onclick="shareResult('${result.dataset_id}')"
222
+ class="flex items-center gap-1 text-sm text-blue-500 hover:text-blue-700"
223
+ title="Share link to these search results"
224
+ >
225
+ <i data-lucide="share-2"></i>
226
+ Share Results
227
+ </button>
228
  <button
229
  onclick="findSimilarFromResult('${
230
  result.dataset_id
 
308
  }
309
  }
310
 
311
+ // Add this function to update the URL
312
+ function updateURL(params) {
313
+ const newURL = new URL(window.location);
314
+ Object.entries(params).forEach(([key, value]) => {
315
+ if (value) {
316
+ newURL.searchParams.set(key, value);
317
+ } else {
318
+ newURL.searchParams.delete(key);
319
+ }
320
+ });
321
+ window.history.pushState({}, "", newURL);
322
+ }
323
+
324
+ // Modify the searchDatasets function
325
  const searchDatasets = _.debounce(async (query, page = 1) => {
326
  if (query.length < MIN_SEARCH_LENGTH) {
327
  document.getElementById("resultsContainer").innerHTML = "";
328
+ updateURL({ q: null, similar: null }); // Clear URL params
329
  return;
330
  }
331
 
332
  document.getElementById("searchLoader").classList.remove("hidden");
333
  document.getElementById("errorMessage").classList.add("hidden");
334
 
335
+ // Update URL with search query
336
+ updateURL({ q: query, similar: null });
337
+
338
  try {
339
  const response = await fetch(
340
  `${API_URL}/search/datasets?query=${encodeURIComponent(query)}&k=${
 
419
  findSimilarDatasets();
420
  }
421
 
422
+ // Modify the findSimilarDatasets function
423
  async function findSimilarDatasets(page = 1) {
424
  const datasetId = document.getElementById("datasetInput").value;
425
  if (!datasetId) return;
426
 
427
+ // Update URL with similar dataset ID
428
+ updateURL({ similar: datasetId, q: null });
429
+
430
  const similarLoader = document.getElementById("similarLoader");
431
  if (similarLoader) {
432
  similarLoader.classList.remove("hidden");
 
458
  console.log("Displaying results:", results);
459
  if (results && results.length > 0) {
460
  container.innerHTML = `
461
+ <div class="flex justify-between items-center mb-4">
462
  <h2 class="text-lg font-semibold">Results</h2>
463
+ <div class="flex items-center gap-4">
464
+ <span class="text-sm text-gray-500">Found ${
465
+ results.length
466
+ } results</span>
467
+ <button
468
+ onclick="shareResults()"
469
+ class="flex items-center gap-1 text-sm text-blue-500 hover:text-blue-700"
470
+ title="Share link to these search results"
471
+ >
472
+ <i data-lucide="share-2"></i>
473
+ Share Results
474
+ </button>
475
+ </div>
476
  </div>
477
  ${results.map((result) => createResultCard(result)).join("")}
478
  ${
479
  results.length >= RESULTS_PER_PAGE * page &&
480
  RESULTS_PER_PAGE * (page + 1) <= MAX_RESULTS
481
+ ? `<div class="mt-4 flex items-center justify-between">
482
+ <button
483
+ onclick="loadMore()"
484
+ class="px-6 py-3 bg-gray-100 hover:bg-gray-200 text-gray-700 rounded-lg transition-colors flex items-center gap-2"
485
+ >
486
+ <i data-lucide="more-horizontal"></i>
487
+ Load More Results
488
+ </button>
489
+ <button
490
+ onclick="shareResults()"
491
+ class="flex items-center gap-1 text-sm text-blue-500 hover:text-blue-700"
492
+ title="Share link to these search results"
493
+ >
494
+ <i data-lucide="share-2"></i>
495
+ Share Results
496
+ </button>
497
+ </div>`
498
  : results.length >= MAX_RESULTS
499
  ? `<div class="text-center mt-4 p-6 bg-blue-50 rounded-lg">
500
+ <p class="text-gray-700 mb-3">You've reached the end of our dataset journey! (${MAX_RESULTS} results)</p>
501
+ <p class="text-gray-600 mb-4">Can't find what you're looking for? Why not create and share your own dataset?</p>
502
+ <div class="flex items-center justify-center gap-4">
503
  <a href="https://huggingface.co/docs/datasets/upload_dataset"
504
  target="_blank"
505
  class="inline-flex items-center gap-2 text-blue-500 hover:text-blue-700">
506
  <i data-lucide="external-link"></i>
507
  Learn how to share your dataset on Hugging Face
508
  </a>
509
+ <button
510
+ onclick="shareResults()"
511
+ class="flex items-center gap-1 text-blue-500 hover:text-blue-700"
512
+ title="Share link to these search results"
513
+ >
514
+ <i data-lucide="share-2"></i>
515
+ Share Results
516
+ </button>
517
+ </div>
518
+ </div>`
519
  : ""
520
  }
521
  `;
 
598
  : "rotate(90deg)";
599
  }
600
 
601
+ // Update the share function name and remove the datasetId parameter
602
+ async function shareResults() {
603
+ const activeTab = document.querySelector(".tab-trigger.active").id;
604
+ const currentURL = new URL(window.location);
605
+
606
+ // Update URL based on active tab
607
+ if (activeTab === "searchTab") {
608
+ const searchQuery = document.getElementById("searchInput").value;
609
+ currentURL.searchParams.set("q", searchQuery);
610
+ currentURL.searchParams.delete("similar");
611
+ } else {
612
+ const datasetId = document.getElementById("datasetInput").value;
613
+ currentURL.searchParams.set("similar", datasetId);
614
+ currentURL.searchParams.delete("q");
615
+ }
616
+
617
+ try {
618
+ if (navigator.share) {
619
+ // Use native sharing on supported devices
620
+ await navigator.share({
621
+ title: "Hub Semantic Search",
622
+ text: "Check out these dataset search results",
623
+ url: currentURL.toString(),
624
+ });
625
+ } else {
626
+ // Fallback to clipboard copy
627
+ await navigator.clipboard.writeText(currentURL.toString());
628
+
629
+ // Show temporary success message
630
+ const button = event.target.closest("button");
631
+ const originalHTML = button.innerHTML;
632
+ button.innerHTML = '<i data-lucide="check"></i> Copied!';
633
+ lucide.createIcons();
634
+
635
+ setTimeout(() => {
636
+ button.innerHTML = originalHTML;
637
+ lucide.createIcons();
638
+ }, 2000);
639
+ }
640
+ } catch (error) {
641
+ console.error("Error sharing:", error);
642
+ }
643
+ }
644
+
645
  // Update the event listeners section
646
+ document.addEventListener("DOMContentLoaded", async () => {
647
  const datasetInput = document.getElementById("datasetInput");
648
  let programmaticFocus = false;
649
 
 
677
  }
678
  programmaticFocus = false;
679
  });
680
+
681
+ // Handle initial URL parameters
682
+ if (INITIAL_SEARCH) {
683
+ switchTab("search");
684
+ document.getElementById("searchInput").value = INITIAL_SEARCH;
685
+ await searchDatasets(INITIAL_SEARCH);
686
+ } else if (INITIAL_SIMILAR) {
687
+ switchTab("similar");
688
+ document.getElementById("datasetInput").value = INITIAL_SIMILAR;
689
+ await findSimilarDatasets();
690
+ }
691
  });
692
  </script>
693
  </body>