ManageModal.svelte 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import dayjs from 'dayjs';
  4. import { getContext, createEventDispatcher } from 'svelte';
  5. const dispatch = createEventDispatcher();
  6. import Modal from '$lib/components/common/Modal.svelte';
  7. import AddMemoryModal from './AddMemoryModal.svelte';
  8. import { deleteMemoriesByUserId, deleteMemoryById, getMemories } from '$lib/apis/memories';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import { error } from '@sveltejs/kit';
  11. import EditMemoryModal from './EditMemoryModal.svelte';
  12. import localizedFormat from 'dayjs/plugin/localizedFormat';
  13. const i18n = getContext('i18n');
  14. dayjs.extend(localizedFormat);
  15. export let show = false;
  16. let memories = [];
  17. let loading = true;
  18. let showAddMemoryModal = false;
  19. let showEditMemoryModal = false;
  20. let selectedMemory = null;
  21. $: if (show && memories.length === 0 && loading) {
  22. (async () => {
  23. memories = await getMemories(localStorage.token);
  24. loading = false;
  25. })();
  26. }
  27. </script>
  28. <Modal size="xl" bind:show>
  29. <div>
  30. <div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-1">
  31. <div class=" text-lg font-medium self-center">{$i18n.t('Memory')}</div>
  32. <button
  33. class="self-center"
  34. on:click={() => {
  35. show = false;
  36. }}
  37. >
  38. <svg
  39. xmlns="http://www.w3.org/2000/svg"
  40. viewBox="0 0 20 20"
  41. fill="currentColor"
  42. class="w-5 h-5"
  43. >
  44. <path
  45. d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
  46. />
  47. </svg>
  48. </button>
  49. </div>
  50. <div class="flex flex-col w-full px-5 pb-5 dark:text-gray-200">
  51. <div
  52. class=" flex flex-col w-full sm:flex-row sm:justify-center sm:space-x-6 h-[28rem] max-h-screen outline outline-1 rounded-xl outline-gray-100 dark:outline-gray-800 mb-4 mt-1"
  53. >
  54. {#if memories.length > 0}
  55. <div class="text-left text-sm w-full mb-4 overflow-y-scroll">
  56. <div class="relative overflow-x-auto">
  57. <table class="w-full text-sm text-left text-gray-600 dark:text-gray-400 table-auto">
  58. <thead
  59. class="text-xs text-gray-700 uppercase bg-transparent dark:text-gray-200 border-b-2 dark:border-gray-800"
  60. >
  61. <tr>
  62. <th scope="col" class="px-3 py-2"> {$i18n.t('Name')} </th>
  63. <th scope="col" class="px-3 py-2 hidden md:flex">
  64. {$i18n.t('Last Modified')}
  65. </th>
  66. <th scope="col" class="px-3 py-2 text-right" />
  67. </tr>
  68. </thead>
  69. <tbody>
  70. {#each memories as memory}
  71. <tr class="border-b dark:border-gray-800 items-center">
  72. <td class="px-3 py-1">
  73. <div class="line-clamp-1">
  74. {memory.content}
  75. </div>
  76. </td>
  77. <td class=" px-3 py-1 hidden md:flex h-[2.5rem]">
  78. <div class="my-auto whitespace-nowrap">
  79. {dayjs(memory.updated_at * 1000).format('LLL')}
  80. </div>
  81. </td>
  82. <td class="px-3 py-1">
  83. <div class="flex justify-end w-full">
  84. <Tooltip content="Edit">
  85. <button
  86. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  87. on:click={() => {
  88. selectedMemory = memory;
  89. showEditMemoryModal = true;
  90. }}
  91. >
  92. <svg
  93. xmlns="http://www.w3.org/2000/svg"
  94. fill="none"
  95. viewBox="0 0 24 24"
  96. stroke-width="1.5"
  97. stroke="currentColor"
  98. class="w-4 h-4 s-FoVA_WMOgxUD"
  99. ><path
  100. stroke-linecap="round"
  101. stroke-linejoin="round"
  102. d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L6.832 19.82a4.5 4.5 0 0 1-1.897 1.13l-2.685.8.8-2.685a4.5 4.5 0 0 1 1.13-1.897L16.863 4.487Zm0 0L19.5 7.125"
  103. class="s-FoVA_WMOgxUD"
  104. /></svg
  105. >
  106. </button>
  107. </Tooltip>
  108. <Tooltip content="Delete">
  109. <button
  110. class="self-center w-fit text-sm px-2 py-2 hover:bg-black/5 dark:hover:bg-white/5 rounded-xl"
  111. on:click={async () => {
  112. const res = await deleteMemoryById(
  113. localStorage.token,
  114. memory.id
  115. ).catch((error) => {
  116. toast.error(`${error}`);
  117. return null;
  118. });
  119. if (res) {
  120. toast.success($i18n.t('Memory deleted successfully'));
  121. memories = await getMemories(localStorage.token);
  122. }
  123. }}
  124. >
  125. <svg
  126. xmlns="http://www.w3.org/2000/svg"
  127. fill="none"
  128. viewBox="0 0 24 24"
  129. stroke-width="1.5"
  130. stroke="currentColor"
  131. class="w-4 h-4"
  132. >
  133. <path
  134. stroke-linecap="round"
  135. stroke-linejoin="round"
  136. d="m14.74 9-.346 9m-4.788 0L9.26 9m9.968-3.21c.342.052.682.107 1.022.166m-1.022-.165L18.16 19.673a2.25 2.25 0 0 1-2.244 2.077H8.084a2.25 2.25 0 0 1-2.244-2.077L4.772 5.79m14.456 0a48.108 48.108 0 0 0-3.478-.397m-12 .562c.34-.059.68-.114 1.022-.165m0 0a48.11 48.11 0 0 1 3.478-.397m7.5 0v-.916c0-1.18-.91-2.164-2.09-2.201a51.964 51.964 0 0 0-3.32 0c-1.18.037-2.09 1.022-2.09 2.201v.916m7.5 0a48.667 48.667 0 0 0-7.5 0"
  137. />
  138. </svg>
  139. </button>
  140. </Tooltip>
  141. </div>
  142. </td>
  143. </tr>
  144. {/each}
  145. </tbody>
  146. </table>
  147. </div>
  148. </div>
  149. {:else}
  150. <div class="text-center flex h-full text-sm w-full">
  151. <div class=" my-auto pb-10 px-4 w-full text-gray-500">
  152. {$i18n.t('Memories accessible by LLMs will be shown here.')}
  153. </div>
  154. </div>
  155. {/if}
  156. </div>
  157. <div class="flex text-sm font-medium gap-1.5">
  158. <button
  159. class=" px-3.5 py-1.5 font-medium hover:bg-black/5 dark:hover:bg-white/5 outline outline-1 outline-gray-300 dark:outline-gray-800 rounded-3xl"
  160. on:click={() => {
  161. showAddMemoryModal = true;
  162. }}>{$i18n.t('Add Memory')}</button
  163. >
  164. <button
  165. class=" px-3.5 py-1.5 font-medium text-red-500 hover:bg-black/5 dark:hover:bg-white/5 outline outline-1 outline-red-300 dark:outline-red-800 rounded-3xl"
  166. on:click={async () => {
  167. const res = await deleteMemoriesByUserId(localStorage.token).catch((error) => {
  168. toast.error(`${error}`);
  169. return null;
  170. });
  171. if (res) {
  172. toast.success($i18n.t('Memory cleared successfully'));
  173. memories = [];
  174. }
  175. }}>{$i18n.t('Clear memory')}</button
  176. >
  177. </div>
  178. </div>
  179. </div>
  180. </Modal>
  181. <AddMemoryModal
  182. bind:show={showAddMemoryModal}
  183. on:save={async () => {
  184. memories = await getMemories(localStorage.token);
  185. }}
  186. />
  187. <EditMemoryModal
  188. bind:show={showEditMemoryModal}
  189. memory={selectedMemory}
  190. on:save={async () => {
  191. memories = await getMemories(localStorage.token);
  192. }}
  193. />