Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot edit max length #119

Open
wants to merge 1 commit into
base: staging
Choose a base branch
from
Open
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
36 changes: 28 additions & 8 deletions src/app/create/[id]/createBlueprintSteps/ExtractFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ const ExtractFields = ({
const [regexGeneratedOutputs, setRegexGeneratedOutputs] = useState<string[]>(
Array(store.decomposedRegexes?.length ?? 0).fill('')
);
const [regexGeneratedOutputErrors, setRegexGeneratedOutputErrors] = useState<string[]>(
Array(store.decomposedRegexes?.length ?? 0).fill('')
);

const [isExtractSubjectChecked, setIsExtractSubjectChecked] = useState(false);
const [isExtractReceiverChecked, setIsExtractReceiverChecked] = useState(false);
Expand Down Expand Up @@ -143,23 +146,37 @@ const ExtractFields = ({
revealPrivateFields
);

const outputUpdated =
JSON.stringify(regexOutputs) !== JSON.stringify(regexGeneratedOutputs[index]);

setRegexGeneratedOutputs((prev) => {
const updated = [...prev];
// @ts-ignore
updated[index] = regexOutputs;
return updated;
});

setRegexGeneratedOutputErrors((prev) => {
const updated = [...prev];
// @ts-ignore
updated[index] = '';
return updated;
});

// update the max length of the regex at that particular index
const decomposedRegexes = [...store.decomposedRegexes];
decomposedRegexes[index].maxLength = regexOutputs[0].length ?? 64;
setField('decomposedRegexes', decomposedRegexes);
// Only update when the output changes, so we can still set maxLength
if (outputUpdated) {
const totalLength = regexOutputs.reduce((acc, cur) => (acc += cur.length), 0);
const decomposedRegexes = [...store.decomposedRegexes];
decomposedRegexes[index].maxLength = totalLength ?? 64;
setField('decomposedRegexes', decomposedRegexes);
}
} catch (error) {
console.error('Error testing decomposed regex:', error);
setRegexGeneratedOutputs((prev) => {
setRegexGeneratedOutputErrors((prev) => {
const updated = [...prev];
// @ts-ignore
updated[index] = ['Error: ' + error];
updated[index] = 'Error: ' + error;
return updated;
});
}
Expand Down Expand Up @@ -576,6 +593,9 @@ const ExtractFields = ({
setField('decomposedRegexes', updatedRegexes);

setRegexGeneratedOutputs(regexGeneratedOutputs.filter((_, i) => i !== index));
setRegexGeneratedOutputErrors(
regexGeneratedOutputErrors.filter((_, i) => i !== index)
);
}}
>
Delete
Expand Down Expand Up @@ -792,13 +812,13 @@ const ExtractFields = ({
<Label>Output</Label>
<div
className={`rounded-lg border p-2 text-sm ${
JSON.stringify(regexGeneratedOutputs[index]).includes('Error:')
regexGeneratedOutputErrors[index]
? 'border-red-500 bg-red-100'
: 'border-grey-500 bg-neutral-100'
}`}
>
{JSON.stringify(regexGeneratedOutputs[index]).includes('Error: ')
? JSON.stringify(regexGeneratedOutputs[index])
{regexGeneratedOutputErrors[index]
? JSON.stringify(regexGeneratedOutputErrors[index])
: regexGeneratedOutputs
? `${regex.name}: ${JSON.stringify(regexGeneratedOutputs[index])}`
: ''}
Expand Down