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: toggle hardcoded schema versions #5

Merged
merged 2 commits into from
Dec 10, 2024
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
18 changes: 13 additions & 5 deletions api/useQuerySchemas.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
import { useQuery } from "@tanstack/react-query";
import { useQuery, UseQueryOptions } from "@tanstack/react-query";
import axios from "axios";

const DIGITAL_PLANNING_DATA_SCHEMAS_JSON_URL =
"https://theopensystemslab.github.io/digital-planning-data-schemas/v0.7.0/schema.json";
"https://theopensystemslab.github.io/digital-planning-data-schemas";

const useQuerySchemas = () => {
// TODO: type the version more strictly
const useQuerySchemas = (
version: string,
options?: Readonly<Omit<UseQueryOptions<unknown>, "queryKey" | "queryFn">>
) => {
const fetchFn = async (url: string) => {
const response = await axios.get(url);
return response.data;
};

return useQuery({
queryKey: ["schemas"],
queryFn: () => fetchFn(DIGITAL_PLANNING_DATA_SCHEMAS_JSON_URL),
queryKey: ["schemas", version],
queryFn: () =>
fetchFn(
`${DIGITAL_PLANNING_DATA_SCHEMAS_JSON_URL}/${version}/schema.json`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So something tricky to highlight here - since the latest release (v0.7.1), this URL now accounts for a /schemas subdirectory and each released version may have multiple schemas published within in (eg application.json, prototypeApplication.json, preApplication.json, decision.json and so on).

In my mind, the meaningful things to toggle here are:

  • Which schema: Application, PreApplication, PrototypeApplication, so on
  • Which version: "latest" or "next" only?
    • eg this would have tech benefit of letting us deal with same base URL then and just sort of ignore very early versions, which probably aren't as important to visually document? Main purpose of this viewer in my mind is to share with consumers to help explain what they can recieve now and what changes are queued up! Older version in /dist are nice audit history but no longer in active use for any integrations!
    • I know we need to smooth out latest & @next dist folders though! See Smooth out CI build process for @next & latest /dist branch folders digital-planning-data-schemas#242)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok cool! There's some groundwork here which will apply to future work but sounds like next steps are:

  • add another toggle for schema type
  • fix the smooth out CI build issue
  • Change the current version toggle to just include next and latest branches

Let me know if there is anything that should be changed implementation wise for this current PR, otherwise I feel like it could be merged as an incremental step towards the above goals 🥅

),
staleTime: 1000 * 60 * 10, // cache for 10 minutes
...options,
});
};

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@tanstack/react-query": "^5.59.15",
"@types/prismjs": "^1.26.4",
"axios": "^1.7.7",
"formik": "^2.4.6",
"prismjs": "^1.29.0",
"react": "^18.3.1",
"react-dom": "^18.3.1"
Expand Down
43 changes: 43 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 84 additions & 44 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,58 +1,98 @@
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";
import { useQueryClient } from "@tanstack/react-query";

import { JsonSchemaViewer } from "@stoplight/json-schema-viewer";
import { Provider as MosaicProvider, injectStyles } from "@stoplight/mosaic";
import { injectStyles } from "@stoplight/mosaic";
import { useFormik } from "formik";
import useQuerySchemas from "./../api/useQuerySchemas";

import InputLabel from "@mui/material/InputLabel";
import MenuItem from "@mui/material/MenuItem";
import Select from "@mui/material/Select";
import { useState } from "react";
import Header from "./components/Header";

// TODO: grab this programatically instead of hard-coding
const LATEST_WORKING_VERSION = "v0.7.0";

const App = () => {
injectStyles();
const queryClient = useQueryClient();
const [selectedVersion, setSelectedVersion] = useState(
LATEST_WORKING_VERSION
);

const { data: schema, isLoading, isError } = useQuerySchemas();
const {
data: schema,
isLoading,
isError,
} = useQuerySchemas(selectedVersion, { retry: false });

const formik = useFormik({
initialValues: {
version: selectedVersion,
},
onSubmit: (values) => setSelectedVersion(values.version),
});

return (
<Box py={2} style={{ maxWidth: 1000 }} mx="auto">
<Typography textAlign="center" variant="h2">
Digital planning data schemas
</Typography>
<Typography pt={4} maxWidth={800} mx='auto'>
Digital Planning Data schemas aim to encourage more interoperability and
consistency between systems by offering a central, version controlled
specification for documenting and validating planning data.
</Typography>
<MosaicProvider>
<Box
p={6}
my={4}
maxWidth={800}
style={{
borderRadius: 2,
backgroundColor: "#fff5ef",
}}
mx="auto"
>
{isLoading ? (
<Typography>Loading...</Typography>
) : (
<>
<Typography pb={2}>{schema.$id}</Typography>
<JsonSchemaViewer
name="Digital planning data schemas"
schema={schema}
hideTopBar={false}
emptyText="No schema defined"
expanded={true}
defaultExpandedDepth={0}
renderRootTreeLines={true}
/>
</>
)}
{isError && (
<Typography pt={4}>Sorry, please try again later.</Typography>
<Header>
{isLoading ? (
<Typography>Loading...</Typography>
) : (
<>
<form onSubmit={formik.handleSubmit}>
<InputLabel shrink id="select-version-label">
Version
</InputLabel>
<Select
sx={{ marginBottom: 4 }}
labelId="select-version-label"
defaultValue={selectedVersion}
id="select-version"
onChange={(e) => {
queryClient.invalidateQueries({
queryKey: ["schemas", selectedVersion],
});
formik.setFieldValue("version", e.target.value, false);
formik.submitForm();
}}
value={formik.values.version}
>
// TODO: add query to populate with a list of possible versions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

<MenuItem value={"v0.7.1"}>v0.7.1</MenuItem>
<MenuItem value={"v0.7.0"}>v0.7.0</MenuItem>
<MenuItem value={"v0.6.0"}>v0.6.0</MenuItem>
<MenuItem value={"v0.5.0"}>v0.5.0</MenuItem>
<MenuItem value={"v0.4.0"}>v0.4.0</MenuItem>
<MenuItem value={"v0.4.1"}>v0.4.1</MenuItem>
<MenuItem value={"v0.3.0"}>v0.3.0</MenuItem>
<MenuItem value={"v0.2.3"}>v0.2.3</MenuItem>
<MenuItem value={"v0.2.2"}>v0.2.2</MenuItem>
<MenuItem value={"v0.2.1"}>v0.2.1</MenuItem>
<MenuItem value={"v0.2.0"}>v0.2.0</MenuItem>
<MenuItem value={"v0.1.2"}>v0.1.2</MenuItem>
<MenuItem value={"v0.1.1"}>v0.1.1</MenuItem>
<MenuItem value={"v0.1.0"}>v0.1.0</MenuItem>
</Select>
</form>
{!isError && (
<JsonSchemaViewer
name="Digital planning data schemas"
schema={schema}
hideTopBar={false}
emptyText="No schema defined"
expanded={true}
defaultExpandedDepth={0}
renderRootTreeLines={true}
/>
)}
</Box>
</MosaicProvider>
</Box>
</>
)}
{isError && (
<Typography pt={4}>Sorry, please try again later.</Typography>
)}
</Header>
);
};

Expand Down
39 changes: 39 additions & 0 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Box from "@mui/material/Box";
import Typography from "@mui/material/Typography";

import { Provider as MosaicProvider, injectStyles } from "@stoplight/mosaic";

import { ComponentProps } from "react";

const Header = ({ children }: ComponentProps<"header">) => {
injectStyles();

return (
<Box p={2} style={{ maxWidth: 1000 }} mx="auto">
<Typography textAlign="center" variant="h2">
Digital planning data schemas
</Typography>
<Typography pt={4} maxWidth={800} mx="auto">
Digital Planning Data schemas aim to encourage more interoperability and
consistency between systems by offering a central, version controlled
specification for documenting and validating planning data.
</Typography>
<MosaicProvider>
<Box
p={6}
my={4}
maxWidth={800}
style={{
borderRadius: 2,
backgroundColor: "#fff5ef",
}}
mx="auto"
>
{children}
</Box>
</MosaicProvider>
</Box>
);
};

export default Header;