Refactor SSE event handling and consolidate notification contexts

This commit is contained in:
Chris Scott committed 2026-01-12 23:56:50 -05:00
1 parent 9851ec06e0
commit f7fd3176d4
30 files changed
+2226 -988

No files matched your search

@@ -10,7 +10,7 @@ import { useVariants } from '@/hooks/useVariants'
import { useUserBash } from '@/stores/userBashStore'
import { useMobile } from '@/hooks/useMobile'
import { useSessionStatusForSession } from '@/stores/sessionStatusStore'
import { usePermissionContext } from '@/contexts/PermissionContext'
import { usePermissions } from '@/contexts/EventContext'
import { ChevronDown, Upload, X } from 'lucide-react'
import { CommandSuggestions } from '@/components/command/CommandSuggestions'
@@ -680,7 +680,7 @@ const { model, modelString } = useModelSelection(opcodeUrl, directory)
const currentModel = modelString || ''
const displayModelName = model?.modelID || currentModel
const isMobile = useMobile()
const { setShowDialog, hasPermissionsForSession } = usePermissionContext()
const { setShowDialog, hasForSession: hasPermissionsForSession } = usePermissions()
const hasPendingPermissionForSession = hasPermissionsForSession(sessionID)
const { hasVariants, currentVariant, cycleVariant } = useVariants(opcodeUrl, directory)
const sessionStatus = useSessionStatusForSession(sessionID)
@@ -2,7 +2,7 @@ import { useState, useRef, useEffect } from 'react'
import type { components } from '@/api/opencode-types'
import { useSettings } from '@/hooks/useSettings'
import { useUserBash } from '@/stores/userBashStore'
import { usePermissionContext } from '@/contexts/PermissionContext'
import { usePermissions, useQuestions } from '@/contexts/EventContext'
import { detectFileReferences } from '@/lib/fileReferences'
import { ExternalLink, Loader2 } from 'lucide-react'
import { CopyButton } from '@/components/ui/copy-button'
@@ -72,7 +72,8 @@ function parseTodoOutput(output: string): Todo[] | null {
export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCallPartProps) {
const { preferences } = useSettings()
const { userBashCommands } = useUserBash()
const { getPermissionForCallID } = usePermissionContext()
const { getForCallID: getPermissionForCallID } = usePermissions()
const { getForCallID: getQuestionForCallID } = useQuestions()
const outputRef = useRef<HTMLDivElement>(null)
const isUserBashCommand = part.tool === 'bash' &&
part.state.status === 'completed' &&
@@ -83,6 +84,8 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
const pendingPermission = getPermissionForCallID(part.callID, part.sessionID)
const isWaitingPermission = part.state.status === 'running' && !!pendingPermission
const pendingQuestion = getQuestionForCallID(part.callID, part.sessionID)
const isWaitingQuestion = part.state.status === 'running' && !!pendingQuestion
useEffect(() => {
if (part.tool === 'bash' && expanded && outputRef.current) {
@@ -97,7 +100,9 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
case 'error':
return 'text-red-600 dark:text-red-400'
case 'running':
return isWaitingPermission ? 'text-orange-600 dark:text-orange-400' : 'text-yellow-600 dark:text-yellow-400'
if (isWaitingPermission) return 'text-orange-600 dark:text-orange-400'
if (isWaitingQuestion) return 'text-blue-600 dark:text-blue-400'
return 'text-yellow-600 dark:text-yellow-400'
default:
return 'text-muted-foreground'
}
@@ -255,7 +260,9 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
const getBorderStyle = () => {
switch (part.state.status) {
case 'running':
return isWaitingPermission ? 'border-orange-500/50 shadow-sm shadow-orange-500/20' : 'border-yellow-500/50 shadow-sm shadow-yellow-500/10'
if (isWaitingPermission) return 'border-orange-500/50 shadow-sm shadow-orange-500/20'
if (isWaitingQuestion) return 'border-blue-500/50 shadow-sm shadow-blue-500/20'
return 'border-yellow-500/50 shadow-sm shadow-yellow-500/10'
case 'pending':
return 'border-blue-500/30'
case 'error':
@@ -312,7 +319,7 @@ export function ToolCallPart({ part, onFileClick, onChildSessionClick }: ToolCal
</span>
) : null
})()}
<span className="text-muted-foreground text-xs ml-auto">({isWaitingPermission ? 'waiting' : part.state.status})</span>
<span className="text-muted-foreground text-xs ml-auto">({isWaitingPermission ? 'awaiting permission' : isWaitingQuestion ? 'awaiting answer' : part.state.status})</span>
</button>
{expanded && (
@@ -0,0 +1,27 @@
import { Bell, HelpCircle } from 'lucide-react'
import { PendingActionBadge } from '@/components/ui/pending-action-badge'
import { usePermissions, useQuestions } from '@/contexts/EventContext'
export function PendingActionsGroup() {
const { pendingCount: permissionCount, setShowDialog } = usePermissions()
const { pendingCount: questionCount, navigateToCurrent } = useQuestions()
return (
<>
<PendingActionBadge
count={permissionCount}
icon={Bell}
color="orange"
onClick={() => setShowDialog(true)}
label="permission"
/>
<PendingActionBadge
count={questionCount}
icon={HelpCircle}
color="blue"
onClick={navigateToCurrent}
label="question"
/>
</>
)
}
@@ -1,32 +0,0 @@
import { usePermissionContext } from '@/contexts/PermissionContext'
import { useMobile } from '@/hooks/useMobile'
import { Bell } from 'lucide-react'
export function GlobalPermissionNotification() {
const { pendingCount, setShowDialog } = usePermissionContext()
const isMobile = useMobile()
if (pendingCount === 0) return null
return (
<button
onClick={() => setShowDialog(true)}
className={`fixed z-50 ${
isMobile ? 'bottom-20 right-4' : 'top-4 right-4'
} bg-gradient-to-br from-orange-500 to-orange-600 hover:from-orange-400 hover:to-orange-500 border-2 border-orange-400/60 hover:border-orange-300 shadow-lg shadow-orange-500/40 hover:shadow-orange-500/60 ring-2 ring-orange-500/20 hover:ring-orange-500/40 text-white rounded-full transition-all duration-200 ${
isMobile ? 'w-14 h-14' : 'w-12 h-12'
} hover:scale-110 active:scale-95 flex items-center justify-center`}
title={`${pendingCount} pending permission${pendingCount > 1 ? 's' : ''}`}
>
<Bell className="w-6 h-6" />
{pendingCount > 0 && (
<span
className={`absolute -top-1 -right-1 bg-destructive text-destructive-foreground text-xs font-bold rounded-full w-5 h-5 flex items-center justify-center border-2 border-background`}
>
{pendingCount > 9 ? '9+' : pendingCount}
</span>
)}
</button>
)
}
@@ -0,0 +1,499 @@
import { useState, useEffect, useRef, useCallback } from 'react'
import { X, ChevronLeft, ChevronRight, Check, Loader2 } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Textarea } from '@/components/ui/textarea'
import type { QuestionRequest, QuestionInfo } from '@/api/types'
import { cn } from '@/lib/utils'
import { showToast } from '@/lib/toast'
interface QuestionPromptProps {
question: QuestionRequest
onReply: (requestID: string, answers: string[][]) => Promise<void>
onReject: (requestID: string) => Promise<void>
}
export function QuestionPrompt({ question, onReply, onReject }: QuestionPromptProps) {
const questions = question.questions
const isSingleSelect = questions.length === 1 && !questions[0]?.multiple
const totalSteps = isSingleSelect ? 1 : questions.length + 1
const [currentIndex, setCurrentIndex] = useState(0)
const [answers, setAnswers] = useState<string[][]>(() => questions.map(() => []))
const [customInputs, setCustomInputs] = useState<string[]>(() => questions.map(() => ''))
const [confirmedCustoms, setConfirmedCustoms] = useState<string[]>(() => questions.map(() => ''))
const [expandedOther, setExpandedOther] = useState<number | null>(null)
const [isSubmitting, setIsSubmitting] = useState(false)
const isConfirmStep = !isSingleSelect && currentIndex === questions.length
const currentQuestion = isConfirmStep ? null : questions[currentIndex]
const isMultiSelect = currentQuestion?.multiple === true
const goToNext = useCallback(() => {
if (currentIndex < totalSteps - 1) {
setCurrentIndex(prev => prev + 1)
setExpandedOther(null)
}
}, [currentIndex, totalSteps])
const goToPrev = useCallback(() => {
if (currentIndex > 0) {
setCurrentIndex(prev => prev - 1)
setExpandedOther(null)
}
}, [currentIndex])
const handleSubmitSingle = useCallback(async (label: string) => {
setIsSubmitting(true)
try {
await onReply(question.id, [[label]])
} catch {
showToast.error('Failed to submit answer')
} finally {
setIsSubmitting(false)
}
}, [onReply, question.id])
const selectOption = useCallback((questionIndex: number, label: string) => {
const isMultiple = questions[questionIndex]?.multiple
setAnswers(prev => {
const updated = [...prev]
const current = updated[questionIndex] ?? []
if (isMultiple) {
const exists = current.includes(label)
updated[questionIndex] = exists
? current.filter(l => l !== label)
: [...current, label]
} else {
updated[questionIndex] = [label]
}
return updated
})
if (!isMultiple) {
if (isSingleSelect) {
handleSubmitSingle(label)
} else {
setTimeout(() => goToNext(), 150)
}
}
}, [questions, isSingleSelect, goToNext, handleSubmitSingle])
const handleCustomInput = useCallback((questionIndex: number, value: string) => {
setCustomInputs(prev => {
const updated = [...prev]
updated[questionIndex] = value
return updated
})
}, [])
const confirmCustomInput = useCallback((questionIndex: number) => {
const value = customInputs[questionIndex]?.trim()
if (!value) {
setExpandedOther(null)
return
}
const oldCustom = confirmedCustoms[questionIndex]
setAnswers(prev => {
const updated = [...prev]
const current = updated[questionIndex] ?? []
if (questions[questionIndex]?.multiple) {
const withoutOld = oldCustom ? current.filter(l => l !== oldCustom) : current
updated[questionIndex] = [...withoutOld, value]
} else {
updated[questionIndex] = [value]
if (!isSingleSelect) {
setTimeout(() => goToNext(), 150)
}
}
return updated
})
setConfirmedCustoms(prev => {
const updated = [...prev]
updated[questionIndex] = value
return updated
})
setExpandedOther(null)
if (isSingleSelect) {
handleSubmitSingle(value)
}
}, [customInputs, confirmedCustoms, questions, isSingleSelect, goToNext, handleSubmitSingle])
const handleSubmit = async () => {
setIsSubmitting(true)
try {
await onReply(question.id, answers)
} catch {
showToast.error('Failed to submit answers')
} finally {
setIsSubmitting(false)
}
}
const handleReject = async () => {
setIsSubmitting(true)
try {
await onReject(question.id)
} catch {
showToast.error('Failed to dismiss question')
} finally {
setIsSubmitting(false)
}
}
const hasAnswerForQuestion = (index: number) => {
return (answers[index]?.length ?? 0) > 0
}
const allQuestionsAnswered = questions.every((_, i) => hasAnswerForQuestion(i))
return (
<div
className="w-full bg-gradient-to-br from-blue-500/10 to-blue-600/5 border-2 border-blue-500/30 rounded-xl shadow-lg shadow-blue-500/10 backdrop-blur-sm mb-3 overflow-hidden"
>
<div className="flex items-center justify-between px-3 py-2 border-b border-blue-500/20 bg-blue-500/5">
<div className="flex items-center gap-2">
{totalSteps > 1 && (
<button
onClick={goToPrev}
disabled={currentIndex === 0}
className="p-1 rounded hover:bg-blue-500/20 disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
>
<ChevronLeft className="w-4 h-4 text-blue-600 dark:text-blue-400" />
</button>
)}
<span className="text-sm font-semibold text-blue-400">
{isConfirmStep ? 'Review' : (
totalSteps > 1
? `Question ${currentIndex + 1}/${questions.length}`
: (currentQuestion?.header || 'Question')
)}
</span>
{totalSteps > 1 && (
<button
onClick={goToNext}
disabled={currentIndex === totalSteps - 1}
className="p-1 rounded hover:bg-blue-500/20 disabled:opacity-30 disabled:cursor-not-allowed transition-colors"
>
<ChevronRight className="w-4 h-4 text-blue-600 dark:text-blue-400" />
</button>
)}
</div>
<button
onClick={handleReject}
disabled={isSubmitting}
className="p-1.5 rounded-lg hover:bg-red-500/20 text-muted-foreground hover:text-red-500 transition-colors"
>
<X className="w-4 h-4" />
</button>
</div>
<div className="p-3 max-h-[40vh] overflow-y-auto">
{isConfirmStep ? (
<ConfirmStep
questions={questions}
answers={answers}
onEditQuestion={setCurrentIndex}
/>
) : currentQuestion && (
<QuestionStep
question={currentQuestion}
answers={answers[currentIndex] ?? []}
customInput={customInputs[currentIndex] ?? ''}
confirmedCustom={confirmedCustoms[currentIndex] ?? ''}
expandedOther={expandedOther === currentIndex}
isMultiSelect={isMultiSelect}
onSelectOption={(label) => selectOption(currentIndex, label)}
onExpandOther={() => setExpandedOther(currentIndex)}
onCustomInputChange={(value) => handleCustomInput(currentIndex, value)}
onConfirmCustomInput={() => confirmCustomInput(currentIndex)}
onCollapseOther={() => setExpandedOther(null)}
/>
)}
</div>
{totalSteps > 1 && (
<div className="flex justify-center gap-1.5 py-2 border-t border-blue-500/10">
{Array.from({ length: totalSteps }).map((_, i) => (
<button
key={i}
onClick={() => {
setCurrentIndex(i)
setExpandedOther(null)
}}
className={cn(
"w-2 h-2 rounded-full transition-all duration-200",
i === currentIndex
? "bg-blue-500 scale-125"
: i < questions.length && hasAnswerForQuestion(i)
? "bg-green-500/70 hover:bg-green-500"
: "bg-blue-500/30 hover:bg-blue-500/50"
)}
/>
))}
</div>
)}
<div className="flex gap-2 px-3 pb-3">
<Button
variant="outline"
size="sm"
onClick={handleReject}
disabled={isSubmitting}
className="flex-1 h-10 border-blue-500/30 hover:bg-blue-500/10 hover:border-blue-500/50"
>
Dismiss
</Button>
{!isSingleSelect && (
isConfirmStep ? (
<Button
size="sm"
onClick={handleSubmit}
disabled={isSubmitting || !allQuestionsAnswered}
className="flex-1 h-10 bg-blue-600 hover:bg-blue-700 text-white"
>
{isSubmitting ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
'Submit'
)}
</Button>
) : (
<Button
size="sm"
onClick={goToNext}
disabled={currentIndex === totalSteps - 1}
className="flex-1 h-10 bg-blue-600 hover:bg-blue-700 text-white"
>
{currentIndex === questions.length - 1 ? 'Review' : 'Next'}
<ChevronRight className="w-4 h-4 ml-1" />
</Button>
)
)}
</div>
</div>
)
}
interface QuestionStepProps {
question: QuestionInfo
answers: string[]
customInput: string
confirmedCustom: string
expandedOther: boolean
isMultiSelect: boolean
onSelectOption: (label: string) => void
onExpandOther: () => void
onCustomInputChange: (value: string) => void
onConfirmCustomInput: () => void
onCollapseOther: () => void
}
function QuestionStep({
question,
answers,
customInput,
confirmedCustom,
expandedOther,
isMultiSelect,
onSelectOption,
onExpandOther,
onCustomInputChange,
onConfirmCustomInput,
onCollapseOther,
}: QuestionStepProps) {
const textareaRef = useRef<HTMLTextAreaElement>(null)
useEffect(() => {
if (expandedOther && textareaRef.current) {
textareaRef.current.focus()
}
}, [expandedOther])
const isCustomSelected = confirmedCustom && answers.includes(confirmedCustom)
return (
<div className="space-y-3">
<p className="text-sm font-semibold text-foreground">
{question.question}
{isMultiSelect && (
<span className="text-foreground/60 font-normal ml-1">(select all that apply)</span>
)}
</p>
<div className="space-y-2">
{question.options.map((option, i) => {
const isSelected = answers.includes(option.label)
return (
<button
key={i}
onClick={() => onSelectOption(option.label)}
className={cn(
"w-full text-left p-3 rounded-lg border-2 transition-all duration-200 active:scale-[0.98]",
isSelected
? "border-blue-500 bg-blue-500/10"
: "border-border hover:border-blue-500/50 hover:bg-blue-500/5"
)}
>
<div className="flex items-start justify-between gap-2">
<div className="flex items-center gap-2 min-w-0">
<div className={cn(
"w-5 h-5 rounded-full border-2 flex items-center justify-center flex-shrink-0 transition-colors",
isSelected
? "border-blue-500 bg-blue-500"
: "border-muted-foreground"
)}>
{isSelected && <Check className="w-3 h-3 text-white" />}
</div>
<span className={cn(
"text-sm font-semibold",
isSelected ? "text-blue-400" : "text-foreground"
)}>
{option.label}
</span>
</div>
</div>
{option.description && (
<p className="text-xs text-foreground/70 mt-1 ml-7">
{option.description}
</p>
)}
</button>
)
})}
<button
onClick={() => {
if (expandedOther) {
onCollapseOther()
} else {
onExpandOther()
}
}}
className={cn(
"w-full text-left p-3 rounded-lg border-2 transition-all duration-200",
expandedOther || isCustomSelected
? "border-blue-500 bg-blue-500/10"
: "border-border hover:border-blue-500/50 hover:bg-blue-500/5"
)}
>
<div className="flex items-center gap-2">
<div className={cn(
"w-5 h-5 rounded-full border-2 flex items-center justify-center flex-shrink-0 transition-colors",
isCustomSelected
? "border-blue-500 bg-blue-500"
: "border-muted-foreground"
)}>
{isCustomSelected && <Check className="w-3 h-3 text-white" />}
</div>
<span className={cn(
"text-sm font-semibold",
expandedOther || isCustomSelected ? "text-blue-400" : "text-foreground"
)}>
Other...
</span>
</div>
</button>
{expandedOther && (
<div className="ml-7 space-y-2 animate-in slide-in-from-top-2 duration-200">
<Textarea
ref={textareaRef}
value={customInput}
onChange={(e) => onCustomInputChange(e.target.value)}
placeholder="Type your own answer..."
className="min-h-[80px] text-sm resize-none border-blue-500/30 focus:border-blue-500"
onKeyDown={(e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault()
onConfirmCustomInput()
}
if (e.key === 'Escape') {
onCollapseOther()
}
}}
/>
<div className="flex gap-2">
<Button
size="sm"
variant="outline"
onClick={onCollapseOther}
className="flex-1 h-8"
>
Cancel
</Button>
<Button
size="sm"
onClick={onConfirmCustomInput}
disabled={!customInput.trim()}
className="flex-1 h-8 bg-blue-600 hover:bg-blue-700 text-white"
>
Confirm
</Button>
</div>
</div>
)}
{!expandedOther && isCustomSelected && (
<div className="ml-7 text-xs text-muted-foreground">
{confirmedCustom}
</div>
)}
</div>
</div>
)
}
interface ConfirmStepProps {
questions: QuestionInfo[]
answers: string[][]
onEditQuestion: (index: number) => void
}
function ConfirmStep({ questions, answers, onEditQuestion }: ConfirmStepProps) {
return (
<div className="space-y-3">
<p className="text-sm font-semibold text-foreground">Review your answers</p>
<div className="space-y-2">
{questions.map((q, i) => {
const answer = answers[i] ?? []
const hasAnswer = answer.length > 0
return (
<button
key={i}
onClick={() => onEditQuestion(i)}
className={cn(
"w-full text-left p-3 rounded-lg border transition-colors",
hasAnswer
? "border-green-500/40 bg-green-500/10 hover:bg-green-500/15"
: "border-red-500/40 bg-red-500/10 hover:bg-red-500/15"
)}
>
<div className="flex items-start justify-between gap-2">
<div className="min-w-0 flex-1">
<p className="text-xs text-foreground/60 truncate">{q.header}</p>
<p className={cn(
"text-sm font-semibold mt-0.5",
hasAnswer ? "text-green-400" : "text-red-400"
)}>
{hasAnswer ? answer.join(', ') : '(not answered)'}
</p>
</div>
<ChevronRight className="w-4 h-4 text-foreground/50 flex-shrink-0 mt-1" />
</div>
</button>
)
})}
</div>
</div>
)
}
@@ -3,6 +3,7 @@ import { Checkbox } from "@/components/ui/checkbox";
import { Trash2, Clock } from "lucide-react";
import { formatDistanceToNow } from "date-fns";
import type { Session } from "@/api/types";
import { useSessionStatusForSession } from "@/stores/sessionStatusStore";
interface SessionCardProps {
session: Session;
@@ -21,6 +22,9 @@ export const SessionCard = ({
onToggleSelection,
onDelete,
}: SessionCardProps) => {
const sessionStatus = useSessionStatusForSession(session.id);
const isSessionBusy = sessionStatus.type === 'busy' || sessionStatus.type === 'retry' || sessionStatus.type === 'compact';
return (
<Card
className={`p-3 cursor-pointer transition-all ${
@@ -45,9 +49,17 @@ export const SessionCard = ({
className="w-5 h-5 flex-shrink-0 mt-0.5"
/>
<div className="flex-1 min-w-0">
<h3 className="text-base font-semibold text-orange-600 dark:text-orange-400 truncate">
{session.title || "Untitled Session"}
</h3>
<div className="flex items-center gap-2">
<h3 className="text-base font-semibold text-orange-600 dark:text-orange-400 truncate">
{session.title || "Untitled Session"}
</h3>
{isSessionBusy && (
<span className="relative flex h-2.5 w-2.5 flex-shrink-0">
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-green-400 opacity-75"></span>
<span className="relative inline-flex rounded-full h-2.5 w-2.5 bg-green-500"></span>
</span>
)}
</div>
<div className="flex items-center gap-3 mt-1 text-xs text-muted-foreground">
<span className="flex items-center gap-1">
<Clock className="w-3 h-3" />
+30 -6
View File
@@ -1,17 +1,17 @@
import { Settings } from "lucide-react";
import { Settings, Bell, HelpCircle, MoreVertical } from "lucide-react";
import { Button } from "@/components/ui/button";
import { BackButton } from "@/components/ui/back-button";
import { PageHeader } from "@/components/ui/page-header";
import { useSettingsDialog } from "@/hooks/useSettingsDialog";
import { useTheme } from "@/hooks/useTheme";
import { EditSessionTitleDialog } from "@/components/session/EditSessionTitleDialog";
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger } from "@/components/ui/dropdown-menu";
import { MoreVertical } from "lucide-react";
import { DropdownMenu, DropdownMenuContent, DropdownMenuTrigger, DropdownMenuItem, DropdownMenuSeparator } from "@/components/ui/dropdown-menu";
import { cn } from "@/lib/utils";
import type { ReactNode } from "react";
import { useState, useEffect } from "react";
import { useMobile } from "@/hooks/useMobile";
import { Loader2, X } from "lucide-react";
import { usePermissions, useQuestions } from "@/contexts/EventContext";
interface HeaderProps {
children: ReactNode;
@@ -173,6 +173,10 @@ function HeaderActions({ children, className }: { children: ReactNode; className
function HeaderMobileDropdown({ children, className }: { children: ReactNode; className?: string }) {
const isMobile = useMobile();
const { pendingCount: permissionCount, setShowDialog } = usePermissions();
const { pendingCount: questionCount, navigateToCurrent } = useQuestions();
const totalPending = permissionCount + questionCount;
if (!isMobile) return null;
@@ -182,13 +186,33 @@ function HeaderMobileDropdown({ children, className }: { children: ReactNode; cl
<Button
variant="outline"
size="icon"
className={cn("text-foreground border-border hover:bg-accent transition-all duration-200 h-8 w-8", className)}
title="Options"
className={cn("text-foreground border-border hover:bg-accent transition-all duration-200 h-8 w-8 relative", className)}
title={totalPending > 0 ? `${totalPending} pending notification${totalPending > 1 ? 's' : ''}` : "Options"}
>
<MoreVertical className="w-4 h-4" />
{totalPending > 0 ? (
<>
<Bell className="w-4 h-4" />
<span className="absolute top-1 right-1 w-2 h-2 rounded-full bg-orange-500 animate-pulse" />
</>
) : (
<MoreVertical className="w-4 h-4" />
)}
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
{permissionCount > 0 && (
<DropdownMenuItem onClick={() => setShowDialog(true)} className="gap-2">
<Bell className="w-4 h-4 text-orange-500" />
<span>{permissionCount} pending permission{permissionCount > 1 ? 's' : ''}</span>
</DropdownMenuItem>
)}
{questionCount > 0 && (
<DropdownMenuItem onClick={navigateToCurrent} className="gap-2">
<HelpCircle className="w-4 h-4 text-blue-500" />
<span>{questionCount} pending question{questionCount > 1 ? 's' : ''}</span>
</DropdownMenuItem>
)}
{totalPending > 0 && children && <DropdownMenuSeparator />}
{children}
</DropdownMenuContent>
</DropdownMenu>
@@ -0,0 +1,64 @@
import { type LucideIcon } from 'lucide-react'
import { Button } from './button'
import { cn } from '@/lib/utils'
type BadgeColor = 'orange' | 'blue'
const colorStyles: Record<BadgeColor, { bg: string; hover: string; text: string }> = {
orange: {
bg: 'bg-orange-500/10',
hover: 'hover:bg-orange-500/20',
text: 'text-orange-500',
},
blue: {
bg: 'bg-blue-500/10',
hover: 'hover:bg-blue-500/20',
text: 'text-blue-500',
},
}
interface PendingActionBadgeProps {
count: number
icon: LucideIcon
color: BadgeColor
onClick: () => void
label: string
className?: string
}
export function PendingActionBadge({
count,
icon: Icon,
color,
onClick,
label,
className,
}: PendingActionBadgeProps) {
if (count === 0) return null
const styles = colorStyles[color]
return (
<Button
variant="ghost"
size="icon"
onClick={onClick}
className={cn(
'relative h-8 w-8 transition-all duration-200',
styles.bg,
styles.hover,
styles.text,
className
)}
title={`${count} pending ${label}${count > 1 ? 's' : ''}`}
>
<Icon className="w-4 h-4" />
<span
className={cn(
'absolute -top-0.5 -right-0.5 w-2 h-2 rounded-full animate-pulse',
color === 'orange' ? 'bg-orange-500' : 'bg-blue-500'
)}
/>
</Button>
)
}