refactor: Delete isPlainObject type guard and replace its usages with direct typeof checks.

This commit is contained in:
2025-11-24 21:22:38 +01:00
parent 63be4663ab
commit f5ca74b63c
8 changed files with 86 additions and 95 deletions
@@ -12,7 +12,7 @@ import {
toIssuedTimestamp,
} from '../utils/date';
import { describeDocumentSummary, type DocumentSummaryRow } from './documentSummary';
import { isPlainObject } from '../utils/typeGuards';
import { useFolderManager } from '../folders/FolderManagerContext';
type Identifier = string | number;
@@ -128,11 +128,11 @@ const resolveOptionName = (source?: QuickAddOption | string | null): string => {
if (!source) {
return '';
}
if (isPlainObject(source)) {
const raw = source.name ?? source.label ?? '';
return `${raw}`.trim();
if (typeof source === 'string') {
return source.trim();
}
return `${source}`.trim();
const raw = source.name ?? source.label ?? '';
return `${raw}`.trim();
};
const normalizeQuickAddOption = (option?: QuickAddOption | string | null): QuickAddEntry | null => {
@@ -140,17 +140,17 @@ const normalizeQuickAddOption = (option?: QuickAddOption | string | null): Quick
return null;
}
const label = (() => {
if (isPlainObject(option)) {
const sourceLabel = option.label ?? option.name ?? '';
return `${sourceLabel}`.trim();
if (typeof option === 'string') {
return option.trim();
}
return `${option}`.trim();
const sourceLabel = option.label ?? option.name ?? '';
return `${sourceLabel}`.trim();
})();
if (!label) {
return null;
}
return {
id: isPlainObject(option) && option.id ? option.id : label,
id: typeof option !== 'string' && option.id ? option.id : label,
label,
original: option,
};
@@ -245,7 +245,7 @@ export const TagSection: React.FC<TagSectionProps> = ({
return;
}
if (item.state === 'all' && onRemove) {
const payload = isPlainObject(item.payload)
const payload = item.payload && typeof item.payload === 'object' && 'id' in item.payload
? (item.payload as TagEntry)
: tags.find((tag) => (tag.id ?? tag.label) === item.id) ?? { id: item.id, label: item.label };
onRemove(payload);
@@ -379,7 +379,7 @@ export const CorrespondentSection: React.FC<CorrespondentSectionProps> = ({
return;
}
if (item.state === 'all' && onRemove) {
const payload = isPlainObject(item.payload)
const payload = item.payload && typeof item.payload === 'object' && 'id' in item.payload
? (item.payload as CorrespondentEntry)
: entries.find((entry) => (entry.id ?? entry.name) === item.id) ?? { id: item.id, name: item.label };
onRemove(payload);
@@ -393,7 +393,7 @@ export const CorrespondentSection: React.FC<CorrespondentSectionProps> = ({
if (!resolvedName) {
return;
}
const payload = isPlainObject(source)
const payload = typeof source !== 'string'
? { ...source, name: resolvedName }
: { id: null, name: resolvedName };
onAdd({ name: resolvedName, option: payload, input: null });
@@ -405,26 +405,26 @@ export const CorrespondentSection: React.FC<CorrespondentSectionProps> = ({
<div className={containerClass}>
{hasEntries
? entries.map((entry) => {
const key = entry.id ?? entry.name;
return (
<span key={key} className="correspondent-pill">
<span className="correspondent-pill__label">
{entry.name}
{showCount && entry.count ? ` (${entry.count})` : ''}
</span>
{onRemove ? (
<button
type="button"
className="correspondent-pill__remove"
onClick={() => onRemove(entry)}
aria-label={`Remove ${entry.name}`}
>
<IconX className="icon-inline" aria-hidden="true" />
</button>
) : null}
const key = entry.id ?? entry.name;
return (
<span key={key} className="correspondent-pill">
<span className="correspondent-pill__label">
{entry.name}
{showCount && entry.count ? ` (${entry.count})` : ''}
</span>
);
})
{onRemove ? (
<button
type="button"
className="correspondent-pill__remove"
onClick={() => onRemove(entry)}
aria-label={`Remove ${entry.name}`}
>
<IconX className="icon-inline" aria-hidden="true" />
</button>
) : null}
</span>
);
})
: !showQuickAdd && <span className="meta">No correspondents yet.</span>}
{showQuickAdd ? (
<SelectionAssignmentMenu
@@ -518,7 +518,7 @@ const DocumentSummarySection: React.FC<DocumentSummarySectionProps> = ({
if (active) {
setFolderName(name);
}
}).catch(() => {});
}).catch(() => { });
}
return () => {
active = false;
@@ -674,21 +674,21 @@ const DocumentSummarySection: React.FC<DocumentSummarySectionProps> = ({
const titleMetaDisplay = editableTitle && isTitleEditing
? renderTitleEditForm('doc-title-edit--inline')
: (
<>
<span className="detail-meta__value">{document?.title}</span>
{editableTitle ? (
<button
type="button"
className="icon-button"
onClick={startTitleEdit}
aria-label="Edit title"
title="Edit title"
>
<EditIcon className="icon-inline" />
</button>
) : null}
</>
);
<>
<span className="detail-meta__value">{document?.title}</span>
{editableTitle ? (
<button
type="button"
className="icon-button"
onClick={startTitleEdit}
aria-label="Edit title"
title="Edit title"
>
<EditIcon className="icon-inline" />
</button>
) : null}
</>
);
const issuedDisplay = editableIssued && isIssuedEditing ? (
<form className="doc-issued-edit" onSubmit={submitIssuedEdit}>
@@ -758,20 +758,20 @@ const DocumentSummarySection: React.FC<DocumentSummarySectionProps> = ({
onRemove={
onCorrespondentRemove
? (entry) =>
onCorrespondentRemove({
documentId: document.id,
correspondentId: entry.id,
})
onCorrespondentRemove({
documentId: document.id,
correspondentId: entry.id,
})
: undefined
}
onAdd={
onCorrespondentAdd
? ({ name, option }) =>
onCorrespondentAdd({
document,
name,
option,
})
onCorrespondentAdd({
document,
name,
option,
})
: undefined
}
showCount