Suggestions.svelte 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <script lang="ts">
  2. import Bolt from '$lib/components/icons/Bolt.svelte';
  3. import { onMount, getContext } from 'svelte';
  4. const i18n = getContext('i18n');
  5. export let submitPrompt: Function;
  6. export let suggestionPrompts = [];
  7. let prompts = [];
  8. $: prompts = suggestionPrompts
  9. .reduce((acc, current) => [...acc, ...[current]], [])
  10. .sort(() => Math.random() - 0.5);
  11. // suggestionPrompts.length <= 4
  12. // ? suggestionPrompts
  13. // : suggestionPrompts.sort(() => Math.random() - 0.5).slice(0, 4);
  14. onMount(() => {
  15. const containerElement = document.getElementById('suggestions-container');
  16. if (containerElement) {
  17. containerElement.addEventListener('wheel', function (event) {
  18. if (event.deltaY !== 0) {
  19. // If scrolling vertically, prevent default behavior
  20. event.preventDefault();
  21. // Adjust horizontal scroll position based on vertical scroll
  22. containerElement.scrollLeft += event.deltaY;
  23. }
  24. });
  25. }
  26. });
  27. </script>
  28. {#if prompts.length > 0}
  29. <div class="mb-2 flex gap-1 text-sm font-medium items-center text-gray-400 dark:text-gray-600">
  30. <Bolt />
  31. {$i18n.t('Suggested')}
  32. </div>
  33. {/if}
  34. <div class="w-full">
  35. <div
  36. class="relative w-full flex gap-2 snap-x snap-mandatory md:snap-none overflow-x-auto tabs"
  37. id="suggestions-container"
  38. >
  39. {#each prompts as prompt, promptIdx}
  40. <div class="snap-center shrink-0">
  41. <button
  42. class="flex flex-col flex-1 shrink-0 w-64 justify-between h-36 p-5 px-6 bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800 rounded-3xl transition group"
  43. on:click={() => {
  44. submitPrompt(prompt.content);
  45. }}
  46. >
  47. <div class="flex flex-col text-left">
  48. {#if prompt.title && prompt.title[0] !== ''}
  49. <div
  50. class=" font-medium dark:text-gray-300 dark:group-hover:text-gray-200 transition"
  51. >
  52. {prompt.title[0]}
  53. </div>
  54. <div class="text-sm text-gray-600 font-normal line-clamp-2">{prompt.title[1]}</div>
  55. {:else}
  56. <div
  57. class=" text-sm font-medium dark:text-gray-300 dark:group-hover:text-gray-100 transition line-clamp-2"
  58. >
  59. {prompt.content}
  60. </div>
  61. {/if}
  62. </div>
  63. <div class="w-full flex justify-between">
  64. <div
  65. class="text-xs text-gray-400 group-hover:text-gray-500 dark:text-gray-600 dark:group-hover:text-gray-500 transition self-center"
  66. >
  67. {$i18n.t('Prompt')}
  68. </div>
  69. <div
  70. class="self-end p-1 rounded-lg text-gray-300 group-hover:text-gray-800 dark:text-gray-700 dark:group-hover:text-gray-100 transition"
  71. >
  72. <svg
  73. xmlns="http://www.w3.org/2000/svg"
  74. viewBox="0 0 16 16"
  75. fill="currentColor"
  76. class="size-4"
  77. >
  78. <path
  79. fill-rule="evenodd"
  80. d="M8 14a.75.75 0 0 1-.75-.75V4.56L4.03 7.78a.75.75 0 0 1-1.06-1.06l4.5-4.5a.75.75 0 0 1 1.06 0l4.5 4.5a.75.75 0 0 1-1.06 1.06L8.75 4.56v8.69A.75.75 0 0 1 8 14Z"
  81. clip-rule="evenodd"
  82. />
  83. </svg>
  84. </div>
  85. </div>
  86. </button>
  87. </div>
  88. {/each}
  89. <!-- <div class="snap-center shrink-0">
  90. <img
  91. class="shrink-0 w-80 h-40 rounded-lg shadow-xl bg-white"
  92. src="https://images.unsplash.com/photo-1604999565976-8913ad2ddb7c?ixlib=rb-1.2.1&amp;ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&amp;auto=format&amp;fit=crop&amp;w=320&amp;h=160&amp;q=80"
  93. />
  94. </div> -->
  95. </div>
  96. </div>
  97. <style>
  98. .tabs::-webkit-scrollbar {
  99. display: none; /* for Chrome, Safari and Opera */
  100. }
  101. .tabs {
  102. -ms-overflow-style: none; /* IE and Edge */
  103. scrollbar-width: none; /* Firefox */
  104. }
  105. </style>