feat: complete dynamic JSON-driven photo tools frontend connected to FastAPI backend
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
import { SectionConfig, resolveLocalised } from '@/types';
|
||||
|
||||
export function resolvePath(obj: any, pathStr: string, locale: string = 'en'): any {
|
||||
if (!obj || !pathStr) return undefined;
|
||||
const parts = pathStr.split('.');
|
||||
let current = obj;
|
||||
|
||||
for (const part of parts) {
|
||||
if (current === undefined || current === null) return undefined;
|
||||
current = current[part];
|
||||
}
|
||||
|
||||
// If result is a localised object, resolve it
|
||||
if (typeof current === 'object' && current !== null && ('en' in current || 'hi' in current)) {
|
||||
return resolveLocalised(current, locale);
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
export function interpolateString(template: string, context: Record<string, any>, locale: string = 'en'): string {
|
||||
if (typeof template !== 'string') return '';
|
||||
return template.replace(/\{\{\s*([a-zA-Z0-9_\.]+)(\|([^}]+))?\s*\}\}/g, (_, key, __, defaultVal) => {
|
||||
const val = resolvePath(context, key, locale);
|
||||
if (val !== undefined && val !== null) {
|
||||
return String(val);
|
||||
}
|
||||
return defaultVal !== undefined ? defaultVal.trim() : '';
|
||||
});
|
||||
}
|
||||
|
||||
export function resolveBindings<T = any>(data: T, context: Record<string, any>, locale: string = 'en'): T {
|
||||
if (data === null || data === undefined) return data;
|
||||
|
||||
if (typeof data === 'string') {
|
||||
// Exact placeholder match replacing entire value with an array/object
|
||||
const exactMatch = data.match(/^\{\{\s*([a-zA-Z0-9_\.]+)\s*\}\}$/);
|
||||
if (exactMatch) {
|
||||
const resolved = resolvePath(context, exactMatch[1], locale);
|
||||
if (resolved !== undefined) return resolved;
|
||||
}
|
||||
return interpolateString(data, context, locale) as unknown as T;
|
||||
}
|
||||
|
||||
if (Array.isArray(data)) {
|
||||
return data.map((item) => resolveBindings(item, context, locale)) as unknown as T;
|
||||
}
|
||||
|
||||
if (typeof data === 'object') {
|
||||
// Check if it's a localized object { en: '...', hi: '...' }
|
||||
if (Object.keys(data).some((k) => k === 'en' || k === 'hi')) {
|
||||
const resolvedText = resolveLocalised(data as any, locale);
|
||||
return interpolateString(resolvedText, context, locale) as unknown as T;
|
||||
}
|
||||
|
||||
const result: Record<string, any> = {};
|
||||
for (const [k, v] of Object.entries(data)) {
|
||||
result[k] = resolveBindings(v, context, locale);
|
||||
}
|
||||
return result as unknown as T;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
export function isSectionVisible(
|
||||
section: SectionConfig,
|
||||
currentContext: { device?: 'mobile' | 'desktop'; locale?: string; flags?: Record<string, boolean> }
|
||||
): boolean {
|
||||
if (!section.visibleWhen) return true;
|
||||
const { device, locale, flag } = section.visibleWhen;
|
||||
|
||||
if (device && currentContext.device && device !== currentContext.device) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (locale && currentContext.locale && locale !== currentContext.locale) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (flag && currentContext.flags && currentContext.flags[flag] === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { SiteConfig, PageConfig, ToolItem, AppItem, CategoryItem } from '@/types';
|
||||
import { getEndpointById } from '../catalog/loader';
|
||||
|
||||
const CONTENT_DIR = path.join(process.cwd(), 'content');
|
||||
|
||||
// In-memory cache for speed and resilience
|
||||
let siteConfigCache: SiteConfig | null = null;
|
||||
let pagesCache = new Map<string, PageConfig>();
|
||||
let toolsCache = new Map<string, ToolItem>();
|
||||
let appsCache = new Map<string, AppItem>();
|
||||
let categoriesCache = new Map<string, CategoryItem>();
|
||||
|
||||
function readJsonFile<T>(filePath: string): T | null {
|
||||
try {
|
||||
if (!fs.existsSync(filePath)) return null;
|
||||
const raw = fs.readFileSync(filePath, 'utf8');
|
||||
return JSON.parse(raw) as T;
|
||||
} catch (e) {
|
||||
console.error(`Failed to read/parse JSON from ${filePath}:`, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function getSiteConfig(): SiteConfig {
|
||||
if (siteConfigCache) return siteConfigCache;
|
||||
const config = readJsonFile<SiteConfig>(path.join(CONTENT_DIR, 'site.config.json'));
|
||||
if (!config) {
|
||||
throw new Error('Crucial site.config.json missing or invalid');
|
||||
}
|
||||
siteConfigCache = config;
|
||||
return config;
|
||||
}
|
||||
|
||||
export function getPageConfig(slug: string): PageConfig | null {
|
||||
const normSlug = slug === '' || slug === '/' ? 'home' : slug.replace(/^\/+|\/+$/g, '');
|
||||
if (pagesCache.has(normSlug)) return pagesCache.get(normSlug)!;
|
||||
|
||||
const filePath = path.join(CONTENT_DIR, 'pages', `${normSlug}.json`);
|
||||
const page = readJsonFile<PageConfig>(filePath);
|
||||
if (page) {
|
||||
pagesCache.set(normSlug, page);
|
||||
}
|
||||
return page;
|
||||
}
|
||||
|
||||
export function getAllTools(): ToolItem[] {
|
||||
if (toolsCache.size > 0) return Array.from(toolsCache.values());
|
||||
|
||||
const toolsDir = path.join(CONTENT_DIR, 'collections', 'tools');
|
||||
if (fs.existsSync(toolsDir)) {
|
||||
const files = fs.readdirSync(toolsDir).filter((f) => f.endsWith('.json'));
|
||||
for (const file of files) {
|
||||
const tool = readJsonFile<ToolItem>(path.join(toolsDir, file));
|
||||
if (tool && tool.status === 'published') {
|
||||
toolsCache.set(tool.slug, tool);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(toolsCache.values());
|
||||
}
|
||||
|
||||
export function getToolBySlug(slug: string): ToolItem | null {
|
||||
getAllTools();
|
||||
return toolsCache.get(slug) || null;
|
||||
}
|
||||
|
||||
export function getToolById(id: string): ToolItem | null {
|
||||
getAllTools();
|
||||
for (const t of toolsCache.values()) {
|
||||
if (t.id === id) return t;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getAllApps(): AppItem[] {
|
||||
if (appsCache.size > 0) return Array.from(appsCache.values());
|
||||
|
||||
const appsDir = path.join(CONTENT_DIR, 'collections', 'apps');
|
||||
if (fs.existsSync(appsDir)) {
|
||||
const files = fs.readdirSync(appsDir).filter((f) => f.endsWith('.json'));
|
||||
for (const file of files) {
|
||||
const app = readJsonFile<AppItem>(path.join(appsDir, file));
|
||||
if (app && app.status === 'published') {
|
||||
appsCache.set(app.slug, app);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(appsCache.values());
|
||||
}
|
||||
|
||||
export function getAppBySlug(slug: string): AppItem | null {
|
||||
getAllApps();
|
||||
return appsCache.get(slug) || null;
|
||||
}
|
||||
|
||||
export function getAppById(id: string): AppItem | null {
|
||||
getAllApps();
|
||||
for (const a of appsCache.values()) {
|
||||
if (a.id === id) return a;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getAllCategories(): CategoryItem[] {
|
||||
if (categoriesCache.size > 0) return Array.from(categoriesCache.values());
|
||||
|
||||
const dir = path.join(CONTENT_DIR, 'collections', 'categories');
|
||||
if (fs.existsSync(dir)) {
|
||||
const files = fs.readdirSync(dir).filter((f) => f.endsWith('.json'));
|
||||
for (const file of files) {
|
||||
const cat = readJsonFile<CategoryItem>(path.join(dir, file));
|
||||
if (cat) {
|
||||
categoriesCache.set(cat.slug, cat);
|
||||
}
|
||||
}
|
||||
}
|
||||
return Array.from(categoriesCache.values());
|
||||
}
|
||||
|
||||
export function getTemplate(name: string): any {
|
||||
const filePath = path.join(CONTENT_DIR, 'templates', `${name}.json`);
|
||||
return readJsonFile(filePath);
|
||||
}
|
||||
|
||||
export function getLocaleDictionary(locale: string = 'en'): Record<string, string> {
|
||||
const filePath = path.join(CONTENT_DIR, 'locales', `${locale}.json`);
|
||||
const data = readJsonFile<{ system: Record<string, string> }>(filePath);
|
||||
return data?.system || {};
|
||||
}
|
||||
|
||||
export function checkIntegrity(): { valid: boolean; errors: string[] } {
|
||||
const errors: string[] = [];
|
||||
const tools = getAllTools();
|
||||
const apps = getAllApps();
|
||||
const appIds = new Set(apps.map((a) => a.id));
|
||||
|
||||
for (const tool of tools) {
|
||||
if (!appIds.has(tool.homeApp)) {
|
||||
errors.push(`Tool '${tool.id}' references missing homeApp '${tool.homeApp}'`);
|
||||
}
|
||||
const epId = tool.tryIt?.endpointRef?.id;
|
||||
if (epId && !getEndpointById(epId)) {
|
||||
errors.push(`Tool '${tool.id}' references missing endpoint '${epId}'`);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
valid: errors.length === 0,
|
||||
errors,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user