|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { HTMLTable, Button, MenuItem, Spinner, Label } from "@blueprintjs/core"; |
| 3 | +import { Select } from "@blueprintjs/select"; |
| 4 | +import { |
| 5 | + getSupabaseContext, |
| 6 | + getLoggedInClient, |
| 7 | + SupabaseContext, |
| 8 | +} from "~/utils/supabaseContext"; |
| 9 | +import { |
| 10 | + getNodes, |
| 11 | + getNodeSchemas, |
| 12 | + nodeSchemaSignature, |
| 13 | + type NodeSignature, |
| 14 | + type PConcept, |
| 15 | +} from "@repo/database/lib/queries"; |
| 16 | +import { DGSupabaseClient } from "@repo/database/lib/client"; |
| 17 | + |
| 18 | +const NodeRow = ({ node }: { node: PConcept }) => { |
| 19 | + return ( |
| 20 | + <tr> |
| 21 | + <td>{node.name}</td> |
| 22 | + <td>{node.created}</td> |
| 23 | + <td>{node.last_modified}</td> |
| 24 | + <td> |
| 25 | + <pre>{JSON.stringify({ ...node, Content: null }, null, 2)}</pre> |
| 26 | + </td> |
| 27 | + <td> |
| 28 | + <pre> |
| 29 | + {JSON.stringify({ ...node.Content, Document: null }, null, 2)} |
| 30 | + </pre> |
| 31 | + <span |
| 32 | + data-link-title={node.Content?.text} |
| 33 | + data-link-uid={node.Content?.source_local_id} |
| 34 | + > |
| 35 | + <span className="rm-page-ref__brackets">[[</span> |
| 36 | + <span |
| 37 | + className="rm-page-ref rm-page-ref--link" |
| 38 | + onClick={(event) => { |
| 39 | + void (async (event) => { |
| 40 | + if (event.shiftKey) { |
| 41 | + if (node.Content?.source_local_id) { |
| 42 | + await window.roamAlphaAPI.ui.rightSidebar.addWindow({ |
| 43 | + window: { |
| 44 | + // @ts-expect-error TODO: fix this |
| 45 | + "block-uid": node.Content.source_local_id, |
| 46 | + type: "outline", |
| 47 | + }, |
| 48 | + }); |
| 49 | + } |
| 50 | + } else if (node.Content?.Document?.source_local_id) { |
| 51 | + await window.roamAlphaAPI.ui.mainWindow.openPage({ |
| 52 | + page: { |
| 53 | + uid: node.Content.Document.source_local_id, |
| 54 | + }, |
| 55 | + }); |
| 56 | + } |
| 57 | + })(event); |
| 58 | + }} |
| 59 | + > |
| 60 | + {node.Content?.text} |
| 61 | + </span> |
| 62 | + <span className="rm-page-ref__brackets">]]</span> |
| 63 | + </span> |
| 64 | + </td> |
| 65 | + <td> |
| 66 | + <pre>{JSON.stringify(node.Content?.Document, null, 2)}</pre> |
| 67 | + </td> |
| 68 | + </tr> |
| 69 | + ); |
| 70 | +}; |
| 71 | + |
| 72 | +const NodeTable = ({ nodes }: { nodes: PConcept[] }) => { |
| 73 | + return ( |
| 74 | + <HTMLTable> |
| 75 | + <thead> |
| 76 | + <tr> |
| 77 | + <th>Name</th> |
| 78 | + <th>Created</th> |
| 79 | + <th>Last Modified</th> |
| 80 | + <th>Concept</th> |
| 81 | + <th>Content</th> |
| 82 | + <th>Document</th> |
| 83 | + </tr> |
| 84 | + </thead> |
| 85 | + <tbody> |
| 86 | + {nodes.map((node: PConcept) => ( |
| 87 | + <NodeRow node={node} key={node.id} /> |
| 88 | + ))} |
| 89 | + </tbody> |
| 90 | + </HTMLTable> |
| 91 | + ); |
| 92 | +}; |
| 93 | + |
| 94 | +const AdminPanel = () => { |
| 95 | + const [context, setContext] = useState<SupabaseContext | null>(null); |
| 96 | + const [supabase, setSupabase] = useState<DGSupabaseClient | null>(null); |
| 97 | + const [schemas, setSchemas] = useState<NodeSignature[]>([]); |
| 98 | + const [showingSchema, setShowingSchema] = |
| 99 | + useState<NodeSignature>(nodeSchemaSignature); |
| 100 | + const [nodes, setNodes] = useState<PConcept[]>([]); |
| 101 | + const [loading, setLoading] = useState(true); |
| 102 | + const [loadingNodes, setLoadingNodes] = useState(true); |
| 103 | + const [error, setError] = useState<string | null>(null); |
| 104 | + |
| 105 | + useEffect(() => { |
| 106 | + let ignore = false; |
| 107 | + void (async () => { |
| 108 | + try { |
| 109 | + if (!ignore) { |
| 110 | + setContext(await getSupabaseContext()); |
| 111 | + } |
| 112 | + // Ask for logged-in client _after_ the context |
| 113 | + if (!ignore) { |
| 114 | + setSupabase(await getLoggedInClient()); |
| 115 | + } |
| 116 | + } catch (e) { |
| 117 | + setError((e as Error).message); |
| 118 | + console.error("AdminPanel init failed", e); |
| 119 | + } finally { |
| 120 | + setLoading(false); |
| 121 | + } |
| 122 | + })(); |
| 123 | + return () => { |
| 124 | + ignore = true; |
| 125 | + }; |
| 126 | + }, []); |
| 127 | + |
| 128 | + useEffect(() => { |
| 129 | + let ignore = false; |
| 130 | + void (async () => { |
| 131 | + if (!ignore && supabase !== null && context !== null) { |
| 132 | + try { |
| 133 | + setSchemas(await getNodeSchemas(supabase, context.spaceId)); |
| 134 | + } catch (e) { |
| 135 | + setError((e as Error).message); |
| 136 | + console.error("getNodeSchemas failed", e); |
| 137 | + } finally { |
| 138 | + setLoading(false); |
| 139 | + } |
| 140 | + } |
| 141 | + })(); |
| 142 | + return () => { |
| 143 | + ignore = true; |
| 144 | + }; |
| 145 | + }, [supabase, context]); |
| 146 | + |
| 147 | + useEffect(() => { |
| 148 | + let ignore = false; |
| 149 | + void (async () => { |
| 150 | + if ( |
| 151 | + !ignore && |
| 152 | + schemas !== null && |
| 153 | + supabase !== null && |
| 154 | + context !== null |
| 155 | + ) { |
| 156 | + const spaceId = context.spaceId; |
| 157 | + try { |
| 158 | + setLoadingNodes(true); |
| 159 | + setNodes( |
| 160 | + await getNodes({ |
| 161 | + supabase, |
| 162 | + spaceId, |
| 163 | + schemaLocalIds: showingSchema.sourceLocalId, |
| 164 | + }), |
| 165 | + ); |
| 166 | + } catch (e) { |
| 167 | + setError((e as Error).message); |
| 168 | + console.error("getNodes failed", e); |
| 169 | + } finally { |
| 170 | + setLoadingNodes(false); |
| 171 | + } |
| 172 | + } |
| 173 | + })(); |
| 174 | + return () => { |
| 175 | + ignore = true; |
| 176 | + }; |
| 177 | + }, [schemas, showingSchema, context, supabase]); |
| 178 | + |
| 179 | + if (loading) { |
| 180 | + return ( |
| 181 | + <div className="p-3"> |
| 182 | + <Spinner /> |
| 183 | + <span className="mx-2">Loading admin data…</span> |
| 184 | + </div> |
| 185 | + ); |
| 186 | + } |
| 187 | + |
| 188 | + if (error) { |
| 189 | + return <p className="text-red-700">{error}</p>; |
| 190 | + } |
| 191 | + |
| 192 | + return ( |
| 193 | + <> |
| 194 | + <p> |
| 195 | + Context:{" "} |
| 196 | + <code>{JSON.stringify({ ...context, spacePassword: "****" })}</code> |
| 197 | + </p> |
| 198 | + {schemas.length > 0 ? ( |
| 199 | + <> |
| 200 | + <Label> |
| 201 | + Display: |
| 202 | + <div className="mx-2 inline-block"> |
| 203 | + <Select |
| 204 | + items={schemas} |
| 205 | + onItemSelect={(choice) => { |
| 206 | + setShowingSchema(choice); |
| 207 | + }} |
| 208 | + itemRenderer={(node, { handleClick, modifiers }) => ( |
| 209 | + <MenuItem |
| 210 | + active={modifiers.active} |
| 211 | + key={node.sourceLocalId} |
| 212 | + label={node.name} |
| 213 | + onClick={handleClick} |
| 214 | + text={node.name} |
| 215 | + /> |
| 216 | + )} |
| 217 | + > |
| 218 | + <Button text={showingSchema.name} /> |
| 219 | + </Select> |
| 220 | + </div> |
| 221 | + </Label> |
| 222 | + <div>{loadingNodes ? <Spinner /> : <NodeTable nodes={nodes} />}</div> |
| 223 | + </> |
| 224 | + ) : ( |
| 225 | + <p>No node schemas found</p> |
| 226 | + )} |
| 227 | + </> |
| 228 | + ); |
| 229 | +}; |
| 230 | + |
| 231 | +export default AdminPanel; |
0 commit comments