1- import React from "react" ;
1+ import React , { useContext } from "react" ;
22// import { rawConnect } from "@fireproof/cloud";
33import { Link , useNavigate , useParams } from "react-router-dom" ;
4- import { useFireproof } from "use-fireproof" ;
4+ import { toCloud , useFireproof } from "use-fireproof" ;
5+ import { AppContext } from "../../../../app-context.jsx" ;
56import { Button } from "../../../../components/Button.jsx" ;
67import { DynamicTable } from "../../../../components/DynamicTable.jsx" ;
78import { headersForDocs } from "../../../../components/dynamicTableHelpers.js" ;
9+ import { SimpleTokenStrategy } from "../../../../../../core/gateways/cloud/to-cloud.js" ;
810// import { DEFAULT_ENDPOINT } from "../../../../helpers.js";
911
1012interface Document {
@@ -14,14 +16,33 @@ interface Document {
1416
1517export function LedgerDocuments ( ) {
1618 const { tenantId, ledgerId } = useParams ( ) ;
17- // const { cloud } = useContext(AppContext);
19+ const { cloud } = useContext ( AppContext ) ;
20+ const cloudToken = cloud . getCloudToken ( ) ;
21+
22+ // Wait for token before rendering the main component
23+ if ( cloudToken . isPending ) {
24+ return < div > Loading...</ div > ;
25+ }
26+ if ( ! cloudToken . data ) {
27+ return < div > Not found</ div > ;
28+ }
29+
30+ return < LedgerDocumentsContent tenantId = { tenantId } ledgerId = { ledgerId } token = { cloudToken . data . token } /> ;
31+ }
32+
33+ function LedgerDocumentsContent ( { tenantId, ledgerId, token } : { tenantId ?: string ; ledgerId ?: string ; token : string } ) {
1834 const navigate = useNavigate ( ) ;
1935
20- const { useLiveQuery, database } = useFireproof ( ledgerId || "" ) ;
21- // Connect to Fireproof Cloud
22- // if (database && ledgerId && tenantId) {
23- // rawConnect(database as any, `${tenantId}-${ledgerId}`, DEFAULT_ENDPOINT);
24- // }
36+ const { useLiveQuery, database, attach } = useFireproof ( ledgerId || "" , {
37+ attach : toCloud ( {
38+ urls : { base : "fpcloud://localhost:8787?protocol=ws" } ,
39+ tenant : tenantId ,
40+ ledger : ledgerId ,
41+ strategy : new SimpleTokenStrategy ( token ) ,
42+ } ) ,
43+ } ) ;
44+
45+ console . log ( attach . ctx ) ;
2546
2647 const allDocs = useLiveQuery < Document > ( "_id" ) ;
2748 const docs = allDocs . docs . filter ( ( doc ) => ! ! doc ) ;
0 commit comments