import React from 'react';
import {
Body,
Cell,
Checkbox,
Row,
Table,
TableHeader,
css,
cx,
spacing,
Label,
palette,
Tooltip,
Icon,
IconButton,
Select,
Option,
useDarkMode,
} from '@mongodb-js/compass-components';
import { createDebug } from '../utils/logger';
import type { CSVParsableFieldType, CSVField } from '../csv/csv-types';
import { CSVFieldTypeLabels } from '../csv/csv-types';
import { findBrokenCSVTypeExample } from '../csv/csv-utils';
const debug = createDebug('import-preview');
// the max length of a value in the preview
const MAX_VALUE_LENGTH = 80;
// the max length in a cell in the preview table
const MAX_PREVIEW_LENGTH = 1000;
const columnHeaderStyles = css({
display: 'flex',
gap: spacing[1],
minWidth: spacing[6] * 2,
flexDirection: 'column',
alignItems: 'flex-start',
});
const warningCellStylesLight = css({
backgroundColor: palette.yellow.light3,
});
const warningCellStylesDark = css({
backgroundColor: palette.yellow.dark3,
});
const columnNameStyles = css({
display: 'flex',
flexDirection: 'row',
});
const fieldPathHeaderStyles = css({
maxWidth: spacing[7] * 3,
overflow: 'hidden',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
});
const cellContainerStyles = css({
padding: `${spacing[1]}px ${spacing[2]}px`,
});
const cellStyles = css({
maxWidth: spacing[7] * 3,
overflow: 'hidden',
whiteSpace: 'nowrap',
textOverflow: 'ellipsis',
});
const cellUncheckedStyles = css({
opacity: 0.4,
});
const rowIndexStyles = css({
minWidth: 0,
color: palette.gray.base,
});
const fieldTypeContainerStyles = css({
display: 'flex',
alignItems: 'center',
gap: spacing[1],
});
const infoIconCommonStyles = css({
// Hack: Align the icon relative to the SelectBox.
marginBottom: `-${spacing[1]}px`,
marginTop: `-${spacing[1]}px`,
});
const infoIconLightStyles = css({
color: palette.gray.dark2,
});
const infoIconDarkStyles = css({
color: palette.gray.light2,
});
const warningIconCommonStyles = css(infoIconCommonStyles, {
marginLeft: spacing[1],
marginRight: spacing[1],
paddingTop: spacing[1],
});
const warningIconLightStyles = css({
color: palette.red.base,
});
const warningIconDarkStyles = css({
color: palette.red.light1,
});
const typesListStyles = css({
margin: `${spacing[3]}px 0`,
});
const selectStyles = css({
minWidth: spacing[3] * 9,
});
const arrayTextStyles = css({
fontWeight: 'normal',
whiteSpace: 'nowrap',
});
function fieldTypeName(type: CSVParsableFieldType | 'undefined') {
if (type === 'undefined') {
return 'Blank';
}
return CSVFieldTypeLabels[type];
}
function needsMixedWarning(field: Field) {
// Only show the warning for mixed and number types and once the user manually
// changed the type, make the warning go away
return (
field.result &&
['mixed', 'number'].includes(field.result.detected) &&
field.result.detected === field.type
);
}
function needsTypeWarning(field: Field) {
return !!(
field.result && findBrokenCSVTypeExample(field.result.types, field.type)
);
}
function needsWarning(field: Field) {
return needsMixedWarning(field) || needsTypeWarning(field);
}
function SelectFieldType({
fieldPath,
selectedType,
onChange,
}: {
fieldPath: string;
selectedType: CSVParsableFieldType;
onChange: (type: string) => void;
}) {
return (
);
}
type Field = {
isArray?: boolean;
path: string;
type: CSVParsableFieldType;
checked: boolean;
result?: CSVField;
};
function InfoIcon() {
const darkMode = useDarkMode();
return (