Spaces:
Sleeping
Sleeping
File size: 5,662 Bytes
a0da743 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 'use client';
import React, { createContext, useContext, useReducer, useCallback } from 'react';
import {
InterviewSession,
InterviewConfig,
Question,
ChatMessage,
Evaluation,
Answer,
SessionSummary,
} from '@/types/interview';
type InterviewAction =
| { type: 'START_SESSION'; payload: { config: InterviewConfig } }
| { type: 'SET_QUESTIONS'; payload: { questions: Question[] } }
| { type: 'ADD_MESSAGE'; payload: { message: ChatMessage } }
| { type: 'SUBMIT_ANSWER'; payload: { answer: Answer } }
| { type: 'ADD_EVALUATION'; payload: { evaluation: Evaluation } }
| { type: 'NEXT_QUESTION' }
| { type: 'SET_STATUS'; payload: { status: InterviewSession['status'] } }
| { type: 'COMPLETE_SESSION'; payload: { summary: SessionSummary } }
| { type: 'RESET_SESSION' };
interface InterviewState {
session: InterviewSession | null;
}
const initialState: InterviewState = {
session: null,
};
function generateId(): string {
return Date.now().toString(36) + Math.random().toString(36).substring(2);
}
function interviewReducer(state: InterviewState, action: InterviewAction): InterviewState {
switch (action.type) {
case 'START_SESSION':
return {
session: {
id: generateId(),
config: action.payload.config,
questions: [],
answers: [],
evaluations: [],
messages: [],
currentQuestionIndex: 0,
status: 'setup',
startedAt: Date.now(),
},
};
case 'SET_QUESTIONS':
if (!state.session) return state;
return {
session: {
...state.session,
questions: action.payload.questions,
status: 'in-progress',
},
};
case 'ADD_MESSAGE':
if (!state.session) return state;
return {
session: {
...state.session,
messages: [...state.session.messages, action.payload.message],
},
};
case 'SUBMIT_ANSWER':
if (!state.session) return state;
return {
session: {
...state.session,
answers: [...state.session.answers, action.payload.answer],
status: 'evaluating',
},
};
case 'ADD_EVALUATION':
if (!state.session) return state;
return {
session: {
...state.session,
evaluations: [...state.session.evaluations, action.payload.evaluation],
status: 'in-progress',
},
};
case 'NEXT_QUESTION':
if (!state.session) return state;
return {
session: {
...state.session,
currentQuestionIndex: state.session.currentQuestionIndex + 1,
},
};
case 'SET_STATUS':
if (!state.session) return state;
return {
session: {
...state.session,
status: action.payload.status,
},
};
case 'COMPLETE_SESSION':
if (!state.session) return state;
return {
session: {
...state.session,
status: 'completed',
completedAt: Date.now(),
summary: action.payload.summary,
},
};
case 'RESET_SESSION':
return initialState;
default:
return state;
}
}
interface InterviewContextType {
session: InterviewSession | null;
startSession: (config: InterviewConfig) => void;
setQuestions: (questions: Question[]) => void;
addMessage: (message: ChatMessage) => void;
submitAnswer: (answer: Answer) => void;
addEvaluation: (evaluation: Evaluation) => void;
nextQuestion: () => void;
setStatus: (status: InterviewSession['status']) => void;
completeSession: (summary: SessionSummary) => void;
resetSession: () => void;
}
const InterviewContext = createContext<InterviewContextType | null>(null);
export function InterviewProvider({ children }: { children: React.ReactNode }) {
const [state, dispatch] = useReducer(interviewReducer, initialState);
const startSession = useCallback((config: InterviewConfig) => {
dispatch({ type: 'START_SESSION', payload: { config } });
}, []);
const setQuestions = useCallback((questions: Question[]) => {
dispatch({ type: 'SET_QUESTIONS', payload: { questions } });
}, []);
const addMessage = useCallback((message: ChatMessage) => {
dispatch({ type: 'ADD_MESSAGE', payload: { message } });
}, []);
const submitAnswer = useCallback((answer: Answer) => {
dispatch({ type: 'SUBMIT_ANSWER', payload: { answer } });
}, []);
const addEvaluation = useCallback((evaluation: Evaluation) => {
dispatch({ type: 'ADD_EVALUATION', payload: { evaluation } });
}, []);
const nextQuestion = useCallback(() => {
dispatch({ type: 'NEXT_QUESTION' });
}, []);
const setStatus = useCallback((status: InterviewSession['status']) => {
dispatch({ type: 'SET_STATUS', payload: { status } });
}, []);
const completeSession = useCallback((summary: SessionSummary) => {
dispatch({ type: 'COMPLETE_SESSION', payload: { summary } });
}, []);
const resetSession = useCallback(() => {
dispatch({ type: 'RESET_SESSION' });
}, []);
return (
<InterviewContext.Provider
value={{
session: state.session,
startSession,
setQuestions,
addMessage,
submitAnswer,
addEvaluation,
nextQuestion,
setStatus,
completeSession,
resetSession,
}}
>
{children}
</InterviewContext.Provider>
);
}
export function useInterview() {
const context = useContext(InterviewContext);
if (!context) {
throw new Error('useInterview must be used within an InterviewProvider');
}
return context;
}
|