Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Foyer compiles to a pure static SPA (`index.html`, `200.html`, `_app/`) with zer
- **Network Reachability Badges**: Distinct indicators for `public`, `mesh`, and `lan` endpoints plus one-click filter chips.
- **Live Gatus Health**: Optional client-side polling against a [Gatus](https://gatus.io) `/api/v1/endpoints/statuses` endpoint with visibility-aware refresh and stale-data indication.
- **Host Node Bar**: Per-host service counts, one-click host filtering, and IP clipboard copy.
- **Shared Pinned Links**: Optional `links` in `/config.json` show the same pinned links to every visitor, ahead of their own bookmarks.
- **Local Quick Bookmarks**: Browser-stored personal links (`localStorage`) in a dedicated native `<dialog>` modal, immediately searchable from `⌘K`.
- **Optional Weather & Clock**: Minute-aligned UTC/local header clock and optional [Open-Meteo](https://open-meteo.com) telemetry widget.

Expand Down Expand Up @@ -52,6 +53,13 @@ Serve a `/config.json` file at the web root alongside Foyer's static assets (an
"tags": ["metrics", "observability"]
}
],
"links": [
{
"name": "Docs",
"url": "https://docs.example.com",
"icon": "📚"
}
],
"footerLinks": [
{
"label": "Status",
Expand Down
52 changes: 32 additions & 20 deletions src/lib/components/BookmarksSection.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
<script lang="ts">
import { onMount } from 'svelte';
import { createBookmark, parseStoredBookmarks } from '../portal';
import type { PinnedBookmark } from '../types';
import type { PinnedBookmark, PinnedLink } from '../types';

const STORAGE_KEY = 'foyer-bookmarks';

let {
bookmarks = $bindable<PinnedBookmark[]>([])
bookmarks = $bindable<PinnedBookmark[]>([]),
links = []
}: {
bookmarks?: PinnedBookmark[];
/** Shared links from `/config.json`, listed before the browser-local bookmarks. */
links?: PinnedLink[];
} = $props();

let showAddModal = $state(false);
Expand Down Expand Up @@ -84,6 +87,25 @@
}}
/>

{#snippet linkBody(item: PinnedLink)}
<a
href={item.url}
target="_blank"
rel="noopener noreferrer"
class="flex items-center gap-2 min-w-0 flex-1"
>
<span aria-hidden="true" class="text-lg shrink-0">{item.icon}</span>
<div class="min-w-0 flex-1">
<span class="block text-xs font-medium text-heading truncate group-hover:text-accent transition-colors">
{item.name}
</span>
<span class="block text-[10px] font-mono text-muted truncate">
{item.url.replace(/^https?:\/\//, '')}
</span>
</div>
</a>
{/snippet}

<section class="space-y-3" aria-label="Pinned and Quick Links">
<div class="flex items-center justify-between">
<div class="flex items-center gap-2">
Expand All @@ -92,7 +114,7 @@
<span>Pinned & Quick Links</span>
</h2>
<span class="text-[11px] font-mono px-1.5 py-0.5 rounded-full bg-surface-high text-muted">
{bookmarks.length}
{links.length + bookmarks.length}
</span>
</div>

Expand All @@ -106,30 +128,20 @@
</button>
</div>

{#if bookmarks.length === 0}
{#if links.length + bookmarks.length === 0}
<div class="p-6 rounded-xl border border-dashed border-line text-center text-xs text-muted">
No pinned links yet. Click <span class="font-semibold text-heading">+ Add Link</span> to pin frequent bookmarks to your homelab portal.
</div>
{:else}
<div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-6 gap-2.5">
{#each links as link (link.id)}
<div class="group relative flex items-center justify-between p-2.5 rounded-xl border border-line bg-surface/80 hover:bg-surface-high shadow-2xs hover:shadow-xs transition-colors">
{@render linkBody(link)}
</div>
{/each}
{#each bookmarks as bm (bm.id)}
<div class="group relative flex items-center justify-between p-2.5 rounded-xl border border-line bg-surface/80 hover:bg-surface-high shadow-2xs hover:shadow-xs transition-colors">
<a
href={bm.url}
target="_blank"
rel="noopener noreferrer"
class="flex items-center gap-2 min-w-0 flex-1"
>
<span aria-hidden="true" class="text-lg shrink-0">{bm.icon}</span>
<div class="min-w-0 flex-1">
<span class="block text-xs font-medium text-heading truncate group-hover:text-accent transition-colors">
{bm.name}
</span>
<span class="block text-[10px] font-mono text-muted truncate">
{bm.url.replace(/^https?:\/\//, '')}
</span>
</div>
</a>
{@render linkBody(bm)}

<button
type="button"
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/CommandPalette.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
formatWebSearchUrl,
type PaletteEntry
} from '../portal';
import type { HostInfo, PinnedBookmark, ServiceItem } from '../types';
import type { HostInfo, PinnedLink, ServiceItem } from '../types';

let {
services,
Expand All @@ -18,7 +18,7 @@
open = $bindable(false)
}: {
services: ServiceItem[];
bookmarks?: PinnedBookmark[];
bookmarks?: PinnedLink[];
hosts?: HostInfo[];
searchUrlTemplate?: string;
onSelectHost?: (hostId: string) => void;
Expand Down
27 changes: 23 additions & 4 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FooterLink, HostInfo, NetworkType, PortalConfig, ServiceItem, WeatherConfig } from './types';
import type { FooterLink, HostInfo, NetworkType, PinnedLink, PortalConfig, ServiceItem, WeatherConfig } from './types';

export const DEFAULT_SEARCH_URL_TEMPLATE = 'https://duckduckgo.com/?q=%s';

Expand Down Expand Up @@ -33,10 +33,10 @@ export function isValidHttpUrl(candidate: string): boolean {
}
}

/** Allows http(s) URLs or single-slash same-origin paths, rejecting protocol-relative and script schemes. */
/** Allows http(s) URLs or single-slash same-origin paths, rejecting protocol-relative and script schemes. Browsers read a backslash as `/`. */
export function isSafePortalUrl(candidate: string): boolean {
if (candidate.startsWith('/') && !candidate.startsWith('//')) {
return !/[\s\0]/.test(candidate);
return !/[\s\0\\]/.test(candidate);
}
return isValidHttpUrl(candidate);
}
Expand Down Expand Up @@ -136,6 +136,24 @@ function parseFooterLinks(raw: unknown): FooterLink[] | undefined {
return links;
}

function parseLinks(raw: unknown): PinnedLink[] | undefined {
if (!Array.isArray(raw)) return undefined;
const seen = new Set<string>();
const links: PinnedLink[] = [];
for (const entry of raw) {
if (!entry || typeof entry !== 'object') continue;
const obj = entry as Record<string, unknown>;
const name = asNonEmptyString(obj.name);
const url = asNonEmptyString(obj.url);
if (!name || !url || !isSafePortalUrl(url)) continue;
Comment thread
kleinbem marked this conversation as resolved.
Resolved
const id = `cfg:${asNonEmptyString(obj.id) ?? url}`;
if (seen.has(id)) continue;
seen.add(id);
links.push({ id, name, url, icon: asNonEmptyString(obj.icon) ?? '🔗' });
}
return links.length > 0 ? links : undefined;
}

/**
* Validates an untrusted JSON payload into a safe PortalConfig and deduplicates host/service IDs
* so Svelte keyed each-blocks cannot throw on duplicate keys.
Expand Down Expand Up @@ -176,7 +194,8 @@ export function parsePortalConfig(raw: unknown): PortalConfig {
searchCandidate && isValidHttpUrl(searchCandidate.replace('%s', 'test'))
? searchCandidate
: DEFAULT_SEARCH_URL_TEMPLATE,
footerLinks: parseFooterLinks(obj.footerLinks) ?? DEFAULT_CONFIG.footerLinks
footerLinks: parseFooterLinks(obj.footerLinks) ?? DEFAULT_CONFIG.footerLinks,
links: parseLinks(obj.links)
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/lib/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
isValidHttpUrl
} from './config';
import { resolveHealth } from './data/gatus';
import type { HealthStatus, HostInfo, PinnedBookmark, ServiceItem, WeatherConfig } from './types';
import type { HealthStatus, HostInfo, PinnedBookmark, PinnedLink, ServiceItem, WeatherConfig } from './types';

export type NetworkFilter = 'all' | 'public' | 'mesh' | 'issues';

Expand Down Expand Up @@ -85,7 +85,7 @@ export function createBookmark(

export function buildPaletteEntries(
services: ServiceItem[],
bookmarks: PinnedBookmark[] = [],
bookmarks: PinnedLink[] = [],
hosts: HostInfo[] = []
): PaletteEntry[] {
return [
Expand Down
9 changes: 9 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export interface PinnedBookmark {
createdAt: number;
}

/** A pinned link from `/config.json`: the same for every visitor, not removable in the UI. */
export interface PinnedLink {
id: string;
name: string;
url: string;
icon: string;
}

export interface HostInfo {
id: string;
name: string;
Expand Down Expand Up @@ -60,4 +68,5 @@ export interface PortalConfig {
weather?: WeatherConfig;
searchUrlTemplate?: string;
footerLinks?: FooterLink[];
links?: PinnedLink[];
}
4 changes: 2 additions & 2 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@
</div>

<HostBar hosts={config.hosts} counts={metrics.hostCounts} bind:selectedHost />
<BookmarksSection bind:bookmarks />
<BookmarksSection bind:bookmarks links={config.links} />

{#if config.services.length > 0}
<div class="flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3 pt-2">
Expand Down Expand Up @@ -231,7 +231,7 @@

<CommandPalette
services={config.services}
{bookmarks}
bookmarks={[...(config.links ?? []), ...bookmarks]}
hosts={config.hosts}
searchUrlTemplate={config.searchUrlTemplate}
onSelectHost={(hostId) => (selectedHost = selectedHost === hostId ? null : hostId)}
Expand Down
13 changes: 11 additions & 2 deletions test/components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ describe('CommandPalette component', () => {
id: 'bm-1',
name: 'Pinned Router',
url: 'https://192.0.2.1',
icon: '📡',
createdAt: 1
icon: '📡'
}
],
hosts: [{ id: 'core-pi', name: 'core-pi', role: 'Gateway', ip: '192.0.2.2', arch: 'aarch64' }],
Expand Down Expand Up @@ -167,6 +166,16 @@ describe('BookmarksSection & Header components', () => {
assert.doesNotMatch(body, /Add Pinned Link/);
});

it('lists shared config links without a remove button', () => {
const { body } = render(BookmarksSection, {
props: { links: [{ id: 'cfg:docs', name: 'Docs', url: 'https://docs.example.test', icon: '📚' }] }
});
assert.match(body, /Docs/);
assert.match(body, /docs\.example\.test/);
assert.doesNotMatch(body, /No pinned links yet/);
assert.doesNotMatch(body, /Remove bookmark/);
});

it('renders Header with search trigger and ThemeToggle', () => {
const { body } = render(Header, { props: { onOpenSearch: () => {} } });
assert.match(body, /Foyer/);
Expand Down
18 changes: 18 additions & 0 deletions test/config-and-portal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ describe('URL validation & sanitization', () => {
assert.equal(isSafePortalUrl('/grafana/'), true);
assert.equal(isSafePortalUrl('//evil.example.test'), false);
assert.equal(isSafePortalUrl('/bad path with spaces'), false);
assert.equal(isSafePortalUrl('/\\outside.example.test'), false);
assert.equal(isSafePortalUrl('javascript:void(0)'), false);
});
});
Expand Down Expand Up @@ -114,6 +115,23 @@ describe('parsePortalConfig & static/config.json', () => {
assert.deepEqual(cfg.footerLinks, [{ label: 'Docs', url: '/docs' }]);
});

it('parses shared pinned links, defaulting the icon and dropping unsafe or duplicate entries', () => {
const cfg = parsePortalConfig({
links: [
{ name: 'Docs', url: 'https://docs.example.test', icon: '📚' },
{ name: 'Wiki', url: '/wiki' },
{ name: 'Docs again', url: 'https://docs.example.test' },
{ name: 'Bad', url: 'javascript:alert(1)' },
{ url: 'https://nameless.example.test' }
]
});
assert.deepEqual(cfg.links, [
{ id: 'cfg:https://docs.example.test', name: 'Docs', url: 'https://docs.example.test', icon: '📚' },
{ id: 'cfg:/wiki', name: 'Wiki', url: '/wiki', icon: '🔗' }
]);
assert.equal(parsePortalConfig({ links: [] }).links, undefined);
});

it('accepts valid boundary weather coordinates', () => {
const cfg = parsePortalConfig({
weather: { latitude: -53.8, longitude: 151.2, timezone: 'UTC', label: 'Harbor' }
Expand Down
Loading