# email_actions.py # Filepath: ai-email-assistant/email_actions.py # Handles email template management and actions import logging import gradio as gr from google_sheets_utils import get_templates_from_sheet, save_template, delete_template_from_sheet, generate_template_id, update_template_in_sheet logger = logging.getLogger(__name__) def get_templates(spreadsheet): try: return get_templates_from_sheet(spreadsheet.worksheet("Email Templates")) except Exception as e: logger.error(f"Error fetching templates: {str(e)}") return [] def get_template_summaries(spreadsheet): templates = get_templates_from_sheet(spreadsheet.worksheet("Email Templates")) return [f"{template['Name']} ({template['Type']})" for template in templates] def create_email_actions_interface(spreadsheet, current_template): worksheet = spreadsheet.worksheet("Email Templates") with gr.Blocks() as email_actions_block: gr.Markdown("## Email Actions") t_name = gr.Textbox(label="Template Name", elem_id="t_name") t_type = gr.Dropdown( ["Cold Email", "Follow-up", "Thank You", "Other"], label="Template Type", allow_custom_value=True, elem_id="t_type" ) t_subject = gr.Textbox(label="Subject", elem_id="t_subject") t_body = gr.Textbox(label="Body", lines=5, elem_id="t_body") t_id = gr.Textbox(label="Template ID", interactive=False, elem_id="t_id") headers = ["Type", "ID", "Name", "Subject", "Body"] templates_list = gr.Dataframe( headers=headers, label="Email Templates List", interactive=False, elem_id="t_templates_list" ) save_btn = gr.Button("Save New Template", elem_id="t_save_btn") update_btn = gr.Button("Update Existing Template", elem_id="t_update_btn") delete_btn = gr.Button("Delete Template", elem_id="t_delete_btn") clear_btn = gr.Button("Clear Fields", elem_id="t_clear_btn") def refresh_templates(): templates = get_templates(spreadsheet) return [[template.get(h, "") for h in headers] for template in templates] def load_template(evt: gr.SelectData, current_name, current_type, current_subject, current_body, current_id): templates = get_templates(spreadsheet) selected_row = evt.index[0] selected_template = templates[selected_row] logger.debug(f"Selected Email Template: {selected_template}") # Check if ANY field is non-empty fields = [current_name, current_type, current_subject, current_body, current_id] non_empty_fields = any(field and field.strip() != "" for field in fields) logger.debug(f"Any email template field non-empty: {non_empty_fields}") if non_empty_fields: logger.warning("Email template fields not empty, showing warning") gr.Warning("Please clear all fields before loading a new email template.") return [gr.update() for _ in range(5)] # Don't update any fields logger.info("Loading selected email template") try: return ( selected_template["Name"], selected_template["Type"], selected_template["Subject"], selected_template["Body"], selected_template["ID"] ) except KeyError as e: logger.error(f"Error loading template: missing key {e}") gr.Error(f"Error loading template: missing information") return [gr.update() for _ in range(5)] def save_template_action(name, template_type, subject, body): # Check for empty fields if not name or not template_type or not subject or not body: message = gr.HTML("

All fields are required. Please fill in all details before saving.

") return gr.update(), message, refresh_templates() new_id = generate_template_id() template_data = [ "Email Templates", # Type new_id, # ID name, template_type, subject, body ] result = save_template(worksheet, template_data) if result: message = gr.HTML("

Template added successfully.

") else: message = gr.HTML("

Failed to add template.

") return gr.update(), message, refresh_templates() def update_template_action(template_id, name, template_type, subject, body): # Check for empty fields if not template_id: message = gr.HTML("

No template selected. Please select a template to update.

") return gr.update(), message, refresh_templates() if not name or not template_type or not subject or not body: message = gr.HTML("

All fields are required. Please fill in all details before updating.

") return gr.update(), message, refresh_templates() updated_data = [ "Email Templates", # Type template_id, name, template_type, subject, body ] result = update_template_in_sheet(worksheet, template_id, updated_data) if result: message = gr.HTML("

Template updated successfully.

") else: message = gr.HTML("

Failed to update template.

") return gr.update(), message, refresh_templates() def delete_template_action(template_id): if not template_id: message = gr.HTML("

No template selected. Please select a template to delete.

") return message, refresh_templates() result = delete_template_from_sheet(worksheet, template_id) if result: message = gr.HTML("

Template deleted successfully.

") else: message = gr.HTML("

Failed to delete template.

") return message, refresh_templates() def clear_fields(): return [""] * 5 # Clear all 5 fields save_btn.click( save_template_action, inputs=[t_name, t_type, t_subject, t_body], outputs=[templates_list] ) update_btn.click( update_template_action, inputs=[t_id, t_name, t_type, t_subject, t_body], outputs=[templates_list] ) delete_btn.click( delete_template_action, inputs=[t_id], outputs=[templates_list] ) clear_btn.click(clear_fields, outputs=[t_name, t_type, t_subject, t_body, t_id]) templates_list.select( load_template, inputs=[t_name, t_type, t_subject, t_body, t_id], outputs=[t_name, t_type, t_subject, t_body, t_id] ) # Refresh templates on component creation templates_list.value = refresh_templates() # Return a dictionary containing the block and required components/functions return { "block": email_actions_block, "templates_list": templates_list, "t_name": t_name, "t_type": t_type, "t_subject": t_subject, "t_body": t_body, "t_id": t_id, "refresh_templates": refresh_templates, "load_template": load_template, # Add other components or functions you need to access }