Skip to content

Commit

Permalink
Merge pull request #8448 from ever-co/fix/#8360-last-timeslot
Browse files Browse the repository at this point in the history
[Fix] #8360 Last time slot
  • Loading branch information
rahul-rocket authored Oct 18, 2024
2 parents 5eab51b + a0c6c71 commit feb8d46
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ export class LoggerService {
public error<T>(...message: T[]): void {
if (this._log) this._log.error(...message);
}

public warn(...message: any[]): void {
if (this._log) this._log.warn(...message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import { Store } from '../services';
@Injectable({
providedIn: 'root',
})
export class TimeSlotCacheService extends AbstractCacheService<ITimeSlot[]> {
export class TimeSlotCacheService extends AbstractCacheService<ITimeSlot> {
constructor(
protected _storageService: StorageService<ITimeSlot[]>,
protected _storageService: StorageService<ITimeSlot>,
protected _store: Store
) {
super(_storageService, _store);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@
<div class="row">
<div class="col-6">
<button (click)="showImage()" nbButton status="basic">
<nb-icon icon="eye" pack="font-awesome"></nb-icon>
<nb-icon
[icon]="screenshots.length > 0 ? 'eye' : 'eye-slash'"
pack="font-awesome"
></nb-icon>
</button>
</div>
<div class="col-6">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
Subject,
tap
} from 'rxjs';
import * as _ from 'underscore';
import { AlwaysOnService, AlwaysOnStateEnum } from '../always-on/always-on.service';
import { AuthStrategy } from '../auth';
import { GAUZY_ENV } from '../constants';
Expand Down Expand Up @@ -807,11 +806,9 @@ export class TimeTrackerComponent implements OnInit, AfterViewInit {
this.teamSelectorService.load(),
this.taskSelectorService.load(),
this.getTodayTime(),
this.setTimerDetails()
this.setTimerDetails(),
this.getLastTimeSlotImage(arg)
];
if (arg.timeSlotId) {
parallelizedTasks.push(this.getLastTimeSlotImage(arg));
}
await Promise.allSettled(parallelizedTasks);
this._isReady = true;
this._isRefresh$.next(false);
Expand Down Expand Up @@ -1708,21 +1705,22 @@ 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);
let { screenshots }: any = res;
const { screenshots = [] } = res || {};
console.log('Get Last Timeslot Image Response:', screenshots);
if (screenshots && screenshots.length > 0) {
screenshots = _.sortBy(screenshots, 'recordedAt').reverse();
const [lastCaptureScreen] = screenshots;
console.log('Last Capture Screen:', lastCaptureScreen);
this.lastScreenCapture$.next(lastCaptureScreen);
await this.localImage(this.lastScreenCapture);
this.screenshots$.next(screenshots);
this.lastTimeSlot = res;
}
if (this.lastScreenCapture.recordedAt) {
if (this.lastScreenCapture?.recordedAt) {
this.lastScreenCapture$.next({
...this.lastScreenCapture,
textTime: moment(this.lastScreenCapture.recordedAt).fromNow()
Expand All @@ -1735,24 +1733,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 Expand Up @@ -1822,6 +1836,12 @@ export class TimeTrackerComponent implements OnInit, AfterViewInit {
}

public showImage(): void {
if (!this.screenshots.length) {
const message = 'Attempted to open an empty image gallery.';
this.toastrService.warning(message);
this._loggerService.warn(`WARN: ${message}`);
return;
}
this.electronService.ipcRenderer.send('show_image', this.screenshots);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ITaskStatusFindInput,
ITaskUpdateInput,
ITimeLog,
ITimeSlot,
TimeLogSourceEnum,
TimeLogType
} from '@gauzy/contracts';
Expand Down Expand Up @@ -401,25 +402,36 @@ export class TimeTrackerService {
return firstValueFrom(timeLogs$);
}

async getTimeSlot(values) {
async getTimeSlot(values: { timeSlotId: string }): Promise<ITimeSlot> {
const { timeSlotId } = values;
if (!timeSlotId) {
this._loggerService.log.warn('WARN: Time Slot ID should not be empty');
return null;
}
this._loggerService.log.info(`Get Time Slot: ${moment().format()}`);
const { tenantId, organizationId } = this._store;
const params = toParams({
tenantId,
organizationId,
relations: ['screenshots']
relations: ['screenshots'],
order: {
createdAt: 'DESC',
screenshots: {
recordedAt: 'DESC'
}
}
});
let timeSlots$ = this._timeSlotCacheService.getValue(values.timeSlotId);
let timeSlots$ = this._timeSlotCacheService.getValue(timeSlotId);
if (!timeSlots$) {
timeSlots$ = this.http
.get(`${API_PREFIX}/timesheet/time-slot/${values.timeSlotId}`, {
.get<ITimeSlot>(`${API_PREFIX}/timesheet/time-slot/${timeSlotId}`, {
params
})
.pipe(
map((response: any) => response),
shareReplay(1)
);
this._timeSlotCacheService.setValue(timeSlots$, values.timeSlotId);
this._timeSlotCacheService.setValue(timeSlots$, timeSlotId);
}
return firstValueFrom(timeSlots$);
}
Expand Down

0 comments on commit feb8d46

Please sign in to comment.