mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-21 00:01:22 +02:00
Better formatting
This commit is contained in:
@@ -2,7 +2,9 @@ import { useMutation, useQuery } from '@tanstack/react-query';
|
|||||||
import { invoke } from '@tauri-apps/api';
|
import { invoke } from '@tauri-apps/api';
|
||||||
import type { UnlistenFn } from '@tauri-apps/api/event';
|
import type { UnlistenFn } from '@tauri-apps/api/event';
|
||||||
import { emit, listen } from '@tauri-apps/api/event';
|
import { emit, listen } from '@tauri-apps/api/event';
|
||||||
|
import { type } from '@tauri-apps/api/os';
|
||||||
import { useEffect, useRef, useState } from 'react';
|
import { useEffect, useRef, useState } from 'react';
|
||||||
|
import { tryFormatJson } from '../lib/formatters';
|
||||||
import { useKeyValue } from './useKeyValue';
|
import { useKeyValue } from './useKeyValue';
|
||||||
|
|
||||||
interface ReflectResponseService {
|
interface ReflectResponseService {
|
||||||
@@ -67,7 +69,11 @@ export function useGrpc(url: string | null, requestId: string | null) {
|
|||||||
unlisten.current = await listen(`grpc_server_msg_${id}`, async (event) => {
|
unlisten.current = await listen(`grpc_server_msg_${id}`, async (event) => {
|
||||||
await messages.set((prev) => [
|
await messages.set((prev) => [
|
||||||
...prev,
|
...prev,
|
||||||
{ message: event.payload as string, timestamp: new Date().toISOString(), type: 'server' },
|
{
|
||||||
|
message: tryFormatJson(event.payload as string, false),
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
type: 'server',
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
setActiveConnectionId(id);
|
setActiveConnectionId(id);
|
||||||
@@ -95,7 +101,11 @@ export function useGrpc(url: string | null, requestId: string | null) {
|
|||||||
unlisten.current = await listen(`grpc_server_msg_${id}`, (event) => {
|
unlisten.current = await listen(`grpc_server_msg_${id}`, (event) => {
|
||||||
messages.set((prev) => [
|
messages.set((prev) => [
|
||||||
...prev,
|
...prev,
|
||||||
{ message: event.payload as string, timestamp: new Date().toISOString(), type: 'server' },
|
{
|
||||||
|
message: tryFormatJson(event.payload as string, false),
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
type: 'server',
|
||||||
|
},
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
export function tryFormatJson(text: string): string {
|
export function tryFormatJson(text: string, pretty = true): string {
|
||||||
try {
|
try {
|
||||||
return JSON.stringify(JSON.parse(text), null, 2);
|
if (pretty) return JSON.stringify(JSON.parse(text), null, 2);
|
||||||
|
else return JSON.stringify(JSON.parse(text));
|
||||||
} catch (_) {
|
} catch (_) {
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user