CreateKnowledgeBase.svelte 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <script>
  2. import { goto } from '$app/navigation';
  3. import { getContext } from 'svelte';
  4. const i18n = getContext('i18n');
  5. import { createNewKnowledge, getKnowledgeBases } from '$lib/apis/knowledge';
  6. import { toast } from 'svelte-sonner';
  7. import { knowledge } from '$lib/stores';
  8. import AccessControl from '../common/AccessControl.svelte';
  9. let loading = false;
  10. let name = '';
  11. let description = '';
  12. let accessControl = null;
  13. const submitHandler = async () => {
  14. loading = true;
  15. if (name.trim() === '' || description.trim() === '') {
  16. toast.error($i18n.t('Please fill in all fields.'));
  17. name = '';
  18. description = '';
  19. loading = false;
  20. return;
  21. }
  22. const res = await createNewKnowledge(
  23. localStorage.token,
  24. name,
  25. description,
  26. accessControl
  27. ).catch((e) => {
  28. toast.error(`${e}`);
  29. });
  30. if (res) {
  31. toast.success($i18n.t('Knowledge created successfully.'));
  32. knowledge.set(await getKnowledgeBases(localStorage.token));
  33. goto(`/workspace/knowledge/${res.id}`);
  34. }
  35. loading = false;
  36. };
  37. </script>
  38. <div class="w-full max-h-full">
  39. <button
  40. class="flex space-x-1"
  41. on:click={() => {
  42. goto('/workspace/knowledge');
  43. }}
  44. >
  45. <div class=" self-center">
  46. <svg
  47. xmlns="http://www.w3.org/2000/svg"
  48. viewBox="0 0 20 20"
  49. fill="currentColor"
  50. class="w-4 h-4"
  51. >
  52. <path
  53. fill-rule="evenodd"
  54. d="M17 10a.75.75 0 01-.75.75H5.612l4.158 3.96a.75.75 0 11-1.04 1.08l-5.5-5.25a.75.75 0 010-1.08l5.5-5.25a.75.75 0 111.04 1.08L5.612 9.25H16.25A.75.75 0 0117 10z"
  55. clip-rule="evenodd"
  56. />
  57. </svg>
  58. </div>
  59. <div class=" self-center font-medium text-sm">{$i18n.t('Back')}</div>
  60. </button>
  61. <form
  62. class="flex flex-col max-w-lg mx-auto mt-10 mb-10"
  63. on:submit|preventDefault={() => {
  64. submitHandler();
  65. }}
  66. >
  67. <div class=" w-full flex flex-col justify-center">
  68. <div class=" text-2xl font-medium font-primary mb-2.5">
  69. {$i18n.t('Create a knowledge base')}
  70. </div>
  71. <div class="w-full flex flex-col gap-2.5">
  72. <div class="w-full">
  73. <div class=" text-sm mb-2">{$i18n.t('What are you working on?')}</div>
  74. <div class="w-full mt-1">
  75. <input
  76. class="w-full rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  77. type="text"
  78. bind:value={name}
  79. placeholder={$i18n.t('Name your knowledge base')}
  80. required
  81. />
  82. </div>
  83. </div>
  84. <div>
  85. <div class="text-sm mb-2">{$i18n.t('What are you trying to achieve?')}</div>
  86. <div class=" w-full mt-1">
  87. <textarea
  88. class="w-full resize-none rounded-lg py-2 px-4 text-sm bg-gray-50 dark:text-gray-300 dark:bg-gray-850 outline-none"
  89. rows="4"
  90. bind:value={description}
  91. placeholder={$i18n.t('Describe your knowledge base and objectives')}
  92. required
  93. />
  94. </div>
  95. </div>
  96. </div>
  97. </div>
  98. <div class="mt-2">
  99. <div class="px-3 py-2 bg-gray-50 dark:bg-gray-950 rounded-lg">
  100. <AccessControl bind:accessControl accessRoles={['read', 'write']} />
  101. </div>
  102. </div>
  103. <div class="flex justify-end mt-2">
  104. <div>
  105. <button
  106. class=" text-sm px-4 py-2 transition rounded-lg {loading
  107. ? ' cursor-not-allowed bg-gray-100 dark:bg-gray-800'
  108. : ' bg-gray-50 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-800'} flex"
  109. type="submit"
  110. disabled={loading}
  111. >
  112. <div class=" self-center font-medium">{$i18n.t('Create Knowledge')}</div>
  113. {#if loading}
  114. <div class="ml-1.5 self-center">
  115. <svg
  116. class=" w-4 h-4"
  117. viewBox="0 0 24 24"
  118. fill="currentColor"
  119. xmlns="http://www.w3.org/2000/svg"
  120. ><style>
  121. .spinner_ajPY {
  122. transform-origin: center;
  123. animation: spinner_AtaB 0.75s infinite linear;
  124. }
  125. @keyframes spinner_AtaB {
  126. 100% {
  127. transform: rotate(360deg);
  128. }
  129. }
  130. </style><path
  131. d="M12,1A11,11,0,1,0,23,12,11,11,0,0,0,12,1Zm0,19a8,8,0,1,1,8-8A8,8,0,0,1,12,20Z"
  132. opacity=".25"
  133. /><path
  134. d="M10.14,1.16a11,11,0,0,0-9,8.92A1.59,1.59,0,0,0,2.46,12,1.52,1.52,0,0,0,4.11,10.7a8,8,0,0,1,6.66-6.61A1.42,1.42,0,0,0,12,2.69h0A1.57,1.57,0,0,0,10.14,1.16Z"
  135. class="spinner_ajPY"
  136. /></svg
  137. >
  138. </div>
  139. {/if}
  140. </button>
  141. </div>
  142. </div>
  143. </form>
  144. </div>