import * as React from 'react';

interface TruncateProps {
  text: string;
  chars?: number;
}

export class Truncate extends React.Component<TruncateProps> {
  public shouldComponentUpdate(nextProps: TruncateProps): boolean {
    return this.props.text !== nextProps.text;
  }

  public render() {
    const { text, chars } = this.props;

    if (!text) {
      return '-';
    }

    if (chars != null && text.length <= chars) {
      return text;
    }

    return chars ? (
      `${text.substr(0, chars - 1)}…`
    ) : (
      <div className="Column-truncate">{text}</div>
    );
  }
}
