import React from 'react';
import PropTypes from 'prop-types';
import { withTranslation } from 'react-i18next';
import { createSubscribedElement } from '../../UnstatedUtils';
class StatusTable extends React.PureComponent {
renderIndexInfoPanel(indexName, body = {}, aliases = []) {
const collapseId = `collapse-${indexName}`;
const aliasLabels = aliases.map((aliasName) => {
return (
{aliasName}
);
});
return (
{JSON.stringify(body, null, 2)}
);
}
renderIndexInfoPanels() {
const {
indicesData,
aliasesData,
} = this.props;
// data is null
if (indicesData == null) {
return null;
}
/*
"indices": {
"growi": {
...
}
},
*/
const indexNameToDataMap = {};
for (const [indexName, indexData] of Object.entries(indicesData)) {
indexNameToDataMap[indexName] = indexData;
}
// no indices
if (indexNameToDataMap.length === 0) {
return null;
}
/*
"aliases": {
"growi": {
"aliases": {
"growi-alias": {}
}
}
},
*/
const indexNameToAliasMap = {};
for (const [indexName, aliasData] of Object.entries(aliasesData)) {
indexNameToAliasMap[indexName] = Object.keys(aliasData.aliases);
}
return (
{ Object.keys(indexNameToDataMap).map((indexName) => {
return (
{ this.renderIndexInfoPanel(indexName, indexNameToDataMap[indexName], indexNameToAliasMap[indexName]) }
);
}) }
);
}
render() {
const { t } = this.props;
const { isConfigured, isConnected, isNormalized } = this.props;
let connectionStatusLabel = ――;
if (isConfigured != null && !isConfigured) {
connectionStatusLabel = { t('full_text_search_management.connection_status_label_unconfigured') };
}
else if (isConnected != null) {
connectionStatusLabel = isConnected
? { t('full_text_search_management.connection_status_label_connected') }
: { t('full_text_search_management.connection_status_label_disconnected') };
}
let indicesStatusLabel = ――;
if (isNormalized != null) {
indicesStatusLabel = isNormalized
? { t('full_text_search_management.indices_status_label_normalized') }
: { t('full_text_search_management.indices_status_label_unnormalized') };
}
return (
| { t('full_text_search_management.connection_status') } |
{connectionStatusLabel} |
| { t('full_text_search_management.indices_status') } |
{indicesStatusLabel} |
| { t('full_text_search_management.indices_summary') } |
{ this.renderIndexInfoPanels() }
|
);
}
}
/**
* Wrapper component for using unstated
*/
const StatusTableWrapper = (props) => {
return createSubscribedElement(StatusTable, props, []);
};
StatusTable.propTypes = {
t: PropTypes.func.isRequired, // i18next
isConfigured: PropTypes.bool,
isConnected: PropTypes.bool,
isNormalized: PropTypes.bool,
indicesData: PropTypes.object,
aliasesData: PropTypes.object,
};
export default withTranslation()(StatusTableWrapper);