Account.svelte 8.5 KB

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