feat: optionally sort translation mappings by creation date

This commit is contained in:
muena
2026-10-19 06:49:34 +02:00
parent 03a793ad7c
commit a627d9147e
8 changed files with 218 additions and 62 deletions

View File

@@ -16,6 +16,21 @@ function sessionSort(a: VelumSession, b: VelumSession): number {
return Date.parse(b.updatedAt) - Date.parse(a.updatedAt);
}
function mappingDates(previous: VelumSession | undefined, mapping: PlaceholderMapping,
timestamp: string): Record<string, string> {
const dates: Record<string, string> = {};
const previousKeys = new Map(Object.entries(previous?.mapping ?? {})
.map(([key, entry]) => [key.replace(/^<|>$/g, ""), { key, entry }]));
for (const [key, entry] of Object.entries(mapping)) {
const prior = previousKeys.get(key.replace(/^<|>$/g, ""));
if (prior && prior.entry.type === entry.type && prior.entry.original === entry.original) {
const originalDate = previous?.mappingCreatedAt?.[prior.key];
if (originalDate) dates[key] = originalDate;
} else dates[key] = timestamp;
}
return dates;
}
async function saveSessions(sessions: VelumSession[]): Promise<void> {
const maxSessions = getPreferences().maxSessions;
const pruned = [...sessions].sort(sessionSort).slice(0, maxSessions);
@@ -102,6 +117,7 @@ export async function updateSessionMapping(
mapping,
entityTypes,
excludedTerms,
mappingCreatedAt: mappingDates(undefined, mapping, timestamp),
};
await saveSessions([created, ...sessions]);
return created;
@@ -112,6 +128,7 @@ export async function updateSessionMapping(
mapping,
entityTypes,
excludedTerms: excludedTerms ?? sessions[index].excludedTerms,
mappingCreatedAt: mappingDates(sessions[index], mapping, timestamp),
updatedAt: timestamp,
};
const next = [...sessions];
@@ -148,7 +165,7 @@ export async function clearSessionMapping(id: string): Promise<void> {
await saveSessions(
sessions.map((session) =>
session.id === id
? { ...session, mapping: {}, updatedAt: nowIso() }
? { ...session, mapping: {}, mappingCreatedAt: {}, updatedAt: nowIso() }
: session,
),
);