-
Notifications
You must be signed in to change notification settings - Fork 0
feat(friends): persistent pending-invite surface (#47 B1) #59
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
Merged
Merged
Changes from all commits
Commits
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
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,132 @@ | ||
| import { useEffect, useState } from 'react' | ||
| import { MailIcon } from 'lucide-react' | ||
|
|
||
| import { Button } from '@/components/ui/button' | ||
| import { tokens } from '@/design/tokens' | ||
| import { strings } from '@/strings' | ||
|
|
||
| import type { ValidInvite } from './inbox' | ||
| import { | ||
| usePendingInvitesStore, | ||
| type PendingInviteEntry, | ||
| } from './pendingInvitesStore' | ||
|
|
||
| // #47 B1 — the persistent accept surface for incoming invites. The sonner | ||
| // toast (InboxBoot) stays as the immediate affordance; these rows survive on | ||
| // the main view until the envelope's expires_at passes, so a recipient who | ||
| // was tabbed away for a minute still sees and can accept the invite. | ||
|
|
||
| export type PendingInvitesViewProps = { | ||
| entries: ReadonlyArray<PendingInviteEntry> | ||
| now: number | ||
| onAccept: (entry: PendingInviteEntry) => void | ||
| onDismiss: (entry: PendingInviteEntry) => void | ||
| } | ||
|
|
||
| function senderName(invite: ValidInvite): string { | ||
| return ( | ||
| invite.payload.our_display_name?.trim() || | ||
| strings.friends.inbox.senderFallback | ||
| ) | ||
| } | ||
|
|
||
| export function PendingInvitesView({ | ||
| entries, | ||
| now, | ||
| onAccept, | ||
| onDismiss, | ||
| }: PendingInvitesViewProps) { | ||
| if (entries.length === 0) return null | ||
| return ( | ||
| <section | ||
| aria-label={strings.friends.inbox.pending.listAriaLabel} | ||
| className="mx-auto w-full px-4 pt-4 sm:px-6" | ||
| style={{ maxWidth: tokens.sizes.readingMaxWidth }} | ||
| > | ||
| <ul className="flex flex-col gap-2"> | ||
| {entries.map((entry) => { | ||
| const name = senderName(entry.invite) | ||
| const minutesLeft = Math.max( | ||
| 1, | ||
| Math.ceil((entry.invite.payload.expires_at - now) / 60_000) | ||
| ) | ||
| return ( | ||
| <li | ||
| key={entry.key} | ||
| className="flex items-center justify-between gap-3 rounded-lg border border-border-subtle bg-bg-surface px-4 py-3" | ||
| > | ||
| <span className="flex min-w-0 items-center gap-3"> | ||
| <MailIcon | ||
| className="size-4 shrink-0 text-accent-default" | ||
| aria-hidden="true" | ||
| /> | ||
| <span className="flex min-w-0 flex-col"> | ||
| <span className="truncate text-sm text-text-primary"> | ||
| {strings.friends.inbox.inviteBody(name)} | ||
| </span> | ||
| <span className="text-xs text-text-muted"> | ||
| {strings.friends.inbox.pending.expiresIn(minutesLeft)} | ||
| </span> | ||
| </span> | ||
| </span> | ||
| <span className="flex shrink-0 items-center gap-2"> | ||
| <Button | ||
| type="button" | ||
| variant="ghost" | ||
| size="sm" | ||
| onClick={() => onDismiss(entry)} | ||
| aria-label={strings.friends.inbox.pending.dismissAriaLabel( | ||
| name | ||
| )} | ||
| > | ||
| {strings.friends.inbox.pending.dismissCta} | ||
| </Button> | ||
| <Button | ||
| type="button" | ||
| variant="default" | ||
| size="sm" | ||
| onClick={() => onAccept(entry)} | ||
| aria-label={strings.friends.inbox.pending.acceptAriaLabel( | ||
| name | ||
| )} | ||
| > | ||
| {strings.friends.inbox.acceptAction} | ||
| </Button> | ||
| </span> | ||
| </li> | ||
| ) | ||
| })} | ||
| </ul> | ||
| </section> | ||
| ) | ||
| } | ||
|
|
||
| export type PendingInvitesProps = { | ||
| onAccept: (invite: ValidInvite) => void | ||
| } | ||
|
|
||
| export function PendingInvites({ onAccept }: PendingInvitesProps) { | ||
| const pending = usePendingInvitesStore((s) => s.pending) | ||
| const [now, setNow] = useState(() => Date.now()) | ||
|
|
||
| // Refresh the countdown + drop expired rows on a slow tick, only while | ||
| // anything is pending. Acceptance-time expiry is separately re-checked in | ||
| // Home's runGuestJoin, so a stale row can never join a dead session. | ||
| useEffect(() => { | ||
| if (pending.length === 0) return | ||
| const id = setInterval(() => { | ||
| setNow(Date.now()) | ||
| usePendingInvitesStore.getState().prune() | ||
| }, 10_000) | ||
| return () => clearInterval(id) | ||
| }, [pending.length]) | ||
|
|
||
| return ( | ||
| <PendingInvitesView | ||
| entries={pending} | ||
| now={now} | ||
| onAccept={(entry) => onAccept(entry.invite)} | ||
| onDismiss={(entry) => usePendingInvitesStore.getState().remove(entry.key)} | ||
| /> | ||
| ) | ||
| } | ||
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
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,58 @@ | ||
| import { create } from 'zustand' | ||
|
|
||
| import type { ValidInvite } from './inbox' | ||
|
|
||
| // #47 B1 — a persistent home for incoming invites. Envelopes are valid for | ||
| // INVITE_TTL_MS (5 min) but the only accept affordance was a ~4s sonner | ||
| // toast; a recipient who was tabbed away missed it and the invite was | ||
| // unrecoverable on their side while the host saw "Invite sent". Each valid | ||
| // invite is held here until it expires, is dismissed, or is accepted; | ||
| // PendingInvites renders the list as rows on the main view. Lives in | ||
| // features/friends (the alertsUiStore precedent), not src/stores — stores/ | ||
| // modules must not import feature types. | ||
|
|
||
| export type PendingInviteEntry = { | ||
| key: string | ||
| invite: ValidInvite | ||
| receivedAt: number | ||
| } | ||
|
|
||
| // One row per sender+session: a re-sent invite for the same session (the F6 | ||
| // retry path) replaces the earlier entry instead of stacking duplicates. | ||
| export function pendingInviteKey(invite: ValidInvite): string { | ||
| return `${invite.from_ed_pubkey}:${invite.payload.session_topic}` | ||
| } | ||
|
|
||
| type PendingInvitesState = { | ||
| pending: PendingInviteEntry[] | ||
| add: (invite: ValidInvite, now?: number) => void | ||
| remove: (key: string) => void | ||
| // Drop entries whose expires_at has passed. Called on a slow interval by | ||
| // the banner while anything is pending. | ||
| prune: (now?: number) => void | ||
| clear: () => void | ||
| } | ||
|
|
||
| export const usePendingInvitesStore = create<PendingInvitesState>((set) => ({ | ||
| pending: [], | ||
| add: (invite, now = Date.now()) => | ||
| set((s) => { | ||
| const key = pendingInviteKey(invite) | ||
| const kept = s.pending.filter( | ||
| (e) => e.key !== key && e.invite.payload.expires_at > now | ||
| ) | ||
| return { pending: [...kept, { key, invite, receivedAt: now }] } | ||
| }), | ||
| remove: (key) => | ||
| set((s) => | ||
| s.pending.some((e) => e.key === key) | ||
| ? { pending: s.pending.filter((e) => e.key !== key) } | ||
| : s | ||
| ), | ||
| prune: (now = Date.now()) => | ||
| set((s) => { | ||
| const kept = s.pending.filter((e) => e.invite.payload.expires_at > now) | ||
| return kept.length === s.pending.length ? s : { pending: kept } | ||
| }), | ||
| clear: () => set({ pending: [] }), | ||
| })) |
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
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,59 @@ | ||
| import type { Meta, StoryObj } from '@storybook/react-vite' | ||
|
|
||
| import { PendingInvitesView } from '@/features/friends' | ||
| import type { PendingInviteEntry } from '@/features/friends' | ||
|
|
||
| // #47 B1 — persistent pending-invite rows on the main view (pure view; | ||
| // store + expiry wiring live in the PendingInvites container). | ||
|
|
||
| const NOW = 1_700_000_000_000 | ||
|
|
||
| function entry( | ||
| seed: string, | ||
| name: string | null, | ||
| msLeft: number | ||
| ): PendingInviteEntry { | ||
| return { | ||
| key: `${seed}:topic-${seed}`, | ||
| receivedAt: NOW - 30_000, | ||
| invite: { | ||
| from_ed_pubkey: seed.repeat(64).slice(0, 64), | ||
| payload: { | ||
| session_topic: `topic-${seed}`, | ||
| session_password: 'pw', | ||
| our_display_name: name ?? '', | ||
| expires_at: NOW + msLeft, | ||
| sig: '', | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| const meta = { | ||
| title: 'Friends/PendingInvites', | ||
| component: PendingInvitesView, | ||
| args: { | ||
| now: NOW, | ||
| onAccept: () => {}, | ||
| onDismiss: () => {}, | ||
| entries: [entry('a', 'Alex', 4 * 60_000), entry('b', null, 90_000)], | ||
| }, | ||
| } satisfies Meta<typeof PendingInvitesView> | ||
|
|
||
| export default meta | ||
| type Story = StoryObj<typeof meta> | ||
|
|
||
| // Two pending invites: a named sender with minutes left and a fallback-named | ||
| // sender about to expire. | ||
| export const TwoPending: Story = {} | ||
|
|
||
| export const SingleInvite: Story = { | ||
| args: { | ||
| entries: [entry('a', 'Alex', 3 * 60_000)], | ||
| }, | ||
| } | ||
|
|
||
| // Renders nothing — the section only exists while invites are pending. | ||
| export const Empty: Story = { | ||
| args: { entries: [] }, | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Stale
nowcauses incorrect countdown on first invite arrival.nowis initialized once viauseState(() => Date.now())and only updated by the 10-second interval — which doesn't start untilpending.length > 0. If the component has been mounted on the main view for minutes without invites, the first invite's countdown will be based on the stale initialnow, showing a much higher "Expires in N min" than reality for up to 10 seconds until the first interval tick fires.The actual expiry behavior is unaffected (
pruneandrunGuestJoinboth useDate.now()), so this is display-only.Fix: refresh `now` when the interval starts
useEffect(() => { if (pending.length === 0) return + setNow(Date.now()) const id = setInterval(() => { setNow(Date.now()) usePendingInvitesStore.getState().prune() }, 10_000) return () => clearInterval(id) }, [pending.length])📝 Committable suggestion
🤖 Prompt for AI Agents