Spaces:
Sleeping
Sleeping
File size: 9,805 Bytes
fd52f31 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { FaPlus, FaCalendarAlt, FaEdit, FaTrash, FaPlayCircle, FaRegClock, FaFlask, FaMicrophone, FaHeadphones, FaPodcast, FaCheck } from 'react-icons/fa';
import { MdOutlineWorkspaces } from 'react-icons/md';
import { TiFlowMerge } from 'react-icons/ti';
import './Workflows.css';
const Workflows = () => {
const navigate = useNavigate();
const [workflows, setWorkflows] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [deleteConfirmId, setDeleteConfirmId] = useState(null);
useEffect(() => {
fetchWorkflows();
}, []);
const fetchWorkflows = async () => {
try {
const token = localStorage.getItem('token');
const response = await fetch('http://localhost:8000/api/workflows', {
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error('Failed to fetch workflows');
}
const data = await response.json();
setWorkflows(data);
setLoading(false);
} catch (err) {
console.error('Error fetching workflows:', err);
setError(err.message);
setLoading(false);
}
};
const handleWorkflowClick = (workflowId) => {
navigate(`/workflows/workflow/${workflowId}`);
};
const handleCreateWorkflow = () => {
// Use -1 to indicate a new workflow
navigate(`/workflows/workflow/-1`);
};
const handleDeleteClick = (e, workflowId) => {
e.stopPropagation(); // Prevent workflow card click
setDeleteConfirmId(workflowId);
};
const handleDeleteConfirm = async (e, workflowId) => {
e.stopPropagation(); // Prevent workflow card click
try {
const token = localStorage.getItem('token');
const response = await fetch(`http://localhost:8000/api/workflows/${workflowId}`, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (!response.ok) {
throw new Error('Failed to delete workflow');
}
// Remove from state
setWorkflows(workflows.filter(w => w.id !== workflowId));
setDeleteConfirmId(null);
} catch (err) {
console.error('Error deleting workflow:', err);
setError(err.message);
}
};
const handleDeleteCancel = (e) => {
e.stopPropagation(); // Prevent workflow card click
setDeleteConfirmId(null);
};
const getRandomGradient = () => {
const gradients = [
'linear-gradient(135deg, #6366F1, #8B5CF6)',
'linear-gradient(135deg, #3B82F6, #6366F1)',
'linear-gradient(135deg, #10B981, #3B82F6)',
'linear-gradient(135deg, #F59E0B, #10B981)',
'linear-gradient(135deg, #EF4444, #F59E0B)',
'linear-gradient(135deg, #EC4899, #8B5CF6)',
'linear-gradient(135deg, #8B5CF6, #EC4899)',
'linear-gradient(135deg, #6366F1, #EC4899)'
];
return gradients[Math.floor(Math.random() * gradients.length)];
};
const formatDate = (dateString) => {
const date = new Date(dateString);
return new Intl.DateTimeFormat('en-US', {
month: 'short',
day: 'numeric',
year: 'numeric'
}).format(date);
};
if (loading) {
return (
<div className="workflows-container">
<div className="loading-indicator">
<div className="loader"></div>
<span>Loading your workflows...</span>
</div>
</div>
);
}
if (error) {
return (
<div className="workflows-container">
<div className="error-message">
<span>⚠️ Error: {error}</span>
</div>
</div>
);
}
return (
<div className="workflows-container">
<div className="workflows-header">
<div className="header-content">
<TiFlowMerge className="title-icon" />
<h1>Your Podcast Workflows</h1>
</div>
<button className="create-workflow-btn" onClick={handleCreateWorkflow}>
<FaPlus /> New Workflow
</button>
</div>
<div className="workflows-subheader">
<p>Manage and edit your podcast workflow templates</p>
<div className="workflows-stats">
<div className="stat-item">
<MdOutlineWorkspaces />
<span>{workflows.length} Workflows</span>
</div>
</div>
</div>
<div className="workflows-grid">
{workflows.length === 0 ? (
<div className="no-workflows">
<FaPodcast className="empty-icon" />
<h3>No Workflows Yet</h3>
<p>You haven't created any workflows yet. Click the "New Workflow" button to get started!</p>
<button className="create-workflow-empty-btn" onClick={handleCreateWorkflow}>
<FaPlus /> Create Your First Workflow
</button>
</div>
) : (
workflows.map((workflow) => (
<div
key={workflow.id}
className="workflow-card"
onClick={() => handleWorkflowClick(workflow.id)}
style={{ '--card-gradient': getRandomGradient() }}
>
<div className="workflow-card-header">
<div className="workflow-icon">
<TiFlowMerge />
</div>
<div className="workflow-actions">
{deleteConfirmId === workflow.id ? (
<div className="delete-confirm">
<button
className="delete-yes"
onClick={(e) => handleDeleteConfirm(e, workflow.id)}
title="Confirm delete"
>
<FaCheck />
</button>
<button
className="delete-no"
onClick={handleDeleteCancel}
title="Cancel"
>
<FaEdit />
</button>
</div>
) : (
<button
className="delete-btn"
onClick={(e) => handleDeleteClick(e, workflow.id)}
title="Delete workflow"
>
<FaTrash />
</button>
)}
</div>
</div>
<h3>{workflow.name}</h3>
<p className="workflow-description">{workflow.description || 'No description available'}</p>
<div className="workflow-meta">
<div className="meta-item">
<FaCalendarAlt />
<span>{formatDate(workflow.created_at)}</span>
</div>
<div className="meta-item">
<FaRegClock />
<span>Last edited: {formatDate(workflow.updated_at || workflow.created_at)}</span>
</div>
</div>
<div className="workflow-card-footer">
<div className="workflow-status">
{workflow.insights ? (
<span className="status ready">
<FaHeadphones /> Ready
</span>
) : (
<span className="status draft">
<FaFlask /> Draft
</span>
)}
</div>
<button className="open-btn">
<FaPlayCircle /> Open
</button>
</div>
</div>
))
)}
</div>
</div>
);
};
export default Workflows; |