@@ -9,6 +9,7 @@ import {useState, useMemo, useCallback} from 'preact/hooks';
9
9
import { Link , route } from 'preact-router' ;
10
10
import clsx from 'clsx' ;
11
11
import './build-view.css' ;
12
+ import * as _ from '@lhci/utils/src/lodash.js' ;
12
13
13
14
import { AsyncLoader , combineLoadingStates , combineAsyncData } from '../../components/async-loader' ;
14
15
import {
@@ -28,14 +29,32 @@ import {LoadingSpinner} from '../../components/loading-spinner';
28
29
import { LhrComparison } from './lhr-comparison.jsx' ;
29
30
import { Dropdown } from '../../components/dropdown' ;
30
31
32
+ /**
33
+ * @param {{compareUrl?: string, runs: Array<LHCI.ServerCommand.Run>} } props
34
+ * @param {Array<LHCI.ServerCommand.Run> } compareRuns
35
+ * @return {string } */
36
+ function computeSelectedUrl ( props , compareRuns ) {
37
+ if ( props . compareUrl ) return props . compareUrl ;
38
+ if ( ! compareRuns . length ) return '' ;
39
+
40
+ // Choose the shortest URL that exists in both of the builds fallingback to whatever was available in the compare.
41
+ const fallbackUrl = compareRuns [ 0 ] . url ;
42
+ const groupedUrls = _ . groupBy ( props . runs , run => run . url ) ;
43
+ const urlsInBothRuns = groupedUrls
44
+ . filter ( group => new Set ( group . map ( entry => entry . buildId ) ) . size > 1 )
45
+ . map ( group => group [ 0 ] . url )
46
+ . sort ( ( a , b ) => a . length - b . length ) ;
47
+ return urlsInBothRuns [ 0 ] || fallbackUrl ;
48
+ }
49
+
31
50
/** @param {{project: LHCI.ServerCommand.Project, build: LHCI.ServerCommand.Build, ancestorBuild: LHCI.ServerCommand.Build | null, runs: Array<LHCI.ServerCommand.Run>, compareUrl?: string, hasBaseOverride: boolean} } props */
32
51
const BuildView_ = props => {
33
52
const [ openBuildHash , setOpenBuild ] = useState ( /** @type {null|'base'|'compare' } */ ( null ) ) ;
34
53
const [ isOpenLhrLinkHovered , setLhrLinkHover ] = useState ( false ) ;
35
- const selectedUrl = props . compareUrl || ( props . runs [ 0 ] && props . runs [ 0 ] . url ) ;
36
54
const buildHashSelectorCloseFn = useCallback ( ( ) => setOpenBuild ( null ) , [ setOpenBuild ] ) ;
37
55
38
56
const compareRuns = props . runs . filter ( run => run . buildId === props . build . id ) ;
57
+ const selectedUrl = computeSelectedUrl ( props , compareRuns ) ;
39
58
const availableUrls = [ ...new Set ( compareRuns . map ( run => run . url ) ) ] ;
40
59
const run = compareRuns . find ( run => run . url === selectedUrl ) ;
41
60
0 commit comments