Scrum4Me/stores/notifications-store.ts
Madhura68 9e8f33b96e fix(m12): user can answer idea-questions — inline + bell support
Two gaps discovered during the first live grill-session of IDEA-002:
the agent posted a question, but the user had no UI to answer it.
1. Idea-questions only appeared on the Timeline-tab as read-only entries
2. Notifications-bell fetched + handled story-questions only

This fix:

**Inline answer-form in IdeaTimeline** (components/ideas/idea-timeline.tsx)
- Open questions now render an AnswerForm directly under the question text
- Multi-choice options become clickable buttons (one-click submit); free-text
  fallback via collapsed details/textarea
- Plain free-text questions render textarea + Verzend
- Calls existing answerQuestion server-action; toast + router.refresh on success

**Notifications-bell extended for idea-questions**
- stores/notifications-store.ts: NotificationQuestion → discriminated union
  (kind: 'story' | 'idea'); forYouCount treats idea-questions as always-for-you
  (idea is strictly user_id-only — only the owner sees them)
- components/notifications/notifications-bridge.tsx: parallel fetch of
  story-questions (productAccessFilter) + idea-questions (idea.user_id ===
  session.userId); merged + sorted by created_at
- components/notifications/notifications-sheet.tsx: renders idea_code/title
  for kind='idea'
- components/notifications/answer-modal.tsx: header + open-link branch on
  kind (idea → /ideas/[id]?tab=timeline; story → existing /sprint link)
- lib/realtime/use-notifications-realtime.ts: idea-question events also
  trigger close+reconnect on 'open' (loads fresh detail) and remove(id) on
  non-open — same pattern story-questions already use
- components/shared/notifications-bell.tsx: badge counts idea-questions as
  for-you regardless of assignee

**Security gap closed (actions/questions.ts answerQuestion)**
Before: accepted any answer if user has product-access.
After: idea-questions require idea.user_id === session.userId; story-
questions keep the existing productAccessFilter path. (Prisma 7 rejects
\`{ not: null }\` in WHERE; routing happens app-level after a single fetch.)

Tests: 546/546 still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-05 13:05:39 +02:00

82 lines
2.5 KiB
TypeScript

// ST-1105: Zustand store voor het Claude vraag-antwoord-kanaal (M11).
//
// Houdt de huidige set van **open** vragen voor de ingelogde gebruiker bij —
// gevoed door:
// - Initial-state-event van /api/realtime/notifications (init)
// - Realtime SSE-events op scrum4me_changes (upsert/remove)
// - Server Action answerQuestion (optimistic remove via removeOptimistic)
//
// Eenvoudiger dan solo-store: geen drag-and-drop, geen view-transitions, geen
// pending-ops om optimistic-echo te onderdrukken — antwoorden zijn discrete
// acties en de stream kan met dubbele update-events leven.
import { create } from 'zustand'
// Story-questions en idea-questions delen het bel-paneel sinds M12 hotfix.
// `kind` discrimineert; UI rendert label + link op basis daarvan.
export type NotificationQuestion =
| {
kind: 'story'
id: string
product_id: string
story_id: string
task_id: string | null
story_code: string | null
story_title: string
assignee_id: string | null
question: string
options: string[] | null
created_at: string
expires_at: string
}
| {
kind: 'idea'
id: string
product_id: string
idea_id: string
idea_code: string
idea_title: string
question: string
options: string[] | null
created_at: string
expires_at: string
}
interface NotificationsState {
questions: NotificationQuestion[]
init: (qs: NotificationQuestion[]) => void
upsert: (q: NotificationQuestion) => void
remove: (id: string) => void
openCount: () => number
forYouCount: (userId: string | null) => number
}
export const useNotificationsStore = create<NotificationsState>((set, get) => ({
questions: [],
init: (qs) => set({ questions: qs }),
upsert: (q) =>
set((state) => {
const idx = state.questions.findIndex((x) => x.id === q.id)
if (idx === -1) return { questions: [q, ...state.questions] }
const next = state.questions.slice()
next[idx] = q
return { questions: next }
}),
remove: (id) =>
set((state) => ({ questions: state.questions.filter((q) => q.id !== id) })),
openCount: () => get().questions.length,
forYouCount: (userId) => {
if (!userId) return 0
return get().questions.filter((q) => {
// story-questions: assignee = jij; idea-questions: altijd voor jou
// (idee is strikt user_id-only en alleen jij ziet 'm).
if (q.kind === 'story') return q.assignee_id === userId
return true
}).length
},
}))