Skip to content

Commit

Permalink
refactor: getLastTimeSlotImage and localImage methods with additional…
Browse files Browse the repository at this point in the history
… checks and logic
  • Loading branch information
adkif committed Oct 17, 2024
1 parent 90d15d0 commit a0c6c71
Showing 1 changed file with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,9 @@ export class TimeTrackerComponent implements OnInit, AfterViewInit {
}

public async getLastTimeSlotImage(arg): Promise<void> {
if (this._isOffline) return;
if (this._isOffline || this.lastTimeSlot?.id === arg?.timeSlotId) {
return;
}
try {
const res = await this.timeTrackerService.getTimeSlot(arg);
const { screenshots = [] } = res || {};
Expand All @@ -1728,24 +1730,40 @@ export class TimeTrackerComponent implements OnInit, AfterViewInit {
}
}

public async localImage(img, originalBase64Image?: string): Promise<void> {
public async localImage(
img: { thumbUrl?: string; recordedAt?: string; fullUrl?: string } | string,
originalBase64Image?: string
): Promise<void> {
try {
const convScreenshot =
img && img.thumbUrl ? await this._imageViewerService.getBase64ImageFromUrl(img.thumbUrl) : img;
localStorage.setItem(
'lastScreenCapture',
JSON.stringify({
thumbUrl: convScreenshot,
textTime: moment().fromNow(),
createdAt: Date.now(),
recordedAt: Date.now(),
...(originalBase64Image && {
fullUrl: originalBase64Image
})
})
);
// Determine the fullUrl, prioritizing originalBase64Image if provided
const fullUrl = originalBase64Image || (typeof img === 'object' ? img.fullUrl : undefined);

// Fetch the thumbnail or use the image string directly if img is a string
const thumbUrl =
typeof img === 'object' && img.thumbUrl
? await this._imageViewerService.getBase64ImageFromUrl(img.thumbUrl)
: typeof img === 'string'
? img
: undefined;

// Set timestamp, preferring recordedAt if available
const timestamp = typeof img === 'object' && img.recordedAt ? new Date(img.recordedAt) : new Date();

if (fullUrl && thumbUrl) {
const screenCaptureData = {
fullUrl,
thumbUrl,
textTime: moment(timestamp).fromNow(),
createdAt: timestamp,
recordedAt: timestamp
};

localStorage.setItem('lastScreenCapture', JSON.stringify(screenCaptureData));
} else {
this._loggerService.warn('WARN: Invalid image data: missing fullUrl or thumbUrl.');
}
} catch (error) {
console.log('ERROR', error);
this._loggerService.error('ERROR: Storing image:', error.message);
}
}

Expand Down

0 comments on commit a0c6c71

Please sign in to comment.