fix: preserve selected flow in contribution mode
This commit is contained in:
@@ -93,17 +93,46 @@
|
||||
}
|
||||
|
||||
function getActiveFlowId(currentState = getLatestState()) {
|
||||
return normalizeString(currentState.activeFlowId || currentState.flowId).toLowerCase() || 'openai';
|
||||
const selectedFlowId = typeof helpers.getSelectedFlowId === 'function'
|
||||
? normalizeString(helpers.getSelectedFlowId(currentState)).toLowerCase()
|
||||
: '';
|
||||
return selectedFlowId || normalizeString(currentState.activeFlowId || currentState.flowId).toLowerCase() || 'openai';
|
||||
}
|
||||
|
||||
function getActiveTargetId(currentState = getLatestState()) {
|
||||
const activeFlowId = getActiveFlowId(currentState);
|
||||
const selectedTargetId = typeof helpers.getSelectedTargetId === 'function'
|
||||
? normalizeString(helpers.getSelectedTargetId(activeFlowId, currentState)).toLowerCase()
|
||||
: '';
|
||||
if (selectedTargetId) {
|
||||
return selectedTargetId;
|
||||
}
|
||||
if (activeFlowId === 'kiro') {
|
||||
return normalizeString(currentState.kiroTargetId || currentState.targetId || 'kiro-rs').toLowerCase() || 'kiro-rs';
|
||||
}
|
||||
return normalizeString(currentState.openaiIntegrationTargetId || currentState.panelMode || currentState.targetId || 'cpa').toLowerCase() || 'cpa';
|
||||
}
|
||||
|
||||
function applySelectedFlowToState(nextState = {}, flowId = 'openai', targetId = '') {
|
||||
const selectedFlowId = normalizeString(flowId).toLowerCase() || 'openai';
|
||||
const selectedTargetId = normalizeString(targetId).toLowerCase();
|
||||
const baseState = nextState && typeof nextState === 'object' ? nextState : {};
|
||||
if (selectedFlowId === 'openai') {
|
||||
return {
|
||||
...baseState,
|
||||
activeFlowId: selectedFlowId,
|
||||
flowId: selectedFlowId,
|
||||
panelMode: selectedTargetId || normalizeString(baseState.panelMode).toLowerCase() || 'cpa',
|
||||
};
|
||||
}
|
||||
return {
|
||||
...baseState,
|
||||
activeFlowId: selectedFlowId,
|
||||
flowId: selectedFlowId,
|
||||
kiroTargetId: selectedTargetId || normalizeString(baseState.kiroTargetId || baseState.targetId).toLowerCase() || 'kiro-rs',
|
||||
};
|
||||
}
|
||||
|
||||
function getContributionTutorialEntry(currentState = getLatestState()) {
|
||||
const rootScope = typeof window !== 'undefined' ? window : globalThis;
|
||||
const registry = rootScope.MultiPageContributionRegistry || {};
|
||||
@@ -303,11 +332,7 @@
|
||||
}
|
||||
|
||||
function getContributionUploadPageUrl() {
|
||||
const currentState = getLatestState();
|
||||
if (getActiveFlowId(currentState) !== 'openai') {
|
||||
return normalizeString(getContributionTutorialEntry(currentState)?.portalUrl || contributionPortalUrl);
|
||||
}
|
||||
return normalizeString(contributionUploadUrl);
|
||||
return normalizeString(contributionUploadUrl || contributionPortalUrl);
|
||||
}
|
||||
|
||||
function openContributionPortalPage() {
|
||||
@@ -340,12 +365,17 @@
|
||||
}
|
||||
|
||||
async function requestContributionMode(enabled) {
|
||||
const selectedFlowId = getActiveFlowId();
|
||||
const selectedTargetId = getActiveTargetId();
|
||||
if (typeof helpers.persistCurrentSettingsForAction === 'function') {
|
||||
await helpers.persistCurrentSettingsForAction();
|
||||
}
|
||||
const response = await runtime.sendMessage({
|
||||
type: 'SET_ACCOUNT_CONTRIBUTION_MODE',
|
||||
source: 'sidepanel',
|
||||
payload: {
|
||||
enabled: Boolean(enabled),
|
||||
flowId: getActiveFlowId(),
|
||||
flowId: selectedFlowId,
|
||||
adapterId: getContributionEntryAdapterId(),
|
||||
},
|
||||
});
|
||||
@@ -357,8 +387,9 @@
|
||||
throw new Error('贡献模式切换后未返回最新状态。');
|
||||
}
|
||||
|
||||
helpers.applySettingsState?.(response.state);
|
||||
helpers.updateStatusDisplay?.(response.state);
|
||||
const nextState = applySelectedFlowToState(response.state, selectedFlowId, selectedTargetId);
|
||||
helpers.applySettingsState?.(nextState);
|
||||
helpers.updateStatusDisplay?.(nextState);
|
||||
render();
|
||||
}
|
||||
|
||||
@@ -494,8 +525,9 @@
|
||||
}
|
||||
|
||||
if (dom.btnOpenContributionUpload) {
|
||||
dom.btnOpenContributionUpload.hidden = !available;
|
||||
dom.btnOpenContributionUpload.disabled = !available;
|
||||
dom.btnOpenContributionUpload.textContent = activeFlowId === 'openai' ? '已有认证文件?前往上传' : '查看当前 flow 贡献说明';
|
||||
dom.btnOpenContributionUpload.textContent = '已有认证文件?前往上传';
|
||||
}
|
||||
|
||||
if (dom.btnExitContributionMode) {
|
||||
|
||||
@@ -13595,9 +13595,16 @@ const accountContributionManager = window.SidepanelContributionMode?.createContr
|
||||
nickname: String(inputContributionNickname?.value || '').trim(),
|
||||
qq: String(inputContributionQq?.value || '').trim(),
|
||||
}),
|
||||
getSelectedFlowId: () => (typeof getSelectedFlowId === 'function'
|
||||
? getSelectedFlowId(latestState)
|
||||
: String(latestState?.activeFlowId || latestState?.flowId || DEFAULT_ACTIVE_FLOW_ID).trim().toLowerCase() || DEFAULT_ACTIVE_FLOW_ID),
|
||||
getSelectedTargetId: (flowId, state = latestState) => (typeof getSelectedTargetIdForState === 'function'
|
||||
? getSelectedTargetIdForState(state, flowId)
|
||||
: (String(state?.panelMode || state?.kiroTargetId || 'cpa').trim().toLowerCase() || 'cpa')),
|
||||
isModeSwitchBlocked: isContributionModeSwitchBlocked,
|
||||
openConfirmModal,
|
||||
openExternalUrl,
|
||||
persistCurrentSettingsForAction,
|
||||
showToast,
|
||||
startContributionAutoRun: () => startAutoRunFromCurrentSettings(),
|
||||
updateAccountRunHistorySettingsUI,
|
||||
|
||||
@@ -77,6 +77,12 @@ test('contribution mode manager does not project openai-only ui state into kiro
|
||||
updateConfigMenuControls() {},
|
||||
closeConfigMenu() {},
|
||||
closeAccountRecordsPanel() {},
|
||||
getSelectedFlowId() {
|
||||
return 'kiro';
|
||||
},
|
||||
getSelectedTargetId() {
|
||||
return 'kiro-rs';
|
||||
},
|
||||
isModeSwitchBlocked() {
|
||||
return false;
|
||||
},
|
||||
@@ -98,8 +104,9 @@ test('contribution mode manager does not project openai-only ui state into kiro
|
||||
assert.equal(dom.btnContributionMode.disabled, false);
|
||||
assert.equal(dom.btnContributionMode.title, '打开当前 flow 教程;当前已在贡献模式');
|
||||
assert.equal(dom.btnStartContribution.disabled, false);
|
||||
assert.equal(dom.btnOpenContributionUpload.hidden, false);
|
||||
assert.equal(dom.btnOpenContributionUpload.disabled, false);
|
||||
assert.equal(dom.btnOpenContributionUpload.textContent, '查看当前 flow 贡献说明');
|
||||
assert.equal(dom.btnOpenContributionUpload.textContent, '已有认证文件?前往上传');
|
||||
assert.equal(rowVpsUrl.classList.hiddenState, false);
|
||||
});
|
||||
|
||||
@@ -114,15 +121,16 @@ test('combined contribution tutorial button opens current flow page and enables
|
||||
)(windowObject, windowObject, setTimeout, clearTimeout);
|
||||
|
||||
let latestState = {
|
||||
activeFlowId: 'kiro',
|
||||
flowId: 'kiro',
|
||||
activeFlowId: 'openai',
|
||||
flowId: 'openai',
|
||||
accountContributionEnabled: false,
|
||||
supportsAccountContribution: true,
|
||||
contributionAdapterId: '',
|
||||
kiroTargetId: 'kiro-rs',
|
||||
panelMode: 'sub2api',
|
||||
};
|
||||
const openedUrls = [];
|
||||
const sentMessages = [];
|
||||
let persistCount = 0;
|
||||
const dom = {
|
||||
btnContributionMode: createElement(),
|
||||
accountContributionPanel: createElement(),
|
||||
@@ -155,12 +163,27 @@ test('combined contribution tutorial button opens current flow page and enables
|
||||
updateConfigMenuControls() {},
|
||||
closeConfigMenu() {},
|
||||
closeAccountRecordsPanel() {},
|
||||
getSelectedFlowId() {
|
||||
return 'kiro';
|
||||
},
|
||||
getSelectedTargetId() {
|
||||
return 'kiro-rs';
|
||||
},
|
||||
isModeSwitchBlocked() {
|
||||
return false;
|
||||
},
|
||||
openExternalUrl(url) {
|
||||
openedUrls.push(url);
|
||||
},
|
||||
async persistCurrentSettingsForAction() {
|
||||
persistCount += 1;
|
||||
latestState = {
|
||||
...latestState,
|
||||
activeFlowId: 'kiro',
|
||||
flowId: 'kiro',
|
||||
kiroTargetId: 'kiro-rs',
|
||||
};
|
||||
},
|
||||
showToast() {},
|
||||
},
|
||||
runtime: {
|
||||
@@ -169,6 +192,8 @@ test('combined contribution tutorial button opens current flow page and enables
|
||||
return {
|
||||
state: {
|
||||
...latestState,
|
||||
activeFlowId: 'openai',
|
||||
flowId: 'openai',
|
||||
accountContributionEnabled: true,
|
||||
contributionAdapterId: message.payload.adapterId,
|
||||
},
|
||||
@@ -190,6 +215,10 @@ test('combined contribution tutorial button opens current flow page and enables
|
||||
flowId: 'kiro',
|
||||
adapterId: 'kiro-builder-id',
|
||||
});
|
||||
assert.equal(persistCount, 1);
|
||||
assert.equal(latestState.accountContributionEnabled, true);
|
||||
assert.equal(latestState.contributionAdapterId, 'kiro-builder-id');
|
||||
assert.equal(latestState.activeFlowId, 'kiro');
|
||||
assert.equal(latestState.flowId, 'kiro');
|
||||
assert.equal(latestState.kiroTargetId, 'kiro-rs');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user