64 lines
1.9 KiB
TypeScript
64 lines
1.9 KiB
TypeScript
import type { VerificationIssueStage } from './verificationBoardStages';
|
|
|
|
export const VERIFICATION_ISSUE_EVENT_TOPIC = '/sub/verification-issues/events';
|
|
|
|
export type VerificationIssueEventType = 'CREATED' | 'STAGE_CHANGED';
|
|
|
|
export interface VerificationIssueEventDto {
|
|
eventType: VerificationIssueEventType;
|
|
issueId: number;
|
|
title: string;
|
|
stage: VerificationIssueStage;
|
|
previousStage?: VerificationIssueStage | null;
|
|
updatedAt?: string;
|
|
sourceDeviceId?: string | null;
|
|
}
|
|
|
|
export interface VerificationIssueToastItem {
|
|
id: string;
|
|
issueId: number;
|
|
title: string;
|
|
stage: VerificationIssueStage;
|
|
previousStage?: VerificationIssueStage | null;
|
|
eventType: VerificationIssueEventType;
|
|
}
|
|
|
|
export function getVerificationDeviceId(): string {
|
|
let id = localStorage.getItem('gc_device_id');
|
|
if (!id) {
|
|
id = crypto.randomUUID();
|
|
localStorage.setItem('gc_device_id', id);
|
|
}
|
|
return id;
|
|
}
|
|
|
|
export function parseVerificationIssueEvent(raw: string): VerificationIssueEventDto | null {
|
|
try {
|
|
const data = JSON.parse(raw) as VerificationIssueEventDto;
|
|
if (data.issueId == null || !data.eventType || !data.title) return null;
|
|
if (data.eventType !== 'CREATED' && data.eventType !== 'STAGE_CHANGED') return null;
|
|
return data;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
export function shouldShowVerificationIssueEvent(event: VerificationIssueEventDto): boolean {
|
|
const localDevice = getVerificationDeviceId();
|
|
if (event.sourceDeviceId && event.sourceDeviceId === localDevice) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
export function eventToToast(event: VerificationIssueEventDto): VerificationIssueToastItem {
|
|
return {
|
|
id: `${event.eventType}-${event.issueId}-${event.updatedAt ?? Date.now()}`,
|
|
issueId: event.issueId,
|
|
title: event.title,
|
|
stage: event.stage,
|
|
previousStage: event.previousStage,
|
|
eventType: event.eventType,
|
|
};
|
|
}
|