view and open

This commit is contained in:
Tim Bendt
2025-05-09 19:54:56 -06:00
parent 9ad483dca8
commit a7a2cfe8dc
3 changed files with 198 additions and 61 deletions

View File

@@ -61,7 +61,8 @@ class OneDriveTUI(App):
self.access_token = None
self.drives = []
self.followed_items = []
self.current_items = [] # Store currently displayed items
self.current_items = {} # Store currently displayed items
self.msal_app = None
self.cache = msal.SerializableTokenCache()
# Read Azure app credentials from environment variables
@@ -105,6 +106,7 @@ class OneDriveTUI(App):
self.query_one("#view_options").border_title = "My Files"
# Initialize the table
table = self.query_one("#items_table")
table.cursor_type = "row"
table.add_columns("Type", "Name", "Last Modified", "Size", "Web URL")
# Load cached token if available
@@ -256,29 +258,60 @@ class OneDriveTUI(App):
if self.selected_drive_id:
self.load_root_items()
async def on_data_table_row_selected(self, event: DataTable.RowSelected) -> None:
"""Handle row selection in the items table."""
selected_id = event.row_key.value
self.open_item(selected_id)
async def on_data_table_row_highlighted(self, event: DataTable.RowHighlighted) -> None:
self.selected_item_id = event.row_key.value
def open_item(self, selected_id: str):
if selected_id:
# Get an item from current items by ID string
selected_row = self.current_items[selected_id]
item_name = selected_row.get("name")
# Check if it's a folder
is_folder = bool(selected_row.get("folder"))
if is_folder:
self.notify(f"Selected folder: {item_name}")
# Load items in the folder
self.query_one("#status_label").update(f"Loading items in folder: {item_name}")
self.load_root_items(folder_id=selected_id, drive_id=selected_row.get("parentReference", {}).get("driveId", self.selected_drive_id))
else:
self.notify(f"Selected file: {item_name}")
self.action_view_document()
@work
async def load_root_items(self):
async def load_root_items(self, folder_id: str = "", drive_id: str = ""):
"""Load root items from the selected drive."""
if not self.access_token or not self.selected_drive_id:
return
self.query_one("#status_label").update("Loading root items...")
headers = {"Authorization": f"Bearer {self.access_token}"}
url = f"https://graph.microsoft.com/v1.0/me/drive/root/children"
if folder_id and drive_id:
url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{folder_id}/children"
self.selected_drive_id = drive_id
try:
url = f"https://graph.microsoft.com/v1.0/me/drive/root/children"
async with aiohttp.ClientSession() as session:
async with session.get(url, headers=headers) as response:
if response.status != 200:
self.notify(f"Failed to load root items: {response.status}", severity="error")
self.notify(f"Failed to load drive items: {response.status}", severity="error")
return
items_data = await response.json()
self.current_items = items_data.get("value", [])
# Update the table with the root items
self.update_items_table(self.current_items, is_root_view=True)
self.update_items_table(items_data.get("value", []), is_root_view=True)
except Exception as e:
self.notify(f"Error loading root items: {str(e)}", severity="error")
@@ -303,11 +336,11 @@ class OneDriveTUI(App):
return
items_data = await response.json()
self.followed_items = items_data.get("value", [])
self.current_items = self.followed_items
followed_items = items_data.get("value", [])
# Update the table with the followed items
self.update_items_table(self.followed_items)
self.update_items_table(followed_items)
except Exception as e:
self.notify(f"Error loading followed items: {str(e)}", severity="error")
@@ -315,7 +348,7 @@ class OneDriveTUI(App):
def update_items_table(self, items, is_root_view=False):
"""Update the table with the given items."""
table = self.query_one("#items_table")
table = self.query_one(DataTable)
table.clear()
if not items:
@@ -356,8 +389,11 @@ class OneDriveTUI(App):
size_str = "N/A"
web_url = item.get("webUrl", "")
table.add_row(item_type, name, last_modified, size_str, web_url)
item_id = item.get("id")
item_drive_id = item.get("parentReference", {}).get("driveId", self.selected_drive_id)
row_key = table.add_row(item_type, name, last_modified, size_str, web_url, key=item.get("id"))
# add item to to the list of current items keyed by row_key so we can look up all information later
self.current_items[row_key] = item
async def action_next_view(self) -> None:
"""Switch to the next view (Following/Root)."""
@@ -400,44 +436,20 @@ class OneDriveTUI(App):
# Use Textual's built-in open_url method
self.app.open_url(web_url)
async def action_view_document(self) -> None:
def action_view_document(self) -> None:
"""View the selected document using the DocumentViewerScreen."""
table = self.query_one("#items_table")
if table.cursor_row is None:
return
# Get the name of the selected item
selected_row = table.get_row_at(table.cursor_row)
selected_row = self.current_items.get(self.selected_item_id)
if not selected_row:
return
selected_name = selected_row[1]
# Find the item in our list to get its ID
selected_item = None
for item in self.current_items:
if item.get("name") == selected_name:
selected_item = item
break
if not selected_item:
self.notify("Could not find the selected item details", severity="error")
return
# Check if it's a folder - cannot view folders
if selected_item.get("folder"):
self.notify("Cannot preview folders. Use 'Open URL' to view in browser.", severity="warning")
return
# Get the item ID
item_id = selected_item.get("id")
if not item_id:
self.notify("Item ID not found", severity="error")
return
# Open the document viewer screen
viewer = DocumentViewerScreen(item_id, selected_name, self.access_token, selected_item.get("parentReference").get("driveId"))
selected_name = selected_row.get("name")
drive_id = selected_row.get("parentReference", {}).get("driveId", self.selected_drive_id)
web_url = selected_row.get("webUrl", "")
# Open the document viewer screen with all required details
viewer = DocumentViewerScreen(self.selected_item_id, selected_name, self.access_token, drive_id)
viewer.web_url = web_url # Pass the webUrl to the viewer
self.push_screen(viewer)
async def action_quit(self) -> None: