Files
perfil/nuxt4/app/components/auth/ApplicationsList.vue
josedario87 9a3dc1f0e6
All checks were successful
build-and-deploy / build-and-deploy (push) Successful in 55s
Feature: Mejorar UX del tema y persistencia
- Mover botón editar al lado del nombre (siempre visible, sutil)
- Quitar efecto hover del header
- Detectar tema del sistema operativo automáticamente
- Actualizar theme-color dinámicamente (azul cielo día / oscuro noche)
- Usar cookies para persistir tema y filtros (1 año)
- Sincronizar filtros de apps con cookies
2025-10-16 23:03:43 -06:00

480 lines
12 KiB
Vue

<template>
<div class="applications-container">
<div class="applications-header">
<div class="flex items-center justify-between mb-4">
<h2 class="applications-title">
<UIcon name="i-heroicons-squares-2x2" class="w-6 h-6" />
Mis Aplicaciones
</h2>
<UBadge v-if="filteredApplications.length > 0" color="primary" variant="soft" size="lg">
{{ filteredApplications.length }}
</UBadge>
</div>
<!-- Filtros por grupos -->
<div v-if="availableGroups.length > 0" class="filter-section">
<UButton
v-for="group in availableGroups"
:key="group"
size="sm"
:color="selectedGroups.includes(group) ? 'primary' : 'neutral'"
:variant="selectedGroups.includes(group) ? 'soft' : 'ghost'"
@click="toggleGroup(group)"
>
{{ group }}
</UButton>
</div>
</div>
<div v-if="pending" class="empty-state">
<UIcon name="i-heroicons-arrow-path" class="w-12 h-12 animate-spin text-primary" />
<p class="mt-4 text-gray-600 dark:text-gray-400">Cargando aplicaciones...</p>
</div>
<div v-else-if="error" class="empty-state">
<UIcon name="i-heroicons-exclamation-triangle" class="w-16 h-16 text-error" />
<p class="mt-4 text-error font-semibold text-lg">Error al cargar aplicaciones</p>
<p class="text-sm text-gray-500 mt-2">{{ error.message }}</p>
</div>
<div v-else-if="applications.length === 0" class="empty-state">
<UIcon name="i-heroicons-inbox" class="w-16 h-16 text-gray-400" />
<p class="mt-4 text-gray-600 dark:text-gray-400 text-lg">No tienes aplicaciones disponibles</p>
</div>
<div v-else-if="filteredApplications.length === 0" class="empty-state">
<UIcon name="i-heroicons-funnel" class="w-16 h-16 text-gray-400" />
<p class="mt-4 text-gray-600 dark:text-gray-400 text-lg">No hay aplicaciones en los grupos seleccionados</p>
</div>
<div v-else class="applications-grid">
<a
v-for="app in filteredApplications"
:key="app.pk"
:href="app.launchUrl"
:target="app.openInNewTab ? '_blank' : '_self'"
class="app-card"
>
<div class="app-card-content">
<div class="app-icon">
<img
v-if="getAppIconUrl(app)"
:src="getAppIconUrl(app)"
:alt="app.name"
class="w-7 h-7 object-contain"
@error="handleIconError($event, app)"
/>
<UIcon
v-else
name="i-heroicons-cube"
class="w-7 h-7"
/>
</div>
<div class="app-info">
<div class="app-header">
<h4 class="app-name">
{{ app.name }}
</h4>
<UIcon
v-if="app.openInNewTab"
name="i-heroicons-arrow-top-right-on-square"
class="external-icon"
/>
</div>
<div v-if="app.group" class="app-groups">
<UBadge
v-for="group in app.group.split(',')"
:key="group"
size="xs"
color="neutral"
variant="soft"
>
{{ group.trim() }}
</UBadge>
</div>
<p v-if="app.description" class="app-description">
{{ app.description }}
</p>
</div>
</div>
</a>
</div>
</div>
</template>
<script setup lang="ts">
interface Application {
pk: string
name: string
slug: string
launchUrl: string
description?: string
icon?: string
publisher?: string
group?: string
openInNewTab: boolean
}
const { data: applications, pending, error, refresh } = await useFetch<Application[]>('/api/authentik/applications', {
default: () => []
})
// Cookie para persistir filtros seleccionados
const filtersCookie = useCookie<string[]>('app-filters', {
maxAge: 60 * 60 * 24 * 365, // 1 año
sameSite: 'lax',
default: () => []
})
// Estado de filtros (inicializado desde cookie)
const selectedGroups = ref<string[]>(filtersCookie.value || [])
// Estado para iconos fallidos (para no intentar cargarlos de nuevo)
const failedIcons = ref<Set<string>>(new Set())
// Función para obtener la URL del icono de una app
const getAppIconUrl = (app: Application): string | null => {
// Si el icono ya falló, no intentar de nuevo
if (failedIcons.value.has(app.pk)) {
return null
}
// Si tiene meta_icon de Authentik, usarlo
if (app.icon) {
// Si es una URL completa
if (app.icon.startsWith('http://') || app.icon.startsWith('https://')) {
return app.icon
}
// Si es una ruta relativa, usar el dominio de la app
try {
const url = new URL(app.launchUrl)
return `${url.origin}${app.icon.startsWith('/') ? '' : '/'}${app.icon}`
} catch {
return null
}
}
// Intentar obtener el favicon de la URL
try {
const url = new URL(app.launchUrl)
// Intentar primero apple-touch-icon (suele ser de mejor calidad)
return `${url.origin}/apple-touch-icon.png`
} catch {
return null
}
}
// Manejar error al cargar icono
const handleIconError = (event: Event, app: Application) => {
const imgElement = event.target as HTMLImageElement
const currentSrc = imgElement.src
// Marcar como fallido
failedIcons.value.add(app.pk)
// Si estábamos intentando apple-touch-icon, intentar favicon.ico
if (currentSrc.includes('apple-touch-icon')) {
try {
const url = new URL(app.launchUrl)
imgElement.src = `${url.origin}/favicon.ico`
failedIcons.value.delete(app.pk) // Dar otra oportunidad
return
} catch {
// Si falla, el v-else mostrará el icono por defecto
}
}
// Ocultar la imagen para que se muestre el icono por defecto
imgElement.style.display = 'none'
}
// Extraer todos los grupos únicos de las aplicaciones
const availableGroups = computed(() => {
const groups = new Set<string>()
applications.value.forEach(app => {
if (app.group) {
app.group.split(',').forEach(group => {
groups.add(group.trim())
})
}
})
return Array.from(groups).sort()
})
// Filtrar aplicaciones según grupos seleccionados
const filteredApplications = computed(() => {
// Si no hay grupos seleccionados, mostrar todas
if (selectedGroups.value.length === 0) {
return applications.value
}
// Filtrar apps que contengan al menos uno de los grupos seleccionados
return applications.value.filter(app => {
if (!app.group) return false
const appGroups = app.group.split(',').map(g => g.trim())
return selectedGroups.value.some(selectedGroup =>
appGroups.includes(selectedGroup)
)
})
})
// Toggle de selección de grupos
const toggleGroup = (group: string) => {
const index = selectedGroups.value.indexOf(group)
if (index === -1) {
selectedGroups.value.push(group)
} else {
selectedGroups.value.splice(index, 1)
}
// Guardar en cookie
filtersCookie.value = selectedGroups.value
}
// Sincronizar con cookie cuando cambian los filtros
watch(selectedGroups, (newFilters) => {
filtersCookie.value = newFilters
})
// Refrescar automáticamente cada 5 minutos
const refreshInterval = setInterval(() => {
refresh()
}, 5 * 60 * 1000)
// Limpiar interval cuando el componente se desmonte
onUnmounted(() => {
clearInterval(refreshInterval)
})
</script>
<style scoped>
/* Estilos de modo oscuro deben estar fuera de scoped para .dark */
.applications-container {
background: rgba(255, 255, 255, 0.35);
backdrop-filter: blur(20px) saturate(180%);
border-radius: 1.5rem;
padding: 2rem;
box-shadow:
0 8px 32px 0 rgba(31, 38, 135, 0.15),
inset 0 1px 1px 0 rgba(255, 255, 255, 0.3);
border: 1px solid rgba(255, 255, 255, 0.18);
transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
.applications-header {
margin-bottom: 2rem;
}
.applications-title {
display: flex;
align-items: center;
gap: 0.75rem;
font-size: 1.75rem;
font-weight: 700;
color: var(--color-gray-900);
margin: 0;
}
.filter-section {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 4rem 2rem;
text-align: center;
}
.applications-grid {
display: grid;
gap: 1rem;
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
}
@media (min-width: 640px) {
.applications-grid {
grid-template-columns: repeat(2, 1fr);
}
}
@media (min-width: 1024px) {
.applications-grid {
grid-template-columns: repeat(3, 1fr);
}
}
.app-card {
display: block;
padding: 1.25rem;
border-radius: 1.25rem;
background: rgba(255, 255, 255, 0.3);
backdrop-filter: blur(15px) saturate(180%);
border: 1px solid rgba(255, 255, 255, 0.18);
box-shadow:
0 4px 16px 0 rgba(31, 38, 135, 0.1),
inset 0 1px 1px 0 rgba(255, 255, 255, 0.4);
transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
cursor: pointer;
text-decoration: none;
}
.app-card:hover {
transform: translateY(-6px) scale(1.02);
box-shadow:
0 12px 32px 0 rgba(var(--color-primary-500), 0.25),
0 0 0 1px rgba(var(--color-primary-500), 0.5),
inset 0 1px 1px 0 rgba(255, 255, 255, 0.5);
border-color: rgba(var(--color-primary-500), 0.6);
}
.app-card-content {
display: flex;
gap: 1rem;
align-items: start;
}
.app-icon {
flex-shrink: 0;
width: 3rem;
height: 3rem;
border-radius: 0.75rem;
background: rgba(var(--color-primary-500), 0.1);
display: flex;
align-items: center;
justify-content: center;
color: rgb(var(--color-primary-500));
transition: all 0.3s ease;
}
.app-card:hover .app-icon {
background: rgba(var(--color-primary-500), 0.2);
transform: scale(1.1);
}
.app-info {
flex: 1;
min-width: 0;
}
.app-header {
display: flex;
align-items: start;
justify-content: space-between;
gap: 0.5rem;
margin-bottom: 0.5rem;
}
.app-name {
font-size: 1rem;
font-weight: 600;
color: var(--color-gray-900);
margin: 0;
transition: color 0.3s ease;
line-height: 1.3;
}
.app-card:hover .app-name {
color: rgb(var(--color-primary-500));
}
.external-icon {
width: 1rem;
height: 1rem;
color: var(--color-gray-400);
flex-shrink: 0;
margin-top: 0.125rem;
}
.app-groups {
display: flex;
flex-wrap: wrap;
gap: 0.375rem;
margin-bottom: 0.5rem;
}
.app-description {
font-size: 0.8125rem;
color: var(--color-gray-600);
line-height: 1.4;
margin: 0;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
}
/* Responsive */
@media (max-width: 640px) {
.applications-container {
padding: 1.5rem;
}
.applications-title {
font-size: 1.5rem;
}
.applications-grid {
gap: 0.75rem;
grid-template-columns: 1fr;
}
.app-card {
padding: 1rem;
}
.app-card-content {
gap: 0.75rem;
}
.app-icon {
width: 2.5rem;
height: 2.5rem;
}
}
</style>
<style>
/* Estilos de modo oscuro (sin scoped para que .dark funcione correctamente) */
.dark .applications-container {
background: rgba(0, 0, 0, 0.15) !important;
box-shadow:
0 8px 32px 0 rgba(0, 0, 0, 0.5),
inset 0 1px 1px 0 rgba(255, 255, 255, 0.05) !important;
border: 1px solid rgba(255, 255, 255, 0.08) !important;
}
.dark .applications-title {
color: var(--color-gray-100) !important;
}
.dark .app-card {
background: rgba(0, 0, 0, 0.65) !important;
border: 1px solid rgba(255, 255, 255, 0.1) !important;
box-shadow:
0 4px 16px 0 rgba(0, 0, 0, 0.4),
inset 0 1px 1px 0 rgba(255, 255, 255, 0.05) !important;
}
.dark .app-card:hover {
box-shadow:
0 12px 32px 0 rgba(var(--color-primary-500), 0.4),
0 0 0 1px rgba(var(--color-primary-500), 0.7),
inset 0 1px 1px 0 rgba(255, 255, 255, 0.1) !important;
}
.dark .app-name {
color: var(--color-gray-100) !important;
}
.dark .app-description {
color: var(--color-gray-400) !important;
}
</style>