keep terminal tabs stable while switching sessions
This commit is contained in:
@@ -67,18 +67,31 @@ function mergeTerminalSession(
|
|||||||
sessions: TerminalSession[],
|
sessions: TerminalSession[],
|
||||||
next: TerminalSession,
|
next: TerminalSession,
|
||||||
): TerminalSession[] {
|
): TerminalSession[] {
|
||||||
const remaining = sessions.filter(
|
const existingIndex = sessions.findIndex(
|
||||||
(item) => item.terminal_id !== next.terminal_id,
|
(item) => item.terminal_id === next.terminal_id,
|
||||||
);
|
);
|
||||||
return [...remaining, next].sort(
|
if (existingIndex >= 0) {
|
||||||
(left, right) => left.created_at_unix - right.created_at_unix,
|
// Attaching updates session state, not identity or tab position. Removing
|
||||||
|
// and re-inserting here made same-second sessions swap places on click.
|
||||||
|
return sessions.map((item, index) =>
|
||||||
|
index === existingIndex ? next : item,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return orderTerminalSessions([...sessions, next]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function compareTerminalSessions(
|
||||||
|
left: TerminalSession,
|
||||||
|
right: TerminalSession,
|
||||||
|
): number {
|
||||||
|
return (
|
||||||
|
left.created_at_unix - right.created_at_unix ||
|
||||||
|
left.terminal_id.localeCompare(right.terminal_id)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function orderTerminalSessions(sessions: TerminalSession[]): TerminalSession[] {
|
function orderTerminalSessions(sessions: TerminalSession[]): TerminalSession[] {
|
||||||
return [...sessions].sort(
|
return [...sessions].sort(compareTerminalSessions);
|
||||||
(left, right) => left.created_at_unix - right.created_at_unix,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function reconcileTerminalSessions(
|
function reconcileTerminalSessions(
|
||||||
|
|||||||
Reference in New Issue
Block a user