import React from 'react';
import {Flex} from '@gravity-ui/uikit';
import {isEmpty} from 'lodash';
import {InternalLink} from '../../../../../components/InternalLink';
import {useTabletPagePath} from '../../../../../routes';
import type {Location} from '../../../../../types/api/healthcheck';
import {useHealthcheckContext} from '../../HealthcheckContext';
import i18n from '../../i18n';
import {NodeInfo} from './NodeInfo';
import {PoolInfo} from './PoolInfo';
import {IdList, LocationDetails, SectionWithTitle} from './utils';
export type LocationFieldCompute = 'tablet' | 'schema' | 'node' | 'pool' | 'pile' | 'state_storage';
type ComputeLocationType = Location['compute'];
const LocationFieldRenderer: Record<
LocationFieldCompute,
(location: ComputeLocationType) => React.ReactNode
> = {
node: (location: ComputeLocationType) => ,
pool: (location: ComputeLocationType) => ,
tablet: (location: ComputeLocationType) => ,
schema: (location: ComputeLocationType) => ,
pile: (location: ComputeLocationType) => (
),
state_storage: (location: ComputeLocationType) => (
),
};
interface ComputeLocationProps {
location: ComputeLocationType;
hiddenFields?: LocationFieldCompute[];
}
export function ComputeLocation({location, hiddenFields = []}: ComputeLocationProps) {
const {node, tablet, schema, pool, pile, state_storage: stateStorage} = location ?? {};
const fields = React.useMemo(() => {
const fields: LocationFieldCompute[] = [];
if (node) {
fields.push('node');
}
if (pool) {
fields.push('pool');
}
if (tablet) {
fields.push('tablet');
}
if (schema) {
fields.push('schema');
}
if (pile) {
fields.push('pile');
}
if (stateStorage) {
fields.push('state_storage');
}
return fields.filter((field) => !hiddenFields.includes(field));
}, [node, pool, tablet, schema, pile, stateStorage, hiddenFields]);
if (!location || isEmpty(location) || fields.length === 0) {
return null;
}
return (
{fields.map((field) => LocationFieldRenderer[field](location))}
);
}
interface ComputeSectionProps {
location?: ComputeLocationType;
}
function TabletInfo({location}: ComputeSectionProps) {
const {clusterName, database} = useHealthcheckContext();
const getTabletPagePath = useTabletPagePath(database);
const {tablet} = location ?? {};
if (!tablet) {
return null;
}
return (
(
{id}
)}
/>
) : undefined,
title: i18n('label_tablet-id'),
},
{value: tablet.type, title: i18n('label_tablet-type')},
{value: tablet.count, title: i18n('label_tablet-count')},
]}
/>
);
}
function SchemaInfo({location}: ComputeSectionProps) {
const {schema} = location ?? {};
if (!schema) {
return null;
}
return (
);
}
function StateStorageInfo({location}: ComputeSectionProps) {
const stateStorage = location?.state_storage;
if (!stateStorage) {
return null;
}
return (
);
}