<commit_before>import { sortBy } from 'lodash';
import React from 'react';

import { HoverablePopover, Markdown } from 'core/presentation';

import { CurrentVersion } from './CurrentVersion';
import { PendingVersion } from './PendingVersion';
import { EnvironmentItem } from '../../environmentBaseElements/EnvironmentItem';
import { QueryArtifact, QueryArtifactVersion } from '../types';
import { TOOLTIP_DELAY } from '../../utils/defaults';

import './Artifact.less';

type RequiredKeys<T, K extends keyof T> = Exclude<T, K> & Required<Pick<T, K>>;

const hasCreatedAt = (version?: QueryArtifactVersion): version is RequiredKeys<QueryArtifactVersion, 'createdAt'> => {
  return Boolean(version?.createdAt);
};

const filterPendingVersions = (versions: QueryArtifact['versions'], currentVersion?: QueryArtifactVersion) => {
  if (!hasCreatedAt(currentVersion)) {
    // Everything is newer than current
    return versions;
  }
  const currentVersionCreatedAt = new Date(currentVersion.createdAt);
  const newerVersions = versions
    ?.filter(hasCreatedAt)
    ?.filter((version) => new Date(version.createdAt) > currentVersionCreatedAt || version.status === 'DEPLOYING');
  // Sort from newest to oldest
  return sortBy(newerVersions || [], (version) => -1 * new Date(version.createdAt).getTime());
};

export const PinnedVersion = ({ version }: { version: NonNullable<QueryArtifact['pinnedVersion']> }) => {
  const commitMessage = version.gitMetadata?.commitInfo?.message;
  const build = `#${version.buildNumber}`;
  return (<commit_msg>fix(md): sort versions even when no current exists (#9223)<commit_after>import { orderBy } from 'lodash';
import React from 'react';

import { HoverablePopover, Markdown } from 'core/presentation';

import { CurrentVersion } from './CurrentVersion';
import { PendingVersion } from './PendingVersion';
import { EnvironmentItem } from '../../environmentBaseElements/EnvironmentItem';
import { QueryArtifact, QueryArtifactVersion } from '../types';
import { TOOLTIP_DELAY } from '../../utils/defaults';

import './Artifact.less';

type RequiredKeys<T, K extends keyof T> = Exclude<T, K> & Required<Pick<T, K>>;

const hasCreatedAt = (version?: QueryArtifactVersion): version is RequiredKeys<QueryArtifactVersion, 'createdAt'> => {
  return Boolean(version?.createdAt);
};

const sortVersions = (versions: QueryArtifact['versions']) => {
  return orderBy(versions || [], (version) => (version.createdAt ? new Date(version.createdAt).getTime() : 0), [
    'desc',
  ]);
};

const filterPendingVersions = (versions: QueryArtifact['versions'], currentVersion?: QueryArtifactVersion) => {
  if (!hasCreatedAt(currentVersion)) {
    // Everything is newer than current
    return sortVersions(versions);
  }
  const currentVersionCreatedAt = new Date(currentVersion.createdAt);
  const newerVersions = versions
    ?.filter(hasCreatedAt)
    ?.filter((version) => new Date(version.createdAt) > currentVersionCreatedAt || version.status === 'DEPLOYING');

  return sortVersions(newerVersions);
};

export const PinnedVersion = ({ version }: { version: NonNullable<QueryArtifact['pinnedVersion']> }) => {
  const commitMessage = version.gitMetadata?.commitInfo?.message;
  const build = `#${version.buildNumber}`;
  return (
{'fraction_non_alphanumeric': 0.1447004608294931, 'fraction_numerical': 0.0018433179723502304, 'mean_word_length': 5.434782608695652, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '533116', 'n_tokens_mistral': 922, 'n_tokens_neox': 912, 'n_words': 229}