fix: improve review field location fallback

This commit is contained in:
wren
2026-05-06 11:15:52 +08:00
parent 8dbacb8bea
commit 5366270ad6
@@ -450,27 +450,60 @@ function RenderGenericRule({
: Array.isArray((config as any).fields) : Array.isArray((config as any).fields)
? (config as any).fields.map((field: unknown) => String(field)) ? (config as any).fields.map((field: unknown) => String(field))
: []; : [];
const reason = [config.reason, detail.reason, reviewPoint.failMessage, reviewPoint.passMessage]
.find((item) => typeof item === 'string' && item.trim()) as string | undefined;
const passed = typeof rule.res === 'boolean' ? rule.res : reviewPoint.result === true; const passed = typeof rule.res === 'boolean' ? rule.res : reviewPoint.result === true;
const reasonCandidates = passed
? [config.reason, detail.reason, reviewPoint.passMessage]
: [config.reason, detail.reason, reviewPoint.failMessage, reviewPoint.suggestion];
const reason = reasonCandidates.find((item) => typeof item === 'string' && item.trim()) as string | undefined;
const checkType = typeof config.check_type === 'string' ? config.check_type : ''; const checkType = typeof config.check_type === 'string' ? config.check_type : '';
const primitiveType = typeof config.primitive_type === 'string' ? config.primitive_type : ''; const primitiveType = typeof config.primitive_type === 'string' ? config.primitive_type : '';
const badgeText = checkType || primitiveType || '规则检查'; const badgeText = checkType || primitiveType || '规则检查';
const jumpToField = (fieldName: string) => { const getFieldLocatorState = (fieldName: string) => {
const fieldData = reviewPoint.content?.[fieldName]; const fieldData = reviewPoint.content?.[fieldName];
const page = fieldData?.page || reviewPoint.contentPage?.[fieldName]; const page = fieldData?.page || reviewPoint.contentPage?.[fieldName];
const normalizedPage = page ? Number(page) : undefined; const normalizedPage = page ? Number(page) : undefined;
if (normalizedPage && Number.isFinite(normalizedPage)) { const hasPage = !!(normalizedPage && Number.isFinite(normalizedPage));
const rawValue = fieldData?.value;
const normalizedValue =
typeof rawValue === 'string'
? rawValue.trim()
: rawValue == null
? ''
: String(rawValue);
return {
fieldData,
normalizedPage: hasPage ? normalizedPage : undefined,
normalizedValue,
canLocate: hasPage || normalizedValue.length > 0,
};
};
const jumpToField = (fieldName: string) => {
const { fieldData, normalizedPage, normalizedValue } = getFieldLocatorState(fieldName);
if (normalizedPage) {
onReviewPointSelect( onReviewPointSelect(
reviewPoint.id, reviewPoint.id,
normalizedPage, normalizedPage,
fieldData?.char_positions, fieldData?.char_positions,
typeof fieldData?.value === 'string' ? fieldData.value : undefined, normalizedValue || undefined,
); );
return; return;
} }
toastService.info(`${fieldName} 当前没有可定位页码`);
if (normalizedValue) {
onReviewPointSelect(
reviewPoint.id,
undefined,
fieldData?.char_positions,
normalizedValue,
);
toastService.info(`${fieldName} 当前没有页码,已改为按文本定位`);
return;
}
toastService.info(`${fieldName} 当前既没有页码,也没有可定位文本`);
}; };
return ( return (
@@ -492,7 +525,7 @@ function RenderGenericRule({
{fieldNames.length > 0 && ( {fieldNames.length > 0 && (
<div className="mt-3 flex flex-wrap gap-2"> <div className="mt-3 flex flex-wrap gap-2">
{fieldNames.map((fieldName) => { {fieldNames.map((fieldName) => {
const fieldData = reviewPoint.content?.[fieldName]; const { fieldData, normalizedPage, normalizedValue, canLocate } = getFieldLocatorState(fieldName);
const fieldValue = fieldData?.value; const fieldValue = fieldData?.value;
const displayValue = const displayValue =
typeof fieldValue === 'string' typeof fieldValue === 'string'
@@ -505,10 +538,30 @@ function RenderGenericRule({
<button <button
key={fieldName} key={fieldName}
type="button" type="button"
className="min-w-0 flex-1 rounded border border-slate-200 bg-white px-2.5 py-2 text-left hover:border-[#00684a] hover:bg-[#f6fffb]" className={`min-w-0 flex-1 rounded border px-2.5 py-2 text-left ${
onClick={() => jumpToField(fieldName)} canLocate
? 'border-slate-200 bg-white hover:border-[#00684a] hover:bg-[#f6fffb]'
: 'border-slate-200 bg-slate-50 text-slate-400 cursor-not-allowed'
}`}
onClick={() => canLocate && jumpToField(fieldName)}
disabled={!canLocate}
> >
<div className="text-[11px] font-medium text-slate-500">{fieldName}</div> <div className="flex items-center justify-between gap-2 text-[11px] font-medium">
<span className={canLocate ? 'text-slate-500' : 'text-slate-400'}>{fieldName}</span>
{normalizedPage ? (
<span className="rounded bg-emerald-50 px-1.5 py-0.5 text-[10px] text-emerald-700">
{normalizedPage}
</span>
) : normalizedValue ? (
<span className="rounded bg-amber-50 px-1.5 py-0.5 text-[10px] text-amber-700">
</span>
) : (
<span className="rounded bg-slate-100 px-1.5 py-0.5 text-[10px] text-slate-500">
</span>
)}
</div>
<div className="mt-1 text-[12px] leading-5 text-slate-700 break-all">{displayValue}</div> <div className="mt-1 text-[12px] leading-5 text-slate-700 break-all">{displayValue}</div>
</button> </button>
); );