···115115 var openShelfBookID: Worktree.ID? {
116116 selectedTerminalWorktree?.id
117117 }
118118+119119+ /// The rendered book matching the current Shelf selection, if any.
120120+ ///
121121+ /// `openShelfBookID` can briefly point at a worktree/folder that has
122122+ /// just been retired from `openedWorktreeIDs` after its last tab closes.
123123+ /// Views should use this lookup rather than assuming a non-nil open ID
124124+ /// means an open book is still present in `orderedShelfBooks()`.
125125+ func openShelfBook(in books: [ShelfBook]) -> ShelfBook? {
126126+ guard let openShelfBookID else { return nil }
127127+ return books.first(where: { $0.id == openShelfBookID })
128128+ }
118129}
+5-4
supacode/Features/Shelf/Views/ShelfView.swift
···3232 let _ = shelfLogger.event("ShelfView.body")
3333 let state = store.state
3434 let books = state.orderedShelfBooks(customTitles: state.repositoryCustomTitles)
3535- let openBookID = state.openShelfBookID
3636- let openIndex = openBookID.flatMap { id in
3737- books.firstIndex(where: { $0.id == id })
3535+ let openBook = state.openShelfBook(in: books)
3636+ let openBookID = openBook?.id
3737+ let openIndex = openBook.flatMap { book in
3838+ books.firstIndex(where: { $0.id == book.id })
3839 }
39404041 HStack(spacing: 0) {
···4445 openBookArea(for: book, state: state)
4546 }
4647 }
4747- if openBookID == nil {
4848+ if openBook == nil {
4849 emptyOpenArea()
4950 }
5051 }