InputMenu.svelte 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <script lang="ts">
  2. import { DropdownMenu } from 'bits-ui';
  3. import { flyAndScale } from '$lib/utils/transitions';
  4. import { getContext, onMount, tick } from 'svelte';
  5. import { config, user, tools as _tools, mobile } from '$lib/stores';
  6. import { createPicker } from '$lib/utils/google-drive-picker';
  7. import { getTools } from '$lib/apis/tools';
  8. import Dropdown from '$lib/components/common/Dropdown.svelte';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import DocumentArrowUpSolid from '$lib/components/icons/DocumentArrowUpSolid.svelte';
  11. import Switch from '$lib/components/common/Switch.svelte';
  12. import GlobeAltSolid from '$lib/components/icons/GlobeAltSolid.svelte';
  13. import WrenchSolid from '$lib/components/icons/WrenchSolid.svelte';
  14. import CameraSolid from '$lib/components/icons/CameraSolid.svelte';
  15. import PhotoSolid from '$lib/components/icons/PhotoSolid.svelte';
  16. import CommandLineSolid from '$lib/components/icons/CommandLineSolid.svelte';
  17. const i18n = getContext('i18n');
  18. export let screenCaptureHandler: Function;
  19. export let uploadFilesHandler: Function;
  20. export let inputFilesHandler: Function;
  21. export let uploadGoogleDriveHandler: Function;
  22. export let selectedToolIds: string[] = [];
  23. export let onClose: Function;
  24. let tools = {};
  25. let show = false;
  26. $: if (show) {
  27. init();
  28. }
  29. let fileUploadEnabled = true;
  30. $: fileUploadEnabled = $user.role === 'admin' || $user?.permissions?.chat?.file_upload;
  31. const init = async () => {
  32. if ($_tools === null) {
  33. await _tools.set(await getTools(localStorage.token));
  34. }
  35. tools = $_tools.reduce((a, tool, i, arr) => {
  36. a[tool.id] = {
  37. name: tool.name,
  38. description: tool.meta.description,
  39. enabled: selectedToolIds.includes(tool.id)
  40. };
  41. return a;
  42. }, {});
  43. };
  44. function handleFileChange(event) {
  45. const inputFiles = Array.from(event.target?.files);
  46. if (inputFiles && inputFiles.length > 0) {
  47. console.log(inputFiles);
  48. inputFilesHandler(inputFiles);
  49. }
  50. }
  51. </script>
  52. <!-- Hidden file input used to open the camera on mobile -->
  53. <input
  54. id="camera-input"
  55. type="file"
  56. accept="image/*"
  57. capture="environment"
  58. on:change={handleFileChange}
  59. style="display: none;"
  60. />
  61. <Dropdown
  62. bind:show
  63. on:change={(e) => {
  64. if (e.detail === false) {
  65. onClose();
  66. }
  67. }}
  68. >
  69. <Tooltip content={$i18n.t('More')}>
  70. <slot />
  71. </Tooltip>
  72. <div slot="content">
  73. <DropdownMenu.Content
  74. class="w-full max-w-[220px] rounded-xl px-1 py-1 border-gray-300/30 dark:border-gray-700/50 z-50 bg-white dark:bg-gray-850 dark:text-white shadow"
  75. sideOffset={15}
  76. alignOffset={-8}
  77. side="top"
  78. align="start"
  79. transition={flyAndScale}
  80. >
  81. {#if Object.keys(tools).length > 0}
  82. <div class=" max-h-28 overflow-y-auto scrollbar-hidden">
  83. {#each Object.keys(tools) as toolId}
  84. <button
  85. class="flex w-full justify-between gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
  86. on:click={() => {
  87. tools[toolId].enabled = !tools[toolId].enabled;
  88. }}
  89. >
  90. <div class="flex-1 truncate">
  91. <Tooltip
  92. content={tools[toolId]?.description ?? ''}
  93. placement="top-start"
  94. className="flex flex-1 gap-2 items-center"
  95. >
  96. <div class="flex-shrink-0">
  97. <WrenchSolid />
  98. </div>
  99. <div class=" truncate">{tools[toolId].name}</div>
  100. </Tooltip>
  101. </div>
  102. <div class=" flex-shrink-0">
  103. <Switch
  104. state={tools[toolId].enabled}
  105. on:change={async (e) => {
  106. const state = e.detail;
  107. await tick();
  108. if (state) {
  109. selectedToolIds = [...selectedToolIds, toolId];
  110. } else {
  111. selectedToolIds = selectedToolIds.filter((id) => id !== toolId);
  112. }
  113. }}
  114. />
  115. </div>
  116. </button>
  117. {/each}
  118. </div>
  119. <hr class="border-black/5 dark:border-white/5 my-1" />
  120. {/if}
  121. <Tooltip
  122. content={!fileUploadEnabled ? $i18n.t('You do not have permission to upload files') : ''}
  123. className="w-full"
  124. >
  125. <DropdownMenu.Item
  126. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl {!fileUploadEnabled
  127. ? 'opacity-50'
  128. : ''}"
  129. on:click={() => {
  130. if (fileUploadEnabled) {
  131. if (!$mobile) {
  132. screenCaptureHandler();
  133. } else {
  134. const cameraInputElement = document.getElementById('camera-input');
  135. if (cameraInputElement) {
  136. cameraInputElement.click();
  137. }
  138. }
  139. }
  140. }}
  141. >
  142. <CameraSolid />
  143. <div class=" line-clamp-1">{$i18n.t('Capture')}</div>
  144. </DropdownMenu.Item>
  145. </Tooltip>
  146. <Tooltip
  147. content={!fileUploadEnabled ? $i18n.t('You do not have permission to upload files') : ''}
  148. className="w-full"
  149. >
  150. <DropdownMenu.Item
  151. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl {!fileUploadEnabled
  152. ? 'opacity-50'
  153. : ''}"
  154. on:click={() => {
  155. if (fileUploadEnabled) {
  156. uploadFilesHandler();
  157. }
  158. }}
  159. >
  160. <DocumentArrowUpSolid />
  161. <div class="line-clamp-1">{$i18n.t('Upload Files')}</div>
  162. </DropdownMenu.Item>
  163. </Tooltip>
  164. {#if $config?.features?.enable_google_drive_integration}
  165. <DropdownMenu.Item
  166. class="flex gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-xl"
  167. on:click={() => {
  168. uploadGoogleDriveHandler();
  169. }}
  170. >
  171. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 87.3 78" class="w-5 h-5">
  172. <path
  173. d="m6.6 66.85 3.85 6.65c.8 1.4 1.95 2.5 3.3 3.3l13.75-23.8h-27.5c0 1.55.4 3.1 1.2 4.5z"
  174. fill="#0066da"
  175. />
  176. <path
  177. d="m43.65 25-13.75-23.8c-1.35.8-2.5 1.9-3.3 3.3l-25.4 44a9.06 9.06 0 0 0 -1.2 4.5h27.5z"
  178. fill="#00ac47"
  179. />
  180. <path
  181. d="m73.55 76.8c1.35-.8 2.5-1.9 3.3-3.3l1.6-2.75 7.65-13.25c.8-1.4 1.2-2.95 1.2-4.5h-27.502l5.852 11.5z"
  182. fill="#ea4335"
  183. />
  184. <path
  185. d="m43.65 25 13.75-23.8c-1.35-.8-2.9-1.2-4.5-1.2h-18.5c-1.6 0-3.15.45-4.5 1.2z"
  186. fill="#00832d"
  187. />
  188. <path
  189. d="m59.8 53h-32.3l-13.75 23.8c1.35.8 2.9 1.2 4.5 1.2h50.8c1.6 0 3.15-.45 4.5-1.2z"
  190. fill="#2684fc"
  191. />
  192. <path
  193. d="m73.4 26.5-12.7-22c-.8-1.4-1.95-2.5-3.3-3.3l-13.75 23.8 16.15 28h27.45c0-1.55-.4-3.1-1.2-4.5z"
  194. fill="#ffba00"
  195. />
  196. </svg>
  197. <div class="line-clamp-1">{$i18n.t('Google Drive')}</div>
  198. </DropdownMenu.Item>
  199. {/if}
  200. </DropdownMenu.Content>
  201. </div>
  202. </Dropdown>