noogle/pages/index.tsx
Johannes Kirschbauer 8679af38a2
feature/deeplinks
* add: deeplinks
* add: enhanced pagination for large datasets
* add more meta tags
* add data to initial state
* remove preview page, instead link directly into the list
2023-01-24 21:02:17 +01:00

78 lines
2.2 KiB
TypeScript

import React, { useEffect, useState } from "react";
import {
initialPageState,
PageState,
InitialPageState,
} from "../models/internals";
import { PageContext, PageContextProvider } from "../components/pageContext";
import NixFunctions from "../components/NixFunctions";
import { NextRouter, useRouter } from "next/router";
import { LinearProgress } from "@mui/material";
const getInitialProps = async (context: NextRouter) => {
const { query } = context;
const initialProps = { ...initialPageState };
Object.entries(query).forEach(([key, value]) => {
if (value && !Array.isArray(value)) {
try {
const parsedValue = JSON.parse(value) as never;
const initialValue = initialPageState[key as keyof InitialPageState];
if (!initialValue || typeof parsedValue === typeof initialValue) {
initialProps[key as keyof InitialPageState] = JSON.parse(
value
) as never;
} else {
throw "Type of query param does not match the initial values type";
}
} catch (error) {
console.error("Invalid query:", { key, value, error });
}
}
});
const FOTD = Object.entries(query).length === 0;
return {
props: {
...initialProps,
FOTD,
},
};
};
export default function FunctionsPage() {
const router = useRouter();
const [initialProps, setInitialProps] = useState<PageState | null>(null);
useEffect(() => {
if (router.isReady) {
getInitialProps(router).then((r) => {
const { props } = r;
console.info("Url Query changed\n\nUpdating pageState with delta:", {
props,
});
setInitialProps((curr) => ({ ...curr, ...props }));
});
}
}, [router]);
return (
<>
{!initialProps ? (
<LinearProgress />
) : (
<PageContextProvider pageProps={initialProps}>
<PageContext.Consumer>
{(context) => (
<NixFunctions
pageState={context.pageState}
setPageStateVariable={context.setPageStateVariable}
/>
)}
</PageContext.Consumer>
</PageContextProvider>
)}
</>
);
}