Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,12 @@ export const CreateRelationshipDialog: React.FC<

const form = useForm({
schema: SCHEMA.createResourceRelationshipRule.extend({
metadataKeysMatches: z.array(z.object({ key: z.string() })),
metadataKeysMatches: z.array(
z.object({
sourceKey: z.string(),
targetKey: z.string(),
}),
),
targetMetadataEquals: z.array(
z.object({ value: z.string(), key: z.string() }),
),
Expand All @@ -71,10 +76,8 @@ export const CreateRelationshipDialog: React.FC<
const createRule = api.resource.relationshipRules.create.useMutation();

const onSubmit = form.handleSubmit((data) => {
const { metadataKeysMatches } = data;
const keys = metadataKeysMatches.map((item) => item.key);
createRule
.mutateAsync({ ...data, metadataKeysMatches: keys })
.mutateAsync(data)
.then(() => utils.resource.relationshipRules.list.invalidate())
.then(() => setOpen(false));
});
Expand Down Expand Up @@ -105,7 +108,7 @@ export const CreateRelationshipDialog: React.FC<
Add Relationship Rule
</Button>
</DialogTrigger>
<DialogContent className="max-w-xl">
<DialogContent className="max-h-[90vh] max-w-xl overflow-y-auto">
<DialogHeader>
<DialogTitle>Create Relationship Rule</DialogTitle>
</DialogHeader>
Expand Down Expand Up @@ -323,9 +326,26 @@ export const CreateRelationshipDialog: React.FC<
<FormControl>
<div className="flex items-center gap-1 rounded-md border border-neutral-800 px-2 py-1">
<Input
value={field.value.key}
value={field.value.sourceKey}
onChange={(e) =>
field.onChange({
...field.value,
sourceKey: e.target.value,
})
}
placeholder="Enter key..."
className="h-6 w-32 border-0 ring-0 focus-visible:ring-0"
/>
<span className="text-sm text-muted-foreground">
matches
</span>
<Input
value={field.value.targetKey}
onChange={(e) =>
field.onChange({ key: e.target.value })
field.onChange({
...field.value,
targetKey: e.target.value,
})
}
placeholder="Enter key..."
className="h-6 w-32 border-0 ring-0 focus-visible:ring-0"
Expand All @@ -349,7 +369,9 @@ export const CreateRelationshipDialog: React.FC<
type="button"
variant="secondary"
size="sm"
onClick={() => appendMetadataKeysMatch({ key: "" })}
onClick={() =>
appendMetadataKeysMatch({ sourceKey: "", targetKey: "" })
}
>
Add
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,14 @@ export const EditRelationshipDialog: React.FC<EditRelationshipDialogProps> = ({

const form = useForm({
schema: SCHEMA.updateResourceRelationshipRule.extend({
metadataKeysMatches: z.array(z.object({ key: z.string() })).optional(),
metadataKeysMatches: z
.array(z.object({ sourceKey: z.string(), targetKey: z.string() }))
.optional(),
targetMetadataEquals: z
.array(z.object({ key: z.string(), value: z.string() }))
.optional(),
}),
defaultValues: {
...rule,
metadataKeysMatches: rule.metadataKeysMatches.map((match) => ({
key: match.key,
})),
},
defaultValues: rule,
});

const utils = api.useUtils();
Expand Down Expand Up @@ -89,25 +86,14 @@ export const EditRelationshipDialog: React.FC<EditRelationshipDialogProps> = ({
control: form.control,
});

const onSubmit = form.handleSubmit((data) => {
const { metadataKeysMatches, targetMetadataEquals } = data;
const keys = metadataKeysMatches?.map((item) => item.key);
updateRule
.mutateAsync({
id: rule.id,
data: {
...data,
metadataKeysMatches: keys,
targetMetadataEquals: targetMetadataEquals,
},
})
.then(() => setOpen(false));
});
const onSubmit = form.handleSubmit((data) =>
updateRule.mutateAsync({ id: rule.id, data }).then(() => setOpen(false)),
);

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>{children}</DialogTrigger>
<DialogContent>
<DialogContent className="max-h-[90vh] max-w-xl overflow-y-auto">
<DialogHeader>
<DialogTitle>Edit Relationship Rule</DialogTitle>
</DialogHeader>
Expand Down Expand Up @@ -324,9 +310,26 @@ export const EditRelationshipDialog: React.FC<EditRelationshipDialogProps> = ({
<FormControl>
<div className="flex items-center gap-1 rounded-md border border-neutral-800 px-2 py-1">
<Input
value={field.value.key}
value={field.value.sourceKey}
onChange={(e) =>
field.onChange({ key: e.target.value })
field.onChange({
...field.value,
sourceKey: e.target.value,
})
}
placeholder="Enter key..."
className="h-6 w-32 border-0 ring-0 focus-visible:ring-0"
/>
<span className="text-sm text-muted-foreground">
matches
</span>
<Input
value={field.value.targetKey}
onChange={(e) =>
field.onChange({
...field.value,
targetKey: e.target.value,
})
}
placeholder="Enter key..."
className="h-6 w-32 border-0 ring-0 focus-visible:ring-0"
Expand All @@ -350,7 +353,9 @@ export const EditRelationshipDialog: React.FC<EditRelationshipDialogProps> = ({
type="button"
variant="secondary"
size="sm"
onClick={() => appendMetadataKeysMatch({ key: "" })}
onClick={() =>
appendMetadataKeysMatch({ sourceKey: "", targetKey: "" })
}
>
Add
</Button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ export const RelationshipRulesTable: React.FC<RelationshipRulesTableProps> = ({
<Badge
variant="outline"
className="font-mono"
key={match.key}
key={`${match.sourceKey}-${match.targetKey}`}
>
{match.key}
{match.sourceKey} matches {match.targetKey}
</Badge>
))}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,16 @@ export const openapi: Swagger.SwaggerV3 = {
sourceVersion: { type: "string" },
targetKind: { type: "string" },
targetVersion: { type: "string" },
metadataKeysMatch: {
metadataKeysMatches: {
type: "array",
items: { type: "string" },
items: {
type: "object",
properties: {
sourceKey: { type: "string" },
targetKey: { type: "string" },
},
required: ["sourceKey", "targetKey"],
},
},
targetMetadataEquals: {
type: "array",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const log = logger.child({ route: "/v1/resource-relationship-rules/[ruleId]" });
const replaceMetadataMatchRules = async (
tx: Tx,
ruleId: string,
metadataKeysMatch?: string[],
metadataKeysMatch?: { sourceKey: string; targetKey: string }[],
) => {
await tx
.delete(schema.resourceRelationshipRuleMetadataMatch)
Expand All @@ -29,12 +29,16 @@ const replaceMetadataMatchRules = async (
),
);

const metadataKeys = _.uniq(metadataKeysMatch ?? []);
const metadataKeys = _.uniqBy(
metadataKeysMatch ?? [],
({ sourceKey, targetKey }) => `${sourceKey}-${targetKey}`,
);
if (metadataKeys.length > 0)
await tx.insert(schema.resourceRelationshipRuleMetadataMatch).values(
metadataKeys.map((key) => ({
metadataKeys.map(({ sourceKey, targetKey }) => ({
resourceRelationshipRuleId: ruleId,
key,
sourceKey,
targetKey,
})),
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,14 @@ export const openapi: Swagger.SwaggerV3 = {

metadataKeysMatches: {
type: "array",
items: { type: "string" },
items: {
type: "object",
properties: {
sourceKey: { type: "string" },
targetKey: { type: "string" },
},
required: ["sourceKey", "targetKey"],
},
},
},
required: [
Expand Down Expand Up @@ -135,7 +142,14 @@ export const openapi: Swagger.SwaggerV3 = {
targetVersion: { type: "string" },
metadataKeysMatches: {
type: "array",
items: { type: "string" },
items: {
type: "object",
properties: {
sourceKey: { type: "string" },
targetKey: { type: "string" },
},
required: ["sourceKey", "targetKey"],
},
},
targetMetadataEquals: {
type: "array",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export const POST = request()
.values(
metadataKeysMatches.map((key) => ({
resourceRelationshipRuleId: rule.id,
key,
sourceKey: key.sourceKey,
targetKey: key.targetKey,
})),
);

Expand Down
Loading
Loading