"use client";
import { useCallback, useState } from "react";
import yaml from "js-yaml";
import { toolGroups } from "@/config/tools";
import { safeJsonParse } from "@/lib/json";
import { safeYamlParse } from "@/lib/yaml";
import { Editor, EditorProps } from "@/components/ui/editor";
import {
Select,
SelectContent,
SelectItem,
SelectProps,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { ClearButton } from "@/components/buttons/clear";
import { CopyButton } from "@/components/buttons/copy";
import { FileButton } from "@/components/buttons/file";
import { PasteButton } from "@/components/buttons/paste";
import { Configuration } from "@/components/configuration";
import { Configurations } from "@/components/configurations";
import { ControlMenu } from "@/components/control-menu";
import { icons } from "@/components/icons";
import { PageRootSection } from "@/components/page-root-section";
import { PageSection } from "@/components/page-section";
const indentations = {
two: " ",
four: " ",
};
export default function Page() {
const [form, setForm] = useState({
indentation: indentations.two,
json: '{\n "foo": "bar"\n}',
yaml: "foo: bar",
});
const setJsonReactively = useCallback((text: string) => {
const parsed = safeJsonParse(text);
setForm(prev => ({
...prev,
json: text,
yaml: parsed.isErr()
? ""
: yaml.dump(parsed.value, { indent: prev.indentation.length, quotingType: '"' }),
}));
}, []);
const setYamlReactively = useCallback((text: string) => {
const parsed = safeYamlParse(text);
setForm(prev => ({
...prev,
yaml: text,
json: parsed.isErr() ? "" : JSON.stringify(parsed.value, null, prev.indentation),
}));
}, []);
const clearBoth = useCallback(() => {
setForm(prev => ({ ...prev, json: "", yaml: "" }));
}, []);
const onIndentationChange: SelectProps["onValueChange"] = value => {
const parsed = safeJsonParse(form.json);
const jsonYaml = parsed.isErr()
? { json: "", yaml: "" }
: {
json: JSON.stringify(parsed.value, null, value),
yaml: yaml.dump(parsed.value, { indent: value.length, quotingType: '"' }),
};
setForm({
indentation: value,
...jsonYaml,
});
};
const onJsonChange: EditorProps["onChange"] = value => setJsonReactively(value ?? "");
const onYamlChange: EditorProps["onChange"] = value => setYamlReactively(value ?? "");
const indentationConfig = (
}
title="Indentation"
control={
}
/>
);
const jsonPasteButton = ;
const yamlPasteButton = ;
const jsonFileButton = (
);
const yamlFileButton = (
);
const jsonCopyButton = ;
const yamlCopyButton = ;
const clearButton = ;
const jsonControl = (
);
const yamlControl = (
);
return (
);
}