Skip to content

Commit 7bd27b2

Browse files
committed
feat(clerk): expose resolved ledgerId from token strategy
1 parent 9a807f6 commit 7bd27b2

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

use-fireproof/clerk/clerk-token-strategy.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class ClerkTokenStrategy implements TokenStrategie {
1111
private dashApi: DashboardApiImpl<unknown>;
1212
private apiUrl: string;
1313
private lastExpiryMs: number | null = null;
14+
private resolvedLedgerId: string | null = null;
1415

1516
constructor(dashApi: DashboardApiImpl<unknown>, apiUrl: string) {
1617
this.dashApi = dashApi;
@@ -25,6 +26,14 @@ export class ClerkTokenStrategy implements TokenStrategie {
2526
return this.lastExpiryMs;
2627
}
2728

29+
/**
30+
* Get the resolved ledger ID from the last ensureCloudToken call.
31+
* Returns null if no token has been obtained yet.
32+
*/
33+
getLedgerId(): string | null {
34+
return this.resolvedLedgerId;
35+
}
36+
2837
hash(): string {
2938
return this.apiUrl;
3039
}
@@ -85,6 +94,9 @@ export class ClerkTokenStrategy implements TokenStrategie {
8594
if (res.expiresDate) {
8695
this.lastExpiryMs = new Date(res.expiresDate).getTime();
8796
}
97+
if (res.ledger) {
98+
this.resolvedLedgerId = res.ledger;
99+
}
88100
return {
89101
token: res.cloudToken,
90102
...res,

use-fireproof/clerk/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export interface UseFireproofClerkResult extends UseFireproof {
4141
syncStatus: SyncStatus;
4242
/** Last sync error if any */
4343
lastSyncError?: Error;
44+
/** Resolved ledger ID from the dashboard (available after attach) */
45+
ledgerId: string | null;
4446
}
4547

4648
export interface ClerkContextValue {

use-fireproof/clerk/use-fireproof-clerk.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function useFireproofClerk(name: string | Database): UseFireproofClerkRes
5151
const [attachState, setAttachState] = useState<AttachState>({ status: "detached" });
5252
const [syncStatus, setSyncStatus] = useState<SyncStatus>("idle");
5353
const [lastSyncError, setLastSyncError] = useState<Error | undefined>(undefined);
54+
const [ledgerId, setLedgerId] = useState<string | null>(null);
5455

5556
const attachingRef = useRef(false);
5657
const refreshTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
@@ -125,6 +126,7 @@ export function useFireproofClerk(name: string | Database): UseFireproofClerkRes
125126
}
126127

127128
retryCountRef.current = 0;
129+
setLedgerId(strategy.getLedgerId());
128130
setAttachState({ status: "attached", attached });
129131
setSyncStatus("synced");
130132
setLastSyncError(undefined);
@@ -170,6 +172,7 @@ export function useFireproofClerk(name: string | Database): UseFireproofClerkRes
170172
}
171173

172174
retryCountRef.current = 0;
175+
setLedgerId(strategy.getLedgerId());
173176
setAttachState({ status: "attached", attached });
174177
setSyncStatus("synced");
175178
setLastSyncError(undefined);
@@ -351,5 +354,6 @@ export function useFireproofClerk(name: string | Database): UseFireproofClerkRes
351354
isSyncing: attachState.status === "attached",
352355
syncStatus,
353356
lastSyncError,
357+
ledgerId,
354358
};
355359
}

0 commit comments

Comments
 (0)