feat(chat): stamp sessions with selected artist; refactor useArtists

Merged
AR
6mo ago
+367-232

The new-chat bootstrap now forwards the selected artist's account id as artistId when calling POST /api/sessions. The api persists it on sessions.artist_id, grouping the chat under the artist for the sidebar's artist filter.

  • useNewChatBootstrap reads selectedArtist?.account_id from ArtistProvider and passes it through createSession.
  • CreateSessionInput accepts an optional artistId field.

Pairs with api#628 (now merged) and database#27 (now merged). When no artist is selected, artistId is omitted and the session is created without artist linkage.

Test plan

  • Select an artist, start a new chat from /chat, then GET /api/sessions/{sid} → response has artistId === selectedArtist.account_id.
  • No artist selected, start a new chat → response has artistId === null.

Summary by cubic

New chats now tag sessions with the selected artist (artistId), and the bootstrap runs exactly once per (artistId, orgId) with proper loading gates to avoid duplicate POSTs. Switching or clearing the artist while viewing an existing chat hard-redirects to /chat; on bare /chat, the bootstrap re-runs in place.

  • New Features

    • Send selectedArtist.account_id as artistId to POST /api/sessions (omit if none) so sessions.artist_id backs the sidebar artist filter.
    • Add optional artistId to CreateSessionInput.
  • Refactors

    • Split artists logic: roster moved to hooks/artists/useArtistsRoster (@tanstack/react-query keyed on (userId, orgId)), selection to hooks/artists/useArtistSelection (override → saved → first), removed useInitialArtists, persisted per org.
    • Roster query is gated on Privy’s authenticated; setArtists mutates the query cache; refetchArtists powers getArtists.
    • useArtistsRoster surfaces react-query v5 isPending as isLoading so the disabled-query window still blocks the bootstrap, preventing early provisioning with artistId: undefined.
    • useNewChatBootstrap now delegates to hooks/sessions/useProvisionChatSession (useMutation keyed on (artistId, orgId) and gated on artist loading); extracted lib/sessions/provisionChatSession to combine createSession + createSandbox.
    • In Artist.tsx, detect existing-chat URLs and use window.location.href = "/chat" when changing or deselecting the artist; on bare /chat, no navigation.

Summary by CodeRabbit

  • New Features

    • Chats now persist an associated artist and new sessions respect the selected artist.
    • Improved artist roster loading and selection with per-organization persisted choices.
  • Bug Fixes

    • Switching or deselecting an artist while on a chat now navigates to a fresh chat context to avoid stale state.
    • Session provisioning is now gated to avoid premature or duplicate provisioning when artist/org or auth state changes.