Database.svelte 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <script lang="ts">
  2. import fileSaver from 'file-saver';
  3. const { saveAs } = fileSaver;
  4. import { downloadDatabase } from '$lib/apis/utils';
  5. import { onMount, getContext } from 'svelte';
  6. import { config, user } from '$lib/stores';
  7. import { toast } from 'svelte-sonner';
  8. import { getAllUserChats } from '$lib/apis/chats';
  9. const i18n = getContext('i18n');
  10. export let saveHandler: Function;
  11. const exportAllUserChats = async () => {
  12. let blob = new Blob([JSON.stringify(await getAllUserChats(localStorage.token))], {
  13. type: 'application/json'
  14. });
  15. saveAs(blob, `all-chats-export-${Date.now()}.json`);
  16. };
  17. onMount(async () => {
  18. // permissions = await getUserPermissions(localStorage.token);
  19. });
  20. </script>
  21. <form
  22. class="flex flex-col h-full justify-between space-y-3 text-sm"
  23. on:submit|preventDefault={async () => {
  24. saveHandler();
  25. }}
  26. >
  27. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-80">
  28. <div>
  29. <div class=" mb-2 text-sm font-medium">{$i18n.t('Database')}</div>
  30. {#if $config?.enable_admin_export ?? true}
  31. <div class=" flex w-full justify-between">
  32. <!-- <div class=" self-center text-xs font-medium">{$i18n.t('Allow Chat Deletion')}</div> -->
  33. <button
  34. class=" flex rounded-md py-1.5 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  35. type="button"
  36. on:click={() => {
  37. // exportAllUserChats();
  38. downloadDatabase(localStorage.token).catch((error) => {
  39. toast.error(error);
  40. });
  41. }}
  42. >
  43. <div class=" self-center mr-3">
  44. <svg
  45. xmlns="http://www.w3.org/2000/svg"
  46. viewBox="0 0 16 16"
  47. fill="currentColor"
  48. class="w-4 h-4"
  49. >
  50. <path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
  51. <path
  52. fill-rule="evenodd"
  53. d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM8.75 7.75a.75.75 0 0 0-1.5 0v2.69L6.03 9.22a.75.75 0 0 0-1.06 1.06l2.5 2.5a.75.75 0 0 0 1.06 0l2.5-2.5a.75.75 0 1 0-1.06-1.06l-1.22 1.22V7.75Z"
  54. clip-rule="evenodd"
  55. />
  56. </svg>
  57. </div>
  58. <div class=" self-center text-sm font-medium">{$i18n.t('Download Database')}</div>
  59. </button>
  60. </div>
  61. <hr class=" dark:border-gray-700 my-1" />
  62. <button
  63. class=" flex rounded-md py-2 px-3.5 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
  64. on:click={() => {
  65. exportAllUserChats();
  66. }}
  67. >
  68. <div class=" self-center mr-3">
  69. <svg
  70. xmlns="http://www.w3.org/2000/svg"
  71. viewBox="0 0 16 16"
  72. fill="currentColor"
  73. class="w-4 h-4"
  74. >
  75. <path d="M2 3a1 1 0 0 1 1-1h10a1 1 0 0 1 1 1v1a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1V3Z" />
  76. <path
  77. fill-rule="evenodd"
  78. d="M13 6H3v6a2 2 0 0 0 2 2h6a2 2 0 0 0 2-2V6ZM8.75 7.75a.75.75 0 0 0-1.5 0v2.69L6.03 9.22a.75.75 0 0 0-1.06 1.06l2.5 2.5a.75.75 0 0 0 1.06 0l2.5-2.5a.75.75 0 1 0-1.06-1.06l-1.22 1.22V7.75Z"
  79. clip-rule="evenodd"
  80. />
  81. </svg>
  82. </div>
  83. <div class=" self-center text-sm font-medium">
  84. {$i18n.t('Export All Chats (All Users)')}
  85. </div>
  86. </button>
  87. {/if}
  88. </div>
  89. </div>
  90. <!-- <div class="flex justify-end pt-3 text-sm font-medium">
  91. <button
  92. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  93. type="submit"
  94. >
  95. Save
  96. </button>
  97. </div> -->
  98. </form>