Account.svelte 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 } from '$lib/apis/auths';
  6. import UpdatePassword from './Account/UpdatePassword.svelte';
  7. import { getGravatarUrl } from '$lib/apis/utils';
  8. import { copyToClipboard } from '$lib/utils';
  9. const i18n = getContext('i18n');
  10. export let saveHandler: Function;
  11. let profileImageUrl = '';
  12. let name = '';
  13. let showJWTToken = false;
  14. let JWTTokenCopied = false;
  15. let profileImageInputElement: HTMLInputElement;
  16. const submitHandler = async () => {
  17. const updatedUser = await updateUserProfile(localStorage.token, name, profileImageUrl).catch(
  18. (error) => {
  19. toast.error(error);
  20. }
  21. );
  22. if (updatedUser) {
  23. await user.set(updatedUser);
  24. return true;
  25. }
  26. return false;
  27. };
  28. onMount(() => {
  29. name = $user.name;
  30. profileImageUrl = $user.profile_image_url;
  31. });
  32. </script>
  33. <div class="flex flex-col h-full justify-between text-sm">
  34. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[22rem]">
  35. <input
  36. id="profile-image-input"
  37. bind:this={profileImageInputElement}
  38. type="file"
  39. hidden
  40. accept="image/*"
  41. on:change={(e) => {
  42. const files = profileImageInputElement.files ?? [];
  43. let reader = new FileReader();
  44. reader.onload = (event) => {
  45. let originalImageUrl = `${event.target.result}`;
  46. const img = new Image();
  47. img.src = originalImageUrl;
  48. img.onload = function () {
  49. const canvas = document.createElement('canvas');
  50. const ctx = canvas.getContext('2d');
  51. // Calculate the aspect ratio of the image
  52. const aspectRatio = img.width / img.height;
  53. // Calculate the new width and height to fit within 100x100
  54. let newWidth, newHeight;
  55. if (aspectRatio > 1) {
  56. newWidth = 100 * aspectRatio;
  57. newHeight = 100;
  58. } else {
  59. newWidth = 100;
  60. newHeight = 100 / aspectRatio;
  61. }
  62. // Set the canvas size
  63. canvas.width = 100;
  64. canvas.height = 100;
  65. // Calculate the position to center the image
  66. const offsetX = (100 - newWidth) / 2;
  67. const offsetY = (100 - newHeight) / 2;
  68. // Draw the image on the canvas
  69. ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight);
  70. // Get the base64 representation of the compressed image
  71. const compressedSrc = canvas.toDataURL('image/jpeg');
  72. // Display the compressed image
  73. profileImageUrl = compressedSrc;
  74. profileImageInputElement.files = null;
  75. };
  76. };
  77. if (
  78. files.length > 0 &&
  79. ['image/gif', 'image/jpeg', 'image/png'].includes(files[0]['type'])
  80. ) {
  81. reader.readAsDataURL(files[0]);
  82. }
  83. }}
  84. />
  85. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Profile')}</div>
  86. <div class="flex space-x-5">
  87. <div class="flex flex-col">
  88. <div class="self-center">
  89. <button
  90. class="relative rounded-full dark:bg-gray-700"
  91. type="button"
  92. on:click={() => {
  93. profileImageInputElement.click();
  94. }}
  95. >
  96. <img
  97. src={profileImageUrl !== '' ? profileImageUrl : '/user.png'}
  98. alt="profile"
  99. class=" rounded-full w-16 h-16 object-cover"
  100. />
  101. <div
  102. 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"
  103. >
  104. <div class="my-auto text-gray-100">
  105. <svg
  106. xmlns="http://www.w3.org/2000/svg"
  107. viewBox="0 0 20 20"
  108. fill="currentColor"
  109. class="w-5 h-5"
  110. >
  111. <path
  112. 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"
  113. />
  114. </svg>
  115. </div>
  116. </div>
  117. </button>
  118. </div>
  119. <button
  120. class=" text-xs text-gray-600"
  121. on:click={async () => {
  122. const url = await getGravatarUrl($user.email);
  123. profileImageUrl = url;
  124. }}>{$i18n.t('Use Gravatar')}</button
  125. >
  126. </div>
  127. <div class="flex-1">
  128. <div class="flex flex-col w-full">
  129. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name')}</div>
  130. <div class="flex-1">
  131. <input
  132. class="w-full rounded py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  133. type="text"
  134. bind:value={name}
  135. required
  136. />
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. <hr class=" dark:border-gray-700 my-4" />
  142. <UpdatePassword />
  143. <hr class=" dark:border-gray-700 my-4" />
  144. <div class=" w-full justify-between">
  145. <div class="flex w-full justify-between">
  146. <div class=" self-center text-xs font-medium">{$i18n.t('JWT Token')}</div>
  147. </div>
  148. <div class="flex mt-2">
  149. <div class="flex w-full">
  150. <input
  151. class="w-full rounded-l-lg py-1.5 pl-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  152. type={showJWTToken ? 'text' : 'password'}
  153. value={localStorage.token}
  154. disabled
  155. />
  156. <button
  157. class="dark:bg-gray-800 px-2 transition rounded-r-lg"
  158. on:click={() => {
  159. showJWTToken = !showJWTToken;
  160. }}
  161. >
  162. {#if showJWTToken}
  163. <svg
  164. xmlns="http://www.w3.org/2000/svg"
  165. viewBox="0 0 16 16"
  166. fill="currentColor"
  167. class="w-4 h-4"
  168. >
  169. <path
  170. fill-rule="evenodd"
  171. 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"
  172. clip-rule="evenodd"
  173. />
  174. <path
  175. 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"
  176. />
  177. </svg>
  178. {:else}
  179. <svg
  180. xmlns="http://www.w3.org/2000/svg"
  181. viewBox="0 0 16 16"
  182. fill="currentColor"
  183. class="w-4 h-4"
  184. >
  185. <path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" />
  186. <path
  187. fill-rule="evenodd"
  188. 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"
  189. clip-rule="evenodd"
  190. />
  191. </svg>
  192. {/if}
  193. </button>
  194. </div>
  195. <button
  196. class="ml-1.5 px-1.5 py-1 hover:bg-gray-800 transition rounded-lg"
  197. on:click={() => {
  198. copyToClipboard(localStorage.token);
  199. JWTTokenCopied = true;
  200. setTimeout(() => {
  201. JWTTokenCopied = false;
  202. }, 2000);
  203. }}
  204. >
  205. {#if JWTTokenCopied}
  206. <svg
  207. xmlns="http://www.w3.org/2000/svg"
  208. viewBox="0 0 20 20"
  209. fill="currentColor"
  210. class="w-4 h-4"
  211. >
  212. <path
  213. fill-rule="evenodd"
  214. 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"
  215. clip-rule="evenodd"
  216. />
  217. </svg>
  218. {:else}
  219. <svg
  220. xmlns="http://www.w3.org/2000/svg"
  221. viewBox="0 0 16 16"
  222. fill="currentColor"
  223. class="w-4 h-4"
  224. >
  225. <path
  226. fill-rule="evenodd"
  227. 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"
  228. clip-rule="evenodd"
  229. />
  230. <path
  231. fill-rule="evenodd"
  232. 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"
  233. clip-rule="evenodd"
  234. />
  235. </svg>
  236. {/if}
  237. </button>
  238. </div>
  239. </div>
  240. </div>
  241. <div class="flex justify-end pt-3 text-sm font-medium">
  242. <button
  243. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  244. on:click={async () => {
  245. const res = await submitHandler();
  246. if (res) {
  247. saveHandler();
  248. }
  249. }}
  250. >
  251. {$i18n.t('Save')}
  252. </button>
  253. </div>
  254. </div>