import React, { useMemo } from 'react'; import type { TypeCastMap } from 'hadron-type-checker'; import { Binary } from 'bson'; import type { DBRef } from 'bson'; import { variantColors } from '@leafygreen-ui/code'; import { Icon, Link } from './leafygreen'; import { spacing } from '@leafygreen-ui/tokens'; import { css, cx } from '@leafygreen-ui/emotion'; import { Theme, useDarkMode } from '../hooks/use-theme'; type ValueProps = | { [type in keyof TypeCastMap]: { type: type; value: TypeCastMap[type] }; }[keyof TypeCastMap] | { type: 'DBRef'; value: DBRef }; function truncate(str: string, length = 70): string { const truncated = str.slice(0, length); return length < str.length ? `${truncated}…` : str; } type ValueTypes = ValueProps['type']; type PropsByValueType = Omit< Extract, 'type' >; const VALUE_COLOR_BY_THEME_AND_TYPE: Record< Theme, Partial> > = { [Theme.Dark]: { Int32: variantColors.dark[9], Double: variantColors.dark[9], Decimal128: variantColors.dark[9], Date: variantColors.dark[9], Boolean: variantColors.dark[10], String: variantColors.dark[7], ObjectId: variantColors.dark[5], }, [Theme.Light]: { Int32: variantColors.light[9], Double: variantColors.light[9], Decimal128: variantColors.light[9], Date: variantColors.light[9], Boolean: variantColors.light[10], String: variantColors.light[7], ObjectId: variantColors.light[5], }, }; const bsonValue = css({ whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis', }); const bsonValuePrewrap = css({ whiteSpace: 'pre-wrap', }); export const BSONValueContainer: React.FunctionComponent< React.HTMLProps & { type?: ValueTypes; chidren?: React.ReactChildren; } > = ({ type, children, className, ...props }) => { const darkMode = useDarkMode(); const textColorStyle = useMemo(() => { if (!type) { return ''; } const color = VALUE_COLOR_BY_THEME_AND_TYPE[darkMode ? Theme.Dark : Theme.Light]?.[ type ]; if (!color) { return ''; } return css({ color }); }, [type, darkMode]); return (
{children}
); }; const nonSelectable = css({ userSelect: 'none', }); const encryptedHelpLinkStyle = css({ color: 'inherit', marginLeft: spacing[1], }); export const ObjectIdValue: React.FunctionComponent< PropsByValueType<'ObjectId'> > = ({ value }) => { const stringifiedValue = useMemo(() => { return String(value); }, [value]); return ( ObjectId(' {stringifiedValue} ') ); }; export const BinaryValue: React.FunctionComponent< PropsByValueType<'Binary'> > = ({ value }) => { const { stringifiedValue, title, additionalHints } = useMemo(() => { if (value.sub_type === Binary.SUBTYPE_ENCRYPTED) { return { stringifiedValue: '*********', title: 'Encrypted', additionalHints: ( ), }; } if (value.sub_type === Binary.SUBTYPE_UUID) { let uuid: string; try { // Try to get the pretty hex version of the UUID uuid = value.toUUID().toString(); } catch { // If uuid is not following the uuid format converting it to UUID will // fail, we don't want the UI to fail rendering it and instead will // just display "unformatted" hex value of the binary whatever it is uuid = value.toString('hex'); } return { stringifiedValue: `UUID('${uuid}')` }; } return { // The shell displays these values as Binary(Buffer.from(, 'hex'), ). // This would be a bit nicer here, since we want to encourage usage of standard Node.js // driver types, but Buffer.from() is not something that Compass understands (and maybe // shouldn't understand) in text input, and passing a string to Binary() is a legacy // concept as well. stringifiedValue: `BinData(${value.sub_type}, '${truncate( value.toString('base64'), 100 )}')`, }; }, [value]); return ( {stringifiedValue} {additionalHints} ); }; export const CodeValue: React.FunctionComponent> = ({ value, }) => { const stringifiedValue = useMemo(() => { return `Code('${String(value.code)}'${ value.scope ? `, ${JSON.stringify(value.scope)}` : '' })`; }, [value.code, value.scope]); return ( {stringifiedValue} ); }; export const DateValue: React.FunctionComponent> = ({ value, }) => { const stringifiedValue = useMemo(() => { try { return new Date(value).toISOString().replace('Z', '+00:00'); } catch { return String(value); } }, [value]); return ( {stringifiedValue} ); }; export const NumberValue: React.FunctionComponent< PropsByValueType<'Int32' | 'Double'> & { type: 'Int32' | 'Double' } > = ({ type, value }) => { const stringifiedValue = useMemo(() => { return String(value.valueOf()); }, [value]); return ( {stringifiedValue} ); }; export const StringValue: React.FunctionComponent< PropsByValueType<'String'> > = ({ value }) => { const truncatedValue = useMemo(() => { return truncate(value, 70); }, [value]); return ( "{truncatedValue}" ); }; export const RegExpValue: React.FunctionComponent< PropsByValueType<'BSONRegExp'> > = ({ value }) => { const stringifiedValue = useMemo(() => { return `/${value.pattern}/${value.options}`; }, [value.pattern, value.options]); return ( {stringifiedValue} ); }; export const TimestampValue: React.FunctionComponent< PropsByValueType<'Timestamp'> > = ({ value }) => { const stringifiedValue = useMemo(() => { return `Timestamp({ t: ${value.getHighBits()}, i: ${value.getLowBits()} })`; }, [value]); return ( {stringifiedValue} ); }; export const KeyValue: React.FunctionComponent<{ type: 'MinKey' | 'MaxKey'; }> = ({ type }) => { const stringifiedValue = useMemo(() => { return `${type}()`; }, [type]); return ( {stringifiedValue} ); }; export const DBRefValue: React.FunctionComponent> = ({ value, }) => { const stringifiedValue = useMemo(() => { return `DBRef('${value.collection}', '${String(value.oid)}'${ value?.db ? `, '${value.db}'` : '' })`; }, [value.collection, value.oid, value.db]); return ( {stringifiedValue} ); }; export const SymbolValue: React.FunctionComponent< PropsByValueType<'BSONSymbol'> > = ({ value }) => { const stringifiedValue = useMemo(() => { return `Symbol('${String(value)}')`; }, [value]); return ( {stringifiedValue} ); }; export const UnknownValue: React.FunctionComponent<{ type: string; value: unknown; }> = ({ value }) => { const stringifiedValue = useMemo(() => { return String(value); }, [value]); return ( {stringifiedValue} ); }; const BSONValue: React.FunctionComponent = (props) => { switch (props.type) { case 'ObjectId': return ; case 'Date': return ; case 'Binary': return ; case 'Int32': case 'Double': return ; case 'String': return ; case 'BSONRegExp': return ; case 'Code': return ; case 'MinKey': case 'MaxKey': return ; case 'DBRef': return ; case 'Timestamp': return ; case 'BSONSymbol': return ; case 'Object': case 'Array': return ; default: return ( ); } }; export default BSONValue;