mirror of
https://github.com/mountain-loop/yaak.git
synced 2025-12-23 22:48:55 -05:00
31 lines
765 B
TypeScript
31 lines
765 B
TypeScript
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;
|
|
}
|