Skip to content

Commit 083a9a5

Browse files
committed
feat: add portable-text renderer logic
Added portable-text related logic ISSUES CLOSED: #4631
1 parent 3e19034 commit 083a9a5

File tree

13 files changed

+913
-772
lines changed

13 files changed

+913
-772
lines changed

ny-portal/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"@payloadcms/richtext-lexical": "latest",
2828
"@payloadcms/ui": "latest",
2929
"@portabletext/react": "^3.2.1",
30+
"@portabletext/types": "^2.0.13",
3031
"@sanity/client": "^6.28.1",
3132
"@sanity/image-url": "^1.1.0",
3233
"@sanity/ui": "^2.14.3",

ny-portal/src/app/(frontend)/komponenter/[slug]/page.tsx

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import PortableTextRenderer from "@/components/portable-text";
2-
import { PropDocumentation } from "@/components/prop-documentation/PropDocumentation";
1+
import { PortableText } from "@/components/portable-text/PortableText";
32
import { client } from "@/sanity/client";
43
import { componentPageBySlugQuery } from "@/sanity/queries/componentPage";
54

@@ -16,13 +15,9 @@ export default async function Page({
1615
<>
1716
<div>Hei jeg er {data?.title || "ikke i databasen"}</div>
1817

19-
{data?.lede && <PortableTextRenderer value={data.lede} />}
18+
{data?.lede && <PortableText blocks={data.lede} />}
2019

21-
{data?.content && <PortableTextRenderer value={data.content} />}
22-
23-
{data?.component_folder && (
24-
<PropDocumentation component={data.component_folder} />
25-
)}
20+
{data?.content && <PortableText blocks={data.content} />}
2621
</>
2722
);
2823
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type {
2+
PortableTextComponentProps,
3+
PortableTextReactComponents,
4+
} from "@portabletext/react";
5+
import { PortableText as PortableTextReact } from "@portabletext/react";
6+
import type { TypedObject } from "@portabletext/types";
7+
import type { FC } from "react";
8+
import { PropsDocumentation } from "./props-documentation/PropsDocumentation";
9+
10+
interface Props {
11+
blocks: TypedObject[] | null;
12+
}
13+
14+
const portableTextComponents: Record<
15+
string,
16+
FC<PortableTextComponentProps<any>>
17+
> = {
18+
"jokul_component-props": PropsDocumentation,
19+
};
20+
21+
export const baseComponentDefinition: Partial<PortableTextReactComponents> = {
22+
types: portableTextComponents,
23+
};
24+
25+
export const PortableText: FC<Props> = (props) => {
26+
const components = {
27+
...baseComponentDefinition,
28+
types: {
29+
...baseComponentDefinition.types,
30+
},
31+
};
32+
33+
if (props.blocks === null) {
34+
return null;
35+
}
36+
37+
return <PortableTextReact value={props.blocks} components={components} />;
38+
};

ny-portal/src/components/portable-text/index.tsx

-20
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,30 @@
11
import { readFile } from "node:fs/promises";
2-
import { EOL } from "node:os";
32
import { resolve } from "node:path";
3+
import { PortableTextTypeComponentProps } from "next-sanity";
4+
import { FC } from "react";
45
import MissingTypesMessage from "./missing-types.mdx";
56
import { Props } from "./Props";
7+
import type { PropsDocumentation as PropsDocumentationSchema } from "@/models/portable-text/propsDocumentationModel";
68

7-
type Props = { component: string };
8-
9-
export async function PropDocumentation({ component }: Props) {
9+
export const PropsDocumentation: FC<
10+
PortableTextTypeComponentProps<PropsDocumentationSchema>
11+
> = async ({ value }) => {
1012
const exampleFilePath = resolve(
1113
process.cwd(),
1214
"..",
1315
"packages",
1416
"jokul",
1517
"src",
1618
"components",
17-
component,
19+
value.component_folder,
1820
"types.ts",
1921
);
2022

2123
try {
2224
const file = await readFile(exampleFilePath);
2325

24-
return (
25-
<Props
26-
props={
27-
file.toString()
28-
// .split(EOL)
29-
// .filter((line) => !line.startsWith("import"))
30-
// .join(EOL)
31-
}
32-
/>
33-
);
26+
return <Props props={file.toString()} />;
3427
} catch (e) {
3528
return <MissingTypesMessage />;
3629
}
37-
}
30+
};

0 commit comments

Comments
 (0)