[content] src/recording.ts (typescript)


/**
 * Deletes the last 10 seconds of recording data from the CSV file.
 * This is a "panic button" feature that allows users to quickly remove recent sensitive data.
 */
export async function panicButton(): Promise<void> {
    if (!recording.isRecording) {
        vscode.window.showWarningMessage(vscode.l10n.t('No active recording to panic'))
        logToOutput(vscode.l10n.t('No active recording to panic'), 'warning')
        return
    }

    if (!recording.startDateTime) {
        vscode.window.showErrorMessage(vscode.l10n.t('Recording start time not available'))
        logToOutput(vscode.l10n.t('Recording start time not available'), 'error')
        return
    }

    const exportPath = getExportPath()
    if (!exportPath) {
        vscode.window.showErrorMessage(vscode.l10n.t('Export path not available'))
        logToOutput(vscode.l10n.t('Export path not available'), 'error')
        return
    }

    const baseFilePath = generateBaseFilePath(recording.startDateTime, false, recording.customFolderName, sessionUuid)
    if (!baseFilePath) {
        vscode.window.showErrorMessage(vscode.l10n.t('Could not generate file path'))
        logToOutput(vscode.l10n.t('Could not generate file path'), 'error')
        return
    }

    const filePath = path.join(exportPath, `${baseFilePath}.csv`)
    
    try {
        // Check if file exists
        if (!fs.existsSync(filePath)) {
            vscode.window.showWarningMessage(vscode.l10n.t('No recording file found to panic'))
            logToOutput(vscode.l10n.t('No recording file found to panic'), 'warning')
            return
        }

        // Read the current file content
        const fileContent = await fs.promises.readFile(filePath, 'utf-8')
        const lines = fileContent.split('
')
        
        if (lines.length <= 1) {
            vscode.window.showWarningMessage(vscode.l10n.t('Recording file is empty'))
            logToOutput(vscode.l10n.t('Recording file is empty'), 'warning')
            return
        }

        // Calculate the cutoff time (10 seconds ago from now)
        const now = new Date().getTime()
        const cutoffTime = now - recording.startDateTime.getTime() - 10000 // 10 seconds in milliseconds

        // Keep the header line and filter out lines from the last 10 seconds
        const headerLine = lines[0]
        const dataLines = lines.slice(1).filter(line => {
            if (!line.trim()) return false // Skip empty lines
            
            const lineArr = line.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/)
            if (lineArr.length < 2) return false // Skip malformed lines
            
            const timeStr = lineArr[1]
            const time = parseInt(timeStr, 10)
            
            if (isNaN(time)) return false // Skip lines with invalid time
            
            return time <= cutoffTime
        })

        // Rewrite the file with filtered content
        const newContent = [headerLine, ...dataLines].join('
')
        await fs.promises.writeFile(filePath, newContent, 'utf-8')

        // Update the sequence number to reflect the removed entries
        const removedLines = lines.length - dataLines.length - 1 // -1 for header
        if (removedLines > 0) {
            recording.sequence = Math.max(0, recording.sequence - removedLines)
        }

        vscode.window.showInformationMessage(vscode.l10n.t('Panic button activated: Removed last 10 seconds of recording'))
        logToOutput(vscode.l10n.t('Panic button activated: Removed last 10 seconds of recording'), 'info')

    } catch (error) {
        const errorMessage = `Error during panic button operation: ${error}`
        vscode.window.showErrorMessage(errorMessage)
        logToOutput(errorMessage, 'error')
    }
}