/* * Response Code * * for http response codes */ import dayjs from 'dayjs' import { AlertCircle, Info } from 'lucide-react' import React from 'react' import { cn } from 'ui' import { isUnixMicro, unixMicroToIsoTimestamp } from './Logs.utils' import CopyButton from '@/components/ui/CopyButton' export const RowLayout: React.FC = ({ children }) => (
{children}
) // renders a timestamp (either unix microsecond or iso timestamp) export const SelectionDetailedTimestampRow = ({ value }: { value: string | number }) => ( ) export const SelectionDetailedRow = ({ label, value, valueRender, }: { label: string value: string valueRender?: React.ReactNode }) => { return (
{label} {valueRender ?? value}
) } // used for column renderers export const TextFormatter: React.FC<{ className?: string; value: string }> = ({ value, className, }) => ( {value} ) export const ResponseCodeFormatter = ({ value }: { value: string }) => { if (!value) { return (
) } const ResponseCodeItem = ({ children, className, }: { children: React.ReactNode className?: string }) => (
) const split = value.toString().split('')[0] switch (split) { // 2XX || 1XX responses case '1': return {value} case '2': return {value} // 5XX responses case '5': return {value} // 4XX || 3XX responses case '4': case '3': return {value} // All other responses default: return ( {value} ) } } /* * Response Code * * for http response codes */ export const SeverityFormatter = ({ value, uppercase = true, }: { value: string uppercase?: boolean }) => { if (!value) { return (
) } const uppercasedValue = value.toUpperCase() const text = uppercase ? uppercasedValue : value const Layout: React.FC> = ({ className, children, }) =>
{children}
switch (uppercasedValue) { case 'UNCAUGHTEXCEPTION': case 'PANIC': case 'FATAL': case 'ERROR': return (
{text}
) break case 'INFO': case 'DEBUG': return (
{text}
) break case 'LOG': return (
{text}
) break case 'WARNING': return (
{text}
) break // All other responses default: return (
) break } } export const timestampLocalFormatter = (value: string | number) => { const timestamp = isUnixMicro(value) ? unixMicroToIsoTimestamp(value) : value return dayjs(timestamp).format('DD MMM HH:mm:ss') } /* * JSON Syntax Highlighter * * for http response codes */ export function jsonSyntaxHighlight(input: Object) { let json: string = JSON.stringify(input, null, 2) json = json.replace(/&/g, '&').replace(//g, '>') const newJson = json.replace( /("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) { var cls = 'number text-tomato-900' if (/^"/.test(match)) { if (/:$/.test(match)) { cls = 'key text-foreground' } else { cls = 'string text-brand-600' } } else if (/true|false/.test(match)) { cls = 'boolean text-blue-900' } else if (/null/.test(match)) { cls = 'null text-amber-1100' } return '' + match + '' } ) const jsonWithLineWraps = newJson.split(`\n`).map((x) => { return `${x}` }) return jsonWithLineWraps.join('\n') }