varminer-app-header 2.1.4 → 2.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -340,6 +340,87 @@ const getAllDataFromStorage = () => {
340
340
  };
341
341
  }
342
342
  };
343
+ /**
344
+ * Get profile picture URL from object store API using tenant_id and user_id from JWT token
345
+ * @param baseUrl - Base URL for the object store API (default: http://objectstore.impact0mics.local:9012)
346
+ * @returns Profile picture URL or null if tenant_id or user_id is not available
347
+ */
348
+ const getProfilePictureUrl = (baseUrl = "http://objectstore.impact0mics.local:9012") => {
349
+ try {
350
+ const allData = getAllDataFromStorage();
351
+ // Get tenant_id and user_id from decoded token
352
+ let tenantId = null;
353
+ let userId = null;
354
+ if (allData.decodedToken) {
355
+ tenantId = allData.decodedToken.tenant_id || allData.decodedToken.tenant || null;
356
+ userId = allData.decodedToken.user_id || null;
357
+ }
358
+ // If not found in decoded token, try auth data
359
+ if ((!tenantId || !userId) && allData.auth) {
360
+ tenantId = tenantId || allData.auth.tenant_id || allData.auth.tenant || null;
361
+ userId = userId || allData.auth.user_id || null;
362
+ }
363
+ // Construct URL if we have both tenant_id and user_id
364
+ if (tenantId && userId) {
365
+ // Remove trailing slash from baseUrl if present
366
+ const cleanBaseUrl = baseUrl.replace(/\/$/, '');
367
+ return `${cleanBaseUrl}/v1/objectStore/profilePicture/path/${tenantId}/${userId}`;
368
+ }
369
+ return null;
370
+ }
371
+ catch (err) {
372
+ console.error("Error getting profile picture URL:", err);
373
+ return null;
374
+ }
375
+ };
376
+ /**
377
+ * Fetch profile picture from API with headers and return as blob URL
378
+ * This function fetches the image with required headers (X-Message-Id, X-Correlation-Id)
379
+ * and converts it to a blob URL that can be used in img src
380
+ * @param baseUrl - Base URL for the object store API (default: http://objectstore.impact0mics.local:9012)
381
+ * @param messageId - Optional message ID for X-Message-Id header (default: generated UUID)
382
+ * @param correlationId - Optional correlation ID for X-Correlation-Id header (default: generated UUID)
383
+ * @returns Promise that resolves to blob URL string or null if fetch fails
384
+ */
385
+ const fetchProfilePictureAsBlobUrl = async (baseUrl = "http://objectstore.impact0mics.local:9012", messageId, correlationId) => {
386
+ try {
387
+ const profilePictureUrl = getProfilePictureUrl(baseUrl);
388
+ if (!profilePictureUrl) {
389
+ return null;
390
+ }
391
+ // Generate message ID and correlation ID if not provided
392
+ const msgId = messageId || `msg-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
393
+ const corrId = correlationId || `corr-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
394
+ // Fetch the image with required headers
395
+ const response = await fetch(profilePictureUrl, {
396
+ method: 'GET',
397
+ headers: {
398
+ 'X-Message-Id': msgId,
399
+ 'X-Correlation-Id': corrId,
400
+ },
401
+ });
402
+ // Check if the response is successful
403
+ if (!response.ok) {
404
+ console.warn(`Failed to fetch profile picture: ${response.status} ${response.statusText}`);
405
+ return null;
406
+ }
407
+ // Check if the response is an image
408
+ const contentType = response.headers.get('content-type');
409
+ if (!contentType || !contentType.startsWith('image/')) {
410
+ console.warn(`Profile picture response is not an image: ${contentType}`);
411
+ return null;
412
+ }
413
+ // Convert response to blob
414
+ const blob = await response.blob();
415
+ // Create blob URL
416
+ const blobUrl = URL.createObjectURL(blob);
417
+ return blobUrl;
418
+ }
419
+ catch (err) {
420
+ console.error("Error fetching profile picture:", err);
421
+ return null;
422
+ }
423
+ };
343
424
 
344
425
  const translations = {
345
426
  en: {
@@ -547,6 +628,34 @@ const AppHeader = ({ language: languageProp }) => {
547
628
  const [messageCount, setMessageCount] = React.useState(() => {
548
629
  return getMessageCountFromStorage() ?? undefined;
549
630
  });
631
+ // State for profile picture blob URL
632
+ const [profilePictureBlobUrl, setProfilePictureBlobUrl] = React.useState(null);
633
+ // Fetch profile picture from API when component mounts or user data changes
634
+ React.useEffect(() => {
635
+ const fetchProfilePicture = async () => {
636
+ // Try to fetch profile picture from API
637
+ const blobUrl = await fetchProfilePictureAsBlobUrl();
638
+ if (blobUrl) {
639
+ // Clean up previous blob URL if it exists
640
+ setProfilePictureBlobUrl((prevUrl) => {
641
+ if (prevUrl) {
642
+ URL.revokeObjectURL(prevUrl);
643
+ }
644
+ return blobUrl;
645
+ });
646
+ }
647
+ };
648
+ fetchProfilePicture();
649
+ // Cleanup function to revoke blob URL when component unmounts or effect re-runs
650
+ return () => {
651
+ setProfilePictureBlobUrl((prevUrl) => {
652
+ if (prevUrl) {
653
+ URL.revokeObjectURL(prevUrl);
654
+ }
655
+ return null;
656
+ });
657
+ };
658
+ }, []); // Only run once on mount - fetch when component loads
550
659
  React.useEffect(() => {
551
660
  const allData = getAllDataFromStorage();
552
661
  // Try to get user data from various sources
@@ -609,11 +718,12 @@ const AppHeader = ({ language: languageProp }) => {
609
718
  userInitials = userInitials || profileData.initials || undefined;
610
719
  }
611
720
  }
721
+ // Use fetched blob URL if available, otherwise fall back to other sources
612
722
  setUser({
613
723
  name: userName || "",
614
724
  email: userEmail || "",
615
725
  role: userRole || "",
616
- avatar: userAvatar,
726
+ avatar: profilePictureBlobUrl || userAvatar,
617
727
  initials: userInitials,
618
728
  });
619
729
  // Update online status
@@ -638,7 +748,7 @@ const AppHeader = ({ language: languageProp }) => {
638
748
  setCurrentLanguage(languageProp);
639
749
  setI18nLocaleToStorage(languageProp);
640
750
  }
641
- }, [languageProp]);
751
+ }, [languageProp, profilePictureBlobUrl]); // Also update when profile picture is fetched
642
752
  const finalRoutes = DEFAULT_ROUTES;
643
753
  // Get online status from localStorage dynamically
644
754
  const [isOnlineStatus, setIsOnlineStatus] = React.useState(() => {