Account.svelte 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 { generateInitialsImage, canvasPixelTest } from '$lib/utils';
  9. import { copyToClipboard } from '$lib/utils';
  10. const i18n = getContext('i18n');
  11. export let saveHandler: Function;
  12. let profileImageUrl = '';
  13. let name = '';
  14. let showJWTToken = false;
  15. let JWTTokenCopied = false;
  16. let profileImageInputElement: HTMLInputElement;
  17. const submitHandler = async () => {
  18. if (name !== $user.name) {
  19. if (profileImageUrl === generateInitialsImage($user.name) || profileImageUrl === '') {
  20. profileImageUrl = generateInitialsImage(name);
  21. }
  22. }
  23. const updatedUser = await updateUserProfile(localStorage.token, name, profileImageUrl).catch(
  24. (error) => {
  25. toast.error(error);
  26. }
  27. );
  28. if (updatedUser) {
  29. await user.set(updatedUser);
  30. return true;
  31. }
  32. return false;
  33. };
  34. onMount(() => {
  35. name = $user.name;
  36. profileImageUrl = $user.profile_image_url;
  37. });
  38. </script>
  39. <div class="flex flex-col h-full justify-between text-sm">
  40. <div class=" space-y-3 pr-1.5 overflow-y-scroll max-h-[22rem]">
  41. <input
  42. id="profile-image-input"
  43. bind:this={profileImageInputElement}
  44. type="file"
  45. hidden
  46. accept="image/*"
  47. on:change={(e) => {
  48. const files = profileImageInputElement.files ?? [];
  49. let reader = new FileReader();
  50. reader.onload = (event) => {
  51. let originalImageUrl = `${event.target.result}`;
  52. const img = new Image();
  53. img.src = originalImageUrl;
  54. img.onload = function () {
  55. const canvas = document.createElement('canvas');
  56. const ctx = canvas.getContext('2d');
  57. // Calculate the aspect ratio of the image
  58. const aspectRatio = img.width / img.height;
  59. // Calculate the new width and height to fit within 100x100
  60. let newWidth, newHeight;
  61. if (aspectRatio > 1) {
  62. newWidth = 100 * aspectRatio;
  63. newHeight = 100;
  64. } else {
  65. newWidth = 100;
  66. newHeight = 100 / aspectRatio;
  67. }
  68. // Set the canvas size
  69. canvas.width = 100;
  70. canvas.height = 100;
  71. // Calculate the position to center the image
  72. const offsetX = (100 - newWidth) / 2;
  73. const offsetY = (100 - newHeight) / 2;
  74. // Draw the image on the canvas
  75. ctx.drawImage(img, offsetX, offsetY, newWidth, newHeight);
  76. // Get the base64 representation of the compressed image
  77. const compressedSrc = canvas.toDataURL('image/jpeg');
  78. // Display the compressed image
  79. profileImageUrl = compressedSrc;
  80. profileImageInputElement.files = null;
  81. };
  82. };
  83. if (
  84. files.length > 0 &&
  85. ['image/gif', 'image/jpeg', 'image/png'].includes(files[0]['type'])
  86. ) {
  87. reader.readAsDataURL(files[0]);
  88. }
  89. }}
  90. />
  91. <!-- <div class="flex space-x-5">
  92. <div class="flex flex-col">
  93. <div class="self-center">
  94. <div class=" mb-2.5 text-sm font-medium">{$i18n.t('Profile')}</div>
  95. <button
  96. class="relative rounded-full dark:bg-gray-700"
  97. type="button"
  98. on:click={() => {
  99. profileImageInputElement.click();
  100. }}
  101. >
  102. <img
  103. src={profileImageUrl !== '' ? profileImageUrl : generateInitialsImage(name)}
  104. alt="profile"
  105. class=" rounded-full size-20 object-cover"
  106. />
  107. <div
  108. 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"
  109. >
  110. <div class="my-auto text-gray-100">
  111. <svg
  112. xmlns="http://www.w3.org/2000/svg"
  113. viewBox="0 0 20 20"
  114. fill="currentColor"
  115. class="w-5 h-5"
  116. >
  117. <path
  118. 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"
  119. />
  120. </svg>
  121. </div>
  122. </div>
  123. </button>
  124. </div>
  125. </div>
  126. <div class="flex-1 flex flex-col self-center">
  127. <div class="flex flex-col w-full">
  128. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name')}</div>
  129. <div class="flex-1">
  130. <input
  131. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  132. type="text"
  133. bind:value={name}
  134. required
  135. />
  136. </div>
  137. <div class="mt-2">
  138. <button
  139. 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"
  140. on:click={async () => {
  141. const url = await getGravatarUrl($user.email);
  142. profileImageUrl = url;
  143. }}>{$i18n.t('Use Gravatar')}</button
  144. >
  145. <button
  146. 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"
  147. on:click={async () => {
  148. if (canvasPixelTest()) {
  149. profileImageUrl = generateInitialsImage(name);
  150. } else {
  151. toast.info(
  152. $i18n.t(
  153. 'Fingerprint spoofing detected: Unable to use initials as avatar. Defaulting to default profile image.'
  154. ),
  155. {
  156. duration: 1000 * 10
  157. }
  158. );
  159. }
  160. }}>{$i18n.t('Use Initials')}</button
  161. >
  162. </div>
  163. </div>
  164. </div>
  165. </div> -->
  166. <div class="flex space-x-5">
  167. <div class="flex flex-col">
  168. <div class="self-center mt-2">
  169. <button
  170. class="relative rounded-full dark:bg-gray-700"
  171. type="button"
  172. on:click={() => {
  173. profileImageInputElement.click();
  174. }}
  175. >
  176. <img
  177. src={profileImageUrl !== '' ? profileImageUrl : generateInitialsImage(name)}
  178. alt="profile"
  179. class=" rounded-full size-16 object-cover"
  180. />
  181. <div
  182. 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"
  183. >
  184. <div class="my-auto text-gray-100">
  185. <svg
  186. xmlns="http://www.w3.org/2000/svg"
  187. viewBox="0 0 20 20"
  188. fill="currentColor"
  189. class="w-5 h-5"
  190. >
  191. <path
  192. 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"
  193. />
  194. </svg>
  195. </div>
  196. </div>
  197. </button>
  198. </div>
  199. </div>
  200. <div class="flex-1 flex flex-col self-center">
  201. <div class=" font-medium mb-1.5">{$i18n.t('Profile Image')}</div>
  202. <div>
  203. <button
  204. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-lg px-4 py-1 bg-gray-100 dark:bg-gray-850"
  205. on:click={async () => {
  206. if (canvasPixelTest()) {
  207. profileImageUrl = generateInitialsImage(name);
  208. } else {
  209. toast.info(
  210. $i18n.t(
  211. 'Fingerprint spoofing detected: Unable to use initials as avatar. Defaulting to default profile image.'
  212. ),
  213. {
  214. duration: 1000 * 10
  215. }
  216. );
  217. }
  218. }}>{$i18n.t('Use Initials')}</button
  219. >
  220. <button
  221. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-lg px-4 py-1 bg-gray-100 dark:bg-gray-850"
  222. on:click={async () => {
  223. const url = await getGravatarUrl($user.email);
  224. profileImageUrl = url;
  225. }}>{$i18n.t('Use Gravatar')}</button
  226. >
  227. <button
  228. class=" text-xs text-center text-gray-800 dark:text-gray-400 rounded-lg px-4 py-1 bg-gray-100 dark:bg-gray-850"
  229. on:click={async () => {
  230. profileImageUrl = '/user.png';
  231. }}>{$i18n.t('Remove')}</button
  232. >
  233. </div>
  234. </div>
  235. </div>
  236. <div class="flex-1">
  237. <div class="flex flex-col w-full">
  238. <div class=" mb-1 text-xs text-gray-500">{$i18n.t('Name')}</div>
  239. <div class="flex-1">
  240. <input
  241. class="w-full rounded-lg py-2 px-4 text-sm dark:text-gray-300 dark:bg-gray-850 outline-none"
  242. type="text"
  243. bind:value={name}
  244. required
  245. />
  246. </div>
  247. </div>
  248. </div>
  249. <hr class=" dark:border-gray-700 my-4" />
  250. <UpdatePassword />
  251. <hr class=" dark:border-gray-700 my-4" />
  252. <div class=" w-full justify-between">
  253. <div class="flex w-full justify-between">
  254. <div class=" self-center text-xs font-medium">{$i18n.t('JWT Token')}</div>
  255. </div>
  256. <div class="flex mt-2">
  257. <div class="flex w-full">
  258. <input
  259. class="w-full rounded-l-lg py-1.5 pl-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
  260. type={showJWTToken ? 'text' : 'password'}
  261. value={localStorage.token}
  262. disabled
  263. />
  264. <button
  265. class="dark:bg-gray-800 px-2 transition rounded-r-lg"
  266. on:click={() => {
  267. showJWTToken = !showJWTToken;
  268. }}
  269. >
  270. {#if showJWTToken}
  271. <svg
  272. xmlns="http://www.w3.org/2000/svg"
  273. viewBox="0 0 16 16"
  274. fill="currentColor"
  275. class="w-4 h-4"
  276. >
  277. <path
  278. fill-rule="evenodd"
  279. 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"
  280. clip-rule="evenodd"
  281. />
  282. <path
  283. 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"
  284. />
  285. </svg>
  286. {:else}
  287. <svg
  288. xmlns="http://www.w3.org/2000/svg"
  289. viewBox="0 0 16 16"
  290. fill="currentColor"
  291. class="w-4 h-4"
  292. >
  293. <path d="M8 9.5a1.5 1.5 0 1 0 0-3 1.5 1.5 0 0 0 0 3Z" />
  294. <path
  295. fill-rule="evenodd"
  296. 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"
  297. clip-rule="evenodd"
  298. />
  299. </svg>
  300. {/if}
  301. </button>
  302. </div>
  303. <button
  304. class="ml-1.5 px-1.5 py-1 hover:bg-gray-800 transition rounded-lg"
  305. on:click={() => {
  306. copyToClipboard(localStorage.token);
  307. JWTTokenCopied = true;
  308. setTimeout(() => {
  309. JWTTokenCopied = false;
  310. }, 2000);
  311. }}
  312. >
  313. {#if JWTTokenCopied}
  314. <svg
  315. xmlns="http://www.w3.org/2000/svg"
  316. viewBox="0 0 20 20"
  317. fill="currentColor"
  318. class="w-4 h-4"
  319. >
  320. <path
  321. fill-rule="evenodd"
  322. 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"
  323. clip-rule="evenodd"
  324. />
  325. </svg>
  326. {:else}
  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="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"
  336. clip-rule="evenodd"
  337. />
  338. <path
  339. fill-rule="evenodd"
  340. 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"
  341. clip-rule="evenodd"
  342. />
  343. </svg>
  344. {/if}
  345. </button>
  346. </div>
  347. </div>
  348. </div>
  349. <div class="flex justify-end pt-3 text-sm font-medium">
  350. <button
  351. class=" px-4 py-2 bg-emerald-700 hover:bg-emerald-800 text-gray-100 transition rounded-lg"
  352. on:click={async () => {
  353. const res = await submitHandler();
  354. if (res) {
  355. saveHandler();
  356. }
  357. }}
  358. >
  359. {$i18n.t('Save')}
  360. </button>
  361. </div>
  362. </div>