Skip to content

Commit

Permalink
Merge pull request #2542 from quantified-uncertainty/VariableType
Browse files Browse the repository at this point in the history
Saving variableType and docstrings
  • Loading branch information
OAGr authored Nov 17, 2023
2 parents ae5df78 + eac623b commit 502d211
Show file tree
Hide file tree
Showing 13 changed files with 120 additions and 19 deletions.
11 changes: 8 additions & 3 deletions packages/components/src/components/SquigglePlayground/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ import { ResizableTwoPanelLayout } from "./ResizableTwoPanelLayout.js";

export type ModelExport = {
variableName: string;
variableType: string;
title?: string;
docstring: string;
};

/*
Expand Down Expand Up @@ -118,9 +120,12 @@ export const SquigglePlayground: React.FC<SquigglePlaygroundProps> = (
const _output = output.output?.output;
if (_output && _output.ok) {
const exports = _output.value.exports;
const _exports: ModelExport[] = exports.entries().map((e) => {
return { variableName: e[0], title: e[1].title() };
});
const _exports: ModelExport[] = exports.entries().map((e) => ({
variableName: e[0],
variableType: e[1].tag,
title: e[1].title(),
docstring: e[1].context?.docstring() || "",
}));
onExportsChange && onExportsChange(_exports);
} else {
onExportsChange && onExportsChange([]);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "ModelExport" ADD COLUMN "variableType" TEXT NOT NULL DEFAULT 'OTHER';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "ModelExport" ADD COLUMN "docstring" TEXT NOT NULL DEFAULT '';
2 changes: 2 additions & 0 deletions packages/hub/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ model ModelExport {
modelRevisionId String
variableName String
title String?
docstring String @default("")
variableType String @default("OTHER")
@@unique([modelRevisionId, variableName], name: "uniqueKey")
}
Expand Down
4 changes: 4 additions & 0 deletions packages/hub/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,12 @@ type ModelEdge {
}

type ModelExport implements Node {
docstring: String!
id: ID!
modelRevision: ModelRevision!
title: String
variableName: String!
variableType: String!
}

type ModelRevision implements Node {
Expand Down Expand Up @@ -546,8 +548,10 @@ type SquiggleErrorOutput implements SquiggleOutput {
}

input SquiggleModelExportInput {
docstring: String
title: String
variableName: String!
variableType: String!
}

type SquiggleOkOutput implements SquiggleOutput {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ export const EditSquiggleSnippetModel: FC<Props> = ({
exports {
id
variableName
variableType
title
docstring
}
relativeValuesExports {
id
Expand Down Expand Up @@ -177,6 +179,8 @@ export const EditSquiggleSnippetModel: FC<Props> = ({
exports: revision.exports.map((item) => ({
title: item.title,
variableName: item.variableName,
variableType: item.variableType,
docstring: item.docstring,
})),
};
}, [content, revision.relativeValuesExports, revision.exports]);
Expand Down
4 changes: 3 additions & 1 deletion packages/hub/src/app/models/[owner]/[slug]/ModelLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const Query = graphql`
exports {
id
variableName
variableType
title
}
relativeValuesExports {
Expand Down Expand Up @@ -79,8 +80,9 @@ export const ModelLayout: FC<
});

const modelExports = model.currentRevision.exports.map(
({ variableName, title }) => ({
({ variableName, variableType, title }) => ({
variableName,
variableType,
title: title || undefined,
})
);
Expand Down
14 changes: 10 additions & 4 deletions packages/hub/src/graphql/mutations/updateSquiggleSnippetModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const SquiggleSnippetContentInput = builder.inputType(
const SquiggleModelExportInput = builder.inputType("SquiggleModelExportInput", {
fields: (t) => ({
variableName: t.string({ required: true }),
variableType: t.string({ required: true }),
docstring: t.string({ required: false }),
title: t.string({ required: false }),
}),
});
Expand Down Expand Up @@ -158,10 +160,14 @@ builder.mutationField("updateSquiggleSnippetModel", (t) =>
},
exports: {
createMany: {
data: (input.exports ?? []).map(({ variableName, title }) => ({
variableName,
title: title ?? null,
})),
data: (input.exports ?? []).map(
({ variableName, variableType, docstring, title }) => ({
variableName,
variableType,
docstring: docstring ?? undefined,
title: title ?? null,
})
),
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions packages/hub/src/graphql/types/ModelRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ builder.prismaNode("ModelExport", {
fields: (t) => ({
modelRevision: t.relation("modelRevision"),
variableName: t.exposeString("variableName"),
variableType: t.exposeString("variableType"),
docstring: t.exposeString("docstring"),
title: t.exposeString("title", { nullable: true }),
}),
});
Expand Down
42 changes: 34 additions & 8 deletions packages/hub/src/lib/ExportsDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,29 @@ import { FormProvider, useFieldArray, useForm } from "react-hook-form";
import { graphql, useFragment } from "react-relay";

import {
ButtonWithDropdown,
BarChartIcon,
BookOpenIcon,
CalculatorIcon,
CodeBracketIcon,
CommentIcon,
CurlyBracketsIcon,
Dropdown,
DropdownMenu,
DropdownMenuActionItem,
DropdownMenuHeader,
DropdownMenuModalActionItem,
LinkIcon,
HashIcon,
ScaleIcon,
ShareIcon,
TextAreaFormField,
TextTooltip,
SquareBracketIcon,
TableCellsIcon,
} from "@quri/ui";
import { modelExportRoute, modelForRelativeValuesExportRoute } from "@/routes";
import { DropdownMenuNextLinkItem } from "@/components/ui/DropdownMenuNextLinkItem";

type ModelExport = { variableName: string; title?: string };
type ModelExport = {
title?: string;
variableName: string;
variableType: string;
};
type RelativeValuesExport = { slug: string; variableName: string };

const nonRelativeValuesExports = (
Expand All @@ -42,6 +48,26 @@ export const totalImportLength = (
nonRelativeValuesExports(modelExports, relativeValuesExports).length +
relativeValuesExports.length;

// I assume it would be appropriate to move this elsewhere, once we need it elsewhere.
const typeIcon = (type: string) => {
switch (type) {
case "Number":
return HashIcon;
case "Array":
return SquareBracketIcon;
case "Dict":
return CurlyBracketsIcon;
case "Lambda":
return CodeBracketIcon;
case "TableChart":
return TableCellsIcon;
case "Calculator":
return CalculatorIcon;
default:
return ShareIcon;
}
};

export const ExportsDropdown: FC<
PropsWithChildren<{
modelExports: ModelExport[];
Expand All @@ -68,7 +94,7 @@ export const ExportsDropdown: FC<
variableName: exportItem.variableName,
})}
title={`${exportItem.title || exportItem.variableName}`}
icon={ShareIcon}
icon={typeIcon(exportItem.variableType)}
close={close}
/>
))}{" "}
Expand Down
6 changes: 3 additions & 3 deletions packages/hub/src/models/components/ModelCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const Fragment = graphql`
currentRevision {
exports {
variableName
variableType
title
}
relativeValuesExports {
Expand All @@ -40,17 +41,16 @@ type Props = {

export const ModelCard: FC<Props> = ({ modelRef, showOwner = true }) => {
const model = useFragment(Fragment, modelRef);
const rvExports = model.currentRevision.relativeValuesExports;
const exports = model.currentRevision.exports;

const modelUrl = modelRoute({
owner: model.owner.slug,
slug: model.slug,
});

const modelExports = model.currentRevision.exports.map(
({ variableName, title }) => ({
({ variableName, variableType, title }) => ({
variableName,
variableType,
title: title || undefined,
})
);
Expand Down
42 changes: 42 additions & 0 deletions packages/ui/src/icons/HeroIcons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -359,3 +359,45 @@ export const ShareIcon: FC<IconProps> = (props) => (
/>
</Icon>
);

export const HashIcon: FC<IconProps> = (props) => (
<Icon {...props} viewBox="0 0 24 24">
<path
fillRule="evenodd"
d="M11.097 1.515a.75.75 0 01.589.882L10.666 7.5h4.47l1.079-5.397a.75.75 0 111.47.294L16.665 7.5h3.585a.75.75 0 010 1.5h-3.885l-1.2 6h3.585a.75.75 0 010 1.5h-3.885l-1.08 5.397a.75.75 0 11-1.47-.294l1.02-5.103h-4.47l-1.08 5.397a.75.75 0 01-1.47-.294l1.02-5.103H3.75a.75.75 0 110-1.5h3.885l1.2-6H5.25a.75.75 0 010-1.5h3.885l1.08-5.397a.75.75 0 01.882-.588zM10.365 9l-1.2 6h4.47l1.2-6h-4.47z"
clipRule="evenodd"
/>
</Icon>
);

export const CalculatorIcon: FC<IconProps> = (props) => (
<Icon {...props} viewBox="0 0 24 24">
<path
fillRule="evenodd"
d="M6.32 1.827a49.255 49.255 0 0111.36 0c1.497.174 2.57 1.46 2.57 2.93V19.5a3 3 0 01-3 3H6.75a3 3 0 01-3-3V4.757c0-1.47 1.073-2.756 2.57-2.93zM7.5 11.25a.75.75 0 01.75-.75h.008a.75.75 0 01.75.75v.008a.75.75 0 01-.75.75H8.25a.75.75 0 01-.75-.75v-.008zm.75 1.5a.75.75 0 00-.75.75v.008c0 .414.336.75.75.75h.008a.75.75 0 00.75-.75V13.5a.75.75 0 00-.75-.75H8.25zm-.75 3a.75.75 0 01.75-.75h.008a.75.75 0 01.75.75v.008a.75.75 0 01-.75.75H8.25a.75.75 0 01-.75-.75v-.008zm.75 1.5a.75.75 0 00-.75.75v.008c0 .414.336.75.75.75h.008a.75.75 0 00.75-.75V18a.75.75 0 00-.75-.75H8.25zm1.748-6a.75.75 0 01.75-.75h.007a.75.75 0 01.75.75v.008a.75.75 0 01-.75.75h-.007a.75.75 0 01-.75-.75v-.008zm.75 1.5a.75.75 0 00-.75.75v.008c0 .414.335.75.75.75h.007a.75.75 0 00.75-.75V13.5a.75.75 0 00-.75-.75h-.007zm-.75 3a.75.75 0 01.75-.75h.007a.75.75 0 01.75.75v.008a.75.75 0 01-.75.75h-.007a.75.75 0 01-.75-.75v-.008zm.75 1.5a.75.75 0 00-.75.75v.008c0 .414.335.75.75.75h.007a.75.75 0 00.75-.75V18a.75.75 0 00-.75-.75h-.007zm1.754-6a.75.75 0 01.75-.75h.008a.75.75 0 01.75.75v.008a.75.75 0 01-.75.75h-.008a.75.75 0 01-.75-.75v-.008zm.75 1.5a.75.75 0 00-.75.75v.008c0 .414.336.75.75.75h.008a.75.75 0 00.75-.75V13.5a.75.75 0 00-.75-.75h-.008zm-.75 3a.75.75 0 01.75-.75h.008a.75.75 0 01.75.75v.008a.75.75 0 01-.75.75h-.008a.75.75 0 01-.75-.75v-.008zm.75 1.5a.75.75 0 00-.75.75v.008c0 .414.336.75.75.75h.008a.75.75 0 00.75-.75V18a.75.75 0 00-.75-.75h-.008zm1.748-6a.75.75 0 01.75-.75h.008a.75.75 0 01.75.75v.008a.75.75 0 01-.75.75h-.008a.75.75 0 01-.75-.75v-.008zm.75 1.5a.75.75 0 00-.75.75v.008c0 .414.336.75.75.75h.008a.75.75 0 00.75-.75V13.5a.75.75 0 00-.75-.75h-.008zm-8.25-6A.75.75 0 018.25 6h7.5a.75.75 0 01.75.75v.75a.75.75 0 01-.75.75h-7.5a.75.75 0 01-.75-.75v-.75zm9 9a.75.75 0 00-1.5 0V18a.75.75 0 001.5 0v-2.25z"
clipRule="evenodd"
/>
</Icon>
);

export const VariableIcon: FC<IconProps> = (props) => (
<Icon {...props} viewBox="0 0 24 24">
<path
fillRule="evenodd"
d="M19.253 2.292a.75.75 0 01.955.461A28.123 28.123 0 0121.75 12c0 3.266-.547 6.388-1.542 9.247a.75.75 0 11-1.416-.494c.94-2.7 1.458-5.654 1.458-8.753s-.519-6.054-1.458-8.754a.75.75 0 01.461-.954zm-14.227.013a.75.75 0 01.414.976A23.183 23.183 0 003.75 12c0 3.085.6 6.027 1.69 8.718a.75.75 0 01-1.39.563c-1.161-2.867-1.8-6-1.8-9.281 0-3.28.639-6.414 1.8-9.281a.75.75 0 01.976-.414zm4.275 5.052a1.5 1.5 0 012.21.803l.716 2.148L13.6 8.246a2.438 2.438 0 012.978-.892l.213.09a.75.75 0 11-.584 1.381l-.214-.09a.937.937 0 00-1.145.343l-2.021 3.033 1.084 3.255 1.445-.89a.75.75 0 11.786 1.278l-1.444.889a1.5 1.5 0 01-2.21-.803l-.716-2.148-1.374 2.062a2.437 2.437 0 01-2.978.892l-.213-.09a.75.75 0 01.584-1.381l.214.09a.938.938 0 001.145-.344l2.021-3.032-1.084-3.255-1.445.89a.75.75 0 11-.786-1.278l1.444-.89z"
clipRule="evenodd"
/>
</Icon>
);

export const SquareBracketIcon: FC<IconProps> = (props) => (
<Icon {...props} viewBox="0 0 24 24">
<path d="M5.2,5.2V20.4H8a1.2,1.2,0,0,1,0,2.4H4a1.200028,1.200028,0,0,1-1.2-1.2V4A1.200028,1.200028,0,0,1,4,2.8H8a1.2,1.2,0,0,1,0,2.4ZM21.6,2.8H17.6a1.2,1.2,0,0,0,0,2.4h2.8V20.4H17.6a1.2,1.2,0,0,0,0,2.4h4a1.200028,1.200028,0,0,0,1.2-1.2V4A1.200028,1.200028,0,0,0,21.6,2.8Z" />
</Icon>
);

export const CurlyBracketsIcon: FC<IconProps> = (props) => (
<Icon {...props} viewBox="0 0 24 24">
<path d="M5.479785,11.948535A3.495033,3.495033,0,0,1,4.905078,12.8a3.495033,3.495033,0,0,1,.574707,0.851465C6,14.724414,6,15.98291,6,17.2c0,2.593652,0.184424,3.2,2,3.2a1.2,1.2,0,0,1,0,2.4c-1.914453,0-3.219775-0.690234-3.879785-2.051465C3.6,19.675586,3.6,18.41709,3.6,17.2c0-2.593652-0.184424-3.2-2-3.2a1.2,1.2,0,0,1,0-2.4c1.815576,0,2-0.606348,2-3.2,0-1.21709,0-2.475586,0.520215-3.51465C4.380225,3.490234,5.685547,2.8,7.6,2.8a1.2,1.2,0,0,1,0,2.4c-1.815576,0-2,0.606348-2,3.2C5.6,9.62291,5.6,10.881406,5.479785,11.948535ZM24,11.6c-1.815576,0-2-0.606348-2-3.2,0-1.21709,0-2.475586-0.520215-3.51465C20.197755,3.490234,19.144531,2.8,18,2.8a1.2,1.2,0,0,0,0,2.4c1.815576,0,2,0.606348,2,3.2,0,1.21709,0,2.475586,0.520215,3.51465A3.495033,3.495033,0,0,0,20.694922,12.8a3.495033,3.495033,0,0,0-.574707,0.851465C19.6,14.724414,19.6,15.98291,19.6,17.2c0,2.593652-0.184424,3.2-2,3.2a1.2,1.2,0,0,0,0,2.4c1.914453,0,3.219775-0.690234,3.879785-2.051465C22,19.675586,22,18.41709,22,17.2c0-2.593652,0.184424-3.2,2-3.2a1.2,1.2,0,0,0,0-2.4Z" />{" "}
</Icon>
);
5 changes: 5 additions & 0 deletions packages/ui/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,13 @@ export {
ScatterPlotIcon,
TableCellsIcon,
UserCircleIcon,
CalculatorIcon,
UserIcon,
WrenchIcon,
VariableIcon,
SquareBracketIcon,
CurlyBracketsIcon,
HashIcon,
} from "./icons/HeroIcons.js";
export type { IconProps } from "./icons/Icon.js";
export { PlusIcon } from "./icons/PlusIcon.js";
Expand Down

4 comments on commit 502d211

@vercel
Copy link

@vercel vercel bot commented on 502d211 Nov 17, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 502d211 Nov 17, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 502d211 Nov 17, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 502d211 Nov 17, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

quri-ui – ./packages/ui

quri-ui.vercel.app
quri-ui-quantified-uncertainty.vercel.app
quri-ui-git-main-quantified-uncertainty.vercel.app

Please sign in to comment.