Skip to content

Commit 3144b2f

Browse files
authored
Update SyncedBlocks to use new API (#2251)
1 parent 5805c24 commit 3144b2f

File tree

9 files changed

+24
-11
lines changed

9 files changed

+24
-11
lines changed

bun.lockb

0 Bytes
Binary file not shown.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
],
2121
"dependencies": {
2222
"@geist-ui/icons": "^1.0.2",
23-
"@gitbook/api": "^0.38.0",
23+
"@gitbook/api": "^0.39.0",
2424
"@radix-ui/react-checkbox": "^1.0.4",
2525
"@radix-ui/react-popover": "^1.0.7",
2626
"@sentry/nextjs": "^7.94.1",

src/app/(space)/(core)/~gitbook/pdf/page.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ async function PDFPageDocument(props: {
235235
spaceId: space.id,
236236
revisionId: refContext.revisionId,
237237
},
238+
contentRefContext: refContext,
238239
resolveContentRef: (ref) => resolveContentRef(ref, refContext),
239240
getId: (id) => pagePDFContainerId(page, id),
240241
}}

src/components/DocumentView/BlockSyncedBlock.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { DocumentBlockSyncedBlock } from '@gitbook/api';
22

3-
import { getSyncedBlock } from '@/lib/api';
3+
import { getSyncedBlockContent } from '@/lib/api';
44
import { resolveContentRefWithFiles } from '@/lib/references';
55

66
import { BlockProps } from './Block';
@@ -14,9 +14,14 @@ export async function BlockSyncedBlock(props: BlockProps<DocumentBlockSyncedBloc
1414
return null;
1515
}
1616

17-
const syncedBlock = await getSyncedBlock(
17+
// We can't resolve the synced block without an organization context.
18+
if (!context.contentRefContext) {
19+
return null;
20+
}
21+
22+
const syncedBlock = await getSyncedBlockContent(
1823
apiToken,
19-
block.data.ref.organization,
24+
context.contentRefContext.space.organization,
2025
block.data.ref.syncedBlock,
2126
);
2227

src/components/DocumentView/CodeBlock/PlainCodeBlock.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export function PlainCodeBlock(props: { code: string; syntax: string }) {
4848
document={document}
4949
context={{
5050
mode: 'default',
51+
contentRefContext: null,
5152
resolveContentRef: async () => null,
5253
}}
5354
block={block}

src/components/DocumentView/DocumentView.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ContentRef, JSONDocument } from '@gitbook/api';
22

33
import { ContentTarget } from '@/lib/api';
4-
import { ResolveContentRefOptions, ResolvedContentRef } from '@/lib/references';
4+
import { ContentRefContext, ResolveContentRefOptions, ResolvedContentRef } from '@/lib/references';
55
import { ClassValue } from '@/lib/tailwind';
66

77
import { Blocks } from './Blocks';
@@ -18,6 +18,12 @@ export interface DocumentContext {
1818
*/
1919
content?: ContentTarget;
2020

21+
/**
22+
* The context for resolving content refs.
23+
* If null, content refs cannot be resolved.
24+
*/
25+
contentRefContext: ContentRefContext | null;
26+
2127
/**
2228
* Resolve a content reference.
2329
*/

src/components/PageBody/PageBody.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export function PageBody(props: {
7070
context={{
7171
mode: 'default',
7272
content: contentTarget,
73+
contentRefContext: context,
7374
resolveContentRef: (ref, options) =>
7475
resolveContentRef(ref, context, options),
7576
}}

src/components/Search/server-actions.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ function transformAnswer(
125125
document={answer.answer.document}
126126
context={{
127127
mode: 'default',
128+
contentRefContext: null,
128129
resolveContentRef: async () => null,
129130
}}
130131
style={['space-y-5']}

src/lib/api.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,16 @@ export const getUserById = cache(
152152
/**
153153
* Get a synced block by its ref.
154154
*/
155-
export const getSyncedBlock = cache(
156-
'api.getSyncedBlock',
155+
export const getSyncedBlockContent = cache(
156+
'api.getSyncedBlockContent',
157157
async (
158158
apiToken: string,
159159
organizationId: string,
160160
syncedBlockId: string,
161161
options: CacheFunctionOptions,
162162
) => {
163163
try {
164-
const response = await apiWithToken(apiToken).orgs.getSyncedBlock(
164+
const response = await apiWithToken(apiToken).orgs.getSyncedBlockContent(
165165
organizationId,
166166
syncedBlockId,
167167
{
@@ -174,7 +174,6 @@ export const getSyncedBlock = cache(
174174
tags: [
175175
getAPICacheTag({
176176
tag: 'synced-block',
177-
organization: organizationId,
178177
syncedBlock: syncedBlockId,
179178
}),
180179
],
@@ -835,7 +834,6 @@ export function getAPICacheTag(
835834
| {
836835
tag: 'synced-block';
837836
syncedBlock: string;
838-
organization: string;
839837
},
840838
): string {
841839
switch (spec.tag) {
@@ -846,7 +844,7 @@ export function getAPICacheTag(
846844
case 'collection':
847845
return `collection:${spec.collection}`;
848846
case 'synced-block':
849-
return `synced-block:${spec.organization}:${spec.syncedBlock}`;
847+
return `synced-block:${spec.syncedBlock}`;
850848
default:
851849
assertNever(spec);
852850
}

0 commit comments

Comments
 (0)