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

chore: playground form map additional properties #1652

Merged
merged 13 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 11 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
120 changes: 68 additions & 52 deletions packages/ui/app/src/playground/form/PlaygroundObjectPropertyForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,60 @@ import { PlusCircle } from "iconoir-react";
import dynamic from "next/dynamic";
import { FC, memo, useCallback, useEffect, useMemo, useState } from "react";
import { renderTypeShorthandRoot } from "../../type-shorthand";
import { WithLabel } from "../WithLabel";
import { castToRecord, getEmptyValueForType, isExpandable } from "../utils";
import { PlaygroundMapForm } from "./PlaygroundMapForm";
import { PlaygroundTypeReferenceForm } from "./PlaygroundTypeReferenceForm";

const Markdown = dynamic(() => import("../../mdx/Markdown").then(({ Markdown }) => Markdown));

const ADD_ALL_KEY = "__FERN_ADD_ALL__" as const;

const ADDITIONAL_PROPERTIES_KEY_SHAPE = {
type: "primitive",
value: {
type: "string",
regex: undefined,
minLength: undefined,
maxLength: undefined,
default: undefined,
},
} as const;

const ADDITIONAL_PROPERTIES_VALUE_SHAPE = {
type: "primitive",
value: {
type: "string",
regex: undefined,
minLength: undefined,
maxLength: undefined,
default: undefined,
},
} as const;

// TODO: This is hardcoded for now, but change to dynamic type references, by setting value
const ADDITIONAL_PROPERTIES_DEFAULT_SHAPE = {
type: "alias",
value: {
type: "optional",
shape: {
type: "alias",
value: {
type: "map",
keyShape: {
type: "alias",
value: ADDITIONAL_PROPERTIES_KEY_SHAPE,
},
valueShape: {
type: "alias",
value: ADDITIONAL_PROPERTIES_VALUE_SHAPE,
},
},
},
default: undefined,
},
} as const;

interface PlaygroundObjectPropertyFormProps {
id: string;
property: ObjectProperty;
Expand Down Expand Up @@ -114,7 +161,7 @@ export const PlaygroundObjectPropertiesForm = memo<PlaygroundObjectPropertiesFor
const [additionalProperties, setAdditionalProperties] = useState<unknown>({});

const onChangeAdditionalObjectProperty = useCallback(
(key: string, newValue: unknown) => {
(newValue: unknown) => {
onChange((oldValue: unknown) => {
const oldObject = castToRecord(oldValue);
const val = castToRecord(newValue);
Expand Down Expand Up @@ -253,65 +300,34 @@ export const PlaygroundObjectPropertiesForm = memo<PlaygroundObjectPropertiesFor
/>
</FernDropdown>
)}

{extraProperties != null && (
<div className={cn("flex-1 shrink min-w-0 mt-8")}>
<PlaygroundObjectPropertyForm
id={"extraProperties"}
<WithLabel
property={{
key: PropertyKey("Optional Extra Properties"),
valueShape: {
type: "alias",
value: {
type: "optional",
shape: {
type: "alias",
value: {
type: "map",
keyShape: {
type: "alias",
value: {
type: "primitive",
value: {
type: "string",
regex: undefined,
minLength: undefined,
maxLength: undefined,
default: undefined,
},
},
},
valueShape: {
type: "alias",
value: {
type: "primitive",
value: {
type: "string",
regex: undefined,
minLength: undefined,
maxLength: undefined,
default: undefined,
},
},
},
},
},
default: undefined,
},
},
valueShape: ADDITIONAL_PROPERTIES_DEFAULT_SHAPE,
description: undefined,
availability: undefined,
}}
onChange={onChangeAdditionalObjectProperty}
value={Object.keys(castToRecord(value)).reduce((acc: Record<string, unknown>, key) => {
if (!properties.some((p) => p.key === key)) {
acc[key] = castToRecord(value)[key];
}
return acc;
}, {})}
value={"Optional Extra Properties"}
onRemove={() => onChangeAdditionalObjectProperty(undefined)}
types={types}
disabled={disabled}
/>
>
<PlaygroundMapForm
id="extraProperties"
keyShape={ADDITIONAL_PROPERTIES_KEY_SHAPE}
valueShape={ADDITIONAL_PROPERTIES_VALUE_SHAPE}
onChange={onChangeAdditionalObjectProperty}
value={Object.keys(castToRecord(value)).reduce((acc: Record<string, unknown>, key) => {
if (!properties.some((p) => p.key === key)) {
acc[key] = castToRecord(value)[key];
}
return acc;
}, {})}
types={types}
/>
</WithLabel>
</div>
)}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,7 @@ export class DocsDefinitionCacheImpl implements DocsDefinitionCache {

private async cacheResponse({ url, value }: { url: URL; value: CachedDocsResponse }): Promise<void> {
if (this.redisDocsCache) {
try {
await this.redisDocsCache.set({ url, value });
} catch (e) {
console.error("failed cache replaceDocsForInstanceId", new Error().stack, JSON.stringify(value).length);
throw e;
}
await this.redisDocsCache.set({ url, value });
}
this.localDocsCache.set({ url, value });
}
Expand Down
Loading