- Agregar fallback para crypto.randomUUID en useTemplates.ts (compatibilidad con navegadores antiguos) - Envolver sidebar con ClientOnly para evitar hydration mismatch - Agregar botón de configuración de impresoras en sidebar - Agregar drawer lateral para gestión de impresoras en desktop
101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
import type { Operation } from './usePrintQueue'
|
|
|
|
export interface PrintTemplate {
|
|
id: string
|
|
name: string
|
|
description?: string
|
|
operations: Operation[]
|
|
createdAt: number
|
|
updatedAt: number
|
|
}
|
|
|
|
const STORAGE_KEY = 'printercentral-templates'
|
|
|
|
// Función para generar UUID compatible con todos los navegadores
|
|
function generateId(): string {
|
|
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
return crypto.randomUUID()
|
|
}
|
|
// Fallback para navegadores sin crypto.randomUUID
|
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
const r = Math.random() * 16 | 0
|
|
const v = c === 'x' ? r : (r & 0x3 | 0x8)
|
|
return v.toString(16)
|
|
})
|
|
}
|
|
|
|
export function useTemplates() {
|
|
const templates = useState<PrintTemplate[]>('templates', () => [])
|
|
const initialized = useState('templatesInitialized', () => false)
|
|
|
|
// Cargar de localStorage al iniciar (solo cliente)
|
|
if (import.meta.client && !initialized.value) {
|
|
const stored = localStorage.getItem(STORAGE_KEY)
|
|
if (stored) {
|
|
try {
|
|
templates.value = JSON.parse(stored)
|
|
} catch (e) {
|
|
console.error('Error parsing templates:', e)
|
|
}
|
|
}
|
|
initialized.value = true
|
|
}
|
|
|
|
// Guardar en localStorage cuando cambie
|
|
watch(templates, (val) => {
|
|
if (import.meta.client) {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(val))
|
|
}
|
|
}, { deep: true })
|
|
|
|
function saveTemplate(name: string, description: string, operations: Operation[]): PrintTemplate {
|
|
const template: PrintTemplate = {
|
|
id: generateId(),
|
|
name,
|
|
description,
|
|
operations: JSON.parse(JSON.stringify(operations)),
|
|
createdAt: Date.now(),
|
|
updatedAt: Date.now()
|
|
}
|
|
templates.value = [...templates.value, template]
|
|
return template
|
|
}
|
|
|
|
function updateTemplate(id: string, updates: Partial<Omit<PrintTemplate, 'id' | 'createdAt'>>) {
|
|
const idx = templates.value.findIndex(t => t.id === id)
|
|
if (idx !== -1) {
|
|
templates.value = templates.value.map((t, i) =>
|
|
i === idx ? { ...t, ...updates, updatedAt: Date.now() } : t
|
|
)
|
|
}
|
|
}
|
|
|
|
function deleteTemplate(id: string) {
|
|
templates.value = templates.value.filter(t => t.id !== id)
|
|
}
|
|
|
|
function loadTemplate(id: string): Operation[] | null {
|
|
const template = templates.value.find(t => t.id === id)
|
|
return template ? JSON.parse(JSON.stringify(template.operations)) : null
|
|
}
|
|
|
|
function duplicateTemplate(id: string): PrintTemplate | null {
|
|
const template = templates.value.find(t => t.id === id)
|
|
if (!template) return null
|
|
return saveTemplate(
|
|
`${template.name} (copia)`,
|
|
template.description || '',
|
|
template.operations
|
|
)
|
|
}
|
|
|
|
return {
|
|
templates: readonly(templates),
|
|
saveTemplate,
|
|
updateTemplate,
|
|
deleteTemplate,
|
|
loadTemplate,
|
|
duplicateTemplate
|
|
}
|
|
}
|