let commandIdCounter = 0;
/**
* Generate a unique command ID
*/
export function generateCommandId(): string {
return `cmd_${Date.now()}_${++commandIdCounter}`;
}
export interface ProgressPayload {
currentChunk?: number;
totalChunks?: number;
chunkSize?: number;
[key: string]: unknown;
}
export interface ProgressUpdate {
type: "command_progress";
commandId: string;
commandType: string;
status: "started" | "in_progress" | "completed" | "error";
progress: number;
totalItems: number;
processedItems: number;
message: string;
timestamp: number;
currentChunk?: number;
totalChunks?: number;
chunkSize?: number;
payload?: ProgressPayload;
}
/**
* Send progress update to UI
*/
export function sendProgressUpdate(
commandId: string,
commandType: string,
status: ProgressUpdate["status"],
progress: number,
totalItems: number,
processedItems: number,
message: string,
payload: ProgressPayload | null = null,
): ProgressUpdate {
const update: ProgressUpdate = {
type: "command_progress",
commandId,
commandType,
status,
progress,
totalItems,
processedItems,
message,
timestamp: Date.now(),
};
// Add optional chunk information if present
if (payload) {
if (
payload.currentChunk !== undefined &&
payload.totalChunks !== undefined
) {
update.currentChunk = payload.currentChunk;
update.totalChunks = payload.totalChunks;
update.chunkSize = payload.chunkSize;
}
update.payload = payload;
}
// Send to UI
figma.ui.postMessage(update);
console.log(`Progress update: ${status} - ${progress}% - ${message}`);
return update;
}