InputMenu.svelte 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. const detectMobile = () => {
  45. const userAgent = navigator.userAgent || navigator.vendor || window.opera;
  46. return /android|iphone|ipad|ipod|windows phone/i.test(userAgent);
  47. };
  48. function handleFileChange(event) {
  49. const inputFiles = Array.from(event.target?.files);
  50. if (inputFiles && inputFiles.length > 0) {
  51. console.log(inputFiles);
  52. inputFilesHandler(inputFiles);
  53. }
  54. }
  55. </script>
  56. <!-- Hidden file input used to open the camera on mobile -->
  57. <input
  58. id="camera-input"
  59. type="file"
  60. accept="image/*"
  61. capture="environment"
  62. on:change={handleFileChange}
  63. style="display: none;"
  64. />
  65. <Dropdown
  66. bind:show
  67. on:change={(e) => {
  68. if (e.detail === false) {
  69. onClose();
  70. }
  71. }}
  72. >
  73. <Tooltip content={$i18n.t('More')}>
  74. <slot />
  75. </Tooltip>
  76. <div slot="content">
  77. <DropdownMenu.Content
  78. 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"
  79. sideOffset={15}
  80. alignOffset={-8}
  81. side="top"
  82. align="start"
  83. transition={flyAndScale}
  84. >
  85. {#if Object.keys(tools).length > 0}
  86. <div class=" max-h-28 overflow-y-auto scrollbar-hidden">
  87. {#each Object.keys(tools) as toolId}
  88. <button
  89. class="flex w-full justify-between gap-2 items-center px-3 py-2 text-sm font-medium cursor-pointer rounded-xl"
  90. on:click={() => {
  91. tools[toolId].enabled = !tools[toolId].enabled;
  92. }}
  93. >
  94. <div class="flex-1 truncate">
  95. <Tooltip
  96. content={tools[toolId]?.description ?? ''}
  97. placement="top-start"
  98. className="flex flex-1 gap-2 items-center"
  99. >
  100. <div class="flex-shrink-0">
  101. <WrenchSolid />
  102. </div>
  103. <div class=" truncate">{tools[toolId].name}</div>
  104. </Tooltip>
  105. </div>
  106. <div class=" flex-shrink-0">
  107. <Switch
  108. state={tools[toolId].enabled}
  109. on:change={async (e) => {
  110. const state = e.detail;
  111. await tick();
  112. if (state) {
  113. selectedToolIds = [...selectedToolIds, toolId];
  114. } else {
  115. selectedToolIds = selectedToolIds.filter((id) => id !== toolId);
  116. }
  117. }}
  118. />
  119. </div>
  120. </button>
  121. {/each}
  122. </div>
  123. <hr class="border-black/5 dark:border-white/5 my-1" />
  124. {/if}
  125. <Tooltip
  126. content={!fileUploadEnabled ? $i18n.t('You do not have permission to upload files') : ''}
  127. className="w-full"
  128. >
  129. <DropdownMenu.Item
  130. 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
  131. ? 'opacity-50'
  132. : ''}"
  133. on:click={() => {
  134. if (fileUploadEnabled) {
  135. if (!detectMobile()) {
  136. screenCaptureHandler();
  137. } else {
  138. const cameraInputElement = document.getElementById('camera-input');
  139. if (cameraInputElement) {
  140. cameraInputElement.click();
  141. }
  142. }
  143. }
  144. }}
  145. >
  146. <CameraSolid />
  147. <div class=" line-clamp-1">{$i18n.t('Capture')}</div>
  148. </DropdownMenu.Item>
  149. </Tooltip>
  150. <Tooltip
  151. content={!fileUploadEnabled ? $i18n.t('You do not have permission to upload files') : ''}
  152. className="w-full"
  153. >
  154. <DropdownMenu.Item
  155. 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
  156. ? 'opacity-50'
  157. : ''}"
  158. on:click={() => {
  159. if (fileUploadEnabled) {
  160. uploadFilesHandler();
  161. }
  162. }}
  163. >
  164. <DocumentArrowUpSolid />
  165. <div class="line-clamp-1">{$i18n.t('Upload Files')}</div>
  166. </DropdownMenu.Item>
  167. </Tooltip>
  168. {#if $config?.features?.enable_google_drive_integration}
  169. <DropdownMenu.Item
  170. 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"
  171. on:click={() => {
  172. uploadGoogleDriveHandler();
  173. }}
  174. >
  175. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 87.3 78" class="w-5 h-5">
  176. <path
  177. 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"
  178. fill="#0066da"
  179. />
  180. <path
  181. 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"
  182. fill="#00ac47"
  183. />
  184. <path
  185. 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"
  186. fill="#ea4335"
  187. />
  188. <path
  189. 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"
  190. fill="#00832d"
  191. />
  192. <path
  193. 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"
  194. fill="#2684fc"
  195. />
  196. <path
  197. 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"
  198. fill="#ffba00"
  199. />
  200. </svg>
  201. <div class="line-clamp-1">{$i18n.t('Google Drive')}</div>
  202. </DropdownMenu.Item>
  203. {/if}
  204. </DropdownMenu.Content>
  205. </div>
  206. </Dropdown>