user-avatar.svelte 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <script lang="ts" context="module">
  2. export type Size = 'full' | 'sm' | 'md' | 'lg' | 'xl' | 'xxl' | 'xxxl';
  3. </script>
  4. <script lang="ts">
  5. import { imageLoad } from '$lib/utils/image-load';
  6. import { UpdateUserDtoAvatarColorEnum, api } from '@api';
  7. interface User {
  8. id: string;
  9. firstName: string;
  10. lastName: string;
  11. email: string;
  12. profileImagePath: string;
  13. avatarColor: UpdateUserDtoAvatarColorEnum;
  14. }
  15. export let user: User;
  16. export let color: UpdateUserDtoAvatarColorEnum = user.avatarColor;
  17. export let size: Size = 'full';
  18. export let rounded = true;
  19. export let interactive = false;
  20. export let showTitle = true;
  21. export let autoColor = false;
  22. export let showProfileImage = true;
  23. let showFallback = true;
  24. const colorClasses: Record<UpdateUserDtoAvatarColorEnum, string> = {
  25. primary: 'bg-immich-primary dark:bg-immich-dark-primary text-immich-dark-fg dark:text-immich-fg',
  26. pink: 'bg-pink-400 text-immich-bg',
  27. red: 'bg-red-500 text-immich-bg',
  28. yellow: 'bg-yellow-500 text-immich-bg',
  29. blue: 'bg-blue-500 text-immich-bg',
  30. green: 'bg-green-600 text-immich-bg',
  31. purple: 'bg-purple-600 text-immich-bg',
  32. orange: 'bg-orange-600 text-immich-bg',
  33. gray: 'bg-gray-600 text-immich-bg',
  34. amber: 'bg-amber-600 text-immich-bg',
  35. };
  36. const sizeClasses: Record<Size, string> = {
  37. full: 'w-full h-full',
  38. sm: 'w-7 h-7',
  39. md: 'w-10 h-10',
  40. lg: 'w-12 h-12',
  41. xl: 'w-20 h-20',
  42. xxl: 'w-24 h-24',
  43. xxxl: 'w-28 h-28',
  44. };
  45. // Get color based on the user UUID.
  46. function getUserColor() {
  47. const seed = parseInt(user.id.split('-')[0], 16);
  48. const colors = Object.keys(colorClasses).filter((color) => color !== 'primary') as UpdateUserDtoAvatarColorEnum[];
  49. const randomIndex = seed % colors.length;
  50. return colors[randomIndex];
  51. }
  52. $: colorClass = colorClasses[autoColor ? getUserColor() : color];
  53. $: sizeClass = sizeClasses[size];
  54. $: title = `${user.firstName} ${user.lastName} (${user.email})`;
  55. $: interactiveClass = interactive
  56. ? 'border-2 border-immich-primary hover:border-immich-dark-primary dark:hover:border-immich-primary dark:border-immich-dark-primary transition-colors'
  57. : '';
  58. </script>
  59. <figure
  60. class="{sizeClass} {colorClass} {interactiveClass} overflow-hidden shadow-md"
  61. class:rounded-full={rounded}
  62. title={showTitle ? title : undefined}
  63. >
  64. {#if showProfileImage && user.profileImagePath}
  65. <img
  66. src={api.getProfileImageUrl(user.id)}
  67. alt="Profile image of {title}"
  68. class="h-full w-full object-cover"
  69. class:hidden={showFallback}
  70. draggable="false"
  71. use:imageLoad
  72. on:image-load={() => (showFallback = false)}
  73. />
  74. {/if}
  75. {#if showFallback}
  76. <span
  77. class="flex h-full w-full select-none items-center justify-center"
  78. class:text-xs={size === 'sm'}
  79. class:text-lg={size === 'lg'}
  80. class:text-xl={size === 'xl'}
  81. class:text-2xl={size === 'xxl'}
  82. class:text-3xl={size === 'xxxl'}
  83. class:font-medium={!autoColor}
  84. class:font-semibold={autoColor}
  85. >
  86. {((user.firstName[0] || '') + (user.lastName[0] || '')).toUpperCase()}
  87. </span>
  88. {/if}
  89. </figure>