Skip to content

Commit

Permalink
Fix slug
Browse files Browse the repository at this point in the history
  • Loading branch information
djohalo2 authored and tstikvoort committed Jul 12, 2024
1 parent d028be0 commit 1aaf9b5
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/components/PageTreeViewItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const PageTreeViewItem = ({
onClick={onItemClick}>
<Flex align="center" gap={3}>
<UrlText isDisabled={isDisabled} textOverflow="ellipsis">
{parentPath ? page.slug?.current : getRootPageSlug(page, config)}
{parentPath ? page.slug?.current : getRootPageSlug(page, config) ?? '/'}
</UrlText>
{!isDisabled && (isHovered || hasActionOpen) && (
<PageTreeViewItemActions
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export const getLanguageFieldName = (config: PageTreeConfig) =>
config.documentInternationalization?.languageFieldName ?? 'language';

export const getRootPageSlug = (page: RawPageMetadata, config: PageTreeConfig) => {
if (!config.documentInternationalization) return '/';
if (!config.documentInternationalization) return;

const language = page[getLanguageFieldName(config)];
if (typeof language != 'string') {
throw new Error(`Language field is not a string: ${language}`);
}
return `${language}`;
return language;
};
7 changes: 5 additions & 2 deletions src/helpers/page-tree.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { PageTreeConfig, PageTreeItem, RawPageMetadata } from '../types';
import { NestedPageTreeItem, PageTreeConfig, RawPageMetadata } from '../types';
import { findPageTreeItemById, flatMapPageTree, getAllPageMetadata, mapRawPageMetadatasToPageTree } from './page-tree';

const config: PageTreeConfig = {
Expand Down Expand Up @@ -41,7 +41,7 @@ const rawChildContentPage: RawPageMetadata = {

const rawPages: RawPageMetadata[] = [rawHomePage, rawParentContentPage, rawChildContentPage];

const pageTree: PageTreeItem[] = [
const pageTree: NestedPageTreeItem[] = [
{
...rawHomePage,
isDraft: false,
Expand All @@ -59,6 +59,7 @@ const pageTree: PageTreeItem[] = [
isDraft: false,
isPublished: true,
path: '/en/parent/child',
children: [],
},
],
},
Expand Down Expand Up @@ -140,6 +141,7 @@ describe('Page tree helpers', () => {
isDraft: true,
isPublished: true,
path: '/en',
children: [],
},
]);
});
Expand Down Expand Up @@ -177,6 +179,7 @@ describe('Page tree helpers', () => {
isDraft: false,
isPublished: true,
path: '/en/parent/child',
children: [],
});
});

Expand Down
2 changes: 1 addition & 1 deletion src/helpers/page-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ const mapPageTreeItems = (
return getChildPages(parentId).map(page => {
const pagePath = parentPath
? `${parentPath === '/' ? '' : parentPath}/${page.slug?.current}`
: getRootPageSlug(page, config);
: `/${getRootPageSlug(page, config) ?? ''}`;
const children = orderBy(mapPageTreeItems(config, pagesWithPublishedState, page._id, pagePath), 'path');

return {
Expand Down
1 change: 1 addition & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { defineConfig, UserConfig } from 'vitest/config';
export default defineConfig({
plugins: [react()] as UserConfig['plugins'],
test: {
include: ['src/**/*.test.ts'],
environment: 'jsdom',
},
resolve: {
Expand Down

0 comments on commit 1aaf9b5

Please sign in to comment.