import { useState } from 'react' import { Bookmark, Trash2 } from 'lucide-react' import { PageMeta } from '@/components/common/PageMeta' import { Button } from '@/components/ui/Button' import { useAuthStore } from '@/store/authStore' import { usePermissions } from '@/hooks/usePermissions' import { stepsApi } from '@/api/steps' import { StepLibraryBrowser } from '@/components/step-library/StepLibraryBrowser' import { StepFormModal } from '@/components/step-library/StepFormModal' import { toast } from '@/lib/toast' import type { Step, StepListItem } from '@/types/step' export default function StepLibraryPage() { const user = useAuthStore((s) => s.user) const { canCreateSteps } = usePermissions() // Create/edit modal state const [createOpen, setCreateOpen] = useState(false) const [editingStep, setEditingStep] = useState(null) // Delete confirmation state const [deletingStep, setDeletingStep] = useState(null) const [isDeleting, setIsDeleting] = useState(false) const [deleteError, setDeleteError] = useState(null) // Increment to trigger StepLibraryBrowser reload const [refreshKey, setRefreshKey] = useState(0) const refresh = () => setRefreshKey(k => k + 1) // Fetch full step before opening edit modal (StepListItem lacks content) const handleEdit = async (step: StepListItem) => { try { const full = await stepsApi.get(step.id) setEditingStep(full) } catch { toast.error('Failed to load step for editing') } } const handleDeleteRequest = (step: StepListItem) => { setDeletingStep(step) } const handleDeleteConfirm = async () => { if (!deletingStep) return setIsDeleting(true) setDeleteError(null) try { await stepsApi.delete(deletingStep.id) setDeletingStep(null) refresh() } catch (err) { console.error('Failed to delete step:', err) setDeleteError('Failed to delete step. Please try again.') } finally { setIsDeleting(false) } } const handleSave = async (step: StepListItem) => { try { const full = await stepsApi.get(step.id) await stepsApi.create({ title: full.title, step_type: full.step_type, content: full.content, visibility: 'private', category_id: full.category_id, tags: full.tags, }) toast.success(`"${full.title}" saved to My Steps`) refresh() } catch { toast.error('Failed to save step to your library') } } // eslint-disable-next-line @typescript-eslint/no-unused-vars const handleFormSuccess = (_step: Step) => { setCreateOpen(false) setEditingStep(null) refresh() } const handleCloseModal = () => { setCreateOpen(false) setEditingStep(null) } return ( <>
{/* Page Header */}

Solutions Library

Reusable solutions from your team's troubleshooting sessions

{canCreateSteps && ( )}
{/* Browser fills remaining height */}
{ handleEdit(step) }} onDelete={handleDeleteRequest} onSave={handleSave} currentUserId={user?.id} refreshKey={refreshKey} showCreateButton={false} />
{/* Create / Edit Modal */} {/* Delete Confirmation Dialog */} {deletingStep && (

Delete Step

Are you sure you want to delete{' '} "{deletingStep.title}"?

This cannot be undone.

{deleteError && (

{deleteError}

)}
)}
) }