okami.mom/albedo

git clone https://git.nkp.pet/okami.mom/albedo

Aly Raffaufrefactor(tui): centralize shared session state60af226

main
1.3 KiB44 linesraw
1package tui
2
3import (
4	"albedo/cli/internal/daemon"
5	"slices"
6)
7
8func (m *AppModel) session(id string) (daemon.Session, bool) {
9	if m.ActiveSession != nil && m.ActiveSession.ID == id {
10		return *m.ActiveSession, true
11	}
12	if i := slices.IndexFunc(m.Sessions, func(s daemon.Session) bool { return s.ID == id }); i >= 0 {
13		return m.Sessions[i], true
14	}
15	return daemon.Session{}, false
16}
17
18func (m *AppModel) updateSession(session daemon.Session) {
19	if i := slices.IndexFunc(m.Sessions, func(s daemon.Session) bool { return s.ID == session.ID }); i >= 0 {
20		m.Sessions[i] = session
21	}
22	if m.ActiveSession != nil && m.ActiveSession.ID == session.ID {
23		captured := session
24		m.ActiveSession = &captured
25	}
26	m.updateSessionPickerItems()
27}
28
29func (m *AppModel) applySessionList(sessions []daemon.Session) {
30	m.Sessions = slices.Clone(sessions)
31	if m.ActiveSession != nil {
32		if i := slices.IndexFunc(sessions, func(s daemon.Session) bool { return s.ID == m.ActiveSession.ID }); i >= 0 {
33			// Listings omit details available from a session snapshot.
34			captured := *m.ActiveSession
35			listed := sessions[i]
36			captured.Title, captured.Pinned, captured.Archived, captured.Opens = listed.Title, listed.Pinned, listed.Archived, listed.Opens
37			if listed.ETag != "" {
38				captured.ETag = listed.ETag
39			}
40			m.ActiveSession = &captured
41		}
42	}
43	m.updateSessionPickerItems()
44}