fix: preserve selected flow in contribution mode

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