Skip to content

Commit

Permalink
Merge branch 'next' into fix-previous-steps
Browse files Browse the repository at this point in the history
  • Loading branch information
rifont authored Nov 8, 2024
2 parents 0465f06 + f719edb commit 74c8988
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@
"lastindex",
"Lato",
"Lentczner",
"lezer"
"lezer",
"libarary",
"libauth",
"libspf",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { useFormContext } from 'react-hook-form';
import { motion } from 'framer-motion';
import { RouteFill } from '../icons';
import { Input, InputField } from '../primitives/input';
import { RiArrowRightSLine, RiSettingsLine } from 'react-icons/ri';
// import { RiArrowRightSLine, RiSettingsLine } from 'react-icons/ri';
import * as z from 'zod';
import { Separator } from '../primitives/separator';
import { TagInput } from '../primitives/tag-input';
import { Textarea } from '../primitives/textarea';
import { workflowSchema } from './schema';
import { useTagsQuery } from '@/hooks/use-tags-query';
import { Button } from '../primitives/button';
// import { Button } from '../primitives/button';
import { CopyButton } from '../primitives/copy-button';
import { FormControl, FormField, FormItem, FormLabel, FormMessage } from '../primitives/form/form';
import { Switch } from '../primitives/switch';
Expand Down Expand Up @@ -128,13 +128,13 @@ export function ConfigureWorkflow() {
/>
</SidebarContent>
<Separator />
<SidebarContent size="lg">
{/* <SidebarContent size="lg">
<Button variant="outline" className="flex w-full justify-start gap-1.5 text-xs font-medium" type="button">
<RiSettingsLine className="h-4 w-4 text-neutral-600" />
Configure channel preferences <RiArrowRightSLine className="ml-auto h-4 w-4 text-neutral-600" />
</Button>
</SidebarContent>
<Separator />
<Separator /> */}
</motion.div>
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { useFormContext } from 'react-hook-form';
import { liquid } from '@codemirror/lang-liquid';
import { EditorView } from '@uiw/react-codemirror';
import { useFormContext } from 'react-hook-form';

import { Editor } from '@/components/primitives/editor';
import { FormControl, FormField, FormItem, FormMessage } from '@/components/primitives/form/form';
import { InputField } from '@/components/primitives/input';
import { Editor } from '@/components/primitives/editor';
import { useFetchStep } from '@/hooks/use-fetch-step';
import { parseStepVariablesToLiquidVariables } from '@/utils/parseStepVariablesToLiquidVariables';
import { capitalize } from '@/utils/string';
import { useParams } from 'react-router-dom';

const bodyKey = 'body';

Expand All @@ -15,6 +18,10 @@ export const InAppBody = () => {
formState: { errors },
} = useFormContext();

const { workflowSlug = '', stepSlug = '' } = useParams<{ workflowSlug: string; stepSlug: string }>();

const { step } = useFetchStep({ workflowSlug, stepSlug });

return (
<FormField
control={control}
Expand All @@ -29,7 +36,7 @@ export const InAppBody = () => {
id={field.name}
extensions={[
liquid({
variables: [{ type: 'variable', label: 'asdf' }],
variables: step ? parseStepVariablesToLiquidVariables(step.variables) : [],
}),
EditorView.lineWrapping,
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { FormControl, FormField, FormItem, FormMessage } from '@/components/prim
import { InputField } from '@/components/primitives/input';
import { Editor } from '@/components/primitives/editor';
import { capitalize } from '@/utils/string';
import { useParams } from 'react-router-dom';
import { useFetchStep } from '@/hooks/use-fetch-step';
import { parseStepVariablesToLiquidVariables } from '@/utils/parseStepVariablesToLiquidVariables';

const subjectKey = 'subject';

Expand All @@ -14,6 +17,9 @@ export const InAppSubject = () => {
control,
formState: { errors },
} = useFormContext();
const { workflowSlug = '', stepSlug = '' } = useParams<{ workflowSlug: string; stepSlug: string }>();

const { step } = useFetchStep({ workflowSlug, stepSlug });

return (
<FormField
Expand All @@ -29,7 +35,7 @@ export const InAppSubject = () => {
id={field.name}
extensions={[
liquid({
variables: [{ type: 'variable', label: 'asdf' }],
variables: step ? parseStepVariablesToLiquidVariables(step.variables) : [],
}),
EditorView.lineWrapping,
]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export const InAppTabs = ({ workflow, step }: { workflow: WorkflowResponseDto; s
});
setEditorValue(JSON.stringify(res.previewPayloadExample, null, 2));
};

const formValues = useWatch(form);
useDebouncedEffect(
() => {
Expand Down
61 changes: 61 additions & 0 deletions apps/dashboard/src/utils/parseStepVariablesToLiquidVariables.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { StepDataDto } from '@novu/shared';

interface LiquidVariable {
type: 'variable';
label: string;
detail: string;
}

type JSONSchema = StepDataDto['variables'];

/**
* Parse JSON Schema and extract variables for Liquid autocompletion.
* @param schema - The JSON Schema to parse.
* @returns An array of variable objects suitable for the Liquid language.
*/
export function parseStepVariablesToLiquidVariables(schema: JSONSchema): LiquidVariable[] {
const variables: LiquidVariable[] = [];

function extractProperties(obj: JSONSchema, path = ''): void {
if (typeof obj === 'boolean') return; // Handle boolean schema

if (obj.type === 'object' && obj.properties) {
for (const [key, value] of Object.entries(obj.properties)) {
const fullPath = path ? `${path}.${key}` : key;

// Add each property as a variable for autocompletion
variables.push({
type: 'variable',
label: `${fullPath}`,
detail: typeof value !== 'boolean' ? value.description || 'JSON Schema variable' : 'JSON Schema variable',
});

// Recursively process nested objects
if (typeof value === 'object' && (value.type === 'object' || value.type === 'array')) {
extractProperties(value, fullPath);
}
}
} else if (obj.type === 'array' && obj.items) {
// For arrays, add a placeholder for array indexing
const items = Array.isArray(obj.items) ? obj.items[0] : obj.items;
extractProperties(items, `${path}[0]`);
}

// Handle combinators (allOf, anyOf, oneOf)
['allOf', 'anyOf', 'oneOf'].forEach((combiner) => {
if (Array.isArray(obj[combiner as keyof typeof obj])) {
for (const subSchema of obj[combiner as keyof typeof obj] as JSONSchema[]) {
extractProperties(subSchema, path);
}
}
});

// Handle conditional schemas (if/then/else)
if (obj.if) extractProperties(obj.if, path);
if (obj.then) extractProperties(obj.then, path);
if (obj.else) extractProperties(obj.else, path);
}

extractProperties(schema);
return variables;
}

0 comments on commit 74c8988

Please sign in to comment.