Skip to content

Commit

Permalink
chore: playground form map additional properties (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
RohinBhargava authored Oct 25, 2024
1 parent a15d908 commit fdb259a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 57 deletions.
118 changes: 67 additions & 51 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 @@ -256,62 +303,31 @@ export const PlaygroundObjectPropertiesForm = memo<PlaygroundObjectPropertiesFor

{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
7 changes: 1 addition & 6 deletions servers/fdr/src/services/docs-cache/DocsDefinitionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,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

0 comments on commit fdb259a

Please sign in to comment.