Download survey_apps/main_study/src/App.js from sciminds/same-words-different-judgements: direct link, hf CLI and curl.
- Browser
- Download file 9.42 kB
-
https://huggingface.co/datasets/sciminds/same-words-different-judgements/resolve/main/survey_apps/main_study/src/App.js
- Command line
-
hf download hf://datasets/sciminds/same-words-different-judgements/survey_apps/main_study/src/App.js
-
curl -L -o App.js https://huggingface.co/datasets/sciminds/same-words-different-judgements/resolve/main/survey_apps/main_study/src/App.js
9.42 kB
| import { useEffect, useState } from "react"; | |
| import { signInAnonymously, onAuthStateChanged } from "firebase/auth"; | |
| import { auth, db, firebaseConfigured } from "./firebase"; | |
| import { completionURLFor } from "./config"; | |
| import { doc, setDoc } from "firebase/firestore"; | |
| // Import custom components | |
| import ProlificIdForm from "./components/ProlificIdForm"; | |
| import ConsentForm from "./components/ConsentForm"; | |
| import DemographicForm from "./components/DemographicForm"; | |
| import InstructionsPage from "./components/InstructionsPage"; | |
| import AssignModalityAndTrials from "./components/AssignModalityAndTrials"; | |
| import TrialTaskText from "./components/TrialTaskText"; | |
| import TrialTaskAudio from "./components/TrialTaskAudio"; | |
| import PostSurveyForm from "./components/PostSurveyForm"; | |
| import ThankYouScreen from "./components/ThankYouScreen"; | |
| function App() { | |
| const [userReady, setUserReady] = useState(false); | |
| const [stage, setStage] = useState(() => localStorage.getItem("stage") || "prolificId"); | |
| const [rejected, setRejected] = useState(false); | |
| const [modality, setModality] = useState(() => localStorage.getItem("modality")); | |
| const [assignedTrialIds, setAssignedTrialIds] = useState(() => { | |
| const saved = localStorage.getItem("assignedTrialIds"); | |
| return saved ? JSON.parse(saved) : []; | |
| }); | |
| const [participantNumber, setParticipantNumber] = useState(() => { | |
| const saved = localStorage.getItem("participantNumber"); | |
| return saved ? parseInt(saved, 10) : null; | |
| }); | |
| const [currentTrialIndex, setCurrentTrialIndex] = useState(() => { | |
| const saved = localStorage.getItem("currentTrialIndex"); | |
| return saved ? parseInt(saved, 10) : 0; | |
| }); | |
| const [sessionStartTime, setSessionStartTime] = useState(() => { | |
| const saved = localStorage.getItem("sessionStartTime"); | |
| return saved ? parseInt(saved, 10) : null; | |
| }); | |
| // Persist state values to localStorage when they change | |
| useEffect(() => { | |
| localStorage.setItem("stage", stage); | |
| }, [stage]); | |
| useEffect(() => { | |
| localStorage.setItem("modality", modality); | |
| }, [modality]); | |
| useEffect(() => { | |
| localStorage.setItem("currentTrialIndex", currentTrialIndex); | |
| }, [currentTrialIndex]); | |
| useEffect(() => { | |
| localStorage.setItem("assignedTrialIds", JSON.stringify(assignedTrialIds)); | |
| }, [assignedTrialIds]); | |
| useEffect(() => { | |
| if (participantNumber !== null) { | |
| localStorage.setItem("participantNumber", participantNumber.toString()); | |
| } | |
| }, [participantNumber]); | |
| useEffect(() => { | |
| if (sessionStartTime) { | |
| localStorage.setItem("sessionStartTime", sessionStartTime.toString()); | |
| } | |
| }, [sessionStartTime]); | |
| // 🔐 FIXED: Handle anonymous login only once, prevent flicker/loop | |
| useEffect(() => { | |
| if (!firebaseConfigured) return; | |
| const unsubscribe = onAuthStateChanged(auth, (user) => { | |
| if (user) { | |
| setUserReady(true); | |
| } else { | |
| // Use a short delay to avoid rapid auth state bouncing | |
| setTimeout(() => { | |
| if (!auth.currentUser) { | |
| signInAnonymously(auth) | |
| .then(() => console.log("Signed in anonymously")) | |
| .catch(console.error); | |
| } | |
| }, 100); | |
| } | |
| }); | |
| return () => unsubscribe(); | |
| }, []); | |
| // Clear localStorage when stage is "thankyou" | |
| useEffect(() => { | |
| if (stage === "thankyou") { | |
| localStorage.removeItem("stage"); | |
| localStorage.removeItem("modality"); | |
| localStorage.removeItem("currentTrialIndex"); | |
| localStorage.removeItem("assignedTrialIds"); | |
| localStorage.removeItem("participantNumber"); | |
| localStorage.removeItem("sessionStartTime"); | |
| } | |
| }, [stage]); | |
| // 🎯 STAGE CONTROL | |
| if (!firebaseConfigured) { | |
| return ( | |
| <div style={{ maxWidth: 720, margin: "60px auto", padding: 24, | |
| fontFamily: "system-ui, sans-serif", lineHeight: 1.5 }}> | |
| <h2>Firebase is not configured</h2> | |
| <p>This survey app needs a Firebase project to handle anonymous | |
| sign-in and write participant responses. Without one, the React | |
| UI shell loads but the survey flow can't start.</p> | |
| <p>To configure:</p> | |
| <ol> | |
| <li>Create a Firebase project (or reuse one) at | |
| <a href="https://console.firebase.google.com" target="_blank" | |
| rel="noreferrer"> console.firebase.google.com</a>.</li> | |
| <li>Enable <strong>Anonymous Authentication</strong> and | |
| <strong>Firestore</strong>.</li> | |
| <li>In <code>survey_apps/main/</code>, copy <code>.env.example</code> to | |
| <code>.env</code> and paste your project's web-app config | |
| values.</li> | |
| <li>The dev server hot-reloads — refresh this page once | |
| <code>.env</code> is in place.</li> | |
| </ol> | |
| <p style={{ color: "#555", fontSize: 14, marginTop: 24 }}> | |
| (If you only want to verify the bundle compiles and assets are | |
| served, that's already confirmed by the fact that you can read | |
| this message.) | |
| </p> | |
| </div> | |
| ); | |
| } | |
| if (!userReady) return <p>Signing you in...</p>; | |
| if (stage === "prolificId") { | |
| return <ProlificIdForm onComplete={() => setStage("consent")} />; | |
| } | |
| if (stage === "consent") { | |
| return ( | |
| <ConsentForm | |
| onConsentApproved={() => setStage("demographics")} | |
| onRejected={() => { | |
| setRejected(true); | |
| setStage("thankyou"); | |
| }} | |
| /> | |
| ); | |
| } | |
| if (stage === "demographics") { | |
| return <DemographicForm onComplete={() => setStage("assign")} />; | |
| } | |
| if (stage === "assign") { | |
| return ( | |
| <AssignModalityAndTrials | |
| onComplete={({ modality, assignedTrialIds, participantNumber }) => { | |
| setModality(modality); | |
| setAssignedTrialIds(assignedTrialIds); | |
| setParticipantNumber(participantNumber); | |
| setStage("instructions"); | |
| }} | |
| /> | |
| ); | |
| } | |
| if (stage === "instructions") { | |
| return ( | |
| <InstructionsPage | |
| modality={modality} | |
| onReady={async () => { | |
| const startTime = Date.now(); | |
| setSessionStartTime(startTime); | |
| // Save session start time to Firebase immediately to prevent data loss | |
| try { | |
| await setDoc(doc(db, "participants", auth.currentUser.uid), { | |
| sessionStartTime: new Date(startTime).toISOString() | |
| }, { merge: true }); | |
| } catch (error) { | |
| console.error("Error saving session start time:", error); | |
| } | |
| setStage("trials"); | |
| }} | |
| /> | |
| ); | |
| } | |
| if (stage === "trials") { | |
| // Trial 0 is attention check, trials 1-20 use assignedTrialIds[0-19] | |
| const currentTrialId = currentTrialIndex === 0 ? "ATTENTION_CHECK" : assignedTrialIds[currentTrialIndex - 1]; | |
| const isLast = currentTrialIndex >= assignedTrialIds.length; // Last trial is index 20 (for 20 real trials + 1 attention check) | |
| const goToNext = async () => { | |
| if (!isLast) { | |
| setCurrentTrialIndex((i) => i + 1); | |
| } else { | |
| // Complete session and save timing data to participant document | |
| const sessionEndTime = Date.now(); | |
| try { | |
| await setDoc(doc(db, "participants", auth.currentUser.uid), { | |
| sessionStartTime: sessionStartTime ? new Date(sessionStartTime).toISOString() : null, | |
| sessionEndTime: new Date(sessionEndTime).toISOString(), | |
| totalSessionDurationMs: sessionStartTime ? sessionEndTime - sessionStartTime : null, | |
| sessionCompletedAt: new Date().toISOString(), | |
| sessionCompleted: true | |
| }, { merge: true }); | |
| } catch (error) { | |
| console.error("Error saving session completion:", error); | |
| } | |
| setStage("postsurvey"); | |
| } | |
| }; | |
| const exitEarly = () => setStage("thankyou"); | |
| if (modality === "text") { | |
| return ( | |
| <TrialTaskText | |
| trialIndex={currentTrialIndex} | |
| totalTrials={assignedTrialIds.length + 1} // +1 for attention check | |
| trialId={currentTrialId} | |
| onNext={goToNext} | |
| onExitEarly={exitEarly} | |
| sessionId={sessionStartTime} | |
| participantNumber={participantNumber} | |
| /> | |
| ); | |
| } else if (modality === "audio") { | |
| return ( | |
| <TrialTaskAudio | |
| trialIndex={currentTrialIndex} | |
| totalTrials={assignedTrialIds.length + 1} // +1 for attention check | |
| trialId={currentTrialId} | |
| onNext={goToNext} | |
| onExitEarly={exitEarly} | |
| sessionId={sessionStartTime} | |
| participantNumber={participantNumber} | |
| /> | |
| ); | |
| } else { | |
| return <p>Error: Unknown modality</p>; | |
| } | |
| } | |
| if (stage === "postsurvey") { | |
| return <PostSurveyForm onComplete={() => setStage("thankyou")} />; | |
| } | |
| if (stage === "thankyou") { | |
| // Redirect URL comes from the modality-specific completion code in | |
| // .env (or null if unset → stay on the in-app thank-you screen). | |
| // `rejected` participants (failed screening) get no redirect. | |
| const redirectURL = rejected ? null : completionURLFor(modality); | |
| return ( | |
| <ThankYouScreen | |
| message={ | |
| rejected | |
| ? "Thank you for your interest!" | |
| : "Thank you for completing the survey!" | |
| } | |
| redirectURL={redirectURL} | |
| /> | |
| ); | |
| } | |
| return <p>Unknown state</p>; | |
| } | |
| export default App; |