Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/components/map-projects/MapProject.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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({})
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -6602,7 +6613,7 @@ const MapProject = () => {
bridgeRepoVersion={bridgeProbeAlgo?.target_repo_version}
limit={CANDIDATES_LIMIT}
user={user}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this until OpenConceptLab/react-bridge-match#7 is published: the live file still reads user. After that, nothing reads it and it can be dropped.

ref={bridgeRef}
ref={setBridgeMatch}
mappedRepoURLs={mappedSrcs.map(source => source.url)}
/>
})()
Expand Down
16 changes: 15 additions & 1 deletion src/components/map-projects/__tests__/autoMatchRows.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/components/map-projects/autoMatchRows.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
Loading