import { useCallback, useEffect, useState } from "react" import { Editor } from "./components/Editor" import { Login } from "./components/Login" import { pb } from "./pb" import type { PbPage } from "./types" import "./App.css" function getSlugFromUrl(): string | null { const params = new URLSearchParams(window.location.search) return params.get("page") } export function App() { const [authenticated, setAuthenticated] = useState(pb.authStore.isValid) const [page, setPage] = useState(null) const [loading, setLoading] = useState(false) const [error, setError] = useState("") const slug = getSlugFromUrl() const loadPage = useCallback(async (slug: string) => { setLoading(true) setError("") try { const record = await pb.collection("pages").getFirstListItem(`slug="${slug}"`) setPage(record) } catch { setError(`Page "${slug}" not found`) } finally { setLoading(false) } }, []) useEffect(() => { if (authenticated && slug) { loadPage(slug) } }, [authenticated, slug, loadPage]) function handleLogout() { pb.authStore.clear() setAuthenticated(false) setPage(null) } async function handleDuplicate() { if (!page) return const newSlug = `${page.slug}-copy` try { const record = await pb.collection("pages").create({ title: `${page.title} (copy)`, slug: newSlug, excerpt: page.excerpt, date: page.date, cover: page.cover, doc: page.doc, tags: page.tags, published: false, draft: null, }) window.location.search = `?page=${record.slug}` } catch (err) { console.error("Duplicate failed:", err) alert("Duplicate failed. Check console.") } } async function handleTogglePublished() { if (!page) return try { const updated = await pb.collection("pages").update(page.id, { published: !page.published, }) setPage(updated) } catch (err) { console.error("Toggle published failed:", err) alert("Toggle published failed. Check console.") } } if (!authenticated) { return setAuthenticated(true)} /> } if (!slug) { return (

Add ?page=slug to the URL to edit a page.

) } if (loading) { return
Loading...
} if (error) { return (

{error}

) } if (!page) { return
Loading...
} return (
{page.title} /{page.slug}
) }