Skip to content

Commit e900786

Browse files
committed
feat: add portable-text renderer logic
Added portable-text related logic ISSUES CLOSED: #4631
1 parent 006e4ad commit e900786

File tree

13 files changed

+869
-729
lines changed

13 files changed

+869
-729
lines changed

ny-portal/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"@mdx-js/react": "^2.3.0",
2121
"@next/mdx": "^15.1.6",
2222
"@portabletext/react": "^3.2.1",
23+
"@portabletext/types": "^2.0.13",
2324
"@sanity/client": "^6.28.1",
2425
"@sanity/image-url": "^1.1.0",
2526
"@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,33 @@
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";
67

7-
type Props = { component: string };
8+
type PropsDocumentationSchema = {
9+
component_folder: string;
10+
};
811

9-
export async function PropDocumentation({ component }: Props) {
12+
export const PropsDocumentation: FC<
13+
PortableTextTypeComponentProps<PropsDocumentationSchema>
14+
> = async ({ value }) => {
1015
const exampleFilePath = resolve(
1116
process.cwd(),
1217
"..",
1318
"packages",
1419
"jokul",
1520
"src",
1621
"components",
17-
component,
22+
value.component_folder,
1823
"types.ts",
1924
);
2025

2126
try {
2227
const file = await readFile(exampleFilePath);
2328

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

0 commit comments

Comments
 (0)