Skip to content

Commit f9f59d5

Browse files
committed
feat: load body by url slug
1 parent 5146fbb commit f9f59d5

File tree

13 files changed

+30
-34
lines changed

13 files changed

+30
-34
lines changed

apps/book-server/src/common/plugins/global/errorHandlerPlugin.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const errorHandlerPlugin: FastifyPluginCallback = (fastify, _, done) => {
3434
})
3535
fastify.setErrorHandler((error, request, reply) => {
3636
if (isHttpError(error)) {
37+
console.log('isHttpError', error)
3738
reply.status(error.statusCode).send({
3839
message: error.message,
3940
name: error.name,

apps/book-server/src/graphql/Book.gql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type Query {
1111
}
1212

1313
type Mutation {
14-
deploy(input: BookIDInput!): Void! @auth
15-
build(input: BookIDInput!): Void! @auth
14+
deploy(input: BookIDInput!): Void @auth
15+
build(input: BookIDInput!): Void @auth
1616
}
1717

1818
type Subscription {

apps/book-server/src/graphql/Page.gql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type Query {
2828

2929
type Mutation {
3030
create(input: CreatePageInput!): Page @auth
31-
reorder(input: ReorderInput!): Void! @auth
31+
reorder(input: ReorderInput!): Void @auth
3232
}
3333

3434
input GetPagesInput {

apps/book-server/src/graphql/generated.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ export type GetPagesInput = {
6161
}
6262

6363
export type Mutation = {
64-
build: Scalars['Void']['output']
64+
build?: Maybe<Scalars['Void']['output']>
6565
create?: Maybe<Page>
66-
deploy: Scalars['Void']['output']
67-
reorder: Scalars['Void']['output']
66+
deploy?: Maybe<Scalars['Void']['output']>
67+
reorder?: Maybe<Scalars['Void']['output']>
6868
}
6969

7070
export type MutationBuildArgs = {
@@ -322,7 +322,7 @@ export type MutationResolvers<
322322
ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation'],
323323
> = {
324324
build?: Resolver<
325-
ResolversTypes['Void'],
325+
Maybe<ResolversTypes['Void']>,
326326
ParentType,
327327
ContextType,
328328
RequireFields<MutationBuildArgs, 'input'>
@@ -334,13 +334,13 @@ export type MutationResolvers<
334334
RequireFields<MutationCreateArgs, 'input'>
335335
>
336336
deploy?: Resolver<
337-
ResolversTypes['Void'],
337+
Maybe<ResolversTypes['Void']>,
338338
ParentType,
339339
ContextType,
340340
RequireFields<MutationDeployArgs, 'input'>
341341
>
342342
reorder?: Resolver<
343-
ResolversTypes['Void'],
343+
Maybe<ResolversTypes['Void']>,
344344
ParentType,
345345
ContextType,
346346
RequireFields<MutationReorderArgs, 'input'>

apps/book-server/src/services/PageService/index.mts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,6 @@ export class PageService implements Service {
131131
}
132132

133133
public async getPage(input: GetPageInput, signedWriterId?: string): Promise<Page | null> {
134-
console.log('input', input)
135-
console.log('signedWriterId', signedWriterId)
136134
if (!signedWriterId) {
137135
throw new UnauthorizedError('Not authorized')
138136
}
@@ -213,6 +211,7 @@ export class PageService implements Service {
213211
}
214212

215213
public async reorder(input: ReorderInput, signedWriterId?: string): Promise<void> {
214+
console.log('input', input)
216215
if (!signedWriterId) {
217216
throw new UnauthorizedError('Not authorized')
218217
}

apps/book-web/src/layouts/NextraLayout/NextraLayout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ function NextraLayout({ children, mdxText }: Props) {
103103
index,
104104
},
105105
})
106+
getPagesRefetch()
106107
}
107108

108109
window.addEventListener(nextraCustomEventName.changeItem, changeItem)

apps/book-web/src/lib/generateBookMetadata.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const generatePageMap = (pages: Pages, bookUrl: string) => {
5555
// save key
5656
map.set(page.url_slug, key)
5757
if (page.type === 'separator') {
58-
Object.assign(value, { id: page.id, type: 'separator' })
58+
Object.assign(value, { id: page.url_slug, type: 'separator' })
5959
}
6060
acc[key] = value
6161
return acc
@@ -133,16 +133,6 @@ const generatePageMap = (pages: Pages, bookUrl: string) => {
133133
export const generateBookMetadata = ({ pages, bookUrl }: Args): BookMetadata => {
134134
const pageMap = generatePageMap(pages, bookUrl)
135135

136-
// if (global.window === undefined) {
137-
// const fs = require('fs')
138-
// const path = require('path')
139-
140-
// fs.writeFileSync(
141-
// path.resolve(process.cwd(), './src/lib/', './context.json'),
142-
// JSON.stringify(pageMap, null, 4),
143-
// )
144-
// }
145-
146136
return {
147137
pageOpts: {
148138
frontMatter: {},

apps/book-web/src/pages/[username]/[bookTitle]/[[...pageUrlSlug]].tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,18 @@ export default Home
2727
export const getServerSideProps = (async (ctx) => {
2828
const { params, req } = ctx
2929
const bookUrlSlug = `/${params?.username}/${params?.bookTitle}`
30-
const pageUrlSlug = `/${params?.pageUrlSlug.join('/')}` || '/'
3130

32-
const pages = await getPages(bookUrlSlug, req.cookies)
31+
let pageUrlSlug = params?.pageUrlSlug ? `/${params?.pageUrlSlug?.join('/')}` : ''
3332

34-
if (pages) {
35-
generateBookMetadata({ pages: pages, bookUrl: bookUrlSlug })
33+
if (!pageUrlSlug) {
34+
const pages = await getPages(bookUrlSlug, req.cookies)
35+
36+
if (!pages) {
37+
throw new Error('failed get book metadata')
38+
}
39+
pageUrlSlug = pages[0].url_slug
3640
}
41+
3742
const page = await getPage(bookUrlSlug, pageUrlSlug, req.cookies)
3843
const mdxText = page?.body ?? ''
3944
// const mdxText = `

packages/nextra-editor/src/components/sidebar/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ export function Sidebar({
104104
[docsDirectories],
105105
)
106106

107-
108-
109107
useEffect(() => {
110108
setSortableItems(initSotableItems)
111109
}, [initSotableItems])

packages/nextra-editor/src/components/sidebar/sortable-tree/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ function SortableTree({ items, sidebarRef, showSidebar, onItemsChanged }: Props)
232232
{
233233
detail: {
234234
bookUrlSlug,
235-
targetUrlSlug: activeTreeItem.id.replace(bookUrlSlug, ''),
235+
targetUrlSlug: `${activeTreeItem.id.replace(bookUrlSlug, '')}`,
236236
parentUrlSlug: String(parentId).replace(bookUrlSlug, ''),
237237
index: newParentItem
238238
? newParentItem.childrenIds.findIndex((id) => id === activeTreeItem.id)

0 commit comments

Comments
 (0)