UserMenu.svelte 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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, activeUserCount, USAGE_POOL, mobile, showSidebar } from '$lib/stores';
  8. import { fade, slide } from 'svelte/transition';
  9. import Tooltip from '$lib/components/common/Tooltip.svelte';
  10. const i18n = getContext('i18n');
  11. export let show = false;
  12. export let role = '';
  13. export let className = 'max-w-[240px]';
  14. const dispatch = createEventDispatcher();
  15. </script>
  16. <DropdownMenu.Root
  17. bind:open={show}
  18. onOpenChange={(state) => {
  19. dispatch('change', state);
  20. }}
  21. >
  22. <DropdownMenu.Trigger>
  23. <slot />
  24. </DropdownMenu.Trigger>
  25. <slot name="content">
  26. <DropdownMenu.Content
  27. class="w-full {className} text-sm rounded-xl px-1 py-1.5 border border-gray-300/30 dark:border-gray-700/50 z-50 bg-white dark:bg-gray-850 dark:text-white shadow font-primary"
  28. sideOffset={8}
  29. side="bottom"
  30. align="start"
  31. transition={(e) => fade(e, { duration: 100 })}
  32. >
  33. <button
  34. class="flex rounded-md py-2 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  35. on:click={async () => {
  36. await showSettings.set(true);
  37. show = false;
  38. if ($mobile) {
  39. showSidebar.set(false);
  40. }
  41. }}
  42. >
  43. <div class=" self-center mr-3">
  44. <svg
  45. xmlns="http://www.w3.org/2000/svg"
  46. fill="none"
  47. viewBox="0 0 24 24"
  48. stroke-width="1.5"
  49. stroke="currentColor"
  50. class="w-5 h-5"
  51. >
  52. <path
  53. stroke-linecap="round"
  54. stroke-linejoin="round"
  55. 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"
  56. />
  57. <path
  58. stroke-linecap="round"
  59. stroke-linejoin="round"
  60. d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
  61. />
  62. </svg>
  63. </div>
  64. <div class=" self-center font-medium">{$i18n.t('Settings')}</div>
  65. </button>
  66. <button
  67. class="flex rounded-md py-2 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  68. on:click={() => {
  69. dispatch('show', 'archived-chat');
  70. show = false;
  71. if ($mobile) {
  72. showSidebar.set(false);
  73. }
  74. }}
  75. >
  76. <div class=" self-center mr-3">
  77. <ArchiveBox className="size-5" strokeWidth="1.5" />
  78. </div>
  79. <div class=" self-center font-medium">{$i18n.t('Archived Chats')}</div>
  80. </button>
  81. {#if role === 'admin'}
  82. <button
  83. class="flex rounded-md py-2 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  84. on:click={() => {
  85. goto('/playground');
  86. show = false;
  87. if ($mobile) {
  88. showSidebar.set(false);
  89. }
  90. }}
  91. >
  92. <div class=" self-center mr-3">
  93. <svg
  94. xmlns="http://www.w3.org/2000/svg"
  95. fill="none"
  96. viewBox="0 0 24 24"
  97. stroke-width="1.5"
  98. stroke="currentColor"
  99. class="size-5"
  100. >
  101. <path
  102. stroke-linecap="round"
  103. stroke-linejoin="round"
  104. 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"
  105. />
  106. </svg>
  107. </div>
  108. <div class=" self-center font-medium">{$i18n.t('Playground')}</div>
  109. </button>
  110. <button
  111. class="flex rounded-md py-2 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  112. on:click={() => {
  113. goto('/admin');
  114. show = false;
  115. if ($mobile) {
  116. showSidebar.set(false);
  117. }
  118. }}
  119. >
  120. <div class=" self-center mr-3">
  121. <svg
  122. xmlns="http://www.w3.org/2000/svg"
  123. fill="none"
  124. viewBox="0 0 24 24"
  125. stroke-width="1.5"
  126. stroke="currentColor"
  127. class="w-5 h-5"
  128. >
  129. <path
  130. stroke-linecap="round"
  131. stroke-linejoin="round"
  132. 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"
  133. />
  134. </svg>
  135. </div>
  136. <div class=" self-center font-medium">{$i18n.t('Admin Panel')}</div>
  137. </button>
  138. {/if}
  139. <hr class=" dark:border-gray-800 my-1.5 p-0" />
  140. <button
  141. class="flex rounded-md py-2 px-3 w-full hover:bg-gray-50 dark:hover:bg-gray-800 transition"
  142. on:click={() => {
  143. localStorage.removeItem('token');
  144. location.href = '/auth';
  145. show = false;
  146. }}
  147. >
  148. <div class=" self-center mr-3">
  149. <svg
  150. xmlns="http://www.w3.org/2000/svg"
  151. viewBox="0 0 20 20"
  152. fill="currentColor"
  153. class="w-5 h-5"
  154. >
  155. <path
  156. fill-rule="evenodd"
  157. 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"
  158. clip-rule="evenodd"
  159. />
  160. <path
  161. fill-rule="evenodd"
  162. 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"
  163. clip-rule="evenodd"
  164. />
  165. </svg>
  166. </div>
  167. <div class=" self-center font-medium">{$i18n.t('Sign Out')}</div>
  168. </button>
  169. {#if $activeUserCount}
  170. <hr class=" dark:border-gray-800 my-1.5 p-0" />
  171. <Tooltip
  172. content={$USAGE_POOL && $USAGE_POOL.length > 0
  173. ? `${$i18n.t('Running')}: ${$USAGE_POOL.join(', ')} ✨`
  174. : ''}
  175. >
  176. <div class="flex rounded-md py-1.5 px-3 text-xs gap-2.5 items-center">
  177. <div class=" flex items-center">
  178. <span class="relative flex size-2">
  179. <span
  180. class="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"
  181. />
  182. <span class="relative inline-flex rounded-full size-2 bg-green-500" />
  183. </span>
  184. </div>
  185. <div class=" ">
  186. <span class=" font-medium">
  187. {$i18n.t('Active Users')}:
  188. </span>
  189. <span class=" font-semibold">
  190. {$activeUserCount}
  191. </span>
  192. </div>
  193. </div>
  194. </Tooltip>
  195. {/if}
  196. <!-- <DropdownMenu.Item class="flex items-center px-3 py-2 text-sm font-medium">
  197. <div class="flex items-center">Profile</div>
  198. </DropdownMenu.Item> -->
  199. </DropdownMenu.Content>
  200. </slot>
  201. </DropdownMenu.Root>