-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tracing): pretty print JSON strings for structured data outputs (#…
- Loading branch information
1 parent
aabe535
commit b371f7c
Showing
7 changed files
with
54 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from "react"; | ||
import { css } from "@emotion/react"; | ||
|
||
import { JSONBlock } from "@phoenix/components/code"; | ||
import { usePrettyText } from "@phoenix/hooks/usePrettyText"; | ||
import { assertUnreachable } from "@phoenix/typeUtils"; | ||
|
||
export function PrettyText({ children }: { children: string }) { | ||
const { text, textType } = usePrettyText(children); | ||
if (textType === "string") { | ||
return ( | ||
<pre | ||
css={css` | ||
white-space: pre-wrap; | ||
text-wrap: wrap; | ||
margin: 0; | ||
`} | ||
> | ||
{text} | ||
</pre> | ||
); | ||
} | ||
if (textType === "json") { | ||
return <JSONBlock value={text} />; | ||
} | ||
assertUnreachable(textType); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./PrettyText"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { useMemo } from "react"; | ||
|
||
type TextType = "string" | "json"; | ||
/** | ||
* A hook that takes in text, detects if it is a JSON string, and formats it | ||
* @param text | ||
* @returns formatted text | ||
*/ | ||
export function usePrettyText(text: string): { | ||
text: string; | ||
textType: TextType; | ||
} { | ||
return useMemo(() => { | ||
try { | ||
const parsed = JSON.parse(text); | ||
return { text: JSON.stringify(parsed, null, 2), textType: "json" }; | ||
} catch (e) { | ||
return { text, textType: "string" }; | ||
} | ||
}, [text]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters