Repository navigation
fix(mobile): a run that finishes during a resume is not marked read unseen #28193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
playcations
wants to merge
3
commits into
pingdotgg:main
Choose a base branch
from
playcations:fix/mobile-completion-read-dwell
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+105
−8
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
6369972
fix(mobile): a run that finishes during a resume is not marked read u…
playcations 75ebe83
Merge remote-tracking branch 'github/main' into fix/mobile-completion…
maria-rcks 378e8ef
fix(mobile): a covered thread does not count down to read after a resume
playcations File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
apps/mobile/src/features/threads/threadViewInterruption.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
|
|
||
| import { nextThreadViewState } from "./threadViewInterruption"; | ||
|
|
||
| describe("nextThreadViewState", () => { | ||
| it("counts a thread opened while synced and in the foreground at once", () => { | ||
| expect(nextThreadViewState(null, "env:a", true)).toEqual({ key: "env:a", interrupted: false }); | ||
| }); | ||
|
|
||
| it("waits for a thread opened while syncing or in the background", () => { | ||
| expect(nextThreadViewState(null, "env:a", false)).toEqual({ key: "env:a", interrupted: true }); | ||
| }); | ||
|
|
||
| it("keeps waiting after a resume or sync interrupts the view, until another thread opens", () => { | ||
| const opened = nextThreadViewState(null, "env:a", true); | ||
| const hidden = nextThreadViewState(opened, "env:a", false); | ||
| expect(hidden).toEqual({ key: "env:a", interrupted: true }); | ||
| expect(nextThreadViewState(hidden, "env:a", true)).toBe(hidden); | ||
| expect(nextThreadViewState(hidden, "env:b", true)).toEqual({ | ||
| key: "env:b", | ||
| interrupted: false, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns the same state while nothing changes", () => { | ||
| const opened = nextThreadViewState(null, "env:a", true); | ||
| expect(nextThreadViewState(opened, "env:a", true)).toBe(opened); | ||
| expect(nextThreadViewState(opened, null, true)).toBeNull(); | ||
| }); | ||
| }); |
17 changes: 17 additions & 0 deletions
17
apps/mobile/src/features/threads/threadViewInterruption.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| export interface ThreadViewState { | ||
| readonly key: string; | ||
| /** The view began, or has since been, hidden or syncing, so reading waits for it to settle. */ | ||
| readonly interrupted: boolean; | ||
| } | ||
|
|
||
| /** The next view state for the shown thread; returns `previous` itself when nothing changed. */ | ||
| export function nextThreadViewState( | ||
| previous: ThreadViewState | null, | ||
| key: string | null, | ||
| visible: boolean, | ||
| ): ThreadViewState | null { | ||
| if (key === null) return null; | ||
| if (previous?.key !== key) return { key, interrupted: !visible }; | ||
| if (!visible && !previous.interrupted) return { key, interrupted: true }; | ||
| return previous; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import type { ScopedThreadRef } from "@t3tools/contracts"; | ||
| import { useEffect, useMemo, useState } from "react"; | ||
|
|
||
| import { useAppActive } from "../../lib/appState"; | ||
| import { scopedThreadKey } from "../../lib/scopedEntities"; | ||
| import { nextThreadViewState, type ThreadViewState } from "./threadViewInterruption"; | ||
|
|
||
| /** How long a thread shows, synced and in the foreground, after a resume or sync before it counts as read. */ | ||
| const THREAD_READ_DWELL_MS = 2_000; | ||
|
|
||
| /** | ||
| * The thread while it counts as viewed. A synced open counts at once; after a resume or sync a | ||
| * finished run may land unseen, so the view counts only once the thread has shown, synced, in the | ||
| * foreground and uncovered, for a moment. Backing out sooner keeps it unread. | ||
| */ | ||
| export function useSettledThreadView( | ||
| ref: ScopedThreadRef | null, | ||
| synchronizing: boolean, | ||
| covered: boolean, | ||
| ): ScopedThreadRef | null { | ||
| const active = useAppActive(); | ||
| const key = ref === null ? null : scopedThreadKey(ref.environmentId, ref.threadId); | ||
| const visible = active && !synchronizing && key !== null; | ||
| const [opened, setOpened] = useState<ThreadViewState | null>(null); | ||
| const next = nextThreadViewState(opened, key, visible); | ||
| if (next !== opened) setOpened(next); | ||
| const interrupted = next?.interrupted ?? false; | ||
| // A fresh view each time the thread shows again, so the wait restarts even if no timer ran | ||
| // while the app was in the background or the thread was covered. | ||
| const view = useMemo( | ||
| () => (visible && !covered && key !== null ? { key } : null), | ||
| [visible, covered, key], | ||
| ); | ||
| const [settled, setSettled] = useState<{ readonly key: string } | null>(null); | ||
| // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- times how long a resumed or synced thread has shown in the foreground | ||
| useEffect(() => { | ||
| if (view === null || !interrupted) return; | ||
| const timer = setTimeout(() => setSettled(view), THREAD_READ_DWELL_MS); | ||
| return () => clearTimeout(timer); | ||
| }, [view, interrupted]); | ||
| if (!visible) return null; | ||
| if (!interrupted) return ref; | ||
| return view !== null && settled === view ? ref : null; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.