clean up UI on drive viewer
This commit is contained in:
@@ -30,6 +30,9 @@ from textual.worker import Worker, get_current_worker
|
||||
from textual import work
|
||||
from textual.widgets.option_list import Option
|
||||
|
||||
# Import file icons utility - note the updated import
|
||||
from utils.file_icons import get_file_icon
|
||||
|
||||
# Import our DocumentViewerScreen
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), "maildir_gtd"))
|
||||
from maildir_gtd.screens.DocumentViewer import DocumentViewerScreen
|
||||
@@ -96,26 +99,24 @@ class OneDriveTUI(App):
|
||||
yield Header(show_clock=True)
|
||||
|
||||
with Container(id="main_container"):
|
||||
yield LoadingIndicator(id="loading")
|
||||
yield Label("Authenticating with Microsoft Graph API...", id="status_label")
|
||||
with Horizontal(id="top_bar"):
|
||||
yield Button("\uf148 Up", id="back_button", classes="hide")
|
||||
yield Label("Authenticating with Microsoft Graph API...", id="status_label")
|
||||
yield LoadingIndicator(id="loading")
|
||||
yield OptionList(
|
||||
Option("Following", id="following"),
|
||||
Option("Root", id="root"),
|
||||
id="view_options"
|
||||
)
|
||||
|
||||
with Container(id="auth_container"):
|
||||
yield Label("", id="auth_message")
|
||||
yield Button("Login", id="login_button", variant="primary")
|
||||
|
||||
with Container(id="content_container", classes="hide"):
|
||||
with Horizontal():
|
||||
with Vertical(id="navigation_container"):
|
||||
|
||||
yield OptionList(
|
||||
Option("Following", id="following"),
|
||||
Option("Root", id="root"),
|
||||
id="view_options"
|
||||
)
|
||||
|
||||
with Vertical(id="items_container"):
|
||||
yield DataTable(id="items_table")
|
||||
yield Label("No items found", id="no_items_label", classes="hide")
|
||||
with Vertical(id="items_container"):
|
||||
yield DataTable(id="items_table")
|
||||
yield Label("No items found", id="no_items_label", classes="hide")
|
||||
|
||||
yield Footer()
|
||||
|
||||
@@ -126,7 +127,7 @@ class OneDriveTUI(App):
|
||||
# Initialize the table
|
||||
table = self.query_one("#items_table")
|
||||
table.cursor_type = "row"
|
||||
table.add_columns("Type", "Name", "Last Modified", "Size", "Web URL")
|
||||
table.add_columns("◇", "Name", "Last Modified", "Size", "Web URL")
|
||||
|
||||
# Load cached token if available
|
||||
if os.path.exists(self.cache_file):
|
||||
@@ -183,6 +184,8 @@ class OneDriveTUI(App):
|
||||
|
||||
async def on_button_pressed(self, event: Button.Pressed) -> None:
|
||||
"""Handle button presses."""
|
||||
if event.button.id == "back_button":
|
||||
self.action_navigate_back()
|
||||
if event.button.id == "login_button":
|
||||
self.initiate_device_flow()
|
||||
|
||||
@@ -275,7 +278,7 @@ class OneDriveTUI(App):
|
||||
elif selected_option == "root":
|
||||
self.current_view = "Root"
|
||||
if self.selected_drive_id:
|
||||
self.load_root_items()
|
||||
self.load_drive_folder_items()
|
||||
|
||||
async def on_data_table_row_selected(self, event: DataTable.RowSelected) -> None:
|
||||
"""Handle row selection in the items table."""
|
||||
@@ -287,6 +290,7 @@ class OneDriveTUI(App):
|
||||
|
||||
def open_item(self, selected_id: str):
|
||||
if selected_id:
|
||||
self.folder_history.append(FolderHistoryEntry(self.current_folder_id, self.current_folder_name, self.selected_drive_id))
|
||||
# Get an item from current items by ID string
|
||||
selected_row = self.current_items[selected_id]
|
||||
|
||||
@@ -294,19 +298,19 @@ class OneDriveTUI(App):
|
||||
|
||||
# Check if it's a folder
|
||||
is_folder = bool(selected_row.get("folder"))
|
||||
|
||||
if is_folder:
|
||||
self.notify(f"Selected folder: {item_name}")
|
||||
self.notify(f"Selected folder: {item_name}", timeout=1)
|
||||
# Load items in the folder
|
||||
self.query_one("#back_button").remove_class("hide")
|
||||
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))
|
||||
self.load_drive_folder_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, folder_id: str = "", drive_id: str = "", track_history: bool = True):
|
||||
async def load_drive_folder_items(self, folder_id: str = "", drive_id: str = "", track_history: bool = True):
|
||||
"""Load root items from the selected drive."""
|
||||
if not self.access_token or not self.selected_drive_id:
|
||||
return
|
||||
@@ -315,10 +319,10 @@ class OneDriveTUI(App):
|
||||
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:
|
||||
if folder_id and drive_id and folder_id != "root":
|
||||
url = f"https://graph.microsoft.com/v1.0/drives/{drive_id}/items/{folder_id}/children"
|
||||
if track_history:
|
||||
self.folder_history.append(FolderHistoryEntry(folder_id, self.current_folder_name, drive_id))
|
||||
# if track_history:
|
||||
# self.folder_history.append(FolderHistoryEntry(folder_id, self.current_folder_name, drive_id))
|
||||
self.selected_drive_id = drive_id
|
||||
self.current_folder_id = folder_id
|
||||
self.current_folder_name = self.current_items[folder_id].get("name", "Unknown")
|
||||
@@ -338,7 +342,13 @@ class OneDriveTUI(App):
|
||||
except Exception as e:
|
||||
self.notify(f"Error loading root items: {str(e)}", severity="error")
|
||||
|
||||
self.query_one("#status_label").update("Ready")
|
||||
#update the status label with breadcrumbs from the folder_history
|
||||
if self.folder_history:
|
||||
breadcrumbs = " / \uf07b ".join([entry.folder_name for entry in self.folder_history])
|
||||
self.query_one("#status_label").update(f"\uf07b {breadcrumbs} / \uf07b {self.current_folder_name}")
|
||||
else:
|
||||
self.query_one("#status_label").update(f" \uf07b {self.current_folder_name}")
|
||||
|
||||
|
||||
@work
|
||||
async def load_followed_items(self):
|
||||
@@ -384,9 +394,8 @@ class OneDriveTUI(App):
|
||||
name = item.get("name", "Unknown")
|
||||
is_folder = bool(item.get("folder"))
|
||||
|
||||
# Add folder icon if it's a folder and we're in root view
|
||||
|
||||
item_type = "📁" if is_folder else "📄"
|
||||
# Get icon for the file type
|
||||
item_type = get_file_icon(name, is_folder)
|
||||
|
||||
# Format the last modified date
|
||||
last_modified = item.get("lastModifiedDateTime", "")
|
||||
@@ -413,9 +422,21 @@ class OneDriveTUI(App):
|
||||
|
||||
web_url = item.get("webUrl", "")
|
||||
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
|
||||
|
||||
# Limit filename length to 160 characters
|
||||
display_name = name[:50] + '...' if len(name) > 50 else name
|
||||
|
||||
# Add row to table with the appropriate icon class for styling
|
||||
row_key = table.add_row(
|
||||
item_type,
|
||||
display_name,
|
||||
last_modified,
|
||||
size_str,
|
||||
web_url,
|
||||
key=item.get("id")
|
||||
)
|
||||
|
||||
# Add item 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:
|
||||
@@ -424,7 +445,7 @@ class OneDriveTUI(App):
|
||||
if self.current_view == "Following":
|
||||
option_list.highlighted = 1 # Switch to Root
|
||||
self.current_view = "Root"
|
||||
self.load_root_items()
|
||||
self.load_drive_folder_items()
|
||||
else:
|
||||
option_list.highlighted = 0 # Switch to Following
|
||||
self.current_view = "Following"
|
||||
@@ -438,7 +459,7 @@ class OneDriveTUI(App):
|
||||
if self.current_view == "Following":
|
||||
self.load_followed_items()
|
||||
elif self.current_view == "Root":
|
||||
self.load_root_items()
|
||||
self.load_drive_folder_items()
|
||||
self.notify("Refreshed items")
|
||||
|
||||
async def action_toggle_follow(self) -> None:
|
||||
@@ -479,13 +500,15 @@ class OneDriveTUI(App):
|
||||
"""Quit the application."""
|
||||
self.exit()
|
||||
|
||||
async def action_navigate_back(self) -> None:
|
||||
def action_navigate_back(self) -> None:
|
||||
"""Navigate back to the previous folder."""
|
||||
if self.folder_history:
|
||||
previous_entry = self.folder_history.pop()
|
||||
self.current_folder_id = previous_entry.folder_id
|
||||
self.current_folder_name = previous_entry.folder_name
|
||||
self.load_root_items(folder_id=previous_entry.folder_id, drive_id=previous_entry.parent_id, track_history=False)
|
||||
if len(self.folder_history) <= 0:
|
||||
self.query_one("#back_button").add_class("hide")
|
||||
self.load_drive_folder_items(folder_id=previous_entry.folder_id, drive_id=previous_entry.parent_id, track_history=False)
|
||||
else:
|
||||
self.notify("No previous folder to navigate back to")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user