Files
GoMFT/components/configs.templ
T
StarFleetCPTN d68a80bd78 feat: Add Google Photos support for transfer configurations
- Introduce Google Photos as a source and destination option in the configuration forms.
- Implement authentication handling and configuration management for Google Photos.
- Update database schema and migrations to include new fields related to Google Photos.
- Enhance UI components to support Google Photos-specific settings and forms.
- Add tests for Google Photos integration, ensuring proper functionality and authentication flow.
- Limit concurrent transfers for Google Photos to ensure compliance with API restrictions.
2025-03-15 23:16:49 -07:00

366 lines
13 KiB
Templ

package components
import (
"context"
"fmt"
"github.com/starfleetcptn/gomft/internal/db"
)
// Dialog component for confirmation dialogs
templ ConfigDialog(id string, title string, message string, confirmClass string, confirmText string, action string, configID uint, configName string) {
<div id={ id } class="hidden fixed inset-0 bg-secondary-900/50 dark:bg-secondary-900/80 backdrop-blur-sm z-50 flex items-center justify-center">
<div class="bg-white dark:bg-secondary-800 rounded-lg shadow-xl max-w-md w-full mx-4 overflow-hidden">
<div class="px-6 pt-5 pb-3 text-center">
<div class="flex justify-center mb-2">
<i class="fas fa-exclamation-triangle text-yellow-400 text-3xl"></i>
</div>
<h3 class="text-xl font-medium text-secondary-900 dark:text-secondary-100">
{ title }
</h3>
</div>
<div class="px-6 py-4 text-center">
<p class="text-secondary-700 dark:text-secondary-300">
{ message }
</p>
</div>
<div class="px-6 py-4 flex justify-end space-x-3">
<button type="button" class="btn-secondary" onclick={ hideConfigDialog(id) }>
Cancel
</button>
<button
type="button"
class={ confirmClass }
hx-delete={ fmt.Sprintf("/configs/%d", configID) }
hx-target="closest li"
hx-swap="delete"
data-config-name={ configName }
data-config-id={ fmt.Sprint(configID) }
id={ fmt.Sprintf("delete-config-btn-%d", configID) }
onclick={ triggerConfigDelete(id, configID, configName) }>
{ confirmText }
</button>
</div>
</div>
</div>
}
script hideConfigDialog(id string) {
document.getElementById(id).classList.add("hidden");
}
script showConfigDialog(id string) {
document.getElementById(id).classList.remove("hidden");
}
script triggerConfigDelete(dialogId string, configID uint, configName string) {
// Hide the dialog
document.getElementById(dialogId).classList.add("hidden");
// Store data in a way that's accessible to event handlers
window.lastDeletedConfig = {
id: configID,
name: configName
};
// Add custom marker to track this deletion
window.currentlyDeletingConfig = true;
}
type ConfigsData struct {
Configs []db.TransferConfig
Error string
ErrorDetails string
Status string
}
templ Configs(ctx context.Context, data ConfigsData) {
@LayoutWithContext("Transfer Configurations", ctx) {
<script>
// Debug notification system
console.log("Configs template loaded, setting up notification system");
// Create a global notyf instance if it doesn't exist yet
if (!window.notyf) {
window.notyf = new Notyf({
duration: 3000,
position: {
x: 'right',
y: 'top',
},
types: [
{
type: 'success',
background: '#38c172',
icon: {
className: 'fas fa-check-circle',
tagName: 'i'
}
},
{
type: 'error',
background: '#e3342f',
icon: {
className: 'fas fa-exclamation-circle',
tagName: 'i'
}
}
]
});
console.log("Notyf initialized:", window.notyf);
}
// Show status messages based on URL parameters
document.addEventListener('DOMContentLoaded', function() {
// Check for error message
const urlParams = new URLSearchParams(window.location.search);
const errorMsg = urlParams.get('error');
const errorDetails = urlParams.get('details');
const status = urlParams.get('status');
if (errorMsg) {
let message = errorMsg;
if (errorDetails) {
message += ": " + errorDetails;
}
window.notyf.error(message);
}
if (status === 'gdrive_auth_success') {
window.notyf.success("Google Drive authentication completed successfully");
}
});
// Track all HTMX events for debugging
document.addEventListener('htmx:beforeRequest', function(event) {
// Check if this is a DELETE request by examining the URL and method
const path = event.detail.path;
const method = event.detail.verb;
// Pattern match for config deletions (e.g., /configs/123)
if (path && method === 'DELETE' && path.match(/^\/configs\/\d+$/)) {
// This is definitely a delete request - store this information
window.isConfigDeleteRequest = true;
}
});
// Track HTMX after-request events for config deletion
document.addEventListener('htmx:afterRequest', function(event) {
// Check for config deletion multiple ways
const isDeleteRequest =
// Check global flag from the triggerConfigDelete function
window.currentlyDeletingConfig ||
// Check flag from beforeRequest handler
window.isConfigDeleteRequest ||
// Check URL pattern directly from this event
(event.detail.pathInfo && event.detail.pathInfo.requestPath &&
event.detail.pathInfo.requestPath.match(/^\/configs\/\d+$/) &&
event.detail.verb === 'DELETE');
// If this is a successful delete request, show notification
if (isDeleteRequest && event.detail.successful) {
let configName = "Unknown";
// Try multiple sources for config name
if (event.detail.elt && event.detail.elt.getAttribute) {
configName = event.detail.elt.getAttribute('data-config-name') || configName;
}
if (configName === "Unknown" && window.lastDeletedConfig) {
// Fallback to our stored config info
configName = window.lastDeletedConfig.name;
}
window.notyf.success(`Configuration "${configName}" deleted successfully`);
// Clear flags
window.currentlyDeletingConfig = false;
window.isConfigDeleteRequest = false;
window.lastDeletedConfig = null;
}
});
// Track HTMX error events for config deletion
document.addEventListener('htmx:responseError', function(event) {
// Similar logic as success but for errors
const isDeleteRequest =
window.currentlyDeletingConfig ||
window.isConfigDeleteRequest ||
(event.detail.pathInfo && event.detail.pathInfo.requestPath &&
event.detail.pathInfo.requestPath.match(/^\/configs\/\d+$/) &&
event.detail.verb === 'DELETE');
if (isDeleteRequest) {
let configName = "Unknown";
// Try multiple sources for config name
if (event.detail.elt && event.detail.elt.getAttribute) {
configName = event.detail.elt.getAttribute('data-config-name') || configName;
}
if (configName === "Unknown" && window.lastDeletedConfig) {
// Fallback to our stored config info
configName = window.lastDeletedConfig.name;
}
let errorMsg = `Failed to delete configuration "${configName}"`;
if (event.detail.xhr && event.detail.xhr.responseText) {
errorMsg = event.detail.xhr.responseText
// error message is a json object
const error = JSON.parse(errorMsg);
errorMsg = `Error: ${error.error}`;
}
window.notyf.error(errorMsg);
// Clear flags
window.currentlyDeletingConfig = false;
window.isConfigDeleteRequest = false;
window.lastDeletedConfig = null;
}
});
</script>
<div class="py-6">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center mb-8">
<h1 class="text-3xl font-bold text-secondary-900 dark:text-secondary-100">
<i class="fas fa-cogs mr-2 text-primary-600 dark:text-primary-400"></i>
Transfer Configurations
</h1>
<a href="/configs/new" class="btn-primary">
<i class="fas fa-plus mr-2"></i>
New Configuration
</a>
</div>
<div class="mt-6">
if len(data.Configs) == 0 {
<div class="card p-12 flex flex-col items-center justify-center text-center">
<div class="inline-block p-4 rounded-full bg-secondary-100 dark:bg-secondary-700 mb-4">
<i class="fas fa-folder-open text-secondary-400 dark:text-secondary-500 text-3xl"></i>
</div>
<h3 class="mt-2 text-lg font-medium text-secondary-900 dark:text-secondary-100">No configurations</h3>
<p class="mt-1 text-sm text-secondary-500 dark:text-secondary-400">Get started by creating a new transfer configuration.</p>
<div class="mt-6">
<a href="/configs/new" class="btn-primary">
<i class="fas fa-plus mr-2"></i>
Create First Configuration
</a>
</div>
</div>
} else {
<div class="card overflow-hidden">
<ul role="list" class="divide-y divide-secondary-200 dark:divide-secondary-700">
for _, config := range data.Configs {
<li>
<div class="block hover:bg-secondary-50 dark:hover:bg-secondary-750 transition-colors">
<div class="px-4 py-4 sm:px-6">
<div class="flex items-center justify-between">
<div class="flex items-center">
<p class="text-sm font-medium text-primary-600 dark:text-primary-400 truncate">
{ config.Name }
</p>
<!-- Google Drive Authentication Badge -->
if (config.DestinationType == "gdrive" || config.SourceType == "gdrive" || config.SourceType == "gphotos" || config.DestinationType == "gphotos") && !config.GetGoogleAuthenticated() {
<span class="ml-2 px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800 dark:bg-yellow-800 dark:text-yellow-100">
<i class="fas fa-exclamation-triangle mr-1 flex items-center"></i>
Authentication Required
</span>
}
<!-- Google Drive Authentication Status Indicator -->
if (config.DestinationType == "gdrive" || config.SourceType == "gdrive" || config.SourceType == "gphotos" || config.DestinationType == "gphotos") && config.GetGoogleAuthenticated() {
<span class="ml-2 px-2 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800 dark:bg-green-800 dark:text-green-100">
<i class="fas fa-check-circle mr-1 flex items-center"></i>
Authenticated
</span>
}
</div>
<div class="ml-2 flex-shrink-0 flex space-x-2">
<!-- Google Drive Authentication Button -->
if (config.DestinationType == "gdrive" || config.SourceType == "gdrive" || config.SourceType == "gphotos" || config.DestinationType == "gphotos") && !config.GetGoogleAuthenticated() {
<a href={ templ.SafeURL(fmt.Sprintf("/configs/%d/gdrive-auth", config.ID)) } class="btn-warning btn-sm">
<i class="fab fa-google-drive mr-1"></i>
Authenticate
</a>
}
<a href={ templ.SafeURL(fmt.Sprintf("/configs/%d", config.ID)) } class="btn-secondary btn-sm">
<i class="fas fa-edit mr-1"></i>
Edit
</a>
<!-- Add delete dialog for each configuration -->
@ConfigDialog(
fmt.Sprintf("delete-config-dialog-%d", config.ID),
"Delete Configuration",
fmt.Sprintf("Are you sure you want to delete the configuration '%s'? This cannot be undone.", config.Name),
"btn-danger",
"Delete",
"delete",
config.ID,
config.Name,
)
<button
type="button"
onclick={ showConfigDialog(fmt.Sprintf("delete-config-dialog-%d", config.ID)) }
class="btn-danger btn-sm">
<i class="fas fa-trash-alt mr-1"></i>
Delete
</button>
</div>
</div>
<div class="mt-2 sm:flex sm:justify-between">
<div class="sm:flex">
<p class="flex items-center text-sm text-secondary-500 dark:text-secondary-400">
<i class="fas fa-upload flex-shrink-0 mr-1.5 h-5 w-5 text-secondary-400 dark:text-secondary-500"></i>
Source: { config.SourceType }: { config.SourcePath }
</p>
<p class="mt-2 flex items-center text-sm text-secondary-500 dark:text-secondary-400 sm:mt-0 sm:ml-6">
<i class="fas fa-download flex-shrink-0 mr-1.5 h-5 w-5 text-secondary-400 dark:text-secondary-500"></i>
Destination: { config.DestinationType }: { config.DestinationPath }
</p>
</div>
<div class="mt-2 flex items-center text-sm text-secondary-500 dark:text-secondary-400 sm:mt-0">
<i class="fas fa-calendar-alt flex-shrink-0 mr-1.5 h-5 w-5 text-secondary-400 dark:text-secondary-500"></i>
<p>
Updated: { config.UpdatedAt.Format("2006-01-02 15:04:05") }
</p>
</div>
</div>
</div>
</div>
</li>
}
</ul>
</div>
}
</div>
<!-- Help Notice -->
<div class="mt-8 text-center">
<p class="text-sm text-secondary-500 dark:text-secondary-400">
<i class="fas fa-info-circle mr-1 text-primary-500"></i>
Configurations define how files are transferred between systems
</p>
</div>
<!-- Google Drive Auth Help -->
<div class="mt-4 text-center">
<p class="text-sm text-secondary-500 dark:text-secondary-400">
<i class="fab fa-google-drive mr-1 text-blue-500 inline-flex items-center"></i>
Google Drive configurations require authentication. Click the "Authenticate" button to complete setup.
</p>
</div>
</div>
</div>
}
}