picoclaw/pkg/miniapp/frontend/src/hooks/use-api.ts
dj-oyu edcf490fd3 feat: migrate Telegram Mini App to Preact with 4-tab layout
Replace vanilla JS (~1600 lines) with Preact components and consolidate
8 tabs into 4 (Plan, Work, Tools, Dev) for better usability.

- Plan tab: plan status/phases + orch canvas (when enabled)
- Work tab: git/worktrees + improved session display with human-readable labels
- Tools tab: skills + commands + logs + research in card sections
- Dev tab: dev preview (unchanged behavior)

Add Preact dependency, TSX build via bun, shared hooks (useSSE, useAPI),
and utility modules. Simplify index.html to a single mount point.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-20 03:27:14 +09:00

48 lines
1.2 KiB
TypeScript

const API_BASE = location.origin;
function getInitData(): string {
return window.Telegram?.WebApp?.initData || '';
}
export async function apiFetch<T = any>(path: string): Promise<T> {
const sep = path.includes('?') ? '&' : '?';
const res = await fetch(
API_BASE + path + sep + 'initData=' + encodeURIComponent(getInitData()),
);
if (!res.ok) throw new Error('API error: ' + res.status);
return res.json();
}
export async function apiPost<T = any>(
path: string,
body: Record<string, any>,
): Promise<T> {
const sep = path.includes('?') ? '&' : '?';
const res = await fetch(
API_BASE + path + sep + 'initData=' + encodeURIComponent(getInitData()),
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
},
);
if (!res.ok) {
let errMsg = 'API error: ' + res.status;
try {
const data = await res.json();
if (data.error) errMsg = data.error;
} catch {}
throw new Error(errMsg);
}
return res.json();
}
export async function sendCommand(cmd: string): Promise<boolean> {
if (!cmd.startsWith('/')) return false;
try {
await apiPost('/miniapp/api/command', { command: cmd });
return true;
} catch {
return false;
}
}