Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions client/src/utils/apolloClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client';

const client = new ApolloClient({
link: new HttpLink({
uri: import.meta.env.VITE_API_URL
? `${import.meta.env.VITE_API_URL}/graphql`
: '/graphql',
credentials: 'include', // 🔐 Send cookies with each request
headers: {
'Authorization': `Bearer ${localStorage.getItem('token') || ''}`,
}
}),
cache: new InMemoryCache(),
});

export default client;
52 changes: 52 additions & 0 deletions client/src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { jwtDecode } from 'jwt-decode';

interface UserToken {
name?: string;
exp: number;
_id?: string;
email?: string;
username?: string;
}

// AuthService class to manage login/logout/token logic
class AuthService {
// Get user data from token
getProfile() {
return jwtDecode(this.getToken() || '');
}

// Check if user is logged in
loggedIn() {
const token = this.getToken();
return !!token && !this.isTokenExpired(token);
}

// Check if token is expired
isTokenExpired(token: string) {
try {
const decoded = jwtDecode<UserToken>(token);
return decoded.exp < Date.now() / 1000;
} catch (err) {
return true;
}
}

// Get token from localStorage
getToken() {
return localStorage.getItem('id_token');
}

// Login - save token to localStorage
login(idToken: string) {
localStorage.setItem('id_token', idToken);
window.location.assign('/');
}

// Logout - remove token
logout() {
localStorage.removeItem('id_token');
window.location.assign('/');
}
}

export default new AuthService();
34 changes: 34 additions & 0 deletions client/src/utils/handleAnswer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@


// Picks a random Danism and matching Sound depending on correct/wrong
export function getRandomDanismAndSound(isCorrect: boolean): { text: string, sound: string } {
const correctDanisms = [
{ text: "🔥 Wow. You actually got that right. Weird.", sound: "/audio/Dan_correct/Dan-correct-1.wav" },
{ text: "🎯 Well look who remembered a concept. Congrats.", sound: "/audio/Dan_correct/Dan-correct-2.wav" },
{ text: "🚀 Miracles *do* happen. Mark the calendar.", sound: "/audio/Dan_correct/Dan-correct-3.wav" },
{ text: "🌟 I guess I *have* to give you a star now", sound: "/audio/Dan_correct/Dan-correctStar.wav" },
];

const incorrectDanisms = [
{ text: "💀 Sure, that's *a* choice. It's just not the right one.", sound: "/audio/Dan_incorrect/Dan-incorrect-1.wav" },
{ text: "😬 Oof. That was bold. Boldly incorrect.", sound: "/audio/Dan_incorrect/Dan-incorrect-2.wav" },
{ text: "🧠 Take a 7-minute break to recover from that disaster.", sound: "/audio/Dan_incorrect/Dan-incorrect-3.wav" },
{ text: "🛑 Fascinating. Bold. Still wrong.", sound: "/audio/Dan_incorrect/Dan-incorrect-4.wav" },
];

const source = isCorrect ? correctDanisms : incorrectDanisms;
const randomIndex = Math.floor(Math.random() * source.length);
return source[randomIndex];
}

// Picks a random Special Legendary Danism (only text)
export function getSpecialDanism(): string {
const specialQuotes = [
"🌟 Legendary Streak! Even Dr. Dan is impressed... briefly.",
"🌟 5 correct in a row?! You must be an AI in disguise.",
"🌟 Victory streak! Maybe you deserve a 7-minute break after all."
// 👉 Add more special quotes if you want!
];
const randomIndex = Math.floor(Math.random() * specialQuotes.length);
return specialQuotes[randomIndex];
}
7 changes: 7 additions & 0 deletions client/src/utils/preloadSounds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function preloadSounds(paths: string[]): void {
paths.forEach((path) => {
const audio = new Audio(path);
audio.load();
});
}

13 changes: 13 additions & 0 deletions client/src/utils/useBodyClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// src/Utils/useBodyClass.ts
import { useEffect } from 'react';

export const useBodyClass = (className: string) => {
useEffect(() => {
console.log(`✅applying body class: ${className}`);

document.body.classList.add(className);
return () => {
document.body.classList.remove(className);
};
}, [className]);
};
4 changes: 4 additions & 0 deletions client/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export function cn(...inputs: (string | undefined | null | false)[]): string {
return inputs.filter(Boolean).join(" ");
}