From 6c70d6a60b286f7cfbbde4e428d41c8de2c7e77a Mon Sep 17 00:00:00 2001 From: diogo464 Date: Thu, 14 Aug 2025 15:34:42 +0100 Subject: feat: improve DriveLogEntry types with discriminated unions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace generic DriveLogEntry with action-specific discriminated unions - Add DriveLogEntryCreateFile, CreateDir, Remove, Rename types - Update Drive_log parsing to create correct union types based on action - Add type guards (isCreateFileEntry, isRenameEntry, etc.) for safe access - Enhance History UI to properly display rename operations (old → new) - Change log_id to revision to match Rust OperationHeader structure - Improve type safety and maintainability 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- frontend/lib/drive_types.ts | 51 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) (limited to 'frontend/lib/drive_types.ts') diff --git a/frontend/lib/drive_types.ts b/frontend/lib/drive_types.ts index 9220d82..a168a72 100644 --- a/frontend/lib/drive_types.ts +++ b/frontend/lib/drive_types.ts @@ -22,12 +22,57 @@ export interface DriveTreeResponse { root: DriveTreeNode[] } -export interface DriveLogEntry { +// Base interface for all log entries with common fields +export interface DriveLogEntryBase { timestamp: number - log_id: number + revision: number // Changed from log_id to match Rust OperationHeader email: string - action: string +} + +// Specific log entry types based on Rust OperationData variants +export interface DriveLogEntryCreateFile extends DriveLogEntryBase { + action: "create_file" path: string blob_id: string size: number } + +export interface DriveLogEntryCreateDir extends DriveLogEntryBase { + action: "create_dir" + path: string +} + +export interface DriveLogEntryRemove extends DriveLogEntryBase { + action: "remove" + path: string +} + +export interface DriveLogEntryRename extends DriveLogEntryBase { + action: "rename" + old_path: string + new_path: string +} + +// Discriminated union of all possible log entry types +export type DriveLogEntry = + | DriveLogEntryCreateFile + | DriveLogEntryCreateDir + | DriveLogEntryRemove + | DriveLogEntryRename + +// Type guards for working with discriminated unions +export function isCreateFileEntry(entry: DriveLogEntry): entry is DriveLogEntryCreateFile { + return entry.action === "create_file" +} + +export function isCreateDirEntry(entry: DriveLogEntry): entry is DriveLogEntryCreateDir { + return entry.action === "create_dir" +} + +export function isRemoveEntry(entry: DriveLogEntry): entry is DriveLogEntryRemove { + return entry.action === "remove" +} + +export function isRenameEntry(entry: DriveLogEntry): entry is DriveLogEntryRename { + return entry.action === "rename" +} -- cgit