1
0

Notes.svelte 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import fileSaver from 'file-saver';
  4. const { saveAs } = fileSaver;
  5. import { goto } from '$app/navigation';
  6. import { onMount, getContext } from 'svelte';
  7. import { WEBUI_NAME, config, prompts as _prompts, user } from '$lib/stores';
  8. import { getNotes } from '$lib/apis/notes';
  9. import EllipsisHorizontal from '../icons/EllipsisHorizontal.svelte';
  10. import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
  11. import Search from '../icons/Search.svelte';
  12. import Plus from '../icons/Plus.svelte';
  13. import ChevronRight from '../icons/ChevronRight.svelte';
  14. import Spinner from '../common/Spinner.svelte';
  15. import Tooltip from '../common/Tooltip.svelte';
  16. import { capitalizeFirstLetter } from '$lib/utils';
  17. const i18n = getContext('i18n');
  18. let loaded = false;
  19. let importFiles = '';
  20. let query = '';
  21. let notes = [];
  22. let selectedNote = null;
  23. let showDeleteConfirm = false;
  24. const init = async () => {
  25. notes = await getNotes(localStorage.token);
  26. console.log(notes);
  27. };
  28. onMount(async () => {
  29. await init();
  30. loaded = true;
  31. });
  32. </script>
  33. <svelte:head>
  34. <title>
  35. {$i18n.t('Notes')} • {$WEBUI_NAME}
  36. </title>
  37. </svelte:head>
  38. {#if loaded}
  39. <DeleteConfirmDialog
  40. bind:show={showDeleteConfirm}
  41. title={$i18n.t('Delete note?')}
  42. on:confirm={() => {}}
  43. >
  44. <div class=" text-sm text-gray-500">
  45. {$i18n.t('This will delete')} <span class=" font-semibold">{selectedNote.title}</span>.
  46. </div>
  47. </DeleteConfirmDialog>
  48. {#if notes.length > 0}
  49. <div class="flex flex-col gap-1 my-1.5">
  50. <!-- <div class="flex justify-between items-center">
  51. <div class="flex md:self-center text-xl font-medium px-0.5 items-center">
  52. {$i18n.t('Notes')}
  53. <div class="flex self-center w-[1px] h-6 mx-2.5 bg-gray-50 dark:bg-gray-850" />
  54. <span class="text-lg font-medium text-gray-500 dark:text-gray-300">{notes.length}</span>
  55. </div>
  56. </div> -->
  57. <div class=" flex w-full space-x-2">
  58. <div class="flex flex-1">
  59. <div class=" self-center ml-1 mr-3">
  60. <Search className="size-3.5" />
  61. </div>
  62. <input
  63. class=" w-full text-sm pr-4 py-1 rounded-r-xl outline-hidden bg-transparent"
  64. bind:value={query}
  65. placeholder={$i18n.t('Search Notes')}
  66. />
  67. </div>
  68. </div>
  69. </div>
  70. <div class="mb-5 gap-2 grid lg:grid-cols-2 xl:grid-cols-3">
  71. {#each notes as note}
  72. <div
  73. class=" flex space-x-4 cursor-pointer w-full px-3 py-2 dark:hover:bg-white/5 hover:bg-black/5 rounded-xl transition"
  74. >
  75. <div class=" flex flex-1 space-x-4 cursor-pointer w-full">
  76. <a href={`/notes/${note.id}`}>
  77. <div class=" flex-1 flex items-center gap-2 self-center">
  78. <div class=" font-semibold line-clamp-1 capitalize">{note.title}</div>
  79. </div>
  80. <div class=" text-xs px-0.5">
  81. <Tooltip
  82. content={note?.user?.email ?? $i18n.t('Deleted User')}
  83. className="flex shrink-0"
  84. placement="top-start"
  85. >
  86. <div class="shrink-0 text-gray-500">
  87. {$i18n.t('By {{name}}', {
  88. name: capitalizeFirstLetter(
  89. note?.user?.name ?? note?.user?.email ?? $i18n.t('Deleted User')
  90. )
  91. })}
  92. </div>
  93. </Tooltip>
  94. </div>
  95. </a>
  96. </div>
  97. </div>
  98. {/each}
  99. </div>
  100. {:else}
  101. <div class="w-full h-full flex flex-col items-center justify-center">
  102. <div class="pb-20 text-center">
  103. <div class=" text-xl font-medium text-gray-400 dark:text-gray-600">
  104. {$i18n.t('No Notes')}
  105. </div>
  106. <div class="mt-1 text-sm text-gray-300 dark:text-gray-700">
  107. {$i18n.t('Create your first note by clicking on the plus button below.')}
  108. </div>
  109. </div>
  110. </div>
  111. {/if}
  112. <div class="absolute bottom-0 left-0 right-0 p-5 max-w-full flex justify-end">
  113. <div class="flex gap-0.5 justify-end w-full">
  114. <Tooltip content={$i18n.t('Create Note')}>
  115. <button
  116. class="cursor-pointer p-2.5 flex rounded-full bg-gray-50 dark:bg-gray-850 hover:bg-gray-100 dark:hover:bg-gray-800 transition shadow-xl"
  117. type="button"
  118. on:click={async () => {}}
  119. >
  120. <Plus className="size-4.5" strokeWidth="2.5" />
  121. </button>
  122. </Tooltip>
  123. <!-- <button
  124. class="cursor-pointer p-2.5 flex rounded-full hover:bg-gray-100 dark:hover:bg-gray-850 transition shadow-xl"
  125. >
  126. <SparklesSolid className="size-4" />
  127. </button> -->
  128. </div>
  129. </div>
  130. <!-- {#if $user?.role === 'admin'}
  131. <div class=" flex justify-end w-full mb-3">
  132. <div class="flex space-x-2">
  133. <input
  134. id="notes-import-input"
  135. bind:files={importFiles}
  136. type="file"
  137. accept=".md"
  138. hidden
  139. on:change={() => {
  140. console.log(importFiles);
  141. const reader = new FileReader();
  142. reader.onload = async (event) => {
  143. console.log(event.target.result);
  144. };
  145. reader.readAsText(importFiles[0]);
  146. }}
  147. />
  148. <button
  149. class="flex text-xs items-center space-x-1 px-3 py-1.5 rounded-xl bg-gray-50 hover:bg-gray-100 dark:bg-gray-800 dark:hover:bg-gray-700 dark:text-gray-200 transition"
  150. on:click={() => {
  151. const notesImportInputElement = document.getElementById('notes-import-input');
  152. if (notesImportInputElement) {
  153. notesImportInputElement.click();
  154. }
  155. }}
  156. >
  157. <div class=" self-center mr-2 font-medium line-clamp-1">{$i18n.t('Import Notes')}</div>
  158. <div class=" self-center">
  159. <svg
  160. xmlns="http://www.w3.org/2000/svg"
  161. viewBox="0 0 16 16"
  162. fill="currentColor"
  163. class="w-4 h-4"
  164. >
  165. <path
  166. fill-rule="evenodd"
  167. d="M4 2a1.5 1.5 0 0 0-1.5 1.5v9A1.5 1.5 0 0 0 4 14h8a1.5 1.5 0 0 0 1.5-1.5V6.621a1.5 1.5 0 0 0-.44-1.06L9.94 2.439A1.5 1.5 0 0 0 8.878 2H4Zm4 9.5a.75.75 0 0 1-.75-.75V8.06l-.72.72a.75.75 0 0 1-1.06-1.06l2-2a.75.75 0 0 1 1.06 0l2 2a.75.75 0 1 1-1.06 1.06l-.72-.72v2.69a.75.75 0 0 1-.75.75Z"
  168. clip-rule="evenodd"
  169. />
  170. </svg>
  171. </div>
  172. </button>
  173. </div>
  174. </div>
  175. {/if} -->
  176. {:else}
  177. <div class="w-full h-full flex justify-center items-center">
  178. <Spinner />
  179. </div>
  180. {/if}