Skip to content

Feature: Market Mood

The canonical RPC pattern reference for the codebase. Market Mood computes NSE sentiment/breadth (e.g. Nifty 50 mood) entirely from Postgres reads — no secrets, no external HTTP — so it is a pure RPC + cached hook feature. New read features should mirror it.

Location

  • Tier / path: src/tiers/users/features/market-mood/
  • Hook (exemplar): hooks/useNifty50MoodData.js
  • RPC SQL: supabase/migrations/20260420_001_market_mood_rpc_functions.sql
  • Cache utilities: @/lib/cachegetFromCache(key, ttl) / setToCache(key, data)

Data flow

Why it's the reference

  • One hook per RPC, returning { data, loading, error, refetch }.
  • Module-level CACHE_KEY; short TTL (1–2 min) appropriate for intraday data.
  • hasLoaded ref so background refreshes never flicker the spinner.
  • Visibility polling + interval refresh for live data; optional supabase.channel() for frequent INSERTs.

See the full pattern in Data Access Strategy.

Database

Reads only, via the get_nifty50_market_mood RPC (and siblings). All RPCs are SECURITY DEFINER with SET search_path = public. Tables are listed in the table index.

Tests

Co-located tests/; run npm run test:market-mood. Covers the hook (mocked supabase + cache), pure computation utils, and component/page layers.

Gotchas

  • This feature must never acquire an edge function for reads — adding secrets/external HTTP would mean it no longer qualifies as the pure-RPC exemplar.
  • Multi-index variants exist (see documentations/MULTI_INDEX_MARKET_MOOD_PLAN.md).