wip: preserve chat hide notice work

This commit is contained in:
2026-10-18 20:23:14 +01:00
parent 5946f1abc8
commit 2cc2c0d976
5 changed files with 147 additions and 10 deletions
@@ -1106,7 +1106,9 @@ class AppViewModel(
*/
fun removeChat(sessionId: String) = chatStore.remove(sessionId)
fun undoRemoveChat() = chatStore.undoRemove()
fun consumeChatRemovalNotice(noticeId: Long) = chatStore.consumeRemovalNotice(noticeId)
fun undoRemoveChat(sessionId: String) = chatStore.undoRemove(sessionId)
fun loadChatHistory(sessionId: String) {
if (chatStore.value.thread(sessionId)?.loading == true) return
@@ -107,15 +107,24 @@ internal fun ChatListPane(
// A removal is reported for what it is: a local, undoable change, never a
// pretend delete — the server is not asked to remove anything.
val snackbarHostState = remember { SnackbarHostState() }
LaunchedEffect(state.lastRemoved) {
if (state.lastRemoved != null &&
snackbarHostState.showSnackbar(
val removalNotice = state.removalNotice
LaunchedEffect(removalNotice?.id) {
removalNotice?.let { notice ->
// A rapid second hide replaces, rather than queues behind, the first
// report. Do not consume until showSnackbar finishes: consuming first
// changes this effect's key and cancels the snackbar coroutine.
snackbarHostState.currentSnackbarData?.dismiss()
if (snackbarHostState.showSnackbar(
message = CHAT_REMOVED_MESSAGE,
actionLabel = "Undo",
duration = SnackbarDuration.Long,
) == SnackbarResult.ActionPerformed
) {
vm.undoRemoveChat()
duration = SnackbarDuration.Short,
) == SnackbarResult.ActionPerformed) {
vm.undoRemoveChat(notice.sessionId)
}
// A completed notice is not replayed after the pane is recreated. If
// another hide replaced it while this coroutine was cancelled, this
// exact-id consume is a no-op and the newer notice remains pending.
vm.consumeChatRemovalNotice(notice.id)
}
}
var renaming by remember { mutableStateOf<ChatSession?>(null) }
@@ -254,6 +254,12 @@ data class ChatState(
val overrides: ChatSessionOverrides = ChatSessionOverrides(),
/** The id of the session most recently removed, while its removal can be undone. */
val lastRemoved: String? = null,
/**
* The one-shot report that a removal was made. It is deliberately separate
* from [lastRemoved]: consuming a rendered notice must not make the local
* hide irreversible, and revisiting the list must not report it again.
*/
val removalNotice: ChatRemovalNotice? = null,
/** What the operator has read of the list (see [ChatSeenState]). */
val seen: ChatSeenState = ChatSeenState(),
/**
@@ -337,6 +343,9 @@ data class ChatState(
get() = filteredSessions.filter { it.closed }
}
/** A one-shot, in-memory report for the list's local-hide snackbar. */
data class ChatRemovalNotice(val id: Long, val sessionId: String)
/**
* The multi-chat state model: a small, Android-free holder of [ChatState] with
* the transitions the chat surface needs. Keeping it free of Compose and of the
@@ -378,6 +387,9 @@ class ChatStore(
*/
private var localMessageSeq = 0
/** Monotonic identity for one-shot local-hide reports. */
private var removalNoticeSeq = 0L
// ---- Session list --------------------------------------------------------
fun sessionsLoading() {
@@ -533,15 +545,31 @@ class ChatStore(
open = _state.value.open.filterNot { it.sessionId == sessionId },
activeId = if (_state.value.activeId == sessionId) null else _state.value.activeId,
lastRemoved = sessionId,
// Keep only the newest report. A snackbar host has one visible slot;
// retaining older reports would turn rapid hides into a long queue.
removalNotice = ChatRemovalNotice(++removalNoticeSeq, sessionId),
)
}
/** Restores the most recently removed session to the list and forgets it. */
fun undoRemove() {
/**
* Marks [noticeId] handled after its snackbar finishes. This transient event
* is never persisted, so refreshes, recomposition and re-entry cannot replay
* a finished report.
*/
fun consumeRemovalNotice(noticeId: Long) {
if (_state.value.removalNotice?.id == noticeId) {
_state.value = _state.value.copy(removalNotice = null)
}
}
/** Restores [sessionId] only when it is still the current undoable removal. */
fun undoRemove(sessionId: String? = null) {
val id = _state.value.lastRemoved ?: return
if (sessionId != null && sessionId != id) return
_state.value = _state.value.copy(
overrides = _state.value.overrides.restore(id),
lastRemoved = null,
removalNotice = null,
)
}