diff --git a/src/components/map-projects/MapProject.jsx b/src/components/map-projects/MapProject.jsx index 5db29a0..cf3424d 100644 --- a/src/components/map-projects/MapProject.jsx +++ b/src/components/map-projects/MapProject.jsx @@ -90,7 +90,7 @@ import MapProjectDeleteConfirmDialog from './MapProjectDeleteConfirmDialog'; import ConfigurationForm from './ConfigurationForm' import Controls from './Controls' import DataGridControls from './DataGridControls' -import { getPreviewEligibleRowIndexes, getRowsToProcess, countAIOnlyRows, getAutoMatchAIOnOpen, takeRunRows, clearRunRowStatuses, canRunAlgorithm, spendsMatchQuota, shouldStopAIStep, getAIRequestIdempotencyKey, getCandidatePoolFingerprint, hasCurrentAnalysis, getScispacyRowResults, getPendingRowLookups, waitForLookups, AI_LOOKUP_WAIT_MS, RERANK_LOOKUP_WAIT_MS, RERANK_MAX_IN_FLIGHT, isScispacyWarmingUp } from './autoMatchRows' +import { getPreviewEligibleRowIndexes, getRowsToProcess, countAIOnlyRows, getAutoMatchAIOnOpen, takeRunRows, clearRunRowStatuses, canRunAlgorithm, getBridgeToLoad, spendsMatchQuota, shouldStopAIStep, getAIRequestIdempotencyKey, getCandidatePoolFingerprint, hasCurrentAnalysis, getScispacyRowResults, getPendingRowLookups, waitForLookups, AI_LOOKUP_WAIT_MS, RERANK_LOOKUP_WAIT_MS, RERANK_MAX_IN_FLIGHT, isScispacyWarmingUp } from './autoMatchRows' import { aiOnByDefault, getMaxRunRows, getMapperSettings } from '@openconceptlab/oclmap-extensions' import { createAutosaveScheduler, createLatestSender, isToldOnLeave, recordReadOnlyRefusal, saveOnLeave, sendAfter, trackSave, whenSaved, installUnloadGuard, SAVE_SKIPPED } from './autosave' import { blockLeaving, continueLeaving, createEditTracker, createRunClosers, getRunLeaveMessageKey, isUnsavedNewProject, shouldAskBeforeLeaving, shouldWarnBeforeUnload } from './leaveGuard' @@ -195,6 +195,18 @@ const MapProject = () => { }, [location.search]) const bridgeRef = React.useRef() + // In state, so that the page shows BridgeMatch's answer, which comes only once it has loaded. + // It sets its ref on every render, so null alone doesn't mean that it's gone. + const [canBridge, setCanBridge] = React.useState(false) + const setBridgeMatch = React.useCallback(bridgeMatch => { + bridgeRef.current = bridgeMatch + if(bridgeMatch) + setCanBridge(Boolean(bridgeMatch.canBridge())) + }, []) + React.useEffect(() => { + if(!bridgeRef.current) + setCanBridge(false) + }) // Column mapping of the project being copied, applied to the next upload. const templateColumnsRef = React.useRef([]) const facetsRequestsRef = React.useRef({}) @@ -790,7 +802,6 @@ const MapProject = () => { // gets the project's template and its default model. const { canSelectAIModel, canSetAIOutputLocale } = getAIAssistantChoices(user) const CANDIDATES_LIMIT = 15 - const canBridge = bridgeRef?.current?.canBridge() const canScispacy = Boolean( hasCapability(user, 'users.mapper_scispacy') && canBridge && SCISPACY_API_URL && toggles.SCISPACY_LOINC_TOGGLE === true ) @@ -842,7 +853,7 @@ const MapProject = () => { // canBridge comes from the BridgeMatch component, which only mounts once it has a // bridge repo. Before any bridge algo is selected (new project) fall back to the // one the API offers, otherwise the option can never be enabled to select it. - const bridgeProbeAlgo = bridgeAlgo || find(apiAlgos, a => ['ocl-bridge', 'ocl-ciel-bridge'].includes(a.type)) + const bridgeProbeAlgo = getBridgeToLoad(bridgeAlgo, apiAlgos) React.useEffect(() => { if (!OCL_ONLINE_API_URL) return; @@ -6602,7 +6613,7 @@ const MapProject = () => { bridgeRepoVersion={bridgeProbeAlgo?.target_repo_version} limit={CANDIDATES_LIMIT} user={user} - ref={bridgeRef} + ref={setBridgeMatch} mappedRepoURLs={mappedSrcs.map(source => source.url)} /> })() diff --git a/src/components/map-projects/__tests__/autoMatchRows.test.js b/src/components/map-projects/__tests__/autoMatchRows.test.js index 79249f4..06191cc 100644 --- a/src/components/map-projects/__tests__/autoMatchRows.test.js +++ b/src/components/map-projects/__tests__/autoMatchRows.test.js @@ -4,7 +4,7 @@ import assert from 'node:assert/strict' import { getPreviewEligibleRowIndexes, getRowsToProcess, spendsMatchQuota, takeRunRows, clearRunRowStatuses, shouldStopAIStep, getAIRequestIdempotencyKey, getCandidatePoolFingerprint, hasCurrentAnalysis, getScispacyRowResults, getPendingRowLookups, waitForLookups, RERANK_LOOKUP_WAIT_MS, AI_LOOKUP_WAIT_MS, isScispacyWarmingUp, RERANK_MAX_IN_FLIGHT, - countAIOnlyRows, getAutoMatchBlocker, getAutoMatchAIDefault, getAutoMatchAIOnOpen, canRunAlgorithm + countAIOnlyRows, getAutoMatchBlocker, getAutoMatchAIDefault, getAutoMatchAIOnOpen, canRunAlgorithm, getBridgeToLoad } from '../autoMatchRows.js' import { createLimiter } from '../../../services/capacity.js' @@ -404,6 +404,20 @@ test('canRunAlgorithm: a bridge runs only for a user who can bridge', () => { }) }) +test('getBridgeToLoad: a project\'s own bridge loads only for a user still offered it', () => { + const saved = { id: 'bridge', type: 'ocl-bridge', target_repo_url: '/orgs/A/sources/Saved/' } + const offered = { id: 'bridge', type: 'ocl-bridge', target_repo_url: '/orgs/A/sources/Offered/' } + assert.equal(getBridgeToLoad(saved, [{ id: 'search', type: 'ocl-search' }, offered]), saved) + assert.equal(getBridgeToLoad(saved, [{ id: 'other', type: 'ocl-bridge' }]), undefined) + assert.equal(getBridgeToLoad(saved, []), undefined) +}) + +test('getBridgeToLoad: a project without one loads the first bridge the user could add', () => { + const offered = [{ id: 'search', type: 'ocl-search' }, { id: 'first', type: 'ocl-ciel-bridge' }, { id: 'second', type: 'ocl-bridge' }] + assert.equal(getBridgeToLoad(undefined, offered).id, 'first') + assert.equal(getBridgeToLoad(undefined, undefined), undefined) +}) + test('canRunAlgorithm: ScispaCy runs only for a user who can run it', () => { assert.equal(canRunAlgorithm({ type: 'ocl-scispacy' }, { canBridge: true }), false) assert.equal(canRunAlgorithm({ type: 'ocl-scispacy' }, { canScispacy: true }), true) diff --git a/src/components/map-projects/autoMatchRows.js b/src/components/map-projects/autoMatchRows.js index f01e813..c0027bb 100644 --- a/src/components/map-projects/autoMatchRows.js +++ b/src/components/map-projects/autoMatchRows.js @@ -55,6 +55,14 @@ export const canRunAlgorithm = (algo, { canBridge = false, canScispacy = false } return ['custom', 'ocl-search', 'ocl-semantic'].includes(algo?.type) } +// The bridge to load: the project's own, if this user is still offered it, or, +// in a project without one, the first they could add. +export const getBridgeToLoad = (projectBridge, offeredAlgos) => { + if(projectBridge) + return offeredAlgos?.some(algo => algo?.id === projectBridge.id) ? projectBridge : undefined + return offeredAlgos?.find(algo => ['ocl-bridge', 'ocl-ciel-bridge'].includes(algo?.type)) +} + // The rows a run includes: the first maxRows of them, or all of them when // maxRows is null. export const takeRunRows = (rows, maxRows) => {