mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-05-13 11:20:27 +02:00
Split codebase (#455)
This commit is contained in:
30
apps/yaak-client/hooks/useFormatText.ts
Normal file
30
apps/yaak-client/hooks/useFormatText.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import type { EditorProps } from "../components/core/Editor/Editor";
|
||||
import { tryFormatJson, tryFormatXml } from "../lib/formatters";
|
||||
|
||||
export function useFormatText({
|
||||
text,
|
||||
language,
|
||||
pretty,
|
||||
}: {
|
||||
text: string;
|
||||
language: EditorProps["language"];
|
||||
pretty: boolean;
|
||||
}) {
|
||||
return useQuery({
|
||||
placeholderData: (prev) => prev, // Keep previous data on refetch
|
||||
queryKey: [text, language, pretty],
|
||||
queryFn: async () => {
|
||||
if (text === "" || !pretty) {
|
||||
return text;
|
||||
}
|
||||
if (language === "json") {
|
||||
return tryFormatJson(text);
|
||||
}
|
||||
if (language === "xml" || language === "html") {
|
||||
return tryFormatXml(text);
|
||||
}
|
||||
return text;
|
||||
},
|
||||
}).data;
|
||||
}
|
||||
Reference in New Issue
Block a user