Initial Mebbling hub implementation

This commit is contained in:
2026-07-19 00:29:30 +08:00
commit e6ebdb0576
41 changed files with 2571 additions and 0 deletions
+3
View File
@@ -0,0 +1,3 @@
import { redirect } from "next/navigation"; import { getSession } from "@/lib/auth"; import { db } from "@/lib/db"; import { PublishForm } from "./publish-form"; import { WebhookControl } from "./webhook-control";
export const dynamic="force-dynamic";
export default async function Dashboard({searchParams}:{searchParams:Promise<{error?:string;source?:string}>}){const query=await searchParams;const user=await getSession();if(!user)redirect('/login');const sources=db.prepare('SELECT s.id,s.name,s.base_url,s.sync_status,s.last_synced_at,s.last_error,s.webhook_secret_hash,s.last_webhook_at,s.user_id AS owner_id FROM sources s JOIN source_members sm ON sm.source_id=s.id WHERE sm.user_id=? ORDER BY s.id DESC').all(user.id) as any[];return <><h1></h1>{query.error&&<p className="error">{query.error}</p>}{query.source==='shared'?<p> Memos </p>:query.source&&<p></p>}<section className="card"><h2> Memos</h2>{sources.length?<PublishForm sources={sources}/>:<p className="muted"> Memos </p>}</section><section className="card"><h2> Memos</h2><form action="/api/sources" method="post"><label><input name="name" required placeholder="我的 Memos"/></label><label>Memos <input name="baseUrl" type="url" required placeholder="https://memos.example.com"/></label><label>Personal Access Token<input name="token" type="password" required/></label><button></button></form><p className="muted">Token 使 Memos </p></section><section><h2></h2>{sources.map(s=><article className="card" key={s.id}><div className="space"><strong>{s.name}</strong><span className="tag">{s.sync_status}</span></div><p className="meta"> ID{s.id}<br/>{s.base_url}<br/>{s.last_synced_at||'尚未完成'}<br/>Webhook{s.webhook_secret_hash?(s.last_webhook_at?`最近收到:${new Date(s.last_webhook_at+'Z').toLocaleString('zh-TW')}`:'已建立 URL,尚未收到呼叫'):'尚未建立 URL'}{s.last_error&&<><br/><span className="error">{s.last_error}</span></>}</p>{s.owner_id===user.id?<WebhookControl sourceId={s.id} configured={Boolean(s.webhook_secret_hash)}/>:<p className="meta"> webhook</p>}<form action="/api/sync" method="post"><input type="hidden" name="sourceId" value={s.id}/><button></button></form></article>)}</section></>}
+34
View File
@@ -0,0 +1,34 @@
"use client";
import { FormEvent, useState } from "react";
type Source = { id: number; name: string };
export function PublishForm({ sources }: { sources: Source[] }) {
const [error, setError] = useState("");
const [submitting, setSubmitting] = useState(false);
async function submit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setSubmitting(true); setError("");
try {
const response = await fetch("/api/posts", { method: "POST", body: new FormData(event.currentTarget), headers: { Accept: "application/json" } });
const result = await response.json();
if (!response.ok) throw new Error(result.error || "發佈失敗");
window.location.assign(`/posts/${result.id}`);
} catch (reason) {
setError(reason instanceof Error ? reason.message : "發佈失敗");
setSubmitting(false);
}
}
return <form onSubmit={submit} encType="multipart/form-data">
<label>Markdown<textarea name="content" required /></label>
<label><input name="tags" placeholder="旅行, 想法" /></label>
<label><select name="visibility" defaultValue="PUBLIC"><option value="PUBLIC"></option><option value="PROTECTED"></option><option value="PRIVATE"></option></select></label>
<label><select name="sourceId" required>{sources.map((source) => <option key={source.id} value={source.id}>{source.name}</option>)}</select></label>
<label> 10 MB<input name="attachments" type="file" multiple /></label>
{error && <p className="error">{error}</p>}
<button disabled={submitting}>{submitting ? "發佈中…" : "發佈並同步"}</button>
</form>;
}
+20
View File
@@ -0,0 +1,20 @@
"use client";
import { useState } from "react";
export function WebhookControl({ sourceId, configured }: { sourceId: number; configured: boolean }) {
const [url, setUrl] = useState(""); const [error, setError] = useState(""); const [busy, setBusy] = useState(false);
async function generate() {
setBusy(true); setError("");
try {
const response = await fetch(`/api/sources/${sourceId}/webhook`, { method: "POST", headers: { Accept: "application/json" } });
const body = await response.json(); if (!response.ok) throw new Error(body.error || "無法產生 webhook URL"); setUrl(body.url);
} catch (reason) { setError(reason instanceof Error ? reason.message : "無法產生 webhook URL"); }
finally { setBusy(false); }
}
async function copy() { if (url) await navigator.clipboard.writeText(url); }
return <div className="webhook-control"><p className="meta">Webhook{configured ? "已設定" : "尚未設定"}</p>
{url ? <><label className="sr-only" htmlFor={`webhook-${sourceId}`}>Webhook URL</label><input id={`webhook-${sourceId}`} readOnly value={url} onFocus={(event) => event.currentTarget.select()} /><div className="row"><button type="button" onClick={copy}> URL</button><button type="button" className="danger" onClick={generate} disabled={busy}></button></div><p className="meta"> Memos</p></> : <button type="button" onClick={generate} disabled={busy}>{busy ? "產生中…" : configured ? "重新產生 webhook URL" : "產生 webhook URL"}</button>}
{error && <p className="error">{error}</p>}
</div>;
}