Account.svelte 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <script lang="ts">
  2. import { toast } from 'svelte-sonner';
  3. import { onMount, getContext } from 'svelte';
  4. import { user } from '$lib/stores';
  5. import { updateUserProfile, createAPIKey, getAPIKey } from '$lib/apis/auths';
  6. import UpdatePassword from './Account/UpdatePassword.svelte';
  7. import { getGravatarUrl } from '$lib/apis/utils';
  8. import { generateInitialsImage, canvasPixelTest } from '$lib/utils';
  9. import { copyToClipboard } from '$lib/utils';
  10. import Plus from '$lib/components/icons/Plus.svelte';
  11. import Tooltip from '$lib/components/common/Tooltip.svelte';
  12. const i18n = getContext('i18n');
  13. export let saveHandler: Function;
  14. let profileImageUrl = '';
  15. let name = '';
  16. let showAPIKeys = false;
  17. let showJWTToken = false;
  18. let JWTTokenCopied = false;
  19. let APIKey = '';
  20. let showAPIKey = false;
  21. let APIKeyCopied = false;
  22. let profileImageInputElement: HTMLInputElement;
  23. const submitHandler = async () => {
  24. if (name !== $user.name) {
  25. if (profileImageUrl === generateInitialsImage($user.name) || profileImageUrl === '') {
  26. profileImageUrl = generateInitialsImage(name);
  27. }
  28. }
  29. const updatedUser = await updateUserProfile(localStorage.token, name, profileImageUrl).catch(
  30. (error) => {
  31. toast.error(error);
  32. }
  33. );
  34. if (updatedUser) {
  35. await user.set(updatedUser);
  36. return true;
  37. }
  38. return false;
  39. };
  40. const createAPIKeyHandler = async () => {
  41. APIKey = await createAPIKey(localStorage.token);
  42. if (APIKey) {
  43. toast.success($i18n.t('API Key created.'));
  44. } else {
  45. toast.error($i18n.t('Failed to create API Key.'));
  46. }
  47. };
  48. onMount(async () => {
  49. name = $user.name;
  50. profileImageUrl = $user.profile_image_url;
  51. APIKey = await getAPIKey(localStorage.token).catch((error) => {
  52. console.log(error);
  53. return '';
  54. });
  55. });
  56. </script>
  57. <div class="flex flex-col h-full justify-between text-sm">
  58. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[22rem]">
  59. <input
  60. id="profile-image-input"
  61. bind:this={profileImageInputElement}
  62. type="file"
  63. hidden
  64. accept="image/*"
  65. on:change={(e) => {
  66. const files = profileImageInputElement.files ?? [];
  67. let reader = new FileReader();
  68. reader.onload = (event) => {
  69. let originalImageUrl = `${event.target.result}`;
  70. const img = new Image();
  71. img.src = originalImageUrl;
  72. img.onload = function () {
  73. const canvas = document.createElement('canvas');
  74. const ctx = canvas.getContext('2d');
  75. // Calculate the aspect ratio of the image
  76. const aspectRatio = img.width / img.height;
  77. // Calculate the new width and height to fit within 100x100
  78. let newWidth, newHeight;
  79. if (aspectRatio > 1) {
  80. newWidth = 100 * aspectRatio;
  81. newHeight = 100;
  82. } else {
  83. newWidth = 100;
  84. newHeight = 100 / aspectRatio;
  85. }
  86. // Set the canvas size
  87. canvas.width = 100;
  88. canvas.height = 100;
  89. // Calculate the position to center the image
  90. const offsetX = (100 - newWidth) / 2;
  91. const offsetY = (100 - newHeight) / 2;
  92. // Draw the image on the canvas
  93. ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight);
  94. // Get the base64 representation of the compressed image
  95. const compressedSrc = canvas.toDataURL('image/jpeg');
  96. // Display the compressed image
  97. profileImageUrl = compressedSrc;
  98. profileImageInputElement.files = null;
  99. };
  100. };
  101. if (
  102. files.length > 0 &&
  103. ['image/gif', 'image/jpeg', 'image/png'].includes(files[0]['type'])
  104. ) {
  105. reader.readAsDataURL(files[0]);
  106. }
  107. }}
  108. />
  109. <div class="space-y-1">
  110. <!-- <div class=" text-sm font-medium">{$i18n.t('Account')}</div> -->
  111. <div class="flex space-x-5">
  112. <div class="flex flex-col">
  113. <div class="self-center mt-2">
  114. <button
  115. class="relative rounded-full dark:bg-gray-700"
  116. type="button"
  117. on:click={() => {
  118. profileImageInputElement.click();
  119. }}
  120. >
  121. <img
  122. src={profileImageUrl !== '' ? profileImageUrl : generateInitialsImage(name)}
  123. alt="profile"
  124. class=" rounded-full size-16 object-cover"
  125. />
  126. <div
  127. class="absolute flex justify-center rounded-full bottom-0 left-0 right-0 top-0 h-full w-full overflow-hidden bg-gray-700 bg-fixed opacity-0 transition duration-300 ease-in-out hover:opacity-50"
  128. >
  129. <div class="my-auto text-gray-100">
  130. <svg
  131. xmlns="http://www.w3.org/2000/svg"
  132. viewBox="0 0 20 20"
  133. fill="currentColor"
  134. class="w-5 h-5"
  135. >
  136. <path
  137. d="m2.695 14.762-1.262 3.155a.5.5 0 0 0 .65.65l3.155-1.262a4 4 0 0 0 1.343-.886L17.5 5.501a2.121 2.121 0 0 0-3-3L3.58 13.419a4 4 0 0 0-.885 1.343Z"
  138. />
  139. </svg>
  140. </div>
  141. </div>
  142. </button>
  143. </div>
  144. </div>
  145. <div class="flex-1 flex flex-col self-center gap-0.5">
  146. <div class=" mb-0.5 text-sm font-medium">{$i18n.t('Profile Image')}</div>
  147. <div>
  148. <button
  149. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-full px-4 py-0.5 bg-gray-100 dark:bg-gray-850"
  150. on:click={async () => {
  151. if (canvasPixelTest()) {
  152. profileImageUrl = generateInitialsImage(name);
  153. } else {
  154. toast.info(
  155. $i18n.t(
  156. 'Fingerprint spoofing detected: Unable to use initials as avatar. Defaulting to default profile image.'
  157. ),
  158. {
  159. duration: 1000 * 10
  160. }
  161. );
  162. }
  163. }}>{$i18n.t('Use Initials')}</button
  164. >
  165. <button
  166. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-full px-4 py-0.5 bg-gray-100 dark:bg-gray-850"
  167. on:click={async () => {
  168. const url = await getGravatarUrl($user.email);
  169. profileImageUrl = url;
  170. }}>{$i18n.t('Use Gravatar')}</button
  171. >
  172. <button
  173. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-lg px-2 py-1"
  174. on:click={async () => {
  175. profileImageUrl = '/user.png';
  176. }}>{$i18n.t('Remove')}</button
  177. >
  178. </div>
  179. </div>
  180. </div>
  181. <div class="pt-0.5">
  182. <div class="flex flex-col w-full">
  183. <div class=" mb-1 text-xs font-medium">{$i18n.t('Name')}</div>
  184. <div class="flex-1">
  185. <input
  186. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  187. type="text"
  188. bind:value={name}
  189. required
  190. />
  191. </div>
  192. </div>
  193. </div>
  194. </div>
  195. <div class="py-0.5">
  196. <UpdatePassword />
  197. </div>
  198. <hr class=" dark:border-gray-700 my-4" />
  199. <div class="flex justify-between items-center text-sm">
  200. <div class=" font-medium">{$i18n.t('API keys')}</div>
  201. <button
  202. class=" text-xs font-medium text-gray-500"
  203. type="button"
  204. on:click={() => {
  205. showAPIKeys = !showAPIKeys;
  206. }}>{showAPIKeys ? $i18n.t('Hide') : $i18n.t('Show')}</button
  207. >
  208. </div>
  209. {#if showAPIKeys}
  210. <div class="flex flex-col gap-4">
  211. <div class="justify-between w-full">
  212. <div class="flex justify-between w-full">
  213. <div class="self-center text-xs font-medium">{$i18n.t('JWT Token')}</div>
  214. </div>
  215. <div class="flex mt-2">
  216. <div class="flex w-full">
  217. <input
  218. class="w-full rounded-l-lg py-1.5 pl-4 text-sm bg-white dark:text-gray-300 dark:bg-gray-850 outline-none"
  219. type={showJWTToken ? 'text' : 'password'}
  220. value={localStorage.token}
  221. disabled
  222. />
  223. <button
  224. class="px-2 transition rounded-r-lg bg-white dark:bg-gray-850"
  225. on:click={() => {
  226. showJWTToken = !showJWTToken;
  227. }}
  228. >
  229. {#if showJWTToken}
  230. <svg
  231. xmlns="http://www.w3.org/2000/svg"
  232. viewBox="0 0 16 16"
  233. fill="currentColor"
  234. class="w-4 h-4"
  235. >
  236. <path
  237. fill-rule="evenodd"
  238. d="M3.28 2.22a.75.75 0 0 0-1.06 1.06l10.5 10.5a.75.75 0 1 0 1.06-1.06l-1.322-1.323a7.012 7.012 0 0 0 2.16-3.11.87.87 0 0 0 0-.567A7.003 7.003 0 0 0 4.82 3.76l-1.54-1.54Zm3.196 3.195 1.135 1.136A1.502 1.502 0 0 1 9.45 8.389l1.136 1.135a3 3 0 0 0-4.109-4.109Z"
  239. clip-rule="evenodd"
  240. />
  241. <path
  242. d="m7.812 10.994 1.816 1.816A7.003 7.003 0 0 1 1.38 8.28a.87.87 0 0 1 0-.566 6.985 6.985 0 0 1 1.113-2.039l2.513 2.513a3 3 0 0 0 2.806 2.806Z"
  243. />
  244. </svg>
  245. {:else}
  246. <svg
  247. xmlns="http://www.w3.org/2000/svg"
  248. viewBox="0 0 16 16"
  249. fill="currentColor"
  250. class="w-4 h-4"
  251. >
  252. <path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" />
  253. <path
  254. fill-rule="evenodd"
  255. d="M1.38 8.28a.87.87 0 0 1 0-.566 7.003 7.003 0 0 1 13.238.006.87.87 0 0 1 0 .566A7.003 7.003 0 0 1 1.379 8.28ZM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  256. clip-rule="evenodd"
  257. />
  258. </svg>
  259. {/if}
  260. </button>
  261. </div>
  262. <button
  263. class="ml-1.5 px-1.5 py-1 dark:hover:bg-gray-850 transition rounded-lg"
  264. on:click={() => {
  265. copyToClipboard(localStorage.token);
  266. JWTTokenCopied = true;
  267. setTimeout(() => {
  268. JWTTokenCopied = false;
  269. }, 2000);
  270. }}
  271. >
  272. {#if JWTTokenCopied}
  273. <svg
  274. xmlns="http://www.w3.org/2000/svg"
  275. viewBox="0 0 20 20"
  276. fill="currentColor"
  277. class="w-4 h-4"
  278. >
  279. <path
  280. fill-rule="evenodd"
  281. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  282. clip-rule="evenodd"
  283. />
  284. </svg>
  285. {:else}
  286. <svg
  287. xmlns="http://www.w3.org/2000/svg"
  288. viewBox="0 0 16 16"
  289. fill="currentColor"
  290. class="w-4 h-4"
  291. >
  292. <path
  293. fill-rule="evenodd"
  294. d="M11.986 3H12a2 2 0 0 1 2 2v6a2 2 0 0 1-1.5 1.937V7A2.5 2.5 0 0 0 10 4.5H4.063A2 2 0 0 1 6 3h.014A2.25 2.25 0 0 1 8.25 1h1.5a2.25 2.25 0 0 1 2.236 2ZM10.5 4v-.75a.75.75 0 0 0-.75-.75h-1.5a.75.75 0 0 0-.75.75V4h3Z"
  295. clip-rule="evenodd"
  296. />
  297. <path
  298. fill-rule="evenodd"
  299. d="M3 6a1 1 0 0 0-1 1v7a1 1 0 0 0 1 1h7a1 1 0 0 0 1-1V7a1 1 0 0 0-1-1H3Zm1.75 2.5a.75.75 0 0 0 0 1.5h3.5a.75.75 0 0 0 0-1.5h-3.5ZM4 11.75a.75.75 0 0 1 .75-.75h3.5a.75.75 0 0 1 0 1.5h-3.5a.75.75 0 0 1-.75-.75Z"
  300. clip-rule="evenodd"
  301. />
  302. </svg>
  303. {/if}
  304. </button>
  305. </div>
  306. </div>
  307. <div class="justify-between w-full">
  308. <div class="flex justify-between w-full">
  309. <div class="self-center text-xs font-medium">{$i18n.t('API Key')}</div>
  310. </div>
  311. <div class="flex mt-2">
  312. {#if APIKey}
  313. <div class="flex w-full">
  314. <input
  315. class="w-full rounded-l-lg py-1.5 pl-4 text-sm bg-white dark:text-gray-300 dark:bg-gray-850 outline-none"
  316. type={showAPIKey ? 'text' : 'password'}
  317. value={APIKey}
  318. disabled
  319. />
  320. <button
  321. class="px-2 transition rounded-r-lg bg-white dark:bg-gray-850"
  322. on:click={() => {
  323. showAPIKey = !showAPIKey;
  324. }}
  325. >
  326. {#if showAPIKey}
  327. <svg
  328. xmlns="http://www.w3.org/2000/svg"
  329. viewBox="0 0 16 16"
  330. fill="currentColor"
  331. class="w-4 h-4"
  332. >
  333. <path
  334. fill-rule="evenodd"
  335. d="M3.28 2.22a.75.75 0 0 0-1.06 1.06l10.5 10.5a.75.75 0 1 0 1.06-1.06l-1.322-1.323a7.012 7.012 0 0 0 2.16-3.11.87.87 0 0 0 0-.567A7.003 7.003 0 0 0 4.82 3.76l-1.54-1.54Zm3.196 3.195 1.135 1.136A1.502 1.502 0 0 1 9.45 8.389l1.136 1.135a3 3 0 0 0-4.109-4.109Z"
  336. clip-rule="evenodd"
  337. />
  338. <path
  339. d="m7.812 10.994 1.816 1.816A7.003 7.003 0 0 1 1.38 8.28a.87.87 0 0 1 0-.566 6.985 6.985 0 0 1 1.113-2.039l2.513 2.513a3 3 0 0 0 2.806 2.806Z"
  340. />
  341. </svg>
  342. {:else}
  343. <svg
  344. xmlns="http://www.w3.org/2000/svg"
  345. viewBox="0 0 16 16"
  346. fill="currentColor"
  347. class="w-4 h-4"
  348. >
  349. <path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" />
  350. <path
  351. fill-rule="evenodd"
  352. d="M1.38 8.28a.87.87 0 0 1 0-.566 7.003 7.003 0 0 1 13.238.006.87.87 0 0 1 0 .566A7.003 7.003 0 0 1 1.379 8.28ZM11 8a3 3 0 1 1-6 0 3 3 0 0 1 6 0Z"
  353. clip-rule="evenodd"
  354. />
  355. </svg>
  356. {/if}
  357. </button>
  358. </div>
  359. <button
  360. class="ml-1.5 px-1.5 py-1 dark:hover:bg-gray-850 transition rounded-lg"
  361. on:click={() => {
  362. copyToClipboard(APIKey);
  363. APIKeyCopied = true;
  364. setTimeout(() => {
  365. APIKeyCopied = false;
  366. }, 2000);
  367. }}
  368. >
  369. {#if APIKeyCopied}
  370. <svg
  371. xmlns="http://www.w3.org/2000/svg"
  372. viewBox="0 0 20 20"
  373. fill="currentColor"
  374. class="w-4 h-4"
  375. >
  376. <path
  377. fill-rule="evenodd"
  378. d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z"
  379. clip-rule="evenodd"
  380. />
  381. </svg>
  382. {:else}
  383. <svg
  384. xmlns="http://www.w3.org/2000/svg"
  385. viewBox="0 0 16 16"
  386. fill="currentColor"
  387. class="w-4 h-4"
  388. >
  389. <path
  390. fill-rule="evenodd"
  391. d="M11.986 3H12a2 2 0 0 1 2 2v6a2 2 0 0 1-1.5 1.937V7A2.5 2.5 0 0 0 10 4.5H4.063A2 2 0 0 1 6 3h.014A2.25 2.25 0 0 1 8.25 1h1.5a2.25 2.25 0 0 1 2.236 2ZM10.5 4v-.75a.75.75 0 0 0-.75-.75h-1.5a.75.75 0 0 0-.75.75V4h3Z"
  392. clip-rule="evenodd"
  393. />
  394. <path
  395. fill-rule="evenodd"
  396. d="M3 6a1 1 0 0 0-1 1v7a1 1 0 0 0 1 1h7a1 1 0 0 0 1-1V7a1 1 0 0 0-1-1H3Zm1.75 2.5a.75.75 0 0 0 0 1.5h3.5a.75.75 0 0 0 0-1.5h-3.5ZM4 11.75a.75.75 0 0 1 .75-.75h3.5a.75.75 0 0 1 0 1.5h-3.5a.75.75 0 0 1-.75-.75Z"
  397. clip-rule="evenodd"
  398. />
  399. </svg>
  400. {/if}
  401. </button>
  402. <Tooltip content={$i18n.t('Create new key')}>
  403. <button
  404. class=" px-1.5 py-1 dark:hover:bg-gray-850transition rounded-lg"
  405. on:click={() => {
  406. createAPIKeyHandler();
  407. }}
  408. >
  409. <svg
  410. xmlns="http://www.w3.org/2000/svg"
  411. fill="none"
  412. viewBox="0 0 24 24"
  413. stroke-width="2"
  414. stroke="currentColor"
  415. class="size-4"
  416. >
  417. <path
  418. stroke-linecap="round"
  419. stroke-linejoin="round"
  420. d="M16.023 9.348h4.992v-.001M2.985 19.644v-4.992m0 0h4.992m-4.993 0 3.181 3.183a8.25 8.25 0 0 0 13.803-3.7M4.031 9.865a8.25 8.25 0 0 1 13.803-3.7l3.181 3.182m0-4.991v4.99"
  421. />
  422. </svg>
  423. </button>
  424. </Tooltip>
  425. {:else}
  426. <button
  427. class="flex gap-1.5 items-center font-medium px-3.5 py-1.5 rounded-lg bg-gray-100/70 hover:bg-gray-100 dark:bg-gray-850 dark:hover:bg-gray-850 transition"
  428. on:click={() => {
  429. createAPIKeyHandler();
  430. }}
  431. >
  432. <Plus strokeWidth="2" className=" size-3.5" />
  433. {$i18n.t('Create new secret key')}</button
  434. >
  435. {/if}
  436. </div>
  437. </div>
  438. </div>
  439. {/if}
  440. </div>
  441. <div class="flex justify-end pt-3 text-sm font-medium">
  442. <button
  443. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  444. on:click={async () => {
  445. const res = await submitHandler();
  446. if (res) {
  447. saveHandler();
  448. }
  449. }}
  450. >
  451. {$i18n.t('Save')}
  452. </button>
  453. </div>
  454. </div>