2 Commits

25 changed files with 596 additions and 838 deletions

View File

@@ -19,15 +19,10 @@ services:
restart: unless-stopped restart: unless-stopped
ports: ports:
- "3000:3000" - "3000:3000"
env_file:
- ./nest-backend/.env
environment: environment:
DATABASE_URL: postgresql://nest_user:nest_password@db:5432/nest_db DATABASE_URL: postgresql://nest_user:nest_password@db:5432/nest_db
JWT_SECRET: ${JWT_SECRET:-change_me_in_production}
PORT: 3000
ADMIN_USERNAME: ${ADMIN_USERNAME:-admin}
ADMIN_EMAIL: ${ADMIN_EMAIL:-admin@crowmate.fr}
ADMIN_PASSWORD: ${ADMIN_PASSWORD:-change_me}
FRONT_ORIGIN: ${FRONT_ORIGIN:-http://localhost:5173}
INTRA_ORIGIN: ${INTRA_ORIGIN:-http://localhost:5174}
depends_on: depends_on:
db: db:
condition: service_healthy condition: service_healthy
@@ -38,7 +33,9 @@ services:
build: ./nest-front build: ./nest-front
restart: unless-stopped restart: unless-stopped
ports: ports:
- "80:80" - "5173:5173"
environment:
API_URL: http://api:3000
depends_on: depends_on:
- api - api

View File

@@ -10,97 +10,6 @@ import teamRouter from './routes/team.js';
const app = express(); const app = express();
// ── Logger ─────────────────────────────────────────────────────────────────────
const R = '\x1b[0m';
const BOLD = '\x1b[1m';
const DIM = '\x1b[2m';
const RED = '\x1b[31m';
const GREEN = '\x1b[32m';
const YELLOW = '\x1b[33m';
const BLUE = '\x1b[34m';
const MAGENTA = '\x1b[35m';
const CYAN = '\x1b[36m';
const WHITE = '\x1b[37m';
const BG_RED = '\x1b[41m';
const BG_GREEN = '\x1b[42m';
const BG_YELLOW = '\x1b[43m';
const BG_BLUE = '\x1b[44m';
const BG_MAGENTA = '\x1b[45m';
const BG_CYAN = '\x1b[46m';
const METHOD_STYLE: Record<string, string> = {
GET: `${BG_BLUE}${WHITE}${BOLD}`,
POST: `${BG_GREEN}${WHITE}${BOLD}`,
PUT: `${BG_YELLOW}${WHITE}${BOLD}`,
PATCH: `${BG_MAGENTA}${WHITE}${BOLD}`,
DELETE: `${BG_RED}${WHITE}${BOLD}`,
};
function methodBadge(method: string): string {
const style = METHOD_STYLE[method] ?? `${BG_CYAN}${WHITE}${BOLD}`;
return `${style} ${method.padEnd(6)} ${R}`;
}
function statusBadge(code: number): string {
if (code < 300) return `${BG_GREEN}${WHITE}${BOLD} ${code} ${R}`;
if (code < 400) return `${BG_YELLOW}${WHITE}${BOLD} ${code} ${R}`;
return `${BG_RED}${WHITE}${BOLD} ${code} ${R}`;
}
function prettyJson(value: unknown): string {
return JSON.stringify(value, (k, v) => k === 'password' ? '***' : v, 2)
.split('\n')
.map((line, i) => i === 0 ? line : ` ${DIM} ${line}${R}`)
.join('\n');
}
const SEP = `${DIM}${'─'.repeat(60)}${R}`;
app.use((req, res, next) => {
const start = Date.now();
const ts = new Date().toISOString().replace('T', ' ').slice(0, 23);
// Skip health check noise
if (req.originalUrl === '/api/health') { next(); return; }
const originalJson = res.json.bind(res);
let resBody: unknown;
res.json = (body) => { resBody = body; return originalJson(body); };
res.on('finish', () => {
const ms = Date.now() - start;
const userId = req.user?.userId ? `${CYAN}${req.user.userId.slice(0, 8)}${R}` : `${DIM}anon${R}`;
const role = req.user?.role ? `${MAGENTA}${req.user.role}${R}` : `${DIM}-${R}`;
const hasBody = ['POST', 'PUT', 'PATCH'].includes(req.method)
&& req.body && Object.keys(req.body).length > 0;
const lines: string[] = [
SEP,
`${DIM}${ts}${R} ${methodBadge(req.method)} ${BOLD}${req.originalUrl}${R}`,
` ${DIM}┌ user ${R} ${userId} ${DIM}role:${R} ${role}`,
` ${DIM}└ status ${R} ${statusBadge(res.statusCode)} ${DIM}${ms}ms${R}`,
];
if (hasBody) {
lines.push(` ${GREEN}↑ REQUEST BODY${R}`);
lines.push(` ${DIM} ${prettyJson(req.body)}${R}`);
}
if (res.statusCode >= 400 && resBody) {
lines.push(` ${RED}↓ ERROR RESPONSE${R}`);
lines.push(` ${DIM} ${prettyJson(resBody)}${R}`);
} else if (res.statusCode < 300 && resBody && req.method !== 'GET') {
lines.push(` ${GREEN}↓ RESPONSE BODY${R}`);
lines.push(` ${DIM} ${prettyJson(resBody)}${R}`);
}
console.log(lines.join('\n'));
});
next();
});
app.use(cors({ app.use(cors({
origin: [ origin: [
'http://localhost:5173', // nest-front dev 'http://localhost:5173', // nest-front dev
@@ -128,7 +37,7 @@ app.use((_req, res) => res.status(404).json({ error: 'Not found' }));
// Global error handler // Global error handler
app.use((err: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => { app.use((err: Error, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
console.error(`${BG_RED}${WHITE}${BOLD} UNHANDLED ERROR ${R}`, err); console.error(err);
res.status(500).json({ error: 'Internal server error' }); res.status(500).json({ error: 'Internal server error' });
}); });

View File

@@ -1,11 +1,5 @@
import { PrismaClient } from '@prisma/client'; import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient({ const prisma = new PrismaClient();
log: [
{ emit: 'stdout', level: 'query' },
{ emit: 'stdout', level: 'warn' },
{ emit: 'stdout', level: 'error' },
],
});
export default prisma; export default prisma;

View File

@@ -1,4 +1,4 @@
FROM node:22-alpine AS build FROM node:25-alpine AS build
WORKDIR /app WORKDIR /app
@@ -17,8 +17,14 @@ RUN npm run build
FROM nginx:alpine FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html COPY --from=build /app/dist /usr/share/nginx/html
COPY nginx.conf /etc/nginx/conf.d/default.conf COPY nginx.conf /etc/nginx/nginx.conf.template
EXPOSE 80 EXPOSE 5173
CMD ["nginx", "-g", "daemon off;"] # API_URL is the backend's public base URL used by nginx proxy_pass.
# Set this at runtime in Coolify, e.g. API_URL=https://api.crowmate.fr
ENV API_URL=http://localhost:3000
# Substitute ${API_URL} in the nginx template at container start, then launch nginx.
# The quoted variable list prevents envsubst from replacing nginx variables like $host.
CMD ["/bin/sh", "-c", "envsubst '${API_URL}' < /etc/nginx/nginx.conf.template > /etc/nginx/conf.d/default.conf && nginx -g 'daemon off;'"]

View File

@@ -2,5 +2,5 @@ services:
nest-front: nest-front:
build: . build: .
ports: ports:
- "80:80" - "5173:5173"
restart: unless-stopped restart: unless-stopped

View File

@@ -1,14 +1,10 @@
server { server {
listen 80; listen 5173;
root /usr/share/nginx/html; root /usr/share/nginx/html;
index index.html; index index.html;
# Use Docker's embedded DNS resolver; defer resolution to request time
resolver 127.0.0.11 valid=30s;
location /api/ { location /api/ {
set $api_upstream http://api:3000; proxy_pass ${API_URL}/api/;
proxy_pass $api_upstream;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

View File

@@ -1,7 +1,8 @@
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import type { UserRole } from '../../types';
/** /**
* Developer-only overlay to quickly log in as test accounts. * Developer-only overlay to quickly switch user roles for testing.
* Only visible in development mode. * Only visible in development mode.
*/ */
export function DevRoleSwitcher() { export function DevRoleSwitcher() {
@@ -11,8 +12,9 @@ export function DevRoleSwitcher() {
} }
function DevRoleSwitcherInner() { function DevRoleSwitcherInner() {
const { user, isAuthenticated, login, logout } = useAuth(); const { user, isAuthenticated, devSetRole, login, logout } = useAuth();
const ROLES: UserRole[] = ['user', 'dev', 'com'];
const DEV_ACCOUNTS = [ const DEV_ACCOUNTS = [
{ label: 'Dev/Admin (Kestrel)', email: 'kestrel@crowmate.dev' }, { label: 'Dev/Admin (Kestrel)', email: 'kestrel@crowmate.dev' },
{ label: 'Com Staff (Vesper)', email: 'vesper@crowmate.dev' }, { label: 'Com Staff (Vesper)', email: 'vesper@crowmate.dev' },
@@ -46,10 +48,30 @@ function DevRoleSwitcherInner() {
<div style={{ color: 'var(--color-text-muted)', marginBottom: '0.4rem' }}> <div style={{ color: 'var(--color-text-muted)', marginBottom: '0.4rem' }}>
Logged as: <strong style={{ color: 'var(--color-text)' }}>{user?.username}</strong> Logged as: <strong style={{ color: 'var(--color-text)' }}>{user?.username}</strong>
</div> </div>
<div style={{ color: 'var(--color-text-muted)', marginBottom: '0.75rem' }}> <div style={{ color: 'var(--color-text-muted)', marginBottom: '0.5rem' }}>
Role: <strong style={{ color: 'var(--color-green)' }}>{user?.role}</strong> Role: <strong style={{ color: 'var(--color-green)' }}>{user?.role}</strong>
</div> </div>
<div style={{ display: 'flex', gap: '0.3rem', marginBottom: '0.5rem', flexWrap: 'wrap' }}>
{ROLES.map((r) => (
<button
key={r}
onClick={() => devSetRole(r)}
style={{
background: user?.role === r ? 'var(--color-amber)' : 'transparent',
border: '1px solid var(--color-amber)',
color: user?.role === r ? '#000' : 'var(--color-amber)',
padding: '0.1rem 0.4rem',
cursor: 'pointer',
fontFamily: 'var(--font-mono)',
fontSize: '0.65rem',
}}
>
{r}
</button>
))}
</div>
<button <button
onClick={logout} onClick={logout}
style={{ style={{

View File

@@ -2,12 +2,11 @@ import React, {
createContext, createContext,
useCallback, useCallback,
useContext, useContext,
useEffect,
useMemo, useMemo,
useState, useState,
} from 'react'; } from 'react';
import type { User } from '../types'; import type { User, UserRole } from '../types';
import { authApi, usersApi, getToken, setToken, clearToken } from '../utils/api'; import { MOCK_USERS } from '../data/mockData';
// ── Types ────────────────────────────────────────────────────────────────────── // ── Types ──────────────────────────────────────────────────────────────────────
@@ -16,11 +15,12 @@ interface AuthContextValue {
isAuthenticated: boolean; isAuthenticated: boolean;
isStaff: boolean; isStaff: boolean;
isAdmin: boolean; isAdmin: boolean;
isLoading: boolean;
login: (email: string, password: string) => Promise<{ success: boolean; error?: string }>; login: (email: string, password: string) => Promise<{ success: boolean; error?: string }>;
register: (username: string, email: string, password: string) => Promise<{ success: boolean; error?: string }>; register: (username: string, email: string, password: string) => Promise<{ success: boolean; error?: string }>;
logout: () => void; logout: () => void;
updateUsername: (username: string) => Promise<{ success: boolean; error?: string }>; updateUsername: (username: string) => void;
// Dev helper: quickly switch role for testing
devSetRole: (role: UserRole) => void;
} }
// ── Context ──────────────────────────────────────────────────────────────────── // ── Context ────────────────────────────────────────────────────────────────────
@@ -31,6 +31,16 @@ const AuthContext = createContext<AuthContextValue | null>(null);
const STORAGE_KEY = 'crowmate_auth_user'; const STORAGE_KEY = 'crowmate_auth_user';
function loadUserFromStorage(): User | null {
try {
const raw = localStorage.getItem(STORAGE_KEY);
if (!raw) return null;
return JSON.parse(raw) as User;
} catch {
return null;
}
}
function saveUserToStorage(user: User | null): void { function saveUserToStorage(user: User | null): void {
if (user) { if (user) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(user)); localStorage.setItem(STORAGE_KEY, JSON.stringify(user));
@@ -40,89 +50,96 @@ function saveUserToStorage(user: User | null): void {
} }
export function AuthProvider({ children }: { children: React.ReactNode }) { export function AuthProvider({ children }: { children: React.ReactNode }) {
const [user, setUser] = useState<User | null>(null); const [user, setUser] = useState<User | null>(loadUserFromStorage);
const [isLoading, setIsLoading] = useState(true);
// On mount: validate stored token and restore session
useEffect(() => {
const token = getToken();
if (!token) {
setIsLoading(false);
return;
}
authApi.me()
.then((freshUser) => {
setUser(freshUser);
saveUserToStorage(freshUser);
})
.catch(() => {
clearToken();
saveUserToStorage(null);
})
.finally(() => setIsLoading(false));
}, []);
const isAuthenticated = user !== null; const isAuthenticated = user !== null;
const isStaff = user?.role === 'dev' || user?.role === 'com'; const isStaff = user?.role === 'dev' || user?.role === 'com';
const isAdmin = user?.isAdmin === true; const isAdmin = user?.isAdmin === true;
const login = useCallback( const login = useCallback(
async (email: string, password: string): Promise<{ success: boolean; error?: string }> => { async (email: string, _password: string): Promise<{ success: boolean; error?: string }> => {
try { // Simulate network delay
const { token, user: loggedInUser } = await authApi.login(email, password); await new Promise((r) => setTimeout(r, 400));
setToken(token);
setUser(loggedInUser); const found = MOCK_USERS.find(
saveUserToStorage(loggedInUser); (u) => u.email.toLowerCase() === email.toLowerCase()
return { success: true }; );
} catch (err) {
const message = err instanceof Error ? err.message : 'Login failed.'; if (!found) {
return { success: false, error: message }; return { success: false, error: 'No account found with that email address.' };
} }
if (found.isBanned) {
return { success: false, error: 'This account has been suspended.' };
}
setUser(found);
saveUserToStorage(found);
return { success: true };
}, },
[] []
); );
const register = useCallback( const register = useCallback(
async (username: string, email: string, password: string): Promise<{ success: boolean; error?: string }> => { async (username: string, email: string, _password: string): Promise<{ success: boolean; error?: string }> => {
try { await new Promise((r) => setTimeout(r, 500));
const { token, user: newUser } = await authApi.register(username, email, password);
setToken(token); const emailTaken = MOCK_USERS.some(
setUser(newUser); (u) => u.email.toLowerCase() === email.toLowerCase()
saveUserToStorage(newUser); );
return { success: true }; if (emailTaken) {
} catch (err) { return { success: false, error: 'An account with this email already exists.' };
const message = err instanceof Error ? err.message : 'Registration failed.';
return { success: false, error: message };
} }
const usernameTaken = MOCK_USERS.some(
(u) => u.username.toLowerCase() === username.toLowerCase()
);
if (usernameTaken) {
return { success: false, error: 'This username is already taken.' };
}
const newUser: User = {
id: `u${Date.now()}`,
username,
email,
role: 'user',
isAdmin: false,
isBanned: false,
createdAt: new Date().toISOString(),
};
setUser(newUser);
saveUserToStorage(newUser);
return { success: true };
}, },
[] []
); );
const logout = useCallback(() => { const logout = useCallback(() => {
clearToken();
setUser(null); setUser(null);
saveUserToStorage(null); saveUserToStorage(null);
}, []); }, []);
const updateUsername = useCallback( const updateUsername = useCallback((username: string) => {
async (username: string): Promise<{ success: boolean; error?: string }> => { setUser((prev) => {
try { if (!prev) return prev;
const updatedUser = await usersApi.updateUsername(username); const updated = { ...prev, username };
setUser(updatedUser); saveUserToStorage(updated);
saveUserToStorage(updatedUser); return updated;
return { success: true }; });
} catch (err) { }, []);
const message = err instanceof Error ? err.message : 'Failed to update username.';
return { success: false, error: message }; const devSetRole = useCallback((role: UserRole) => {
} setUser((prev) => {
}, if (!prev) return prev;
[] const updated = { ...prev, role, isAdmin: role === 'dev' };
); saveUserToStorage(updated);
return updated;
});
}, []);
const value = useMemo<AuthContextValue>( const value = useMemo<AuthContextValue>(
() => ({ user, isAuthenticated, isStaff, isAdmin, isLoading, login, register, logout, updateUsername }), () => ({ user, isAuthenticated, isStaff, isAdmin, login, register, logout, updateUsername, devSetRole }),
[user, isAuthenticated, isStaff, isAdmin, isLoading, login, register, logout, updateUsername] [user, isAuthenticated, isStaff, isAdmin, login, register, logout, updateUsername, devSetRole]
); );
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>; return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>;

View File

@@ -1,29 +1,17 @@
import { useState, useCallback, useEffect } from 'react'; import { useState, useCallback } from 'react';
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { bugsApi, forumApi, usersApi } from '../../utils/api'; import { MOCK_THREADS, MOCK_BUGS } from '../../data/mockData';
import { formatDate } from '../../utils/format'; import { formatDate } from '../../utils/format';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import type { BugReport, ForumThread } from '../../types';
type Tab = 'profile' | 'threads' | 'bugs' | 'password'; type Tab = 'profile' | 'threads' | 'bugs' | 'password';
export default function AccountPage() { export default function AccountPage() {
const { user, updateUsername } = useAuth(); const { user, updateUsername } = useAuth();
const [activeTab, setActiveTab] = useState<Tab>('profile'); const [activeTab, setActiveTab] = useState<Tab>('profile');
const [userThreads, setUserThreads] = useState<ForumThread[]>([]);
const [userBugs, setUserBugs] = useState<BugReport[]>([]);
useEffect(() => { const userThreads = MOCK_THREADS.filter((t) => t.authorId === user?.id);
if (!user) return; const userBugs = MOCK_BUGS.filter((b) => b.submittedById === user?.id);
forumApi.getThreads({ limit: 200 })
.then((res) => setUserThreads(res.data.filter((t) => t.authorId === user.id)))
.catch(() => setUserThreads([]));
bugsApi.getBugs({ limit: 200 })
.then((res) => setUserBugs(res.data.filter((b) => b.submittedById === user.id)))
.catch(() => setUserBugs([]));
}, [user]);
const tabs: { id: Tab; label: string }[] = [ const tabs: { id: Tab; label: string }[] = [
{ id: 'profile', label: 'Profile' }, { id: 'profile', label: 'Profile' },
@@ -133,23 +121,19 @@ export default function AccountPage() {
// ── Profile Tab ──────────────────────────────────────────────────────────────── // ── Profile Tab ────────────────────────────────────────────────────────────────
function ProfileTab({ user, updateUsername }: { user: NonNullable<ReturnType<typeof useAuth>['user']>; updateUsername: (u: string) => Promise<{ success: boolean; error?: string }> }) { function ProfileTab({ user, updateUsername }: { user: NonNullable<ReturnType<typeof useAuth>['user']>; updateUsername: (u: string) => void }) {
const [editing, setEditing] = useState(false); const [editing, setEditing] = useState(false);
const [username, setUsername] = useState(user.username); const [username, setUsername] = useState(user.username);
const [error, setError] = useState(''); const [error, setError] = useState('');
const [saved, setSaved] = useState(false); const [saved, setSaved] = useState(false);
const handleSave = useCallback(async () => { const handleSave = useCallback(() => {
if (!username.trim()) { setError('Username cannot be empty.'); return; } if (!username.trim()) { setError('Username cannot be empty.'); return; }
if (username.length < 3) { setError('Must be at least 3 characters.'); return; } if (username.length < 3) { setError('Must be at least 3 characters.'); return; }
const result = await updateUsername(username.trim()); updateUsername(username.trim());
if (result.success) { setEditing(false);
setEditing(false); setSaved(true);
setSaved(true); setTimeout(() => setSaved(false), 3000);
setTimeout(() => setSaved(false), 3000);
} else {
setError(result.error ?? 'Failed to update username.');
}
}, [username, updateUsername]); }, [username, updateUsername]);
return ( return (
@@ -226,15 +210,10 @@ function ChangePasswordForm() {
if (Object.keys(next).length > 0) return; if (Object.keys(next).length > 0) return;
setLoading(true); setLoading(true);
try { await new Promise((r) => setTimeout(r, 400));
await usersApi.changePassword(form.current, form.next); setLoading(false);
setForm({ current: '', next: '', confirm: '' }); setForm({ current: '', next: '', confirm: '' });
setErrors({ success: 'Password changed successfully.' }); setErrors({ success: 'Password changed successfully.' });
} catch (err) {
setErrors({ current: err instanceof Error ? err.message : 'Failed to change password.' });
} finally {
setLoading(false);
}
}, [form]); }, [form]);
return ( return (

View File

@@ -1,6 +1,6 @@
import { useState, useCallback, useEffect, useMemo } from 'react'; import { useState, useCallback, useMemo } from 'react';
import { Link, Navigate, useParams } from 'react-router-dom'; import { Link, Navigate, useParams } from 'react-router-dom';
import { bugsApi, ApiError } from '../../utils/api'; import { MOCK_BUGS, MOCK_BUG_COMMENTS } from '../../data/mockData';
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { formatDate, formatDateTime } from '../../utils/format'; import { formatDate, formatDateTime } from '../../utils/format';
import type { BugComment, BugReport, BugSeverity, BugStatus } from '../../types'; import type { BugComment, BugReport, BugSeverity, BugStatus } from '../../types';
@@ -57,36 +57,21 @@ export default function BugDetailPage() {
const { id } = useParams<{ id: string }>(); const { id } = useParams<{ id: string }>();
const { user, isAuthenticated } = useAuth(); const { user, isAuthenticated } = useAuth();
const [bug, setBug] = useState<BugReport | null>(null); // Local state — mirrors the global bug list in memory
const [comments, setComments] = useState<BugComment[]>([]); const [bugs, setBugs] = useState<BugReport[]>(MOCK_BUGS);
const [loading, setLoading] = useState(true); const [comments, setComments] = useState<BugComment[]>(MOCK_BUG_COMMENTS);
const [notFound, setNotFound] = useState(false);
const [newComment, setNewComment] = useState(''); const [newComment, setNewComment] = useState('');
const [commentError, setCommentError] = useState(''); const [commentError, setCommentError] = useState('');
const [submitting, setSubmitting] = useState(false); const [submitting, setSubmitting] = useState(false);
useEffect(() => { const bug = useMemo(() => bugs.find((b) => b.id === id), [bugs, id]);
if (!id) return;
let cancelled = false;
setLoading(true);
bugsApi.getBug(id) const bugComments = useMemo(
.then((data) => { () => comments.filter((c) => c.bugReportId === id).sort((a, b) => a.createdAt.localeCompare(b.createdAt)),
if (cancelled) return; [comments, id]
const { comments: bugComments, ...bugData } = data; );
setBug(bugData);
setComments(bugComments.sort((a, b) => a.createdAt.localeCompare(b.createdAt)));
})
.catch((err) => {
if (cancelled) return;
if (err instanceof ApiError && err.status === 404) setNotFound(true);
})
.finally(() => { if (!cancelled) setLoading(false); });
return () => { cancelled = true; };
}, [id]);
// "I have this too" logic
const alreadyVoted = useMemo( const alreadyVoted = useMemo(
() => !!user && !!bug && bug.meTooBugs.includes(user.id), () => !!user && !!bug && bug.meTooBugs.includes(user.id),
[user, bug] [user, bug]
@@ -96,44 +81,38 @@ export default function BugDetailPage() {
[user, bug] [user, bug]
); );
const handleMeToo = useCallback(async () => { const handleMeToo = useCallback(() => {
if (!user || !bug || alreadyVoted || isOwnReport) return; if (!user || !bug || alreadyVoted || isOwnReport) return;
try { setBugs((prev) =>
await bugsApi.toggleMeToo(bug.id); prev.map((b) =>
setBug((prev) => prev ? { ...prev, meTooBugs: [...prev.meTooBugs, user.id] } : prev); b.id === bug.id ? { ...b, meTooBugs: [...b.meTooBugs, user.id] } : b
} catch { )
// silently ignore );
}
}, [user, bug, alreadyVoted, isOwnReport]); }, [user, bug, alreadyVoted, isOwnReport]);
const handleComment = useCallback(async () => { const handleComment = useCallback(async () => {
if (!user || !bug) return; if (!user) return;
if (!newComment.trim()) { setCommentError('Comment cannot be empty.'); return; } if (!newComment.trim()) { setCommentError('Comment cannot be empty.'); return; }
if (newComment.trim().length < 5) { setCommentError('Comment must be at least 5 characters.'); return; } if (newComment.trim().length < 5) { setCommentError('Comment must be at least 5 characters.'); return; }
setCommentError(''); setCommentError('');
setSubmitting(true); setSubmitting(true);
await new Promise((r) => setTimeout(r, 250));
try { const comment: BugComment = {
const comment = await bugsApi.addComment(bug.id, newComment.trim()); id: `bc${Date.now()}`,
setComments((prev) => [...prev, comment]); bugReportId: id!,
setNewComment(''); authorId: user.id,
} catch (err) { authorName: user.username,
setCommentError(err instanceof Error ? err.message : 'Failed to post comment.'); content: newComment.trim(),
} finally { createdAt: new Date().toISOString(),
setSubmitting(false); };
} setComments((prev) => [...prev, comment]);
}, [user, bug, newComment]); setNewComment('');
setSubmitting(false);
}, [user, newComment, id]);
if (loading) { if (!bug) {
return (
<div style={{ maxWidth: '860px', margin: '0 auto', padding: '3rem 1.5rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}>
Loading...
</div>
);
}
if (notFound || !bug) {
return <Navigate to="/bugs" replace />; return <Navigate to="/bugs" replace />;
} }
@@ -150,6 +129,7 @@ export default function BugDetailPage() {
{/* Header */} {/* Header */}
<div className="crt-box" style={{ padding: '1.75rem', marginBottom: '1rem' }}> <div className="crt-box" style={{ padding: '1.75rem', marginBottom: '1rem' }}>
{/* Badges */}
<div style={{ display: 'flex', gap: '0.4rem', flexWrap: 'wrap', alignItems: 'center', marginBottom: '0.75rem' }}> <div style={{ display: 'flex', gap: '0.4rem', flexWrap: 'wrap', alignItems: 'center', marginBottom: '0.75rem' }}>
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.72rem' }}> <span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.72rem' }}>
{bug.uniqueCode} {bug.uniqueCode}
@@ -158,6 +138,7 @@ export default function BugDetailPage() {
<SeverityBadge severity={bug.severity} /> <SeverityBadge severity={bug.severity} />
</div> </div>
{/* Title */}
<h1 <h1
style={{ style={{
fontFamily: 'var(--font-heading)', fontFamily: 'var(--font-heading)',
@@ -246,11 +227,13 @@ export default function BugDetailPage() {
borderRadius: '6px', borderRadius: '6px',
}} }}
> >
{/* Count */}
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-green)', fontSize: '0.82rem' }}> <div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-green)', fontSize: '0.82rem' }}>
<span style={{ fontSize: '1.1rem' }}>{metooCount}</span>{' '} <span style={{ fontSize: '1.1rem' }}>{metooCount}</span>{' '}
{metooCount === 1 ? 'user has' : 'users have'} this issue {metooCount === 1 ? 'user has' : 'users have'} this issue
</div> </div>
{/* Button logic */}
{!isAuthenticated ? ( {!isAuthenticated ? (
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.78rem' }}> <div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.78rem' }}>
<Link to="/login">Login</Link> to confirm you have this issue <Link to="/login">Login</Link> to confirm you have this issue
@@ -302,20 +285,22 @@ export default function BugDetailPage() {
Discussion Discussion
</span> </span>
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.65rem', border: '1px solid var(--color-border)', padding: '0.05rem 0.4rem' }}> <span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.65rem', border: '1px solid var(--color-border)', padding: '0.05rem 0.4rem' }}>
{comments.length} {bugComments.length}
</span> </span>
</div> </div>
{comments.length === 0 ? ( {/* Comment list */}
{bugComments.length === 0 ? (
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.8rem', padding: '1rem 0' }}> <div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.8rem', padding: '1rem 0' }}>
No comments yet. Be the first to comment. No comments yet. Be the first to comment.
</div> </div>
) : ( ) : (
comments.map((comment) => ( bugComments.map((comment) => (
<CommentItem key={comment.id} comment={comment} /> <CommentItem key={comment.id} comment={comment} />
)) ))
)} )}
{/* Add comment */}
<div style={{ marginTop: '1.25rem' }}> <div style={{ marginTop: '1.25rem' }}>
{isAuthenticated ? ( {isAuthenticated ? (
<div className="crt-box" style={{ padding: '1.25rem' }}> <div className="crt-box" style={{ padding: '1.25rem' }}>

View File

@@ -1,6 +1,6 @@
import { useState, useMemo, useCallback, useEffect } from 'react'; import { useState, useMemo, useCallback } from 'react';
import { Link, useNavigate } from 'react-router-dom'; import { Link, useNavigate } from 'react-router-dom';
import { bugsApi } from '../../utils/api'; import { MOCK_BUGS } from '../../data/mockData';
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { timeAgo } from '../../utils/format'; import { timeAgo } from '../../utils/format';
import type { BugReport, BugSeverity, BugStatus, BugReportFormData } from '../../types'; import type { BugReport, BugSeverity, BugStatus, BugReportFormData } from '../../types';
@@ -66,6 +66,7 @@ function BugCard({ bug, highlight }: BugCardProps) {
</span> </span>
<StatusBadge status={bug.status} /> <StatusBadge status={bug.status} />
<SeverityBadge severity={bug.severity} /> <SeverityBadge severity={bug.severity} />
{/* MeToo count */}
<span <span
style={{ style={{
fontFamily: 'var(--font-mono)', fontFamily: 'var(--font-mono)',
@@ -117,7 +118,7 @@ function BugCard({ bug, highlight }: BugCardProps) {
const GAME_VERSIONS = ['0.9.3-alpha', '0.9.2-alpha', '0.9.1-alpha']; const GAME_VERSIONS = ['0.9.3-alpha', '0.9.2-alpha', '0.9.1-alpha'];
const SEVERITIES: BugSeverity[] = ['low', 'medium', 'high', 'critical']; const SEVERITIES: BugSeverity[] = ['low', 'medium', 'high', 'critical'];
function SubmitBugForm({ onSubmit }: { onSubmit: (data: BugReportFormData) => Promise<void> }) { function SubmitBugForm({ onSubmit }: { onSubmit: (data: BugReportFormData) => void }) {
const [form, setForm] = useState<BugReportFormData>({ const [form, setForm] = useState<BugReportFormData>({
title: '', title: '',
description: '', description: '',
@@ -127,8 +128,6 @@ function SubmitBugForm({ onSubmit }: { onSubmit: (data: BugReportFormData) => Pr
}); });
const [errors, setErrors] = useState<Partial<Record<keyof BugReportFormData, string>>>({}); const [errors, setErrors] = useState<Partial<Record<keyof BugReportFormData, string>>>({});
const [submitted, setSubmitted] = useState(false); const [submitted, setSubmitted] = useState(false);
const [submitting, setSubmitting] = useState(false);
const [submitError, setSubmitError] = useState('');
const set = useCallback(<K extends keyof BugReportFormData>(key: K, value: BugReportFormData[K]) => { const set = useCallback(<K extends keyof BugReportFormData>(key: K, value: BugReportFormData[K]) => {
setForm((prev) => ({ ...prev, [key]: value })); setForm((prev) => ({ ...prev, [key]: value }));
@@ -149,16 +148,9 @@ function SubmitBugForm({ onSubmit }: { onSubmit: (data: BugReportFormData) => Pr
const handleSubmit = useCallback(async (e: React.FormEvent) => { const handleSubmit = useCallback(async (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
if (!validate()) return; if (!validate()) return;
setSubmitting(true); await new Promise((r) => setTimeout(r, 400));
setSubmitError(''); onSubmit(form);
try { setSubmitted(true);
await onSubmit(form);
setSubmitted(true);
} catch (err) {
setSubmitError(err instanceof Error ? err.message : 'Failed to submit report.');
} finally {
setSubmitting(false);
}
}, [form, onSubmit]); }, [form, onSubmit]);
const labelStyle: React.CSSProperties = { const labelStyle: React.CSSProperties = {
@@ -194,12 +186,6 @@ function SubmitBugForm({ onSubmit }: { onSubmit: (data: BugReportFormData) => Pr
&#9654; Submit a Bug Report &#9654; Submit a Bug Report
</div> </div>
{submitError && (
<div style={{ color: 'var(--color-red)', fontFamily: 'var(--font-mono)', fontSize: '0.75rem', marginBottom: '1rem' }}>
[ERROR] {submitError}
</div>
)}
<div style={{ marginBottom: '0.85rem' }}> <div style={{ marginBottom: '0.85rem' }}>
<label style={labelStyle}>Title *</label> <label style={labelStyle}>Title *</label>
<input <input
@@ -264,8 +250,8 @@ function SubmitBugForm({ onSubmit }: { onSubmit: (data: BugReportFormData) => Pr
/> />
</div> </div>
<button type="submit" className="btn-terminal" disabled={submitting} style={{ opacity: submitting ? 0.6 : 1 }}> <button type="submit" className="btn-terminal">
{submitting ? 'Submitting...' : '&#9654; Submit Report'} &#9654; Submit Report
</button> </button>
</div> </div>
</form> </form>
@@ -276,37 +262,44 @@ function SubmitBugForm({ onSubmit }: { onSubmit: (data: BugReportFormData) => Pr
export default function BugReportPage() { export default function BugReportPage() {
const { user, isAuthenticated } = useAuth(); const { user, isAuthenticated } = useAuth();
const [bugs, setBugs] = useState<BugReport[]>([]); const [bugs, setBugs] = useState(MOCK_BUGS);
const [loading, setLoading] = useState(true);
const [statusFilter, setStatusFilter] = useState<BugStatus | 'all'>('all'); const [statusFilter, setStatusFilter] = useState<BugStatus | 'all'>('all');
const [severityFilter, setSeverityFilter] = useState<BugSeverity | 'all'>('all'); const [severityFilter, setSeverityFilter] = useState<BugSeverity | 'all'>('all');
const [showForm, setShowForm] = useState(false); const [showForm, setShowForm] = useState(false);
const fetchBugs = useCallback(() => { // Separate: user's own bugs and all others, both filtered
setLoading(true);
bugsApi.getBugs({ status: statusFilter, severity: severityFilter, limit: 100 })
.then((res) => setBugs(res.data))
.catch(() => setBugs([]))
.finally(() => setLoading(false));
}, [statusFilter, severityFilter]);
useEffect(() => { fetchBugs(); }, [fetchBugs]);
const { myBugs, otherBugs } = useMemo(() => { const { myBugs, otherBugs } = useMemo(() => {
const passes = (b: BugReport) => {
if (statusFilter !== 'all' && b.status !== statusFilter) return false;
if (severityFilter !== 'all' && b.severity !== severityFilter) return false;
return true;
};
const my: BugReport[] = []; const my: BugReport[] = [];
const other: BugReport[] = []; const other: BugReport[] = [];
bugs.forEach((b) => { bugs.forEach((b) => {
if (!passes(b)) return;
if (user && b.submittedById === user.id) my.push(b); if (user && b.submittedById === user.id) my.push(b);
else other.push(b); else other.push(b);
}); });
return { myBugs: my, otherBugs: other }; return { myBugs: my, otherBugs: other };
}, [bugs, user]); }, [bugs, statusFilter, severityFilter, user]);
const handleNewReport = useCallback(async (data: BugReportFormData) => { const handleNewReport = useCallback((data: BugReportFormData) => {
const newBug = await bugsApi.createBug(data); const newBug: BugReport = {
id: `bug${Date.now()}`,
uniqueCode: `HH-${String(bugs.length + 1).padStart(4, '0')}`,
...data,
status: 'open',
submittedById: user?.id ?? 'unknown',
submittedByName: user?.username ?? 'You',
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
notes: [],
meTooBugs: [],
};
setBugs((prev) => [newBug, ...prev]); setBugs((prev) => [newBug, ...prev]);
setShowForm(false); setShowForm(false);
}, []); }, [bugs.length, user]);
const openCount = bugs.filter((b) => b.status === 'open').length; const openCount = bugs.filter((b) => b.status === 'open').length;
const inProgressCount = bugs.filter((b) => b.status === 'in_progress').length; const inProgressCount = bugs.filter((b) => b.status === 'in_progress').length;
@@ -380,78 +373,68 @@ export default function BugReportPage() {
</select> </select>
</div> </div>
{loading && ( {/* "Your Reports" section — only for logged-in users with their own bugs */}
<div className="crt-box" style={{ padding: '2rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}> {isAuthenticated && myBugs.length > 0 && (
Loading reports... <section style={{ marginBottom: '2rem' }}>
</div> <div
style={{
display: 'flex',
alignItems: 'center',
gap: '0.75rem',
marginBottom: '0.75rem',
paddingBottom: '0.4rem',
borderBottom: '2px solid var(--color-yellow)',
}}
>
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-yellow)', fontSize: '0.72rem', letterSpacing: '0.15em', textTransform: 'uppercase' }}>
&#9654; Your Reports
</span>
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-yellow)', fontSize: '0.65rem', background: 'rgba(255,255,0,0.1)', border: '1px solid var(--color-yellow)', padding: '0.05rem 0.4rem' }}>
{myBugs.length}
</span>
</div>
{myBugs.map((bug) => (
<BugCard key={bug.id} bug={bug} highlight />
))}
</section>
)} )}
{!loading && ( {/* All other reports */}
<> <section>
{/* "Your Reports" section */} {isAuthenticated && myBugs.length > 0 && (
{isAuthenticated && myBugs.length > 0 && ( <div
<section style={{ marginBottom: '2rem' }}> style={{
<div display: 'flex',
style={{ alignItems: 'center',
display: 'flex', gap: '0.75rem',
alignItems: 'center', marginBottom: '0.75rem',
gap: '0.75rem', paddingBottom: '0.4rem',
marginBottom: '0.75rem', borderBottom: '2px solid var(--color-border)',
paddingBottom: '0.4rem', }}
borderBottom: '2px solid var(--color-yellow)', >
}} <span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.72rem', letterSpacing: '0.15em', textTransform: 'uppercase' }}>
> All Reports
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-yellow)', fontSize: '0.72rem', letterSpacing: '0.15em', textTransform: 'uppercase' }}> </span>
&#9654; Your Reports <span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.65rem', border: '1px solid var(--color-border)', padding: '0.05rem 0.4rem' }}>
</span> {otherBugs.length}
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-yellow)', fontSize: '0.65rem', background: 'rgba(255,255,0,0.1)', border: '1px solid var(--color-yellow)', padding: '0.05rem 0.4rem' }}> </span>
{myBugs.length} </div>
</span> )}
</div>
{myBugs.map((bug) => (
<BugCard key={bug.id} bug={bug} highlight />
))}
</section>
)}
{/* All other reports */} {otherBugs.length === 0 && myBugs.length === 0 ? (
<section> <div className="crt-box" style={{ padding: '2rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}>
{isAuthenticated && myBugs.length > 0 && ( No bug reports match the selected filters.
<div </div>
style={{ ) : otherBugs.length === 0 && isAuthenticated ? (
display: 'flex', <div className="crt-box" style={{ padding: '1.5rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)', fontSize: '0.8rem' }}>
alignItems: 'center', No other reports match the selected filters.
gap: '0.75rem', </div>
marginBottom: '0.75rem', ) : (
paddingBottom: '0.4rem', otherBugs.map((bug) => (
borderBottom: '2px solid var(--color-border)', <BugCard key={bug.id} bug={bug} />
}} ))
> )}
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.72rem', letterSpacing: '0.15em', textTransform: 'uppercase' }}> </section>
All Reports
</span>
<span style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.65rem', border: '1px solid var(--color-border)', padding: '0.05rem 0.4rem' }}>
{otherBugs.length}
</span>
</div>
)}
{otherBugs.length === 0 && myBugs.length === 0 ? (
<div className="crt-box" style={{ padding: '2rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}>
No bug reports match the selected filters.
</div>
) : otherBugs.length === 0 && isAuthenticated ? (
<div className="crt-box" style={{ padding: '1.5rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)', fontSize: '0.8rem' }}>
No other reports match the selected filters.
</div>
) : (
otherBugs.map((bug) => (
<BugCard key={bug.id} bug={bug} />
))
)}
</section>
</>
)}
</div> </div>
); );
} }

View File

@@ -1,5 +1,5 @@
import { useState, useCallback, useEffect } from 'react'; import { useState, useCallback } from 'react';
import { eventsApi } from '../../utils/api'; import { MOCK_EVENTS, MOCK_POLLS } from '../../data/mockData';
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { formatDateTime } from '../../utils/format'; import { formatDateTime } from '../../utils/format';
import type { EventPost, EventType, Poll, UserRole } from '../../types'; import type { EventPost, EventType, Poll, UserRole } from '../../types';
@@ -54,7 +54,7 @@ function PollCard({ poll, onVote }: { poll: Poll; onVote: (pollId: string, optio
<div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}> <div style={{ display: 'flex', flexDirection: 'column', gap: '0.5rem' }}>
{poll.options.map((option) => { {poll.options.map((option) => {
const percentage = totalVotes > 0 ? Math.round((option.votes / totalVotes) * 100) : 0; const percentage = totalVotes > 0 ? Math.round((option.votes / totalVotes) * 100) : 0;
const userVoted = option.votedUserIds.includes(user?.id ?? ''); const userVoted = option.votedUserIds.includes(user?.id || '');
return ( return (
<div <div
@@ -158,10 +158,12 @@ function PollCard({ poll, onVote }: { poll: Poll; onVote: (pollId: string, optio
function EventCard({ function EventCard({
event, event,
poll,
onVote, onVote,
}: { }: {
event: EventPost; event: EventPost;
onVote: (eventId: string, pollId: string, optionId: string) => void; poll?: Poll;
onVote: (pollId: string, optionId: string) => void;
}) { }) {
return ( return (
<div <div
@@ -238,12 +240,7 @@ function EventCard({
</div> </div>
{/* Poll if exists */} {/* Poll if exists */}
{event.poll && ( {poll && <PollCard poll={poll} onVote={onVote} />}
<PollCard
poll={event.poll}
onVote={(pollId, optionId) => onVote(event.id, pollId, optionId)}
/>
)}
</div> </div>
); );
} }
@@ -252,28 +249,48 @@ function EventCard({
export default function EventsPage() { export default function EventsPage() {
const { user } = useAuth(); const { user } = useAuth();
const [events, setEvents] = useState<EventPost[]>([]); // Filter to show only public events
const [loading, setLoading] = useState(true); const publicEvents = MOCK_EVENTS.filter((event) => event.isPublic);
const [events] = useState<EventPost[]>(publicEvents);
useEffect(() => { const [polls, setPolls] = useState<Poll[]>(MOCK_POLLS);
eventsApi.getEvents(true)
.then((res) => setEvents(res.events))
.catch(() => setEvents([]))
.finally(() => setLoading(false));
}, []);
const handleVote = useCallback( const handleVote = useCallback(
async (eventId: string, _pollId: string, optionId: string) => { (pollId: string, optionId: string) => {
if (!user) return; if (!user) return;
try { setPolls((prevPolls) =>
const updatedEvent = await eventsApi.vote(eventId, [optionId]); prevPolls.map((poll) => {
setEvents((prev) => if (poll.id !== pollId) return poll;
prev.map((e) => (e.id === updatedEvent.id ? updatedEvent : e))
); const hasVotedForOption = poll.options.some((opt) =>
} catch { opt.votedUserIds.includes(user.id)
// silently ignore );
}
return {
...poll,
options: poll.options.map((opt) => {
if (opt.id === optionId) {
// Add vote to this option
return {
...opt,
votes: opt.votedUserIds.includes(user.id) ? opt.votes : opt.votes + 1,
votedUserIds: opt.votedUserIds.includes(user.id)
? opt.votedUserIds
: [...opt.votedUserIds, user.id],
};
} else if (!poll.allowMultipleVotes && hasVotedForOption) {
// Remove vote from other options if single vote
return {
...opt,
votes: opt.votedUserIds.includes(user.id) ? opt.votes - 1 : opt.votes,
votedUserIds: opt.votedUserIds.filter((id) => id !== user.id),
};
}
return opt;
}),
};
})
);
}, },
[user] [user]
); );
@@ -317,7 +334,7 @@ export default function EventsPage() {
{/* Events Grid */} {/* Events Grid */}
<div style={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}> <div style={{ display: 'flex', flexDirection: 'column', gap: '1.5rem' }}>
{loading ? ( {events.length === 0 ? (
<div <div
style={{ style={{
background: 'var(--color-surface)', background: 'var(--color-surface)',
@@ -326,27 +343,21 @@ export default function EventsPage() {
textAlign: 'center', textAlign: 'center',
}} }}
> >
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.85rem' }}> <div
Loading events... style={{
</div> fontFamily: 'var(--font-mono)',
</div> color: 'var(--color-text-muted)',
) : events.length === 0 ? ( fontSize: '0.85rem',
<div }}
style={{ >
background: 'var(--color-surface)',
border: '1px solid var(--color-border)',
padding: '3rem 2rem',
textAlign: 'center',
}}
>
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.85rem' }}>
No events available at the moment. Check back soon! No events available at the moment. Check back soon!
</div> </div>
</div> </div>
) : ( ) : (
events.map((event) => ( events.map((event) => {
<EventCard key={event.id} event={event} onVote={handleVote} /> const poll = event.pollId ? polls.find((p) => p.id === event.pollId) : undefined;
)) return <EventCard key={event.id} event={event} poll={poll} onVote={handleVote} />;
})
)} )}
</div> </div>
</div> </div>

View File

@@ -1,6 +1,6 @@
import { useEffect, useMemo, useState } from 'react'; import { useMemo, useState } from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { forumApi } from '../../utils/api'; import { MOCK_CATEGORIES, MOCK_THREADS } from '../../data/mockData';
import { timeAgo } from '../../utils/format'; import { timeAgo } from '../../utils/format';
import type { ForumCategory, ForumThread } from '../../types'; import type { ForumCategory, ForumThread } from '../../types';
@@ -128,43 +128,15 @@ function CategoryCard({ category, threads }: { category: ForumCategory; threads:
export default function ForumPage() { export default function ForumPage() {
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
const [categories, setCategories] = useState<ForumCategory[]>([]);
const [threads, setThreads] = useState<ForumThread[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState('');
useEffect(() => {
let cancelled = false;
setLoading(true);
Promise.all([
forumApi.getCategories(),
forumApi.getThreads({ limit: 200 }),
])
.then(([cats, threadRes]) => {
if (cancelled) return;
setCategories(cats);
setThreads(threadRes.data);
})
.catch(() => {
if (cancelled) return;
setError('Failed to load forum. Please try again.');
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => { cancelled = true; };
}, []);
const filteredCategories = useMemo(() => { const filteredCategories = useMemo(() => {
if (!search.trim()) return categories; if (!search.trim()) return MOCK_CATEGORIES;
const q = search.toLowerCase(); const q = search.toLowerCase();
return categories.filter((cat) => return MOCK_CATEGORIES.filter((cat) =>
cat.name.toLowerCase().includes(q) || cat.name.toLowerCase().includes(q) ||
threads.some((t) => t.categoryId === cat.id && t.title.toLowerCase().includes(q)) MOCK_THREADS.some((t) => t.categoryId === cat.id && t.title.toLowerCase().includes(q))
); );
}, [search, categories, threads]); }, [search]);
return ( return (
<div style={{ maxWidth: '960px', margin: '0 auto', padding: '4rem 1.5rem' }}> <div style={{ maxWidth: '960px', margin: '0 auto', padding: '4rem 1.5rem' }}>
@@ -201,29 +173,15 @@ export default function ForumPage() {
</div> </div>
</div> </div>
{loading && (
<div className="crt-box" style={{ padding: '2rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}>
Loading forum...
</div>
)}
{error && !loading && (
<div className="crt-box" style={{ padding: '2rem', textAlign: 'center', color: 'var(--color-red)', fontFamily: 'var(--font-mono)' }}>
{error}
</div>
)}
{/* Categories */} {/* Categories */}
{!loading && !error && ( {filteredCategories.length === 0 ? (
filteredCategories.length === 0 ? ( <div className="crt-box" style={{ padding: '2rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}>
<div className="crt-box" style={{ padding: '2rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}> No results found for "{search}"
No results found for "{search}" </div>
</div> ) : (
) : ( filteredCategories.map((cat) => (
filteredCategories.map((cat) => ( <CategoryCard key={cat.id} category={cat} threads={MOCK_THREADS} />
<CategoryCard key={cat.id} category={cat} threads={threads} /> ))
))
)
)} )}
</div> </div>
); );

View File

@@ -1,16 +1,6 @@
import { useEffect, useState } from 'react'; import { TEAM_MEMBERS } from '../../data/mockData';
import { teamApi } from '../../utils/api';
import type { TeamMember } from '../../types';
export default function StudioPage() { export default function StudioPage() {
const [members, setMembers] = useState<TeamMember[]>([]);
useEffect(() => {
teamApi.getMembers()
.then(setMembers)
.catch(() => { /* show empty state */ });
}, []);
return ( return (
<div style={{ maxWidth: '1000px', margin: '0 auto', padding: '4rem 1.5rem' }}> <div style={{ maxWidth: '1000px', margin: '0 auto', padding: '4rem 1.5rem' }}>
{/* Header */} {/* Header */}
@@ -137,7 +127,7 @@ export default function StudioPage() {
gap: '1.25rem', gap: '1.25rem',
}} }}
> >
{members.map((member) => ( {TEAM_MEMBERS.map((member) => (
<div key={member.id} className="crt-box" style={{ padding: '1.5rem' }}> <div key={member.id} className="crt-box" style={{ padding: '1.5rem' }}>
<div style={{ display: 'flex', alignItems: 'center', gap: '1rem', marginBottom: '1rem' }}> <div style={{ display: 'flex', alignItems: 'center', gap: '1rem', marginBottom: '1rem' }}>
{/* Avatar */} {/* Avatar */}

View File

@@ -1,48 +1,23 @@
import { useState, useCallback, useEffect } from 'react'; import { useState, useCallback } from 'react';
import { Link, useParams, Navigate } from 'react-router-dom'; import { Link, useParams, Navigate } from 'react-router-dom';
import { forumApi, ApiError } from '../../utils/api'; import { MOCK_THREADS, MOCK_REPLIES } from '../../data/mockData';
import { useAuth } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { formatDateTime, timeAgo } from '../../utils/format'; import { formatDateTime, timeAgo } from '../../utils/format';
import type { ForumThread, ForumReply } from '../../types';
export default function ThreadPage() { export default function ThreadPage() {
const { id } = useParams<{ id: string }>(); const { id } = useParams<{ id: string }>();
const { user, isAuthenticated } = useAuth(); const { user, isAuthenticated } = useAuth();
const [thread, setThread] = useState<ForumThread | null>(null); const thread = MOCK_THREADS.find((t) => t.id === id);
const [replies, setReplies] = useState<ForumReply[]>([]);
const [loading, setLoading] = useState(true);
const [notFound, setNotFound] = useState(false);
// Local state for new reply (stored in memory, not persisted)
const [replies, setReplies] = useState(
MOCK_REPLIES.filter((r) => r.threadId === id)
);
const [newReply, setNewReply] = useState(''); const [newReply, setNewReply] = useState('');
const [replyError, setReplyError] = useState(''); const [replyError, setReplyError] = useState('');
const [submitting, setSubmitting] = useState(false); const [submitting, setSubmitting] = useState(false);
useEffect(() => {
if (!id) return;
let cancelled = false;
setLoading(true);
forumApi.getThread(id)
.then((data) => {
if (cancelled) return;
const { replies: threadReplies, ...threadData } = data;
setThread(threadData);
setReplies(threadReplies);
})
.catch((err) => {
if (cancelled) return;
if (err instanceof ApiError && err.status === 404) {
setNotFound(true);
}
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => { cancelled = true; };
}, [id]);
const handleReply = useCallback(async () => { const handleReply = useCallback(async () => {
if (!newReply.trim()) { if (!newReply.trim()) {
setReplyError('Reply cannot be empty.'); setReplyError('Reply cannot be empty.');
@@ -55,26 +30,23 @@ export default function ThreadPage() {
setReplyError(''); setReplyError('');
setSubmitting(true); setSubmitting(true);
try { await new Promise((r) => setTimeout(r, 300));
const reply = await forumApi.createReply(id!, newReply.trim());
setReplies((prev) => [...prev, reply]);
setNewReply('');
} catch (err) {
setReplyError(err instanceof Error ? err.message : 'Failed to post reply.');
} finally {
setSubmitting(false);
}
}, [newReply, id]);
if (loading) { const reply = {
return ( id: `r${Date.now()}`,
<div style={{ maxWidth: '860px', margin: '0 auto', padding: '4rem 1.5rem', textAlign: 'center', color: 'var(--color-text-muted)', fontFamily: 'var(--font-mono)' }}> content: newReply.trim(),
Loading thread... authorId: user!.id,
</div> authorName: user!.username,
); threadId: id!,
} createdAt: new Date().toISOString(),
};
if (notFound || !thread) { setReplies((prev) => [...prev, reply]);
setNewReply('');
setSubmitting(false);
}, [newReply, user, id]);
if (!thread) {
return <Navigate to="/forum" replace />; return <Navigate to="/forum" replace />;
} }
@@ -231,7 +203,7 @@ export default function ThreadPage() {
<button <button
className="btn-terminal" className="btn-terminal"
onClick={handleReply} onClick={handleReply}
disabled={submitting || !user} disabled={submitting}
style={{ opacity: submitting ? 0.6 : 1 }} style={{ opacity: submitting ? 0.6 : 1 }}
> >
{submitting ? 'Posting...' : '> Post Reply'} {submitting ? 'Posting...' : '> Post Reply'}

View File

@@ -134,7 +134,6 @@ export interface EventPost {
updatedAt?: string; updatedAt?: string;
isPublic: boolean; // whether visible to community isPublic: boolean; // whether visible to community
pollId?: string; // reference to poll if type is 'poll' pollId?: string; // reference to poll if type is 'poll'
poll?: Poll; // embedded poll from API
} }
export interface PollOption { export interface PollOption {

View File

@@ -1,192 +1,4 @@
import type {
User,
ForumCategory,
ForumThread,
ForumReply,
BugReport,
BugComment,
BugReportFormData,
BugSeverity,
BugStatus,
EventPost,
TeamMember,
} from '../types';
// API base URL. // API base URL.
// - In Docker (Coolify): nginx proxies /api/* to the backend, so we use a relative path. // - In Docker (Coolify): nginx proxies /api/* to the backend, so we use a relative path.
// - Set VITE_API_URL at build time to call the backend directly (e.g. during local dev without nginx). // - Set VITE_API_URL at build time to call the backend directly (e.g. during local dev without nginx).
export const API_BASE = import.meta.env.VITE_API_URL ?? '/api'; export const API_BASE = import.meta.env.VITE_API_URL ?? '/api'
// ── Token storage ─────────────────────────────────────────────────────────────
const TOKEN_KEY = 'crowmate_auth_token';
export function getToken(): string | null {
return localStorage.getItem(TOKEN_KEY);
}
export function setToken(token: string): void {
localStorage.setItem(TOKEN_KEY, token);
}
export function clearToken(): void {
localStorage.removeItem(TOKEN_KEY);
}
// ── Core fetch helper ─────────────────────────────────────────────────────────
export class ApiError extends Error {
status: number;
constructor(status: number, message: string) {
super(message);
this.status = status;
this.name = 'ApiError';
}
}
export async function apiFetch<T = unknown>(
path: string,
options: RequestInit = {}
): Promise<T> {
const token = getToken();
const headers: Record<string, string> = {
'Content-Type': 'application/json',
...(options.headers as Record<string, string> ?? {}),
};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
const res = await fetch(`${API_BASE}${path}`, { ...options, headers });
if (!res.ok) {
const body = await res.json().catch(() => ({}));
const message =
typeof body.error === 'string'
? body.error
: body.error?.message ?? `Request failed (${res.status})`;
throw new ApiError(res.status, message);
}
// 204 No Content
if (res.status === 204) return undefined as T;
return res.json() as Promise<T>;
}
// ── Auth API ──────────────────────────────────────────────────────────────────
export const authApi = {
login: (email: string, password: string) =>
apiFetch<{ token: string; user: User }>('/auth/login', {
method: 'POST',
body: JSON.stringify({ email, password }),
}),
register: (username: string, email: string, password: string) =>
apiFetch<{ token: string; user: User }>('/auth/register', {
method: 'POST',
body: JSON.stringify({ username, email, password }),
}),
me: () => apiFetch<User>('/auth/me'),
};
// ── Users API ─────────────────────────────────────────────────────────────────
export const usersApi = {
updateUsername: (username: string) =>
apiFetch<User>('/users/me/username', {
method: 'PATCH',
body: JSON.stringify({ username }),
}),
changePassword: (currentPassword: string, newPassword: string) =>
apiFetch<{ message: string }>('/users/me/password', {
method: 'PATCH',
body: JSON.stringify({ currentPassword, newPassword }),
}),
};
// ── Forum API ─────────────────────────────────────────────────────────────────
export const forumApi = {
getCategories: () =>
apiFetch<ForumCategory[]>('/forum/categories'),
getThreads: (params?: { categoryId?: string; page?: number; limit?: number }) => {
const q = new URLSearchParams();
if (params?.categoryId) q.set('categoryId', params.categoryId);
q.set('page', String(params?.page ?? 1));
q.set('limit', String(params?.limit ?? 100));
return apiFetch<{ data: ForumThread[]; total: number; page: number; pages: number }>(
`/forum/threads?${q}`
);
},
getThread: (id: string) =>
apiFetch<ForumThread & { replies: ForumReply[] }>(`/forum/threads/${id}`),
createReply: (threadId: string, content: string) =>
apiFetch<ForumReply>(`/forum/threads/${threadId}/replies`, {
method: 'POST',
body: JSON.stringify({ content }),
}),
};
// ── Bugs API ──────────────────────────────────────────────────────────────────
export const bugsApi = {
getBugs: (params?: { status?: BugStatus | 'all'; severity?: BugSeverity | 'all'; page?: number; limit?: number }) => {
const q = new URLSearchParams();
if (params?.status && params.status !== 'all') q.set('status', params.status);
if (params?.severity && params.severity !== 'all') q.set('severity', params.severity);
q.set('page', String(params?.page ?? 1));
q.set('limit', String(params?.limit ?? 100));
return apiFetch<{ data: BugReport[]; total: number; page: number; pages: number }>(
`/bugs?${q}`
);
},
getBug: (id: string) =>
apiFetch<BugReport & { comments: BugComment[] }>(`/bugs/${id}`),
createBug: (data: BugReportFormData) =>
apiFetch<BugReport>('/bugs', {
method: 'POST',
body: JSON.stringify(data),
}),
toggleMeToo: (bugId: string) =>
apiFetch<{ message: string }>(`/bugs/${bugId}/me-too`, { method: 'POST' }),
addComment: (bugId: string, content: string) =>
apiFetch<BugComment>(`/bugs/${bugId}/comments`, {
method: 'POST',
body: JSON.stringify({ content }),
}),
};
// ── Events API ────────────────────────────────────────────────────────────────
export const eventsApi = {
getEvents: (publicOnly = true) => {
const q = new URLSearchParams({ limit: '50' });
if (publicOnly) q.set('public', 'true');
return apiFetch<{ events: EventPost[]; total: number; page: number; pages: number }>(
`/events?${q}`
);
},
vote: (eventId: string, optionIds: string[]) =>
apiFetch<EventPost>(`/events/${eventId}/vote`, {
method: 'POST',
body: JSON.stringify({ optionIds }),
}),
};
// ── Team API ──────────────────────────────────────────────────────────────────
export const teamApi = {
getMembers: () => apiFetch<TeamMember[]>('/team'),
};

View File

@@ -7,7 +7,4 @@ export default defineConfig({
react(), react(),
tailwindcss(), tailwindcss(),
], ],
server: {
port: 80,
},
}) })

View File

@@ -1,4 +1,4 @@
FROM node:22-alpine AS build FROM node:25-alpine AS build
WORKDIR /app WORKDIR /app

View File

@@ -3,14 +3,15 @@ server {
root /usr/share/nginx/html; root /usr/share/nginx/html;
index index.html; index index.html;
# Use Docker's embedded DNS resolver; defer resolution to request time # Docker DNS; resolve API service name at request time.
resolver 127.0.0.11 valid=30s; resolver 127.0.0.11 ipv6=off valid=10s;
set $api_upstream http://api:3000;
location /api/ { location /api/ {
set $api_upstream http://api:3000; proxy_pass $api_upstream/api/;
proxy_pass $api_upstream;
proxy_set_header Host $host; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
} }
location / { location / {

View File

@@ -26,6 +26,7 @@ export default function IntranetBugs() {
const [severityFilter, setSeverityFilter] = useState<BugSeverity | 'all'>('all'); const [severityFilter, setSeverityFilter] = useState<BugSeverity | 'all'>('all');
const [assignedFilter, setAssignedFilter] = useState<string | 'all'>('all'); const [assignedFilter, setAssignedFilter] = useState<string | 'all'>('all');
const [noteText, setNoteText] = useState(''); const [noteText, setNoteText] = useState('');
const [isEnabled, setIsEnabled] = useState(true);
const openCount = bugs.filter((b) => b.status === 'open').length; const openCount = bugs.filter((b) => b.status === 'open').length;
const criticalCount = bugs.filter((b) => b.severity === 'critical').length; const criticalCount = bugs.filter((b) => b.severity === 'critical').length;
@@ -72,13 +73,60 @@ export default function IntranetBugs() {
setNoteText(''); setNoteText('');
}, [noteText, user]); }, [noteText, user]);
if (!isEnabled) {
return (
<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '400px', textAlign: 'center' }}>
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.7rem', letterSpacing: '0.15em', marginBottom: '1rem' }}>
INTRANET / BUG REPORTS
</div>
<h1 style={{ fontFamily: 'var(--font-heading)', color: 'var(--color-red)', fontSize: '2rem', marginBottom: '0.5rem' }}>FUNCTIONALITY DISABLED</h1>
<p style={{ color: 'var(--color-text-muted)', marginBottom: '1.5rem' }}>Bug Reports feature is currently disabled</p>
<button
onClick={() => setIsEnabled(true)}
style={{
background: 'var(--color-green)',
color: 'var(--color-bg)',
border: 'none',
padding: '0.6rem 1.2rem',
fontFamily: 'var(--font-mono)',
fontSize: '0.85rem',
cursor: 'pointer',
textTransform: 'uppercase',
letterSpacing: '0.08em',
}}
>
Re-enable
</button>
</div>
);
}
return ( return (
<div style={{ display: 'grid', gridTemplateColumns: selected ? '1fr 400px' : '1fr', gap: '1.5rem', alignItems: 'start' }}> <div style={{ display: 'grid', gridTemplateColumns: selected ? '1fr 400px' : '1fr', gap: '1.5rem', alignItems: 'start' }}>
{/* Left panel */} {/* Left panel */}
<div> <div>
<div style={{ marginBottom: '1.5rem' }}> <div style={{ marginBottom: '1.5rem' }}>
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.7rem', letterSpacing: '0.15em', marginBottom: '0.5rem' }}> <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '0.5rem' }}>
INTRANET / BUG REPORTS <div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.7rem', letterSpacing: '0.15em' }}>
INTRANET / BUG REPORTS
</div>
<button
onClick={() => setIsEnabled(false)}
style={{
background: 'transparent',
border: '1px solid var(--color-red)',
color: 'var(--color-red)',
padding: '0.3rem 0.7rem',
fontFamily: 'var(--font-mono)',
fontSize: '0.65rem',
cursor: 'pointer',
textTransform: 'uppercase',
letterSpacing: '0.08em',
}}
title="Disable this feature"
>
[DISABLE]
</button>
</div> </div>
<h1 style={{ fontFamily: 'var(--font-heading)', color: 'var(--color-text)', fontSize: '1.8rem', marginBottom: '1rem' }}>BUG DASHBOARD</h1> <h1 style={{ fontFamily: 'var(--font-heading)', color: 'var(--color-text)', fontSize: '1.8rem', marginBottom: '1rem' }}>BUG DASHBOARD</h1>

View File

@@ -125,6 +125,8 @@ export default function IntranetDashboard() {
<NavTile to="/intranet/feed" label="Team Feed" description="Internal staff activity feed. Post updates visible only to the team." icon="[~]" /> <NavTile to="/intranet/feed" label="Team Feed" description="Internal staff activity feed. Post updates visible only to the team." icon="[~]" />
<NavTile to="/intranet/users" label="User Management" description="View all registered users. Promote or ban accounts." icon="[U]" /> <NavTile to="/intranet/users" label="User Management" description="View all registered users. Promote or ban accounts." icon="[U]" />
<NavTile to="/intranet/moderation" label="Forum Moderation" description="Pin, lock, and delete threads and replies from the public forum." icon="[M]" /> <NavTile to="/intranet/moderation" label="Forum Moderation" description="Pin, lock, and delete threads and replies from the public forum." icon="[M]" />
<NavTile to="/intranet/events" label="Event Calendar" description="Manage upcoming events, deadlines, and team meetings." icon="[E]" />
<NavTile to="/intranet/services" label="Service Status" description="Redirection to all the services." icon="[S]" />
</div> </div>
</div> </div>

View File

@@ -1,5 +1,5 @@
import { useState, useCallback, useEffect } from 'react'; import { useState, useCallback } from 'react';
import { useAuth, getToken } from '../../contexts/AuthContext'; import { useAuth } from '../../contexts/AuthContext';
import { formatDateTime } from '../../utils/format'; import { formatDateTime } from '../../utils/format';
import type { EventPost, EventType, Poll, UserRole } from '../../types'; import type { EventPost, EventType, Poll, UserRole } from '../../types';
@@ -244,27 +244,10 @@ function EventCard({
// ── Main Component ───────────────────────────────────────────────────────────── // ── Main Component ─────────────────────────────────────────────────────────────
function apiFetch<T>(path: string, options: RequestInit = {}): Promise<T> {
const token = getToken();
return fetch(`/api${path}`, {
...options,
headers: {
'Content-Type': 'application/json',
...(token ? { Authorization: `Bearer ${token}` } : {}),
...(options.headers as Record<string, string> ?? {}),
},
}).then(async (res) => {
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.error ?? `Request failed (${res.status})`);
}
return res.json() as Promise<T>;
});
}
export default function IntranetEvents() { export default function IntranetEvents() {
const { user } = useAuth(); const { user } = useAuth();
const [events, setEvents] = useState<EventPost[]>([]); const [events, setEvents] = useState<EventPost[]>([]);
const [polls, setPolls] = useState<Poll[]>([]);
const [showCreateForm, setShowCreateForm] = useState(false); const [showCreateForm, setShowCreateForm] = useState(false);
// Form state // Form state
@@ -278,78 +261,124 @@ export default function IntranetEvents() {
const [error, setError] = useState(''); const [error, setError] = useState('');
const [posting, setPosting] = useState(false); const [posting, setPosting] = useState(false);
useEffect(() => {
apiFetch<{ events: EventPost[] }>('/events?limit=50')
.then((res) => setEvents(res.events))
.catch(() => setEvents([]));
}, []);
const handleVote = useCallback( const handleVote = useCallback(
async (pollId: string, optionId: string) => { (pollId: string, optionId: string) => {
const event = events.find((e) => e.pollId === pollId || e.poll?.id === pollId); if (!user) return;
if (!event || !user) return;
try { setPolls((prevPolls) =>
const updated = await apiFetch<EventPost>(`/events/${event.id}/vote`, { prevPolls.map((poll) => {
method: 'POST', if (poll.id !== pollId) return poll;
body: JSON.stringify({ optionIds: [optionId] }),
}); const hasVotedForOption = poll.options.some((opt) =>
setEvents((prev) => prev.map((e) => (e.id === updated.id ? updated : e))); opt.votedUserIds.includes(user.id)
} catch { );
// silently ignore
} return {
...poll,
options: poll.options.map((opt) => {
if (opt.id === optionId) {
// Add vote to this option
return {
...opt,
votes: opt.votedUserIds.includes(user.id) ? opt.votes : opt.votes + 1,
votedUserIds: opt.votedUserIds.includes(user.id)
? opt.votedUserIds
: [...opt.votedUserIds, user.id],
};
} else if (!poll.allowMultipleVotes && hasVotedForOption) {
// Remove vote from other options if single vote
return {
...opt,
votes: opt.votedUserIds.includes(user.id) ? opt.votes - 1 : opt.votes,
votedUserIds: opt.votedUserIds.filter((id) => id !== user.id),
};
}
return opt;
}),
};
})
);
}, },
[events, user] [user]
); );
const handleSubmit = useCallback(async () => { const handleSubmit = useCallback(async () => {
if (!title.trim()) { setError('Title is required.'); return; } // Validation
if (!content.trim()) { setError('Content is required.'); return; } if (!title.trim()) {
setError('Title is required.');
return;
}
if (!content.trim()) {
setError('Content is required.');
return;
}
if (createPoll) { if (createPoll) {
if (!pollQuestion.trim()) { setError('Poll question is required.'); return; } if (!pollQuestion.trim()) {
setError('Poll question is required.');
return;
}
const validOptions = pollOptions.filter((opt) => opt.trim()); const validOptions = pollOptions.filter((opt) => opt.trim());
if (validOptions.length < 2) { setError('Poll must have at least 2 options.'); return; } if (validOptions.length < 2) {
setError('Poll must have at least 2 options.');
return;
}
} }
if (!user) return; if (!user) return;
setError(''); setError('');
setPosting(true); setPosting(true);
await new Promise((r) => setTimeout(r, 300));
try { const newEventId = `evt${Date.now()}`;
const body: Record<string, unknown> = { let newPollId: string | undefined;
type: createPoll ? 'poll' : eventType,
title: title.trim(), // Create poll if needed
content: content.trim(), if (createPoll) {
isPublic, newPollId = `poll${Date.now()}`;
const validOptions = pollOptions.filter((opt) => opt.trim());
const newPoll: Poll = {
id: newPollId,
eventId: newEventId,
question: pollQuestion.trim(),
options: validOptions.map((opt, idx) => ({
id: `opt${Date.now()}_${idx}`,
text: opt.trim(),
votes: 0,
votedUserIds: [],
})),
isActive: true,
allowMultipleVotes: false,
createdAt: new Date().toISOString(),
}; };
setPolls((prev) => [newPoll, ...prev]);
if (createPoll) {
body.poll = {
question: pollQuestion.trim(),
options: pollOptions.filter((o) => o.trim()).map((o) => ({ text: o.trim() })),
};
}
const created = await apiFetch<EventPost>('/events', {
method: 'POST',
body: JSON.stringify(body),
});
setEvents((prev) => [created, ...prev]);
// Reset form
setTitle('');
setContent('');
setEventType('announcement');
setIsPublic(true);
setCreatePoll(false);
setPollQuestion('');
setPollOptions(['', '']);
setShowCreateForm(false);
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to create event.');
} finally {
setPosting(false);
} }
// Create event
const newEvent: EventPost = {
id: newEventId,
type: createPoll ? 'poll' : eventType,
title: title.trim(),
content: content.trim(),
authorId: user.id,
authorName: user.username,
authorRole: user.role,
createdAt: new Date().toISOString(),
isPublic,
pollId: newPollId,
};
setEvents((prev) => [newEvent, ...prev]);
// Reset form
setTitle('');
setContent('');
setEventType('announcement');
setIsPublic(true);
setCreatePoll(false);
setPollQuestion('');
setPollOptions(['', '']);
setPosting(false);
setShowCreateForm(false);
}, [title, content, eventType, isPublic, createPoll, pollQuestion, pollOptions, user]); }, [title, content, eventType, isPublic, createPoll, pollQuestion, pollOptions, user]);
return ( return (
@@ -682,9 +711,10 @@ export default function IntranetEvents() {
{/* Events List */} {/* Events List */}
<div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}> <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
{events.map((event) => ( {events.map((event) => {
<EventCard key={event.id} event={event} poll={event.poll ?? undefined} onVote={handleVote} /> const poll = event.pollId ? polls.find((p) => p.id === event.pollId) : undefined;
))} return <EventCard key={event.id} event={event} poll={poll} onVote={handleVote} />;
})}
</div> </div>
</div> </div>
); );

View File

@@ -8,6 +8,7 @@ export default function IntranetModeration() {
const [selectedThreadId, setSelectedThreadId] = useState<string | null>(null); const [selectedThreadId, setSelectedThreadId] = useState<string | null>(null);
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
const [activeTab, setActiveTab] = useState<'threads' | 'replies'>('threads'); const [activeTab, setActiveTab] = useState<'threads' | 'replies'>('threads');
const [isEnabled, setIsEnabled] = useState(true);
const filteredThreads = useMemo(() => { const filteredThreads = useMemo(() => {
if (!search.trim()) return threads; if (!search.trim()) return threads;
@@ -44,14 +45,62 @@ export default function IntranetModeration() {
return ( return (
<div> <div>
<div style={{ marginBottom: '1.75rem' }}> {!isEnabled ? (
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.7rem', letterSpacing: '0.15em', marginBottom: '0.5rem' }}> <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', minHeight: '400px', textAlign: 'center' }}>
INTRANET / MODERATION <div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.7rem', letterSpacing: '0.15em', marginBottom: '1rem' }}>
INTRANET / MODERATION
</div>
<h1 style={{ fontFamily: 'var(--font-heading)', color: 'var(--color-red)', fontSize: '2rem', marginBottom: '0.5rem' }}>FUNCTIONALITY DISABLED</h1>
<p style={{ color: 'var(--color-text-muted)', marginBottom: '1.5rem' }}>Forum Moderation feature is currently disabled</p>
<button
onClick={() => setIsEnabled(true)}
style={{
background: 'var(--color-green)',
color: 'var(--color-bg)',
border: 'none',
padding: '0.6rem 1.2rem',
fontFamily: 'var(--font-mono)',
fontSize: '0.85rem',
cursor: 'pointer',
textTransform: 'uppercase',
letterSpacing: '0.08em',
}}
>
Re-enable
</button>
</div>
) : (
<div>
<div style={{ marginBottom: '1.75rem' }}>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
<div>
<div style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.7rem', letterSpacing: '0.15em', marginBottom: '0.5rem' }}>
INTRANET / MODERATION
</div>
<h1 style={{ fontFamily: 'var(--font-heading)', color: 'var(--color-text)', fontSize: '1.8rem', marginBottom: '0.5rem' }}>FORUM MODERATION</h1>
<p style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.78rem' }}>
{threads.length} threads &mdash; {replies.length} replies
</p>
</div>
<button
onClick={() => setIsEnabled(false)}
style={{
background: 'transparent',
border: '1px solid var(--color-red)',
color: 'var(--color-red)',
padding: '0.3rem 0.7rem',
fontFamily: 'var(--font-mono)',
fontSize: '0.65rem',
cursor: 'pointer',
textTransform: 'uppercase',
letterSpacing: '0.08em',
height: 'fit-content',
}}
title="Disable this feature"
>
[DISABLE]
</button>
</div> </div>
<h1 style={{ fontFamily: 'var(--font-heading)', color: 'var(--color-text)', fontSize: '1.8rem', marginBottom: '0.5rem' }}>FORUM MODERATION</h1>
<p style={{ fontFamily: 'var(--font-mono)', color: 'var(--color-text-muted)', fontSize: '0.78rem' }}>
{threads.length} threads &mdash; {replies.length} replies
</p>
</div> </div>
{/* Tabs */} {/* Tabs */}
@@ -226,5 +275,7 @@ export default function IntranetModeration() {
</div> </div>
)} )}
</div> </div>
)}
</div>
); );
} }

View File

@@ -133,8 +133,7 @@ export interface EventPost {
createdAt: string; createdAt: string;
updatedAt?: string; updatedAt?: string;
isPublic: boolean; // whether visible to community isPublic: boolean; // whether visible to community
pollId?: string | null; // reference to poll if type is 'poll' pollId?: string; // reference to poll if type is 'poll'
poll?: Poll | null; // embedded poll data from API
} }
export interface PollOption { export interface PollOption {