Files
hapi/web/src/lib/sharePendingState.ts
T
0d12035674 feat(web): searchable session picker on Android share target (#986)
* feat(web): searchable session picker on share target (#980)

Add search to the Android share-target picker so operators can attach
shared content to inactive or older sessions, not just recent actives.
Reuses sidebar search helpers; caps default active list with a search hint.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): defer share pending consume until session is active

ShareSeedConsumer was consuming the sessionStorage transfer id on first
mount even when the target session was inactive. Reopening into a new
session id remounted the chat with the key already gone, dropping the
shared payload. Consume and seed only after sessionActive is true.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): bind share pending transfer to target session id

Deferring consume until active left a global pending slot that the next
unrelated active SessionChat could claim. Bind the pending transfer to
the picked session id, retarget on reopen/spawn id-swap, and only
consume when the mounting session matches.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): wire SessionListSearch date-range props on share picker

Upstream added required customStart/customEnd/onDateRangeChange to
SessionListSearch; share /share must pass them and honor the range when
filtering sessions after rebase onto v0.23.1.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): pass sessionActivityDates to share SessionListSearch

Upstream SessionListSearch now requires activity dates for the date
picker; without them CI typecheck fails and the PR chip stays needs_work.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): use cached machine labels in share picker search

Share picker now uses useMachineLabels like SessionList so stale
machine ids still match search by display name.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): align share picker with sidebar session prep and preview limit

Snapshot via prepareSidebarSessions and pass useSessionPreviewLimit
so search targets and fold cap match the main session list.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): wire SessionListSearch expanded props on share picker

Upstream search now requires expanded/onExpandedChange; default open
on the share picker so the field is available immediately.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): unblock typecheck after SessionSummary and RawSendError fields

Fill new required SessionSummary watermarks in the share picker test
helper, and set deliveryMode on abort-restore send errors.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): mock machines hooks in share page missing-state test

SharePage now calls useMachines for picker search labels; the missing-
share unit test needs those hooks stubbed without a QueryClient.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(web): retarget pending share on automatic session supersession

When SessionDetailRoute follows supersededBySessionId A→B, rewrite the
share pending target so ShareSeedConsumer on B can still claim it.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Debian <heavygee@oos-linux.in.lockhouse>
2026-08-11 14:25:00 +01:00

104 lines
3.8 KiB
TypeScript

/**
* sessionStorage hand-off between the share picker (`/share`) and the
* session mount (`SessionChat`).
*
* The picker stores the IDB transfer id under this key, navigates to
* `/sessions/:id` (or `/sessions/new`), and the session mounter reads + clears
* the key once that session is active. sessionStorage rather than router
* state because:
*
* - it survives the `/sessions/new` -> `/sessions/:id` navigation that
* `NewSessionPage` performs internally with `replace: true`, which
* would drop router history state.
* - it scopes to the PWA window/tab — Android Chrome opens the share
* target in the installed PWA's own window, so collisions with other
* tabs are not a concern.
*
* The payload is bound to a **target session id**:
* - Consume only succeeds when the mounting SessionChat's id matches.
* - That prevents an unrelated active chat from stealing a pending
* share that was armed for an inactive target.
* - When reopen/spawn swaps A → B, call `retargetSharePendingTransfer(A, B)`
* before navigating so B can still seed.
*/
export const SHARE_PENDING_TRANSFER_KEY = 'hapi.share.pendingTransferId'
type SharePendingRecord = {
transferId: string
sessionId: string
}
function readRecord(): SharePendingRecord | null {
try {
const raw = window.sessionStorage.getItem(SHARE_PENDING_TRANSFER_KEY)
if (!raw) return null
// Legacy: bare transfer id string (pre-session-binding). Treat as
// unbound — any active consumer may claim it (restore old behavior
// for in-flight shares during deploy). Prefer writing the object form.
if (raw[0] !== '{') {
return { transferId: raw, sessionId: '' }
}
const parsed = JSON.parse(raw) as Partial<SharePendingRecord>
if (typeof parsed.transferId !== 'string' || typeof parsed.sessionId !== 'string') {
return null
}
return { transferId: parsed.transferId, sessionId: parsed.sessionId }
} catch {
return null
}
}
function writeRecord(record: SharePendingRecord): void {
window.sessionStorage.setItem(SHARE_PENDING_TRANSFER_KEY, JSON.stringify(record))
}
export function setSharePendingTransfer(transferId: string, sessionId: string): void {
try {
writeRecord({ transferId, sessionId })
} catch {
// Quota errors / disabled storage — caller proceeds without seed.
}
}
/**
* Claim the pending transfer for `sessionId`. Returns null (and leaves the
* slot alone) when the pending target is a different session — so another
* active chat cannot steal a share armed for an inactive pick.
*
* Legacy unbound records (`sessionId === ''`) are claimed by the first caller.
*/
export function consumeSharePendingTransfer(sessionId: string): string | null {
try {
const record = readRecord()
if (!record) return null
if (record.sessionId !== '' && record.sessionId !== sessionId) {
return null
}
window.sessionStorage.removeItem(SHARE_PENDING_TRANSFER_KEY)
return record.transferId
} catch {
return null
}
}
/**
* When resume/reopen merges session `fromSessionId` into a new live id
* `toSessionId`, rewrite the pending target so the remounted chat can seed.
*/
export function retargetSharePendingTransfer(fromSessionId: string, toSessionId: string): void {
if (!fromSessionId || !toSessionId || fromSessionId === toSessionId) return
try {
const record = readRecord()
if (!record) return
if (record.sessionId !== fromSessionId) return
writeRecord({ transferId: record.transferId, sessionId: toSessionId })
} catch {
// Ignore storage failures — seed is best-effort.
}
}
export function peekSharePendingTransfer(): SharePendingRecord | null {
return readRecord()
}