Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,7 @@ import {
useTrackOnChange,
type TrackFunction,
} from '@mongodb-js/compass-telemetry/provider';
import {
SCHEMA_ANALYSIS_STATE_ANALYZING,
type SchemaAnalysisStatus,
type SchemaAnalysisError,
} from '../../schema-analysis-types';
import { type SchemaAnalysisError } from '../../schema-analysis-types';
import { MAX_COLLECTION_NESTING_DEPTH } from '../mock-data-generator-modal/utils';
import {
buildChartsUrl,
Expand Down Expand Up @@ -61,7 +57,8 @@ type CollectionHeaderActionsProps = {
hasSchemaAnalysisData: boolean;
schemaAnalysisError: SchemaAnalysisError | null;
analyzedSchemaDepth: number;
schemaAnalysisStatus: SchemaAnalysisStatus | null;
isCollectionEmpty: boolean;
hasUnsupportedStateError: boolean;
};

const CollectionHeaderActions: React.FunctionComponent<
Expand All @@ -76,8 +73,9 @@ const CollectionHeaderActions: React.FunctionComponent<
onOpenMockDataModal,
hasSchemaAnalysisData,
analyzedSchemaDepth,
schemaAnalysisStatus,
schemaAnalysisError,
isCollectionEmpty,
hasUnsupportedStateError,
}: CollectionHeaderActionsProps) => {
const connectionInfo = useConnectionInfo();
const { id: connectionId, atlasMetadata } = connectionInfo;
Expand Down Expand Up @@ -117,21 +115,13 @@ const CollectionHeaderActions: React.FunctionComponent<
const exceedsMaxNestingDepth =
analyzedSchemaDepth > MAX_COLLECTION_NESTING_DEPTH;

const isCollectionEmpty =
!hasSchemaAnalysisData &&
schemaAnalysisStatus !== SCHEMA_ANALYSIS_STATE_ANALYZING;

const hasSchemaAnalysisUnsupportedStateError = Boolean(
schemaAnalysisError && schemaAnalysisError.errorType === 'unsupportedState'
);

const isView = isReadonly && sourceName && !editViewName;

const showViewEdit = isView && !preferencesReadWrite;
const shouldDisableMockDataButton =
!hasSchemaAnalysisData ||
exceedsMaxNestingDepth ||
hasSchemaAnalysisUnsupportedStateError;
hasUnsupportedStateError;

const onMockDataGeneratorCtaButtonClicked = useCallback(() => {
track('Mock Data Generator Opened', {
Expand Down Expand Up @@ -189,7 +179,7 @@ const CollectionHeaderActions: React.FunctionComponent<
enabled={
exceedsMaxNestingDepth ||
isCollectionEmpty ||
hasSchemaAnalysisUnsupportedStateError
hasUnsupportedStateError
}
trigger={
<div>
Expand All @@ -206,7 +196,7 @@ const CollectionHeaderActions: React.FunctionComponent<
}
>
<>
{hasSchemaAnalysisUnsupportedStateError ? (
{hasUnsupportedStateError ? (
<span className={tooltipMessageStyles}>
{schemaAnalysisError?.errorMessage}
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ import { connect } from 'react-redux';
import { openMockDataGeneratorModal } from '../../modules/collection-tab';
import type { CollectionState } from '../../modules/collection-tab';
import {
SCHEMA_ANALYSIS_STATE_COMPLETE,
SCHEMA_ANALYSIS_STATE_ERROR,
SCHEMA_ANALYSIS_STATE_COMPLETE,
type SchemaAnalysisStatus,
type SchemaAnalysisError,
} from '../../schema-analysis-types';
import {
selectHasSchemaAnalysisData,
selectIsCollectionEmpty,
selectHasUnsupportedStateError,
} from '../../stores/collection-tab';

const collectionHeaderStyles = css({
padding: spacing[400],
Expand Down Expand Up @@ -73,6 +78,8 @@ type CollectionHeaderProps = {
analyzedSchemaDepth: number;
schemaAnalysisStatus: SchemaAnalysisStatus | null;
schemaAnalysisError: SchemaAnalysisError | null;
isCollectionEmpty: boolean;
hasUnsupportedStateError: boolean;
};

const getInsightsForPipeline = (pipeline: any[], isAtlas: boolean) => {
Expand Down Expand Up @@ -110,8 +117,9 @@ const CollectionHeader: React.FunctionComponent<CollectionHeaderProps> = ({
onOpenMockDataModal,
hasSchemaAnalysisData,
analyzedSchemaDepth,
schemaAnalysisStatus,
schemaAnalysisError,
isCollectionEmpty,
hasUnsupportedStateError,
}) => {
const darkMode = useDarkMode();
const showInsights = usePreference('showInsights');
Expand Down Expand Up @@ -192,8 +200,9 @@ const CollectionHeader: React.FunctionComponent<CollectionHeaderProps> = ({
onOpenMockDataModal={onOpenMockDataModal}
hasSchemaAnalysisData={hasSchemaAnalysisData}
analyzedSchemaDepth={analyzedSchemaDepth}
schemaAnalysisStatus={schemaAnalysisStatus}
schemaAnalysisError={schemaAnalysisError}
isCollectionEmpty={isCollectionEmpty}
hasUnsupportedStateError={hasUnsupportedStateError}
/>
</div>
<MockDataGeneratorModal />
Expand All @@ -209,15 +218,14 @@ const mapStateToProps = (state: CollectionState) => {
schemaAnalysis && schemaAnalysis.status === SCHEMA_ANALYSIS_STATE_ERROR
? schemaAnalysis.error
: null,
hasSchemaAnalysisData:
schemaAnalysis &&
schemaAnalysis.status === SCHEMA_ANALYSIS_STATE_COMPLETE &&
Object.keys(schemaAnalysis.processedSchema).length > 0,
hasSchemaAnalysisData: selectHasSchemaAnalysisData(state),
analyzedSchemaDepth:
schemaAnalysis && schemaAnalysis.status === SCHEMA_ANALYSIS_STATE_COMPLETE
? schemaAnalysis.schemaMetadata.maxNestingDepth
: 0,
schemaAnalysisStatus: schemaAnalysis?.status || null,
isCollectionEmpty: selectIsCollectionEmpty(state),
hasUnsupportedStateError: selectHasUnsupportedStateError(state),
};
};

Expand Down
Loading
Loading