forked from r.muellner/velum-raycast
feat: optionally sort translation mappings by creation date
This commit is contained in:
@@ -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,
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user