UserMenu.svelte 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <script lang="ts">
  2. import { DropdownMenu } from 'bits-ui';
  3. import { createEventDispatcher, getContext, onMount } from 'svelte';
  4. import { flyAndScale } from '$lib/utils/transitions';
  5. import { goto } from '$app/navigation';
  6. import ArchiveBox from '$lib/components/icons/ArchiveBox.svelte';
  7. import { showSettings, activeUserIds, USAGE_POOL, mobile, showSidebar, user } from '$lib/stores';
  8. import { fade, slide } from 'svelte/transition';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. import { userSignOut } from '$lib/apis/auths';
  11. import QuestionMarkCircle from '$lib/components/icons/QuestionMarkCircle.svelte';
  12. import Map from '$lib/components/icons/Map.svelte';
  13. import Keyboard from '$lib/components/icons/Keyboard.svelte';
  14. import ShortcutsModal from '$lib/components/chat/ShortcutsModal.svelte';
  15. const i18n = getContext('i18n');
  16. export let show = false;
  17. export let role = '';
  18. export let help = false;
  19. export let className = 'max-w-[240px]';
  20. let showShortcuts = false;
  21. const dispatch = createEventDispatcher();
  22. </script>
  23. <ShortcutsModal bind:show={showShortcuts} />
  24. <DropdownMenu.Root
  25. bind:open={show}
  26. onOpenChange={(state) => {
  27. dispatch('change', state);
  28. }}
  29. >
  30. <DropdownMenu.Trigger>
  31. <slot />
  32. </DropdownMenu.Trigger>
  33. <slot name="content">
  34. <DropdownMenu.Content
  35. class="w-full {className} text-sm rounded-xl px-1 py-1.5 z-50 bg-white dark:bg-gray-850 dark:text-white shadow-lg font-primary"
  36. sideOffset={4}
  37. side="bottom"
  38. align="start"
  39. transition={(e) => fade(e, { duration: 100 })}
  40. >
  41. <button
  42. class="flex rounded-md py-1.5 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  43. on:click={async () => {
  44. await showSettings.set(true);
  45. show = false;
  46. if ($mobile) {
  47. showSidebar.set(false);
  48. }
  49. }}
  50. >
  51. <div class=" self-center mr-3">
  52. <svg
  53. xmlns="http://www.w3.org/2000/svg"
  54. fill="none"
  55. viewBox="0 0 24 24"
  56. stroke-width="1.5"
  57. stroke="currentColor"
  58. class="w-5 h-5"
  59. >
  60. <path
  61. stroke-linecap="round"
  62. stroke-linejoin="round"
  63. d="M10.343 3.94c.09-.542.56-.94 1.11-.94h1.093c.55 0 1.02.398 1.11.94l.149.894c.07.424.384.764.78.93.398.164.855.142 1.205-.108l.737-.527a1.125 1.125 0 011.45.12l.773.774c.39.389.44 1.002.12 1.45l-.527.737c-.25.35-.272.806-.107 1.204.165.397.505.71.93.78l.893.15c.543.09.94.56.94 1.109v1.094c0 .55-.397 1.02-.94 1.11l-.893.149c-.425.07-.765.383-.93.78-.165.398-.143.854.107 1.204l.527.738c.32.447.269 1.06-.12 1.45l-.774.773a1.125 1.125 0 01-1.449.12l-.738-.527c-.35-.25-.806-.272-1.203-.107-.397.165-.71.505-.781.929l-.149.894c-.09.542-.56.94-1.11.94h-1.094c-.55 0-1.019-.398-1.11-.94l-.148-.894c-.071-.424-.384-.764-.781-.93-.398-.164-.854-.142-1.204.108l-.738.527c-.447.32-1.06.269-1.45-.12l-.773-.774a1.125 1.125 0 01-.12-1.45l.527-.737c.25-.35.273-.806.108-1.204-.165-.397-.505-.71-.93-.78l-.894-.15c-.542-.09-.94-.56-.94-1.109v-1.094c0-.55.398-1.02.94-1.11l.894-.149c.424-.07.765-.383.93-.78.165-.398.143-.854-.107-1.204l-.527-.738a1.125 1.125 0 01.12-1.45l.773-.773a1.125 1.125 0 011.45-.12l.737.527c.35.25.807.272 1.204.107.397-.165.71-.505.78-.929l.15-.894z"
  64. />
  65. <path
  66. stroke-linecap="round"
  67. stroke-linejoin="round"
  68. d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
  69. />
  70. </svg>
  71. </div>
  72. <div class=" self-center truncate">{$i18n.t('Settings')}</div>
  73. </button>
  74. <button
  75. class="flex rounded-md py-1.5 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  76. on:click={() => {
  77. dispatch('show', 'archived-chat');
  78. show = false;
  79. if ($mobile) {
  80. showSidebar.set(false);
  81. }
  82. }}
  83. >
  84. <div class=" self-center mr-3">
  85. <ArchiveBox className="size-5" strokeWidth="1.5" />
  86. </div>
  87. <div class=" self-center truncate">{$i18n.t('Archived Chats')}</div>
  88. </button>
  89. {#if role === 'admin'}
  90. <a
  91. class="flex rounded-md py-1.5 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  92. href="/playground"
  93. on:click={() => {
  94. show = false;
  95. if ($mobile) {
  96. showSidebar.set(false);
  97. }
  98. }}
  99. >
  100. <div class=" self-center mr-3">
  101. <svg
  102. xmlns="http://www.w3.org/2000/svg"
  103. fill="none"
  104. viewBox="0 0 24 24"
  105. stroke-width="1.5"
  106. stroke="currentColor"
  107. class="size-5"
  108. >
  109. <path
  110. stroke-linecap="round"
  111. stroke-linejoin="round"
  112. d="M14.25 9.75 16.5 12l-2.25 2.25m-4.5 0L7.5 12l2.25-2.25M6 20.25h12A2.25 2.25 0 0 0 20.25 18V6A2.25 2.25 0 0 0 18 3.75H6A2.25 2.25 0 0 0 3.75 6v12A2.25 2.25 0 0 0 6 20.25Z"
  113. />
  114. </svg>
  115. </div>
  116. <div class=" self-center truncate">{$i18n.t('Playground')}</div>
  117. </a>
  118. <a
  119. class="flex rounded-md py-1.5 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  120. href="/admin"
  121. on:click={() => {
  122. show = false;
  123. if ($mobile) {
  124. showSidebar.set(false);
  125. }
  126. }}
  127. >
  128. <div class=" self-center mr-3">
  129. <svg
  130. xmlns="http://www.w3.org/2000/svg"
  131. fill="none"
  132. viewBox="0 0 24 24"
  133. stroke-width="1.5"
  134. stroke="currentColor"
  135. class="w-5 h-5"
  136. >
  137. <path
  138. stroke-linecap="round"
  139. stroke-linejoin="round"
  140. d="M17.982 18.725A7.488 7.488 0 0012 15.75a7.488 7.488 0 00-5.982 2.975m11.963 0a9 9 0 10-11.963 0m11.963 0A8.966 8.966 0 0112 21a8.966 8.966 0 01-5.982-2.275M15 9.75a3 3 0 11-6 0 3 3 0 016 0z"
  141. />
  142. </svg>
  143. </div>
  144. <div class=" self-center truncate">{$i18n.t('Admin Panel')}</div>
  145. </a>
  146. {/if}
  147. {#if help}
  148. <hr class=" border-gray-100 dark:border-gray-800 my-1 p-0" />
  149. <!-- {$i18n.t('Help')} -->
  150. <DropdownMenu.Item
  151. class="flex gap-2 items-center py-1.5 px-3 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  152. id="chat-share-button"
  153. on:click={() => {
  154. window.open('https://docs.openwebui.com', '_blank');
  155. }}
  156. >
  157. <QuestionMarkCircle className="size-5" />
  158. <div class="flex items-center">{$i18n.t('Documentation')}</div>
  159. </DropdownMenu.Item>
  160. <!-- Releases -->
  161. <DropdownMenu.Item
  162. class="flex gap-2 items-center py-1.5 px-3 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  163. id="menu-item-releases"
  164. on:click={() => {
  165. window.open('https://github.com/open-webui/open-webui/releases', '_blank');
  166. }}
  167. >
  168. <Map className="size-5" />
  169. <div class="flex items-center">{$i18n.t('Releases')}</div>
  170. </DropdownMenu.Item>
  171. <DropdownMenu.Item
  172. class="flex gap-2 items-center py-1.5 px-3 text-sm cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-800 rounded-md"
  173. id="chat-share-button"
  174. on:click={() => {
  175. showShortcuts = !showShortcuts;
  176. show = false;
  177. }}
  178. >
  179. <Keyboard className="size-5" />
  180. <div class="flex items-center">{$i18n.t('Keyboard shortcuts')}</div>
  181. </DropdownMenu.Item>
  182. {/if}
  183. <hr class=" border-gray-100 dark:border-gray-800 my-1 p-0" />
  184. <button
  185. class="flex rounded-md py-1.5 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  186. on:click={async () => {
  187. const res = await userSignOut();
  188. user.set(null);
  189. localStorage.removeItem('token');
  190. location.href = res?.redirect_url ?? '/auth';
  191. show = false;
  192. }}
  193. >
  194. <div class=" self-center mr-3">
  195. <svg
  196. xmlns="http://www.w3.org/2000/svg"
  197. viewBox="0 0 20 20"
  198. fill="currentColor"
  199. class="w-5 h-5"
  200. >
  201. <path
  202. fill-rule="evenodd"
  203. d="M3 4.25A2.25 2.25 0 015.25 2h5.5A2.25 2.25 0 0113 4.25v2a.75.75 0 01-1.5 0v-2a.75.75 0 00-.75-.75h-5.5a.75.75 0 00-.75.75v11.5c0 .414.336.75.75.75h5.5a.75.75 0 00.75-.75v-2a.75.75 0 011.5 0v2A2.25 2.25 0 0110.75 18h-5.5A2.25 2.25 0 013 15.75V4.25z"
  204. clip-rule="evenodd"
  205. />
  206. <path
  207. fill-rule="evenodd"
  208. d="M6 10a.75.75 0 01.75-.75h9.546l-1.048-.943a.75.75 0 111.004-1.114l2.5 2.25a.75.75 0 010 1.114l-2.5 2.25a.75.75 0 11-1.004-1.114l1.048-.943H6.75A.75.75 0 016 10z"
  209. clip-rule="evenodd"
  210. />
  211. </svg>
  212. </div>
  213. <div class=" self-center truncate">{$i18n.t('Sign Out')}</div>
  214. </button>
  215. {#if $activeUserIds?.length > 0}
  216. <hr class=" border-gray-100 dark:border-gray-800 my-1 p-0" />
  217. <Tooltip
  218. content={$USAGE_POOL && $USAGE_POOL.length > 0
  219. ? `${$i18n.t('Running')}: ${$USAGE_POOL.join(', ')} ✨`
  220. : ''}
  221. >
  222. <div class="flex rounded-md py-1 px-3 text-xs gap-2.5 items-center">
  223. <div class=" flex items-center">
  224. <span class="relative flex size-2">
  225. <span
  226. class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"
  227. />
  228. <span class="relative inline-flex rounded-full size-2 bg-green-500" />
  229. </span>
  230. </div>
  231. <div class=" ">
  232. <span class="">
  233. {$i18n.t('Active Users')}:
  234. </span>
  235. <span class=" font-semibold">
  236. {$activeUserIds?.length}
  237. </span>
  238. </div>
  239. </div>
  240. </Tooltip>
  241. {/if}
  242. <!-- <DropdownMenu.Item class="flex items-center py-1.5 px-3 text-sm ">
  243. <div class="flex items-center">Profile</div>
  244. </DropdownMenu.Item> -->
  245. </DropdownMenu.Content>
  246. </slot>
  247. </DropdownMenu.Root>