import type { ComponentProps, ElementType, ReactNode } from 'react'; import React, { useMemo } from 'react'; import cx from 'classnames'; import noop from 'lodash/noop'; import type { WebpackChunk } from '@bundle-stats/utils'; import { FILE_TYPE_LABELS, getBundleModulesByChunk, getBundleAssetsByEntryType, getBundleAssetsFileTypeComponentLink, getModuleFileType, getBundleAssetsByChunk, } from '@bundle-stats/utils'; import type { ReportMetricAssetRow } from '../../types'; import { FlexStack } from '../../layout/flex-stack'; import { AssetMetaTag } from '../asset-meta-tag'; import { ComponentLink } from '../component-link'; import { EntryInfo, EntryInfoMetaLink } from '../entry-info'; import * as I18N from './asset-info.i18n'; import css from './asset-info.module.css'; interface ChunkModulesLinkProps { as: ElementType; chunk: WebpackChunk; chunkIds: Array; name: string; } const ChunkModulesLink = ({ as: Component, chunk, chunkIds, name, onClick, }: ChunkModulesLinkProps & ComponentProps<'a'>) => { const fileType = getModuleFileType(name); return ( View chunk modules ); }; type AssetRunNameProps = { run: ReportMetricAssetRow; children: ReactNode; }; const AssetRunName = (props: AssetRunNameProps) => { const { run, children } = props; return ( <> {run.isEntry && } {run.isInitial && } {run.isChunk && } {children} ); }; interface AssetInfoProps { item: ReportMetricAssetRow; chunks?: Array; labels: Array; customComponentLink?: ElementType; onClose: () => void; } export const AssetInfo = (props: AssetInfoProps & ComponentProps<'div'>) => { const { className = '', chunks = null, customComponentLink: CustomComponentLink = ComponentLink, item, labels, onClick = noop, onClose, } = props; const currentRun = item.runs?.[0]; const tags = useMemo(() => { if (!item.isEntry && !item.isInitial && !item.isChunk) { return null; } return ( {item.isEntry && ( {I18N.ENTRYPOINT} )} {item.isInitial && ( {I18N.INITIAL} )} {item.isChunk && ( {I18N.CHUNK} )} ); }, [item]); const fileTypeLabel = FILE_TYPE_LABELS[item.fileType as keyof typeof FILE_TYPE_LABELS]; const currentChunk = chunks?.find( (chunk) => currentRun?.chunkId && currentRun.chunkId === chunk.id, ); const chunkIds: Array = chunks?.filter(Boolean).map((chunk) => chunk.id) || []; return ( {item.fileType && ( {fileTypeLabel} )} {currentChunk && ( {currentChunk.name} )} {currentChunk && (
)}
); };