Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

2.3 KB
Raw Blame
"use client";

import { useSyncExternalStore } from "react";

// Runtime app registry — apps created via Create App or installed from the App
// Store. Persisted to localStorage (per-browser) — no backend table needed. A tiny external store so app-store, create-app, and
// the shell's useInstalledApps all read the same reactive list.

export type AppRow = {
  appId: string;
  title: string;
  glyph: string;
  gradient: string;
  runtime: string; // html | node | python | shell
  entry: string;
  source: "custom" | "store";
  installed: boolean;
};

const KEY = "mso:apps";
const EMPTY: AppRow[] = [];
let rows: AppRow[] | null = null;
const subs = new Set<() => void>();

function load(): AppRow[] {
  if (typeof window === "undefined") return EMPTY;
  try {
    const r = JSON.parse(localStorage.getItem(KEY) || "[]");
    return Array.isArray(r) ? (r as AppRow[]) : EMPTY;
  } catch {
    return EMPTY;
  }
}

function snap(): AppRow[] {
  if (rows === null) rows = load();
  return rows;
}

function commit(next: AppRow[]) {
  if (typeof window !== "undefined") {
    try { localStorage.setItem(KEY, JSON.stringify(next)); }
    catch { throw new Error("Browser storage could not save this change. Free some space or allow site storage, then try again."); }
  }
  rows = next;
  subs.forEach((notify) => notify());
}

function subscribe(cb: () => void) {
  subs.add(cb);
  return () => {
    subs.delete(cb);
  };
}

export function useApps(): AppRow[] {
  return useSyncExternalStore(subscribe, snap, () => EMPTY);
}

// App Store install/uninstall toggle — upsert the row's installed state.
export function setInstalled(p: {
  appId: string;
  installed: boolean;
  title: string;
  glyph: string;
  gradient: string;
  runtime: string;
  entry: string;
}) {
  const cur = snap();
  const i = cur.findIndex((a) => a.appId === p.appId);
  const next = cur.slice();
  if (i >= 0) next[i] = { ...next[i], ...p };
  else next.push({ source: "store", ...p });
  commit(next);
}

// Create App — author a new custom app (installed immediately).
export function createApp(p: {
  appId: string;
  title: string;
  glyph: string;
  gradient: string;
  runtime: string;
  entry: string;
}) {
  const cur = snap();
  if (cur.some((a) => a.appId === p.appId)) throw new Error("App slug sudah dipakai");
  commit([...cur, { ...p, source: "custom", installed: true }]);
}