Skip to content

Commit

Permalink
Update SyncedBlocks to use new API (#2251)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmerich authored Mar 29, 2024
1 parent 5805c24 commit 3144b2f
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
],
"dependencies": {
"@geist-ui/icons": "^1.0.2",
"@gitbook/api": "^0.38.0",
"@gitbook/api": "^0.39.0",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-popover": "^1.0.7",
"@sentry/nextjs": "^7.94.1",
Expand Down
1 change: 1 addition & 0 deletions src/app/(space)/(core)/~gitbook/pdf/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ async function PDFPageDocument(props: {
spaceId: space.id,
revisionId: refContext.revisionId,
},
contentRefContext: refContext,
resolveContentRef: (ref) => resolveContentRef(ref, refContext),
getId: (id) => pagePDFContainerId(page, id),
}}
Expand Down
11 changes: 8 additions & 3 deletions src/components/DocumentView/BlockSyncedBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DocumentBlockSyncedBlock } from '@gitbook/api';

import { getSyncedBlock } from '@/lib/api';
import { getSyncedBlockContent } from '@/lib/api';
import { resolveContentRefWithFiles } from '@/lib/references';

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

const syncedBlock = await getSyncedBlock(
// We can't resolve the synced block without an organization context.
if (!context.contentRefContext) {
return null;
}

const syncedBlock = await getSyncedBlockContent(
apiToken,
block.data.ref.organization,
context.contentRefContext.space.organization,
block.data.ref.syncedBlock,
);

Expand Down
1 change: 1 addition & 0 deletions src/components/DocumentView/CodeBlock/PlainCodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function PlainCodeBlock(props: { code: string; syntax: string }) {
document={document}
context={{
mode: 'default',
contentRefContext: null,
resolveContentRef: async () => null,
}}
block={block}
Expand Down
8 changes: 7 additions & 1 deletion src/components/DocumentView/DocumentView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ContentRef, JSONDocument } from '@gitbook/api';

import { ContentTarget } from '@/lib/api';
import { ResolveContentRefOptions, ResolvedContentRef } from '@/lib/references';
import { ContentRefContext, ResolveContentRefOptions, ResolvedContentRef } from '@/lib/references';
import { ClassValue } from '@/lib/tailwind';

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

/**
* The context for resolving content refs.
* If null, content refs cannot be resolved.
*/
contentRefContext: ContentRefContext | null;

/**
* Resolve a content reference.
*/
Expand Down
1 change: 1 addition & 0 deletions src/components/PageBody/PageBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export function PageBody(props: {
context={{
mode: 'default',
content: contentTarget,
contentRefContext: context,
resolveContentRef: (ref, options) =>
resolveContentRef(ref, context, options),
}}
Expand Down
1 change: 1 addition & 0 deletions src/components/Search/server-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function transformAnswer(
document={answer.answer.document}
context={{
mode: 'default',
contentRefContext: null,
resolveContentRef: async () => null,
}}
style={['space-y-5']}
Expand Down
10 changes: 4 additions & 6 deletions src/lib/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,16 @@ export const getUserById = cache(
/**
* Get a synced block by its ref.
*/
export const getSyncedBlock = cache(
'api.getSyncedBlock',
export const getSyncedBlockContent = cache(
'api.getSyncedBlockContent',
async (
apiToken: string,
organizationId: string,
syncedBlockId: string,
options: CacheFunctionOptions,
) => {
try {
const response = await apiWithToken(apiToken).orgs.getSyncedBlock(
const response = await apiWithToken(apiToken).orgs.getSyncedBlockContent(
organizationId,
syncedBlockId,
{
Expand All @@ -174,7 +174,6 @@ export const getSyncedBlock = cache(
tags: [
getAPICacheTag({
tag: 'synced-block',
organization: organizationId,
syncedBlock: syncedBlockId,
}),
],
Expand Down Expand Up @@ -835,7 +834,6 @@ export function getAPICacheTag(
| {
tag: 'synced-block';
syncedBlock: string;
organization: string;
},
): string {
switch (spec.tag) {
Expand All @@ -846,7 +844,7 @@ export function getAPICacheTag(
case 'collection':
return `collection:${spec.collection}`;
case 'synced-block':
return `synced-block:${spec.organization}:${spec.syncedBlock}`;
return `synced-block:${spec.syncedBlock}`;
default:
assertNever(spec);
}
Expand Down

0 comments on commit 3144b2f

Please sign in to comment.