Account.svelte 12 KB

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