Skip to content

Commit

Permalink
Merged with main
Browse files Browse the repository at this point in the history
  • Loading branch information
OAGr committed Jan 10, 2024
2 parents 829d58e + e01dc48 commit 0b4d932
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 83 deletions.
4 changes: 3 additions & 1 deletion packages/hub/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ type ModelRevision implements Node {
content: ModelContent!
createdAtTimestamp: Float!
exports: [ModelExport!]!
forRelativeValues(input: ModelRevisionForRelativeValuesInput): RelativeValuesExport
forRelativeValues(input: ModelRevisionForRelativeValuesInput!): ModelRevisionForRelativeValuesResult!
id: ID!
model: Model!
relativeValuesExports: [RelativeValuesExport!]!
Expand All @@ -206,6 +206,8 @@ input ModelRevisionForRelativeValuesInput {
variableName: String!
}

union ModelRevisionForRelativeValuesResult = BaseError | NotFoundError | RelativeValuesExport

input ModelRevisionForRelativeValuesSlugOwnerInput {
owner: String!
slug: String!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,22 @@ import { BuildRelativeValuesCacheAction } from "./BuildRelativeValuesCacheAction
import { ClearRelativeValuesCacheAction } from "./ClearRelativeValuesCacheAction";

import { RelativeValuesDefinitionRevision$key } from "@/__generated__/RelativeValuesDefinitionRevision.graphql";
import { RelativeValuesModelRevision$data } from "@/__generated__/RelativeValuesModelRevision.graphql";
import { RelativeValuesExport$data } from "@/__generated__/RelativeValuesExport.graphql";

export const CacheMenu: FC<{
revision: RelativeValuesModelRevision$data;
relativeValuesExport: RelativeValuesExport$data;
isEditable: boolean;
}> = ({ revision, isEditable }) => {
if (!revision.forRelativeValues) {
throw new Error("Not found");
}

}> = ({ relativeValuesExport, isEditable }) => {
const definition = useFragment<RelativeValuesDefinitionRevision$key>(
RelativeValuesDefinitionRevisionFragment,
revision.forRelativeValues.definition.currentRevision
relativeValuesExport.definition.currentRevision
);

const isEmpty = revision.forRelativeValues.cache.length === 0;
const isEmpty = relativeValuesExport.cache.length === 0;

const fullyCached =
!isEmpty &&
revision.forRelativeValues.cache.length >=
relativeValuesExport.cache.length >=
definition.items.length * definition.items.length;

const internals = (
Expand All @@ -63,27 +59,24 @@ export const CacheMenu: FC<{
const withDropdown = (internals: ReactElement) => (
<Dropdown
render={({ close }) => {
if (!revision.forRelativeValues?.id) {
return null; // shouldn't happen, this is mostly for type safety
}
return (
<DropdownMenu>
<DropdownMenuHeader>
{isEmpty
? "Not cached"
: `${revision.forRelativeValues.cache.length}/${
: `${relativeValuesExport.cache.length}/${
definition.items.length * definition.items.length
} pairs cached`}
</DropdownMenuHeader>
{!fullyCached && (
<BuildRelativeValuesCacheAction
exportId={revision.forRelativeValues.id}
exportId={relativeValuesExport.id}
close={close}
/>
)}
{isEmpty ? null : (
<ClearRelativeValuesCacheAction
exportId={revision.forRelativeValues?.id}
exportId={relativeValuesExport.id}
close={close}
/>
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { graphql } from "relay-runtime";

// Used in ModelEvaluator and other relative-values functions.
// Since those functions are not components or hooks, we pass around a RelativeValuesExport$data value.
export const RelativeValuesExport = graphql`
fragment RelativeValuesExport on RelativeValuesExport {
id
...RelativeValuesExportItem
definition {
slug
owner {
slug
}
currentRevision {
...RelativeValuesDefinitionRevision
}
}
cache {
firstItem
secondItem
resultJSON
errorString
}
}
`;
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";

import { notFound } from "next/navigation";
import { FC, PropsWithChildren, useEffect, useState } from "react";
import Skeleton from "react-loading-skeleton";
import { graphql, useFragment } from "react-relay";
Expand Down Expand Up @@ -28,11 +27,11 @@ import {
} from "@/routes";

import { CacheMenu } from "./CacheMenu";
import { RelativeValuesModelRevisionFragment } from "./RelativeValuesModelRevision";
import { RelativeValuesExport } from "./RelativeValuesExport";

import { RelativeValuesDefinitionRevision$key } from "@/__generated__/RelativeValuesDefinitionRevision.graphql";
import { RelativeValuesExport$key } from "@/__generated__/RelativeValuesExport.graphql";
import { RelativeValuesModelLayoutQuery } from "@/__generated__/RelativeValuesModelLayoutQuery.graphql";
import { RelativeValuesModelRevision$key } from "@/__generated__/RelativeValuesModelRevision.graphql";

export const RelativeValuesModelLayout: FC<
PropsWithChildren<{
Expand All @@ -58,10 +57,25 @@ export const RelativeValuesModelLayout: FC<
slug
}
currentRevision {
id
content {
__typename
... on SquiggleSnippet {
id
code
version
}
}
forRelativeValues(input: $forRelativeValues) {
__typename
... on BaseError {
message
}
... on RelativeValuesExport {
...RelativeValuesExport
}
}
...RelativeValuesModelRevision
}
}
}
Expand All @@ -70,25 +84,28 @@ export const RelativeValuesModelLayout: FC<
query
);
const model = extractFromGraphqlErrorUnion(result, "Model");
const revision = useFragment<RelativeValuesModelRevision$key>(
RelativeValuesModelRevisionFragment,
model.currentRevision
);
const revision = model.currentRevision;

const content = extractFromGraphqlErrorUnion(
revision.content,
"SquiggleSnippet"
);

if (!revision.forRelativeValues) {
notFound();
}
const forRelativeValuesKey = extractFromGraphqlErrorUnion(
revision.forRelativeValues,
"RelativeValuesExport"
);

const forRelativeValues = useFragment<RelativeValuesExport$key>(
RelativeValuesExport,
forRelativeValuesKey
);

const definition = revision.forRelativeValues.definition;
const definition = forRelativeValues.definition;

const definitionRevision = useFragment<RelativeValuesDefinitionRevision$key>(
RelativeValuesDefinitionRevisionFragment,
revision.forRelativeValues.definition.currentRevision
forRelativeValues.definition.currentRevision
);

const [evaluatorResult, setEvaluatorResult] = useState<
Expand All @@ -100,9 +117,9 @@ export const RelativeValuesModelLayout: FC<
ModelEvaluator.create(
content.code,
variableName,
revision.forRelativeValues?.cache
forRelativeValues.cache
).then(setEvaluatorResult);
}, [content.code, revision.forRelativeValues, variableName]);
}, [content.code, variableName, forRelativeValues]);

const body = evaluatorResult ? (
evaluatorResult.ok ? (
Expand Down Expand Up @@ -143,7 +160,10 @@ export const RelativeValuesModelLayout: FC<
</div>
<div className="flex items-center gap-4">
{definitionLink}
<CacheMenu revision={revision} isEditable={model.isEditable} />
<CacheMenu
relativeValuesExport={forRelativeValues}
isEditable={model.isEditable}
/>
<StyledTabLink.List>
<StyledTabLink
name="List"
Expand Down

This file was deleted.

13 changes: 5 additions & 8 deletions packages/hub/src/graphql/types/ModelRevision.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { builder } from "@/graphql/builder";
import { prisma } from "@/prisma";

import { NotFoundError } from "../errors/NotFoundError";
import { RelativeValuesExport } from "./RelativeValuesExport";

export const SquiggleSnippet = builder.prismaNode("SquiggleSnippet", {
Expand Down Expand Up @@ -53,12 +54,13 @@ export const ModelRevision = builder.prismaNode("ModelRevision", {
}),
forRelativeValues: t.fieldWithInput({
type: RelativeValuesExport,
nullable: true,
errors: {
types: [NotFoundError],
},
input: {
variableName: t.input.string({ required: true }),
// optional, necessary if the variable is associated with multiple definitions
for: t.input.field({
required: false,
type: builder.inputType(
"ModelRevisionForRelativeValuesSlugOwnerInput",
{
Expand All @@ -70,12 +72,7 @@ export const ModelRevision = builder.prismaNode("ModelRevision", {
),
}),
},
argOptions: { required: false },
async resolve(revision, { input }) {
if (!input) {
return null;
}

const exports = await prisma.relativeValuesExport.findMany({
where: {
modelRevisionId: revision.id,
Expand All @@ -99,7 +96,7 @@ export const ModelRevision = builder.prismaNode("ModelRevision", {
}

if (exports.length === 0) {
return null; // not found
throw new NotFoundError();
}

return exports[0];
Expand Down
6 changes: 2 additions & 4 deletions packages/hub/src/relative-values/values/ModelEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
relativeValueSchema,
} from "./types";

import { RelativeValuesModelRevision$data } from "@/__generated__/RelativeValuesModelRevision.graphql";
import { RelativeValuesExport$data } from "@/__generated__/RelativeValuesExport.graphql";

export const extractOkValues = <A, B>(items: result<A, B>[]): A[] => {
return items
Expand Down Expand Up @@ -102,9 +102,7 @@ export class ModelEvaluator {
static async create(
modelCode: string,
variableName: string,
cache?: NonNullable<
RelativeValuesModelRevision$data["forRelativeValues"]
>["cache"]
cache?: RelativeValuesExport$data["cache"]
): Promise<result<ModelEvaluator, string>> {
// TODO - versioned SqProject
const project = SqProject.create({
Expand Down

0 comments on commit 0b4d932

Please sign in to comment.