Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: portable text renderer basert på fremtind.no sitt oppsett #4632

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ny-portal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@mdx-js/react": "^2.3.0",
"@next/mdx": "^15.1.6",
"@portabletext/react": "^3.2.1",
"@portabletext/types": "^2.0.13",
"@sanity/client": "^6.28.1",
"@sanity/image-url": "^1.1.0",
"@sanity/ui": "^2.14.3",
Expand Down
11 changes: 3 additions & 8 deletions ny-portal/src/app/(frontend)/komponenter/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import PortableTextRenderer from "@/components/portable-text";
import { PropDocumentation } from "@/components/prop-documentation/PropDocumentation";
import { PortableText } from "@/components/portable-text/PortableText";
import { client } from "@/sanity/client";
import { componentPageBySlugQuery } from "@/sanity/queries/componentPage";

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

{data?.lede && <PortableTextRenderer value={data.lede} />}
{data?.lede && <PortableText blocks={data.lede} />}

{data?.content && <PortableTextRenderer value={data.content} />}

{data?.component_folder && (
<PropDocumentation component={data.component_folder} />
)}
{data?.content && <PortableText blocks={data.content} />}
</>
);
}
38 changes: 38 additions & 0 deletions ny-portal/src/components/portable-text/PortableText.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import type {
PortableTextComponentProps,
PortableTextReactComponents,
} from "@portabletext/react";
import { PortableText as PortableTextReact } from "@portabletext/react";
import type { TypedObject } from "@portabletext/types";
import type { FC } from "react";
import { PropsDocumentation } from "./props-documentation/PropsDocumentation";

interface Props {
blocks: TypedObject[] | null;
}

const portableTextComponents: Record<
string,
FC<PortableTextComponentProps<any>>
> = {
"jokul_component-props": PropsDocumentation,
};

export const baseComponentDefinition: Partial<PortableTextReactComponents> = {
types: portableTextComponents,
};

export const PortableText: FC<Props> = (props) => {
const components = {
...baseComponentDefinition,
types: {
...baseComponentDefinition.types,
},
};

if (props.blocks === null) {
return null;
}

return <PortableTextReact value={props.blocks} components={components} />;
};
20 changes: 0 additions & 20 deletions ny-portal/src/components/portable-text/index.tsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
import { readFile } from "node:fs/promises";
import { EOL } from "node:os";
import { resolve } from "node:path";
import { PortableTextTypeComponentProps } from "next-sanity";
import { FC } from "react";
import MissingTypesMessage from "./missing-types.mdx";
import { Props } from "./Props";

type Props = { component: string };
type PropsDocumentationSchema = {
component_folder: string;
};

export async function PropDocumentation({ component }: Props) {
export const PropsDocumentation: FC<
PortableTextTypeComponentProps<PropsDocumentationSchema>
> = async ({ value }) => {
const exampleFilePath = resolve(
process.cwd(),
"..",
"packages",
"jokul",
"src",
"components",
component,
value.component_folder,
"types.ts",
);

try {
const file = await readFile(exampleFilePath);

return (
<Props
props={
file.toString()
// .split(EOL)
// .filter((line) => !line.startsWith("import"))
// .join(EOL)
}
/>
);
return <Props props={file.toString()} />;
} catch (e) {
return <MissingTypesMessage />;
}
}
};
Loading
Loading