Account.svelte 8.9 KB

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