// Store previews as SVG, drawn from the same data the app uses: a theme's colors, shape, layout
// and background become a miniature X timeline (left rail, column of posts, sidebar); a plugin gets
// a card with its name, category glyph and permissions. Used by scripts/build-store.mjs; the site's
// /themes/ and /plugins/ pages and the in-app store show the files it writes.
const esc = value => String(value).replace(/[&<>"']/g, ch => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' })[ch]);
const hex = value => (/^#[0-9a-f]{6}$/i.test(value ?? '') ? value : null);
/** X's own default (light) palette, for a theme that keeps X's colors. */
const X_LIGHT = { page: '#ffffff', bg: '#ffffff', surface: '#f7f9f9', elevated: '#eff3f4', border: '#eff3f4', text: '#0f1419', muted: '#536471', accent: '#1d9bf0', inverse: '#0f1419', onInverse: '#ffffff' };
function palette(theme) {
const c = theme.colors ?? X_LIGHT;
const pick = (key, fallback) => hex(c[key]) ?? fallback;
const bg = pick('bg', X_LIGHT.bg);
const text = pick('text', X_LIGHT.text);
const accent = pick('accent', X_LIGHT.accent);
return {
page: pick('page', bg), bg, surface: pick('surface', bg), elevated: pick('elevated', bg), border: pick('border', pick('surface', bg)),
text, muted: pick('muted', text), accent, inverse: pick('inverse', accent), onInverse: pick('onInverse', bg),
};
}
function backgroundDefs(theme, p, W, H) {
const bg = theme.background ?? { kind: 'none' };
if (bg.kind === 'gradient' && hex(bg.from) && hex(bg.to)) {
const angle = ((Number(bg.angle) || 180) - 90) * Math.PI / 180;
const x = Math.cos(angle) / 2, y = Math.sin(angle) / 2;
const glow = hex(bg.glow);
return {
defs: ``
+ (glow ? `` : ''),
fill: `${glow ? `` : ''}`,
};
}
if (bg.kind === 'image' && typeof bg.url === 'string' && /^data:image\/(png|jpeg|webp|gif|avif);base64,/.test(bg.url)) {
const dim = Math.min(0.9, Math.max(0, Number(bg.dim) || 0));
return { defs: '', fill: `` };
}
if (bg.kind === 'pattern' && hex(bg.ink)) {
const opacity = Math.min(0.6, Math.max(0.04, Number(bg.opacity) || 0.1));
const shapes = {
dots: ``,
grid: ``,
diagonal: ``,
scanlines: ``,
paper: ``,
clouds: ``,
};
const size = bg.pattern === 'clouds' ? 48 : 12;
return {
defs: `${shapes[bg.pattern] ?? shapes.dots}`,
fill: ``,
};
}
return { defs: '', fill: `` };
}
/** A 640×400 miniature of X in `theme` (a sanitized ThemeSpec). */
export function themePreviewSvg(theme) {
const W = 640, H = 400;
const p = palette(theme);
const radius = Math.min(18, Math.max(0, Number(theme.shape?.radius ?? 16))) * 0.7;
const pill = Math.min(20, Number(theme.shape?.button ?? 9999)) ;
const avatar = theme.shape?.avatar ?? 'circle';
const cards = Boolean(theme.layout?.cards);
const frame = Boolean(theme.layout?.frame);
const density = theme.layout?.density ?? 'default';
const postH = density === 'compact' ? 88 : density === 'comfortable' ? 110 : 98;
const { defs, fill } = backgroundDefs(theme, p, W, H);
const colX = 170, colW = 300, sideX = 490, sideW = 130;
const out = [];
out.push(`');
return out.join('');
}
const GLYPHS = {
reading: 'M4 6h9a3 3 0 0 1 3 3v11a2 2 0 0 0-2-2H4zM28 6h-9a3 3 0 0 0-3 3v11a2 2 0 0 1 2-2h10z',
writing: 'M6 26l1.5-6L21 6.5l4.5 4.5L12 24.5zM18.5 9l4.5 4.5',
filters: 'M5 7h22l-8.5 10v7l-5 2.5V17z',
focus: 'M16 9a7 7 0 1 0 0 14 7 7 0 0 0 0-14zM16 4v3M16 25v3M4 16h3M25 16h3',
tools: 'M20 5a6 6 0 0 0-5.6 8.2L5 22.6 9.4 27l9.4-9.4A6 6 0 0 0 27 12l-4 4-4-1-1-4 4-4a6 6 0 0 0-2-2z',
};
const HUES = { reading: '#5b8cff', writing: '#c084fc', filters: '#f472b6', focus: '#2dd4bf', tools: '#fbbf24' };
const CATEGORY = { reading: 'Reading', writing: 'Writing', filters: 'Filters', focus: 'Focus', tools: 'Tools' };
/** A 640×400 banner for a plugin: Night Ink, a large category glyph, the name and category. */
export function pluginPreviewSvg(plugin) {
const W = 640, H = 400;
const glyph = GLYPHS[plugin.category] ?? GLYPHS.tools;
const hue = HUES[plugin.category] ?? HUES.tools;
const name = esc(plugin.name);
const size = plugin.name.length > 18 ? 34 : 40;
return ``;
}