mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 09:18:30 +02:00
Preserve JSON/XPath filter (Closes #22)
This commit is contained in:
@@ -195,7 +195,7 @@ export const Editor = forwardRef<EditorView | undefined, EditorProps>(function E
|
|||||||
placeholderCompartment.current.of(
|
placeholderCompartment.current.of(
|
||||||
placeholderExt(placeholderElFromText(placeholder ?? '')),
|
placeholderExt(placeholderElFromText(placeholder ?? '')),
|
||||||
),
|
),
|
||||||
wrapLinesCompartment.current.of([]),
|
wrapLinesCompartment.current.of(wrapLines ? [EditorView.lineWrapping] : []),
|
||||||
...getExtensions({
|
...getExtensions({
|
||||||
container,
|
container,
|
||||||
readOnly,
|
readOnly,
|
||||||
@@ -357,8 +357,7 @@ function getExtensions({
|
|||||||
blur: () => {
|
blur: () => {
|
||||||
onBlur.current?.();
|
onBlur.current?.();
|
||||||
},
|
},
|
||||||
keydown: (e, cm) => {
|
keydown: (e) => {
|
||||||
console.log('KEY DOWN', e, cm);
|
|
||||||
onKeyDown.current?.(e);
|
onKeyDown.current?.(e);
|
||||||
},
|
},
|
||||||
paste: (e) => {
|
paste: (e) => {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ export function FormattedError({ children }: Props) {
|
|||||||
return (
|
return (
|
||||||
<pre
|
<pre
|
||||||
className={classNames(
|
className={classNames(
|
||||||
'w-full select-auto cursor-text bg-gray-100 p-3 rounded',
|
'w-full select-auto cursor-text bg-background-highlight-secondary p-3 rounded',
|
||||||
'whitespace-pre-wrap border border-fg-danger border-dashed overflow-x-auto',
|
'whitespace-pre-wrap border border-fg-danger border-dashed overflow-x-auto',
|
||||||
)}
|
)}
|
||||||
>
|
>
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
import type { ReactNode } from 'react';
|
import type { ReactNode } from 'react';
|
||||||
import { useCallback, useMemo } from 'react';
|
import { useCallback, useMemo } from 'react';
|
||||||
|
import { createGlobalState } from 'react-use';
|
||||||
import { useContentTypeFromHeaders } from '../../hooks/useContentTypeFromHeaders';
|
import { useContentTypeFromHeaders } from '../../hooks/useContentTypeFromHeaders';
|
||||||
import { useDebouncedState } from '../../hooks/useDebouncedState';
|
import { useDebouncedValue } from '../../hooks/useDebouncedValue';
|
||||||
import { useFilterResponse } from '../../hooks/useFilterResponse';
|
import { useFilterResponse } from '../../hooks/useFilterResponse';
|
||||||
import { useResponseBodyText } from '../../hooks/useResponseBodyText';
|
import { useResponseBodyText } from '../../hooks/useResponseBodyText';
|
||||||
import { useToggle } from '../../hooks/useToggle';
|
|
||||||
import { tryFormatJson, tryFormatXml } from '../../lib/formatters';
|
import { tryFormatJson, tryFormatXml } from '../../lib/formatters';
|
||||||
import type { HttpResponse } from '../../lib/models';
|
import type { HttpResponse } from '../../lib/models';
|
||||||
import { Editor } from '../core/Editor';
|
import { Editor } from '../core/Editor';
|
||||||
@@ -21,25 +21,42 @@ interface Props {
|
|||||||
className?: string;
|
className?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const useFilterText = createGlobalState<Record<string, string | null>>({});
|
||||||
|
|
||||||
export function TextViewer({ response, pretty, className }: Props) {
|
export function TextViewer({ response, pretty, className }: Props) {
|
||||||
const [isSearching, toggleIsSearching] = useToggle();
|
const [filterTextMap, setFilterTextMap] = useFilterText();
|
||||||
const [filterText, setDebouncedFilterText, setFilterText] = useDebouncedState<string>('', 400);
|
const filterText = filterTextMap[response.id] ?? null;
|
||||||
|
const debouncedFilterText = useDebouncedValue(filterText, 300);
|
||||||
|
const setFilterText = useCallback(
|
||||||
|
(v: string | null) => {
|
||||||
|
setFilterTextMap((m) => ({ ...m, [response.id]: v }));
|
||||||
|
},
|
||||||
|
[setFilterTextMap, response],
|
||||||
|
);
|
||||||
|
|
||||||
const contentType = useContentTypeFromHeaders(response.headers);
|
const contentType = useContentTypeFromHeaders(response.headers);
|
||||||
const rawBody = useResponseBodyText(response) ?? '';
|
const rawBody = useResponseBodyText(response) ?? '';
|
||||||
|
const isSearching = filterText != null;
|
||||||
const formattedBody =
|
const formattedBody =
|
||||||
pretty && contentType?.includes('json')
|
pretty && contentType?.includes('json')
|
||||||
? tryFormatJson(rawBody)
|
? tryFormatJson(rawBody)
|
||||||
: pretty && contentType?.includes('xml')
|
: pretty && contentType?.includes('xml')
|
||||||
? tryFormatXml(rawBody)
|
? tryFormatXml(rawBody)
|
||||||
: rawBody;
|
: rawBody;
|
||||||
const filteredResponse = useFilterResponse({ filter: filterText, responseId: response.id });
|
|
||||||
|
|
||||||
const body = filteredResponse ?? formattedBody;
|
const filteredResponse = useFilterResponse({
|
||||||
const clearSearch = useCallback(() => {
|
filter: debouncedFilterText ?? '',
|
||||||
toggleIsSearching();
|
responseId: response.id,
|
||||||
setFilterText('');
|
});
|
||||||
}, [setFilterText, toggleIsSearching]);
|
|
||||||
|
const body = isSearching && filterText?.length > 0 ? filteredResponse : formattedBody;
|
||||||
|
const toggleSearch = useCallback(() => {
|
||||||
|
if (isSearching) {
|
||||||
|
setFilterText(null);
|
||||||
|
} else {
|
||||||
|
setFilterText('');
|
||||||
|
}
|
||||||
|
}, [isSearching, setFilterText]);
|
||||||
|
|
||||||
const isJson = contentType?.includes('json');
|
const isJson = contentType?.includes('json');
|
||||||
const isXml = contentType?.includes('xml') || contentType?.includes('html');
|
const isXml = contentType?.includes('xml') || contentType?.includes('html');
|
||||||
@@ -54,16 +71,17 @@ export function TextViewer({ response, pretty, className }: Props) {
|
|||||||
result.push(
|
result.push(
|
||||||
<div key="input" className="w-full !opacity-100">
|
<div key="input" className="w-full !opacity-100">
|
||||||
<Input
|
<Input
|
||||||
|
key={response.id}
|
||||||
hideLabel
|
hideLabel
|
||||||
autoFocus
|
autoFocus
|
||||||
containerClassName="bg-gray-100 dark:bg-gray-50"
|
containerClassName="bg-background"
|
||||||
size="sm"
|
size="sm"
|
||||||
placeholder={isJson ? 'JSONPath expression' : 'XPath expression'}
|
placeholder={isJson ? 'JSONPath expression' : 'XPath expression'}
|
||||||
label="Filter expression"
|
label="Filter expression"
|
||||||
name="filter"
|
name="filter"
|
||||||
defaultValue={filterText}
|
defaultValue={filterText}
|
||||||
onKeyDown={(e) => e.key === 'Escape' && clearSearch()}
|
onKeyDown={(e) => e.key === 'Escape' && toggleSearch()}
|
||||||
onChange={setDebouncedFilterText}
|
onChange={setFilterText}
|
||||||
/>
|
/>
|
||||||
</div>,
|
</div>,
|
||||||
);
|
);
|
||||||
@@ -75,13 +93,16 @@ export function TextViewer({ response, pretty, className }: Props) {
|
|||||||
size="sm"
|
size="sm"
|
||||||
icon={isSearching ? 'x' : 'filter'}
|
icon={isSearching ? 'x' : 'filter'}
|
||||||
title={isSearching ? 'Close filter' : 'Filter response'}
|
title={isSearching ? 'Close filter' : 'Filter response'}
|
||||||
onClick={clearSearch}
|
onClick={toggleSearch}
|
||||||
className={classNames(isSearching && '!opacity-100')}
|
className={classNames(
|
||||||
|
'bg-background border !border-background-highlight',
|
||||||
|
isSearching && '!opacity-100',
|
||||||
|
)}
|
||||||
/>,
|
/>,
|
||||||
);
|
);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}, [canFilter, clearSearch, filterText, isJson, isSearching, setDebouncedFilterText]);
|
}, [canFilter, filterText, isJson, isSearching, setFilterText, toggleSearch]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Editor
|
<Editor
|
||||||
|
|||||||
@@ -18,6 +18,6 @@ export function useFilterResponse({
|
|||||||
|
|
||||||
return (await invoke('cmd_filter_response', { responseId, filter })) as string | null;
|
return (await invoke('cmd_filter_response', { responseId, filter })) as string | null;
|
||||||
},
|
},
|
||||||
}).data ?? null
|
}).data ?? ''
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user