user-avatar.svelte 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 { UserAvatarColor, api } from '@api';
  7. interface User {
  8. id: string;
  9. name: string;
  10. email: string;
  11. profileImagePath: string;
  12. avatarColor: UserAvatarColor;
  13. }
  14. export let user: User;
  15. export let color: UserAvatarColor = user.avatarColor;
  16. export let size: Size = 'full';
  17. export let rounded = true;
  18. export let interactive = false;
  19. export let showTitle = true;
  20. export let showProfileImage = true;
  21. let showFallback = true;
  22. const colorClasses: Record<UserAvatarColor, string> = {
  23. primary: 'bg-immich-primary dark:bg-immich-dark-primary text-immich-dark-fg dark:text-immich-fg',
  24. pink: 'bg-pink-400 text-immich-bg',
  25. red: 'bg-red-500 text-immich-bg',
  26. yellow: 'bg-yellow-500 text-immich-bg',
  27. blue: 'bg-blue-500 text-immich-bg',
  28. green: 'bg-green-600 text-immich-bg',
  29. purple: 'bg-purple-600 text-immich-bg',
  30. orange: 'bg-orange-600 text-immich-bg',
  31. gray: 'bg-gray-600 text-immich-bg',
  32. amber: 'bg-amber-600 text-immich-bg',
  33. };
  34. const sizeClasses: Record<Size, string> = {
  35. full: 'w-full h-full',
  36. sm: 'w-7 h-7',
  37. md: 'w-10 h-10',
  38. lg: 'w-12 h-12',
  39. xl: 'w-16 h-16',
  40. xxl: 'w-24 h-24',
  41. xxxl: 'w-28 h-28',
  42. };
  43. $: colorClass = colorClasses[color];
  44. $: sizeClass = sizeClasses[size];
  45. $: title = `${user.name} (${user.email})`;
  46. $: interactiveClass = interactive
  47. ? 'border-2 border-immich-primary hover:border-immich-dark-primary dark:hover:border-immich-primary dark:border-immich-dark-primary transition-colors'
  48. : '';
  49. </script>
  50. <figure
  51. class="{sizeClass} {colorClass} {interactiveClass} overflow-hidden shadow-md"
  52. class:rounded-full={rounded}
  53. title={showTitle ? title : undefined}
  54. >
  55. {#if showProfileImage && user.profileImagePath}
  56. <img
  57. src={api.getProfileImageUrl(user.id)}
  58. alt="Profile image of {title}"
  59. class="h-full w-full object-cover"
  60. class:hidden={showFallback}
  61. draggable="false"
  62. use:imageLoad
  63. on:image-load={() => (showFallback = false)}
  64. />
  65. {/if}
  66. {#if showFallback}
  67. <span
  68. class="flex h-full w-full select-none items-center justify-center font-medium"
  69. class:text-xs={size === 'sm'}
  70. class:text-lg={size === 'lg'}
  71. class:text-xl={size === 'xl'}
  72. class:text-2xl={size === 'xxl'}
  73. class:text-3xl={size === 'xxxl'}
  74. >
  75. {(user.name[0] || '').toUpperCase()}
  76. </span>
  77. {/if}
  78. </figure>