feat: standardize correspondent and tag types with usage count
This commit is contained in:
@@ -1,22 +1,11 @@
|
||||
import { useCallback, useState } from 'react';
|
||||
import type { FormEvent, KeyboardEvent } from 'react';
|
||||
|
||||
interface CorrespondentUsage {
|
||||
total?: number;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
|
||||
interface CorrespondentEntry {
|
||||
id?: string;
|
||||
name?: string;
|
||||
usage?: CorrespondentUsage;
|
||||
[key: string]: unknown;
|
||||
}
|
||||
import type { Correspondent } from '../types/documents';
|
||||
|
||||
export interface CorrespondentsPanelProps {
|
||||
correspondents?: CorrespondentEntry[];
|
||||
correspondents?: Correspondent[];
|
||||
onRefresh?: () => void | Promise<void>;
|
||||
onCreate: (payload: { name: string }) => Promise<CorrespondentEntry | void>;
|
||||
onCreate: (payload: { name: string }) => Promise<Correspondent | void>;
|
||||
onUpdate: (id: string, payload: { name: string }) => Promise<void>;
|
||||
onDelete: (id: string) => Promise<void>;
|
||||
onNotify?: (message: string, variant?: string) => void;
|
||||
@@ -37,7 +26,7 @@ function CorrespondentsPanel({
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [deletingId, setDeletingId] = useState<string | null>(null);
|
||||
|
||||
const startEdit = useCallback((correspondent: CorrespondentEntry) => {
|
||||
const startEdit = useCallback((correspondent: Correspondent) => {
|
||||
setEditingId(correspondent.id);
|
||||
setDraftName(correspondent.name);
|
||||
}, []);
|
||||
@@ -68,7 +57,7 @@ function CorrespondentsPanel({
|
||||
}, [editingId, draftName, onUpdate, cancelEdit, onNotify]);
|
||||
|
||||
const handleDelete = useCallback(
|
||||
async (correspondent: CorrespondentEntry) => {
|
||||
async (correspondent: Correspondent) => {
|
||||
if (!correspondent?.id) return;
|
||||
setDeletingId(correspondent.id);
|
||||
try {
|
||||
@@ -121,12 +110,12 @@ function CorrespondentsPanel({
|
||||
[handleSave, cancelEdit],
|
||||
);
|
||||
|
||||
const renderUsage = useCallback((usage?: CorrespondentUsage) => {
|
||||
if (!usage) {
|
||||
const renderUsage = useCallback((correspondent: Correspondent) => {
|
||||
const count = correspondent.usage_count;
|
||||
if (count == null || !Number.isFinite(count)) {
|
||||
return '0';
|
||||
}
|
||||
const total = Number.isFinite(usage.total) ? Number(usage.total) : 0;
|
||||
return total.toString();
|
||||
return count.toString();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
@@ -195,7 +184,7 @@ function CorrespondentsPanel({
|
||||
<span>{correspondent.name}</span>
|
||||
)}
|
||||
</td>
|
||||
<td className="numeric">{renderUsage(correspondent.usage)}</td>
|
||||
<td className="numeric">{renderUsage(correspondent)}</td>
|
||||
<td className="actions">
|
||||
{isEditing ? (
|
||||
<div className="tags-table__edit-controls">
|
||||
|
||||
Reference in New Issue
Block a user