Skip to content

armgit / soju

Public Imported from GitHub
0

Play Windows games on Apple Silicon Macs. Free, open-source app that installs the official Battle.net, Steam, Epic and GOG launchers and lists your games in one library.

14.7 KB
Raw Blame
import { readFileSync } from 'node:fs';
import { createHash } from 'node:crypto';
// Components for didimlabs.com. Content lives in content/*.json; build.mjs renders it to static HTML.
// Strings support a small inline syntax: [label](url), **bold**, `code`.
import { createElement as h, Fragment } from 'react';
import { defineCatalog } from '@json-render/core';
import { schema } from '@json-render/react/schema';
import { defineRegistry } from '@json-render/react';
import { z } from 'zod';

const str = z.string();
const opt = (t) => t.optional();
const link = z.object({ label: str, href: str, primary: opt(z.boolean()), lang: opt(str) });
const image = z.object({ src: str, alt: str, width: opt(z.number()), height: opt(z.number()) });

export const components = {
    Stack: { props: z.object({}), description: 'Groups children with no markup of its own' },
    Hero: {
      props: z.object({
        title: str, lead: str, logo: opt(str), actions: opt(z.array(link)), meta: opt(str), image: opt(image),
        // Home pages: a large headline with the app icon beside it. Other pages give only title/lead.
        headline: opt(str), eyebrow: opt(str), icon: opt(image), command: opt(str), alt: opt(str),
        stores: opt(z.object({ label: str, items: z.array(str) })),
      }),
      description: 'Page heading',
    },
    Section: { props: z.object({ id: opt(str), title: str, intro: opt(str) }), description: 'Titled section' },
    Text: { props: z.object({ text: str }), description: 'Paragraph' },
    Bullets: { props: z.object({ items: z.array(str) }), description: 'Bulleted list' },
    Features: { props: z.object({ items: z.array(z.object({ title: str, text: str })) }), description: 'Grid of short facts' },
    Steps: { props: z.object({ row: opt(z.boolean()), items: z.array(z.object({ title: str, text: opt(str), code: opt(str) })) }), description: 'Numbered steps; row lays them out side by side' },
    Proof: {
      props: z.object({ head: opt(z.array(str)), items: z.array(z.object({ title: str, store: str, status: str, tone: z.enum(['plays', 'partial']), result: str, game: opt(str) })) }),
      description: 'Cards for games that were run, with a status pill',
    },
    Cta: { props: z.object({ title: str, text: opt(str), icon: opt(image), actions: z.array(link) }), description: 'Closing call to action' },
    GameSearch: {
      props: z.object({ id: opt(str), label: str, placeholder: str, button: str, action: str, hint: opt(str) }),
      description: 'Search box that opens the game list filtered by the name typed',
    },
    Table: { props: z.object({ label: str, head: z.array(str), rows: z.array(z.array(str)) }), description: 'Data table' },
    Code: { props: z.object({ code: str }), description: 'Code block' },
    Note: { props: z.object({ text: str }), description: 'Side note' },
    Buttons: { props: z.object({ items: z.array(link) }), description: 'Row of links styled as buttons' },
    GameCard: { props: z.object({ href: str, image, title: str, meta: str, text: str }), description: 'Game teaser' },
    Gallery: { props: z.object({ images: z.array(image) }), description: 'Screenshot grid' },
    Video: { props: z.object({ src: str, poster: str, captions: opt(str), label: str, caption: opt(str) }), description: 'Video' },
    Faq: { props: z.object({ items: z.array(z.object({ q: str, a: str })) }), description: 'Questions and answers' },
    Links: { props: z.object({ items: z.array(link) }), description: 'List of links' },
    Figure: { props: z.object({ image, caption: opt(str) }), description: 'Single image with caption' },
    Claim: { props: z.object({ label: str, missing: str, ko: opt(str) }), description: 'Install-in-Soju button for a website purchase' },
    Notify: { props: z.object({ label: str, button: str, sent: str, failed: str, source: str }), description: 'Release news email sign-up' },
    ContactForm: {
      props: z.object({
        action: str, name: str, email: str, company: str, message: str, send: str,
        sending: str, sent: str, failed: str, thanks: opt(str),
      }),
      description: 'Inquiry form posted to the site worker',
    },
};

let searchJs = '';
try { searchJs = readFileSync(new URL('../docs/game-search.js', import.meta.url)); } catch {}
const SEARCH_JS = '/game-search.js?v=' + createHash('sha256').update(searchJs).digest('hex').slice(0, 8);

export const catalog = defineCatalog(schema, { components, actions: {} });

// [label](url), **bold**, `code` -> React nodes. Everything else is plain text (React escapes it).
export function inline(text) {
  const out = [];
  const re = /\[([^\]]+)\]\(([^)\s]+)\)|\*\*([^*]+)\*\*|`([^`]+)`/g;
  let at = 0, m, k = 0;
  while ((m = re.exec(text))) {
    if (m.index > at) out.push(text.slice(at, m.index));
    if (m[1] !== undefined) out.push(h('a', { key: k++, href: m[2] }, m[1]));
    else if (m[3] !== undefined) out.push(h('strong', { key: k++ }, m[3]));
    else out.push(h('code', { key: k++ }, m[4]));
    at = re.lastIndex;
  }
  if (at < text.length) out.push(text.slice(at));
  return out;
}

const img = (i, extra = {}) => h('img', { src: i.src, alt: i.alt, width: i.width, height: i.height, ...extra });
const button = (l, k) => h('a', { key: k, className: l.primary ? 'btn' : 'btn secondary', href: l.href, lang: l.lang }, l.label);

export const { registry } = defineRegistry(catalog, {
  components: {
    Stack: ({ children }) => h(Fragment, null, children),
    Hero: ({ props }) => props.headline
      ? h('header', { className: 'hero hero-home' },
        h('div', { className: 'hero-copy' },
          h('h1', null, inline(props.headline)),
          h('p', { className: 'lead' }, inline(props.lead)),
          props.actions && h('p', { className: 'actions' }, props.actions.map(button)),
          props.meta && h('p', { className: 'meta' }, inline(props.meta)),
          props.alt && h('p', { className: 'alt' }, inline(props.alt))),
        props.image && h('figure', { className: 'hero-shot' }, img(props.image, { fetchPriority: 'high' })))
      : h('header', { className: 'hero' },
        props.logo ? h('h1', { className: 'with-logo' }, h('img', { src: props.logo, width: 72, height: 72, alt: '' }), props.title) : h('h1', null, props.title),
        h('p', { className: 'lead' }, inline(props.lead)),
        props.actions && h('p', { className: 'actions' }, props.actions.map(button), props.meta && h('span', { className: 'meta' }, props.meta)),
        props.image && img(props.image, { className: 'shot', fetchPriority: 'high' })),
    Section: ({ props, children }) => h('section', { id: props.id, 'aria-labelledby': props.id && props.id + '-title' },
      h('h2', { id: props.id && props.id + '-title' }, props.title),
      props.intro && h('p', { className: 'intro' }, inline(props.intro)),
      children),
    Text: ({ props }) => h('p', null, inline(props.text)),
    Bullets: ({ props }) => h('ul', null, props.items.map((t, k) => h('li', { key: k }, inline(t)))),
    Features: ({ props }) => h('dl', { className: 'facts' }, props.items.map((f, k) =>
      h(Fragment, { key: k }, h('dt', null, f.title), h('dd', null, inline(f.text))))),
    Steps: ({ props }) => h('ol', { className: props.row ? 'steps row' : 'steps' }, props.items.map((s, k) =>
      h('li', { key: k }, h('b', null, s.title), s.text && h('p', null, inline(s.text)), s.code && h('pre', null, h('code', null, s.code))))),
    Table: ({ props }) => h('div', { className: 'table-scroll', role: 'region', 'aria-label': props.label, tabIndex: 0 },
      h('table', null,
        h('thead', null, h('tr', null, props.head.map((c, k) => h('th', { key: k, scope: 'col' }, c)))),
        h('tbody', null, props.rows.map((r, i) => h('tr', { key: i }, r.map((c, k) => k === 0
          ? h('th', { key: k, scope: 'row' }, inline(c)) : h('td', { key: k }, inline(c)))))))),
    Proof: ({ props }) => h('div', { className: 'table-scroll', role: 'region', 'aria-label': (props.head || [])[0] || 'Games', tabIndex: 0 },
      h('table', { className: 'proof' },
        h('thead', null, h('tr', null, (props.head || ['Game', 'Store', 'Result', 'What we saw']).map((c, k) => h('th', { key: k, scope: 'col' }, c)))),
        h('tbody', null, props.items.map((g, k) => h('tr', { key: k, 'data-game': g.game },
          h('th', { scope: 'row' }, g.title),
          h('td', null, g.store),
          h('td', null, h('span', { className: 'pill ' + g.tone }, g.status)),
          h('td', null, inline(g.result))))))),
    GameSearch: ({ props }) => h('form', { id: props.id, className: 'game-search', method: 'get', action: props.action, role: 'search', 'aria-labelledby': 'game-q-title' },
      h('h2', { id: 'game-q-title' }, h('label', { htmlFor: 'game-q' }, props.label)),
      h('span', { className: 'search-row' },
        h('input', { id: 'game-q', name: 'q', type: 'search', placeholder: props.placeholder, autoComplete: 'off' }),
        h('button', { className: 'btn', type: 'submit' }, props.button)),
      h('ul', { id: 'search-results', 'aria-live': 'polite', hidden: true }),
      props.hint && h('p', { className: 'hint' }, inline(props.hint)),
      h('script', { src: SEARCH_JS, defer: true })),
    Cta: ({ props }) => h('section', { className: 'cta' },
      h('h2', null, props.title),
      props.text && h('p', null, inline(props.text)),
      h('p', { className: 'actions' }, props.actions.map(button))),
    Code: ({ props }) => h('pre', null, h('code', null, props.code)),
    Note: ({ props }) => h('p', { className: 'note' }, inline(props.text)),
    Buttons: ({ props }) => h('p', { className: 'actions' }, props.items.map(button)),
    GameCard: ({ props }) => h('a', { className: 'game', href: props.href },
      img(props.image, { loading: 'lazy' }),
      h('span', { className: 'game-body' }, h('b', null, props.title), h('span', { className: 'game-meta' }, props.meta), h('span', null, props.text))),
    Gallery: ({ props }) => h('div', { className: 'gallery' }, props.images.map((i, k) => img(i, { key: k, loading: 'lazy' }))),
    Video: ({ props }) => h('figure', { className: 'video' },
      h('video', { controls: true, playsInline: true, preload: 'none', poster: props.poster, 'aria-label': props.label, width: 1280, height: 720 },
        h('source', { src: props.src, type: 'video/mp4' }),
        props.captions && h('track', { kind: 'captions', src: props.captions, srcLang: 'en', label: 'English', default: true })),
      props.caption && h('figcaption', null, props.caption)),
    Faq: ({ props }) => h('div', { className: 'faq' }, props.items.map((f, k) =>
      h('details', { key: k, open: k === 0 }, h('summary', null, f.q), h('p', null, inline(f.a))))),
    Links: ({ props }) => h('ul', { className: 'links' }, props.items.map((l, k) =>
      h('li', { key: k }, h('a', { href: l.href, lang: l.lang }, l.label)))),
    Figure: ({ props }) => h('figure', { className: 'figure' }, img(props.image, { loading: 'lazy' }),
      props.caption && h('figcaption', null, props.caption)),
    Claim: ({ props }) => h(Fragment, null,
      h('p', { className: 'actions', id: 'claim', hidden: true },
        h('a', { className: 'btn', id: 'claim-link', href: '#' }, props.label),
        props.ko && h('a', { className: 'btn secondary', id: 'claim-link-ko', href: '#' }, props.ko)),
      h('p', { className: 'note' }, inline(props.missing)),
      h('script', { dangerouslySetInnerHTML: { __html: `(function(){var m=location.search.match(/[?&]checkout_id=([0-9a-f-]{36})/);if(!m)return;var u='soju://store/claim?checkout='+m[1];document.getElementById('claim-link').href=u;var k=document.getElementById('claim-link-ko');if(k)k.href=u;document.getElementById('claim').hidden=false;})();` } })),
    Notify: ({ props }) => h(Fragment, null,
      h('form', { className: 'notify', method: 'post', action: '/api/notify', 'data-sent': props.sent, 'data-failed': props.failed },
        h('label', null, props.label, h('span', { className: 'notify-row' },
          h('input', { name: 'email', type: 'email', required: true, maxLength: 200, autoComplete: 'email', placeholder: '[email protected]' }),
          h('button', { className: 'btn', type: 'submit' }, props.button))),
        h('input', { type: 'hidden', name: 'source', value: props.source }),
        h('input', { name: 'website', tabIndex: -1, autoComplete: 'off', className: 'hp', 'aria-hidden': 'true' }),
        h('span', { className: 'form-status', role: 'status', 'aria-live': 'polite' })),
      h('script', { dangerouslySetInnerHTML: { __html: `document.querySelectorAll('form.notify').forEach(function(f){if(f.dataset.on)return;f.dataset.on=1;var s=f.querySelector('.form-status'),b=f.querySelector('button');
f.addEventListener('submit',function(e){e.preventDefault();b.disabled=true;fetch(f.action,{method:'POST',body:new FormData(f)}).then(function(r){return r.json().then(function(j){return {ok:r.ok,j:j};});})
.then(function(x){b.disabled=false;if(x.ok){f.reset();s.textContent=f.dataset.sent;}else{s.textContent=(x.j&&x.j.error)||f.dataset.failed;}}).catch(function(){b.disabled=false;s.textContent=f.dataset.failed;});});});` } })),
    ContactForm: ({ props }) => h(Fragment, null,
      props.thanks && h('p', { className: 'note thanks', id: 'thanks', hidden: true }, props.thanks),
      h('form', { className: 'contact', method: 'post', action: props.action, id: 'contact' },
        h('label', null, props.name, h('input', { name: 'name', required: true, maxLength: 100, autoComplete: 'name' })),
        h('label', null, props.email, h('input', { name: 'email', type: 'email', required: true, maxLength: 200, autoComplete: 'email' })),
        h('label', null, props.company, h('input', { name: 'company', maxLength: 120, autoComplete: 'organization' })),
        h('label', null, props.message, h('textarea', { name: 'message', required: true, minLength: 5, maxLength: 4000, rows: 5 })),
        h('input', { name: 'website', tabIndex: -1, autoComplete: 'off', className: 'hp', 'aria-hidden': 'true' }),
        h('p', { className: 'actions' }, h('button', { className: 'btn', type: 'submit' }, props.send),
          h('span', { className: 'form-status', role: 'status', 'aria-live': 'polite' }))),
      h('script', { dangerouslySetInnerHTML: { __html: `(function(){
var f=document.getElementById('contact'),s=f.querySelector('.form-status'),b=f.querySelector('button');
if(/[?&]thanks=1/.test(location.search)){var t=document.getElementById('thanks');if(t)t.hidden=false;}
f.addEventListener('submit',function(e){e.preventDefault();b.disabled=true;s.textContent=${JSON.stringify(props.sending)};
fetch(f.action,{method:'POST',body:new FormData(f)}).then(function(r){return r.json().then(function(j){return {ok:r.ok,j:j};});})
.then(function(x){if(x.ok){f.reset();s.textContent=${JSON.stringify(props.sent)};}else{s.textContent=(x.j&&x.j.error)||${JSON.stringify(props.failed)};b.disabled=false;}})
.catch(function(){s.textContent=${JSON.stringify(props.failed)};b.disabled=false;});});})();` } })),
  },
});