mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
feat(sdk): support for plugin icons
This commit is contained in:
@@ -4,9 +4,10 @@ import {
|
||||
Blocks, AlertTriangle, PackageOpen, RefreshCw, Trash2, Download, Bug, X, ShieldCheck, UploadCloud,
|
||||
ArrowUpCircle, Github, ExternalLink, ChevronDown, Check, Lock, Search, Link2, KeyRound, ShieldAlert,
|
||||
SlidersHorizontal, ArrowUpDown, CircleDot, MoreHorizontal, RotateCw, ArrowRight, Database, Users, LayoutDashboard,
|
||||
Radio, Luggage, Plane, Globe, Image, CalendarDays, Map, Bell, Cloud, Camera, Compass,
|
||||
BookOpen, Wallet, Puzzle, MapPin, ListChecks, Pencil, Tag, FileText,
|
||||
Radio, Luggage, Globe, Image, CalendarDays, Bell,
|
||||
Wallet, Puzzle, MapPin, ListChecks, Pencil, Tag, FileText,
|
||||
} from 'lucide-react'
|
||||
import PluginIcon from '../shared/PluginIcon'
|
||||
import { adminApi } from '../../api/client'
|
||||
import { usePluginStore } from '../../store/pluginStore'
|
||||
import { useTranslation } from '../../i18n'
|
||||
@@ -70,6 +71,8 @@ interface RegistryItem {
|
||||
repo: string
|
||||
homepage?: string | null
|
||||
type: string
|
||||
/** Lucide icon name from the registry entry; absent → Blocks. */
|
||||
icon?: string | null
|
||||
latest: string | null
|
||||
minTrekVersion: string | null
|
||||
/** The latest version's declared TREK range; null on a legacy registry entry. */
|
||||
@@ -171,11 +174,6 @@ const HEALTH: Record<string, string> = {
|
||||
incompatible: 'bg-warning',
|
||||
}
|
||||
|
||||
// Manifest `icon` is a lucide name; map the common ones, fall back to Blocks.
|
||||
const ICON_MAP: Record<string, React.ComponentType<{ size?: number; className?: string }>> = {
|
||||
Luggage, Plane, Globe, Image, CalendarDays, Map, Bell, Cloud, Camera, Compass, BookOpen, Wallet, Puzzle, Blocks,
|
||||
}
|
||||
|
||||
// Known permissions → human-readable i18n key; unknown ones render as raw code.
|
||||
const PERM_KEYS = [
|
||||
'db:own', 'db:read:trips', 'db:read:users', 'db:read:costs', 'db:read:packing', 'db:read:files', 'db:read:files:content',
|
||||
@@ -297,11 +295,6 @@ function installOffer(item: RegistryItem, t: T): { blocked: boolean; version?: s
|
||||
return { blocked: true, label: t('admin.plugins.incompatible'), title }
|
||||
}
|
||||
|
||||
function PluginIcon({ name, size = 20, className }: { name: string | null; size?: number; className?: string }) {
|
||||
const Icon = (name && Object.prototype.hasOwnProperty.call(ICON_MAP, name) && ICON_MAP[name]) || Blocks
|
||||
return <Icon size={size} className={className} />
|
||||
}
|
||||
|
||||
function ReviewedBadge({ t, compact }: { t: T; compact?: boolean }) {
|
||||
if (compact) return <ShieldCheck size={13} className="text-success shrink-0" aria-label={t('admin.plugins.reviewed')} />
|
||||
return (
|
||||
@@ -1340,7 +1333,7 @@ function RegistryGrid({ items, onInstall, onOpenDetail, busy, t, installedIds, f
|
||||
</span>
|
||||
)}
|
||||
<div className="absolute left-3 -bottom-4 w-11 h-11 rounded-xl bg-surface-card border border-edge grid place-items-center shadow-card z-[1]">
|
||||
<PluginIcon name={item.type === 'widget' ? 'Blocks' : null} size={22} className="text-content-secondary" />
|
||||
<PluginIcon name={item.icon} size={22} className="text-content-secondary" />
|
||||
</div>
|
||||
</div>
|
||||
<div className="pt-6 px-3.5 pb-3.5 flex flex-col flex-1">
|
||||
|
||||
@@ -21,6 +21,7 @@ import userEvent from '@testing-library/user-event';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import { useAddonStore } from '../../store/addonStore';
|
||||
import { usePluginStore } from '../../store/pluginStore';
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { buildUser, buildSettings } from '../../../tests/helpers/factories';
|
||||
import BottomNav from './BottomNav';
|
||||
@@ -113,4 +114,21 @@ describe('BottomNav', () => {
|
||||
await user.click(screen.getByRole('button', { name: 'Add expense' }));
|
||||
expect(mockNavigate).toHaveBeenCalledWith('/trips/42?create=expense');
|
||||
});
|
||||
|
||||
it('FE-COMP-BOTTOMNAV-011: page plugin renders the icon its manifest declares', () => {
|
||||
seedStore(usePluginStore, {
|
||||
plugins: [{ id: 'trip-doctor', name: 'Trip Doctor', type: 'page', icon: 'Stethoscope' }],
|
||||
});
|
||||
const { container } = render(<BottomNav />);
|
||||
expect(screen.getByText('Trip Doctor')).toBeInTheDocument();
|
||||
expect(container.querySelector('.lucide-stethoscope')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('FE-COMP-BOTTOMNAV-012: page plugin with an unknown icon falls back to Blocks', () => {
|
||||
seedStore(usePluginStore, {
|
||||
plugins: [{ id: 'bogus', name: 'Bogus', type: 'page', icon: 'NotAnIcon' }],
|
||||
});
|
||||
const { container } = render(<BottomNav />);
|
||||
expect(container.querySelector('.lucide-blocks')).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,8 +3,9 @@ import { useAddonStore } from '../../store/addonStore'
|
||||
import { usePluginStore } from '../../store/pluginStore'
|
||||
import { useSettingsStore } from '../../store/settingsStore'
|
||||
import { useTranslation } from '../../i18n'
|
||||
import { LayoutGrid, CalendarDays, Globe, Compass, Bookmark, Plus, Blocks } from 'lucide-react'
|
||||
import { LayoutGrid, CalendarDays, Globe, Compass, Bookmark, Plus } from 'lucide-react'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import { resolvePluginIcon } from '../shared/pluginIcon'
|
||||
|
||||
const ADDON_NAV: Record<string, { icon: LucideIcon; labelKey: string }> = {
|
||||
vacay: { icon: CalendarDays, labelKey: 'admin.addons.catalog.vacay.name' },
|
||||
@@ -65,7 +66,7 @@ export default function BottomNav() {
|
||||
const nav = ADDON_NAV[addon.id]
|
||||
return nav ? [{ to: `/${addon.id}`, label: t(nav.labelKey), icon: nav.icon }] : []
|
||||
}),
|
||||
...pagePlugins.map(p => ({ to: `/plugins/${p.id}`, label: p.name, icon: Blocks })),
|
||||
...pagePlugins.map(p => ({ to: `/plugins/${p.id}`, label: p.name, icon: resolvePluginIcon(p.icon) })),
|
||||
]
|
||||
// Split the items so the raised "+" sits dead centre.
|
||||
const splitAt = Math.ceil(items.length / 2)
|
||||
|
||||
@@ -6,6 +6,7 @@ import { server } from '../../../tests/helpers/msw/server';
|
||||
import { useAuthStore } from '../../store/authStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import { useAddonStore } from '../../store/addonStore';
|
||||
import { usePluginStore } from '../../store/pluginStore';
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { buildUser, buildSettings } from '../../../tests/helpers/factories';
|
||||
import Navbar from './Navbar';
|
||||
@@ -304,4 +305,21 @@ describe('Navbar', () => {
|
||||
await user.click(screen.getByText('adminuser'));
|
||||
expect(screen.getByText('Administrator')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-034: page plugin renders the icon its manifest declares', () => {
|
||||
seedStore(usePluginStore, {
|
||||
plugins: [{ id: 'trip-doctor', name: 'Trip Doctor', type: 'page', icon: 'Stethoscope' }],
|
||||
});
|
||||
const { container } = render(<Navbar />);
|
||||
expect(screen.getByRole('link', { name: /trip doctor/i })).toBeInTheDocument();
|
||||
expect(container.querySelector('.lucide-stethoscope')).not.toBeNull();
|
||||
});
|
||||
|
||||
it('FE-COMP-NAVBAR-035: page plugin with an unknown icon falls back to Blocks', () => {
|
||||
seedStore(usePluginStore, {
|
||||
plugins: [{ id: 'bogus', name: 'Bogus', type: 'page', icon: 'NotAnIcon' }],
|
||||
});
|
||||
const { container } = render(<Navbar />);
|
||||
expect(container.querySelector('.lucide-blocks')).not.toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -6,9 +6,10 @@ import { useSettingsStore } from '../../store/settingsStore'
|
||||
import { useAddonStore } from '../../store/addonStore'
|
||||
import { usePluginStore } from '../../store/pluginStore'
|
||||
import { useTranslation } from '../../i18n'
|
||||
import { Plane, LogOut, Settings, ChevronDown, Shield, ArrowLeft, Users, Moon, Sun, Monitor, CalendarDays, Briefcase, Globe, Compass, BookOpen, Bookmark, Blocks } from 'lucide-react'
|
||||
import { Plane, LogOut, Settings, ChevronDown, Shield, ArrowLeft, Users, Moon, Sun, Monitor, CalendarDays, Briefcase, Globe, Compass, BookOpen, Bookmark } from 'lucide-react'
|
||||
import type { LucideIcon } from 'lucide-react'
|
||||
import InAppNotificationBell from './InAppNotificationBell.tsx'
|
||||
import { resolvePluginIcon } from '../shared/pluginIcon'
|
||||
|
||||
const ADDON_ICONS: Record<string, LucideIcon> = { CalendarDays, Briefcase, Globe, Compass, Bookmark }
|
||||
|
||||
@@ -150,7 +151,7 @@ export default function Navbar({ tripTitle, tripId, onBack, showBack, onShare }:
|
||||
>
|
||||
{[{ id: '__trips', path: '/dashboard', label: t('nav.myTrips'), Icon: Briefcase },
|
||||
...globalAddons.map(a => ({ id: a.id, path: `/${a.id}`, label: getAddonName(a), Icon: ADDON_ICONS[a.icon] || CalendarDays })),
|
||||
...pagePlugins.map(p => ({ id: `plugin:${p.id}`, path: `/plugins/${p.id}`, label: p.name, Icon: Blocks }))
|
||||
...pagePlugins.map(p => ({ id: `plugin:${p.id}`, path: `/plugins/${p.id}`, label: p.name, Icon: resolvePluginIcon(p.icon) }))
|
||||
].map(tab => {
|
||||
const isActive = location.pathname === tab.path
|
||||
return (
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Blocks } from 'lucide-react'
|
||||
import PluginIcon from '../shared/PluginIcon'
|
||||
import PluginFrame from './PluginFrame'
|
||||
import type { ActivePlugin } from '../../store/pluginStore'
|
||||
|
||||
@@ -35,7 +35,7 @@ export default function PluginWidgets({ plugins, tripId = null }: { plugins: Act
|
||||
color: 'var(--ink-3)',
|
||||
}}
|
||||
>
|
||||
<Blocks size={14} style={{ flexShrink: 0 }} />
|
||||
<PluginIcon name={p.icon} size={14} style={{ flexShrink: 0 }} />
|
||||
<span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{p.name}</span>
|
||||
</div>
|
||||
{/* min-height is just a pre-resize floor; trek:resize drives the real height. */}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Blocks, Save, Loader2, Link2, Unlink, CheckCircle } from 'lucide-react'
|
||||
import { Save, Loader2, Link2, Unlink, CheckCircle } from 'lucide-react'
|
||||
import PluginIcon from '../shared/PluginIcon'
|
||||
import { pluginsApi, type PluginUserSettingField, type PluginAction } from '../../api/client'
|
||||
import { usePluginStore } from '../../store/pluginStore'
|
||||
import { useToast } from '../shared/Toast'
|
||||
@@ -57,7 +58,7 @@ const SECRET_MASK = '••••••••'
|
||||
* here; the field list is trusted, validated manifest data. Secrets stay write-only
|
||||
* (masked, never echoed back). One form per active plugin that declares user fields.
|
||||
*/
|
||||
function PluginSettingsForm({ id, name }: { id: string; name: string }) {
|
||||
function PluginSettingsForm({ id, name, icon }: { id: string; name: string; icon: string | null }) {
|
||||
const { t } = useTranslation()
|
||||
const toast = useToast()
|
||||
const [fields, setFields] = useState<PluginUserSettingField[] | null>(null)
|
||||
@@ -134,7 +135,7 @@ function PluginSettingsForm({ id, name }: { id: string; name: string }) {
|
||||
return (
|
||||
<div className="rounded-xl border border-border bg-surface p-5">
|
||||
<div className="flex items-center gap-2 mb-4">
|
||||
<Blocks className="w-4 h-4 text-content-secondary" />
|
||||
<PluginIcon name={icon} className="w-4 h-4 text-content-secondary" />
|
||||
<h3 className="text-sm font-semibold text-content">{name}</h3>
|
||||
</div>
|
||||
<div className="space-y-4">
|
||||
@@ -232,7 +233,7 @@ export default function PluginSettingsTab() {
|
||||
</div>
|
||||
{plugins.length === 0
|
||||
? <p className="text-sm text-content-muted">{t('settings.plugins.empty')}</p>
|
||||
: plugins.map(p => <PluginSettingsForm key={p.id} id={p.id} name={p.name} />)}
|
||||
: plugins.map(p => <PluginSettingsForm key={p.id} id={p.id} name={p.name} icon={p.icon} />)}
|
||||
<PluginActivityPanel />
|
||||
</div>
|
||||
)
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { render } from '@testing-library/react'
|
||||
import { Blocks, Stethoscope } from 'lucide-react'
|
||||
import { resolvePluginIcon } from './pluginIcon'
|
||||
import PluginIcon from './PluginIcon'
|
||||
|
||||
describe('resolvePluginIcon', () => {
|
||||
it('resolves a lucide name outside the old 14-icon allowlist', () => {
|
||||
expect(resolvePluginIcon('Stethoscope')).toBe(Stethoscope)
|
||||
})
|
||||
|
||||
it('falls back to Blocks for a missing or unknown name', () => {
|
||||
expect(resolvePluginIcon(null)).toBe(Blocks)
|
||||
expect(resolvePluginIcon(undefined)).toBe(Blocks)
|
||||
expect(resolvePluginIcon('')).toBe(Blocks)
|
||||
expect(resolvePluginIcon('Stethscope')).toBe(Blocks)
|
||||
})
|
||||
|
||||
// A manifest must not be able to reach lucide's non-icon exports.
|
||||
it.each(['icons', 'createLucideIcon', 'default', 'module.exports'])(
|
||||
'falls back to Blocks for the non-icon export %s',
|
||||
(name) => expect(resolvePluginIcon(name)).toBe(Blocks),
|
||||
)
|
||||
|
||||
it('returns a stable reference so nav arrays do not remount the icon', () => {
|
||||
expect(resolvePluginIcon('Stethoscope')).toBe(resolvePluginIcon('Stethoscope'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('PluginIcon', () => {
|
||||
it('renders the plugin-declared icon', () => {
|
||||
const { container } = render(<PluginIcon name="Stethoscope" />)
|
||||
expect(container.querySelector('.lucide-stethoscope')).not.toBeNull()
|
||||
})
|
||||
|
||||
it('renders Blocks when the plugin declares nothing', () => {
|
||||
const { container } = render(<PluginIcon name={null} />)
|
||||
expect(container.querySelector('.lucide-blocks')).not.toBeNull()
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,13 @@
|
||||
import type { CSSProperties } from 'react'
|
||||
import { resolvePluginIcon } from './pluginIcon'
|
||||
|
||||
/** Renders the lucide icon a plugin declares in its manifest (Blocks if unknown). */
|
||||
export default function PluginIcon({ name, size = 20, className, style }: {
|
||||
name: string | null | undefined
|
||||
size?: number
|
||||
className?: string
|
||||
style?: CSSProperties
|
||||
}) {
|
||||
const Icon = resolvePluginIcon(name)
|
||||
return <Icon size={size} className={className} style={style} />
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import * as LucideIcons from 'lucide-react'
|
||||
import { Blocks, type LucideIcon } from 'lucide-react'
|
||||
|
||||
// Lucide names are PascalCase. Restricting the lookup to that shape keeps a manifest
|
||||
// from reaching lucide's non-icon exports (`createLucideIcon`, `icons`, `default`,
|
||||
// `module.exports`), which are all lowercase-initial.
|
||||
const LUCIDE_ICON_NAME = /^[A-Z][A-Za-z0-9]*$/
|
||||
|
||||
/**
|
||||
* Resolves a plugin manifest's `icon` (a lucide icon name, e.g. "Stethoscope") to the
|
||||
* matching lucide component, falling back to Blocks for a missing or unknown name.
|
||||
*
|
||||
* Note lucide icons are forwardRef objects, not functions — never narrow this with
|
||||
* `typeof x === 'function'`. The lookup returns a module-level reference, so the
|
||||
* component identity is stable across renders and is safe to call inline while
|
||||
* building nav/tab arrays.
|
||||
*/
|
||||
export function resolvePluginIcon(name: string | null | undefined): LucideIcon {
|
||||
if (!name || !LUCIDE_ICON_NAME.test(name)) return Blocks
|
||||
const icon = (LucideIcons as unknown as Record<string, LucideIcon | undefined>)[name]
|
||||
return icon ?? Blocks
|
||||
}
|
||||
Reference in New Issue
Block a user