"use client";
import { useCallback, useMemo, useState } from "react";
import * as O from "fp-ts/lib/Option";
import YAML from "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 two = " ";
const four = " ";
export default function Page() {
const [form, setForm] = useState({
indentation: two,
json: '{\n "foo": "bar"\n}',
yaml: "foo: bar",
});
const setJsonReactively = useCallback((text: string) => {
const parsed = safeJsonParse(text);
setForm(prev => ({
...prev,
json: text,
yaml: O.isNone(parsed)
? ""
: YAML.stringify(parsed.value, { indent: prev.indentation.length, simpleKeys: true }),
}));
}, []);
const setYamlReactively = useCallback((text: string) => {
const parsed = safeYamlParse(text, (_, v) => v, { merge: true });
setForm(prev => ({
...prev,
yaml: text,
json: O.isNone(parsed) ? "" : 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 = O.isNone(parsed)
? { json: "", yaml: "" }
: {
json: JSON.stringify(parsed.value, null, value),
yaml: YAML.stringify(parsed.value, { indent: value.length, simpleKeys: true }),
};
setForm({
indentation: value,
...jsonYaml,
});
};
const onJsonChange: EditorProps["onChange"] = value => setJsonReactively(value ?? "");
const onYamlChange: EditorProps["onChange"] = value => setYamlReactively(value ?? "");
const indentationIcon = useMemo(() => , []);
const indentationConfig = (
2 spaces
4 spaces
}
/>
);
const jsonPasteButton = useMemo(
() => ,
[setJsonReactively]
);
const yamlPasteButton = useMemo(
() => ,
[setYamlReactively]
);
const jsonFileButton = useMemo(
() => (
),
[setJsonReactively]
);
const yamlFileButton = useMemo(
() => (
),
[setYamlReactively]
);
const jsonCopyButton = ;
const yamlCopyButton = ;
const clearButton = useMemo(
() => ,
[clearBoth]
);
const jsonControl = (
);
const yamlControl = (
);
return (
);
}