From 840fc5404a27be63d3e6d1bb62f8562002a6fc64 Mon Sep 17 00:00:00 2001 From: Miguel Reese Date: Tue, 27 Oct 2026 23:44:43 -0700 Subject: [PATCH] Let administrators view the site as themselves as a student MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Administrator accounts are created without a student record, so the role switcher's student list never included the signed-in admin. The list now leads with "Me". The first time it reads "Me · create my student record", which adds a blank record under the admin's own ID (logged), links it to the account, and switches to viewing as that student. The record starts with the getting-started steps like any new student. Choosing Student now defaults to your own record when you have one. The dashboard and Search courses only use an account's linked student record while acting as a student, so an administrator's own view is unchanged after creating one. Co-Authored-By: Claude Opus 5.5 --- src/components/RoleSwitcher.tsx | 15 ++++++++++++--- src/data/store.tsx | 14 ++++++++++++++ src/screens/CourseSearch.tsx | 2 +- src/screens/Dashboard.tsx | 2 +- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/components/RoleSwitcher.tsx b/src/components/RoleSwitcher.tsx index b4c0c06..579e45f 100644 --- a/src/components/RoleSwitcher.tsx +++ b/src/components/RoleSwitcher.tsx @@ -3,10 +3,11 @@ import { useNavigate } from 'react-router-dom'; import { useStore } from '../data/store'; import { canSwitchRoles, defaultView, describeView, ROLE_ORDER, ROLE_VIEWS } from '../lib/viewAs'; import { Check } from './icons'; +import { fullName } from '../lib/names'; /** Bottom-left switcher (FW-09): administrators view SignMeUp as each user type without separate accounts. */ export function RoleSwitcher() { - const { db, realUser, viewAs, setViewAs } = useStore(); + const { db, realUser, viewAs, setViewAs, createMyStudentRecord } = useStore(); const [open, setOpen] = useState(false); const nav = useNavigate(); @@ -21,7 +22,14 @@ export function RoleSwitcher() { const active = viewAs?.role ?? 'admin'; const advisors = db.users.filter((u) => u.role === 'advisor' && u.facultyId); + const mine = realUser?.studentId && db.students.find((s) => s.id === realUser.studentId); + function chooseStudent(value: string) { + if (value !== '__mine') return choose('student', { studentId: value }); + const id = createMyStudentRecord(); // first time: start a student record under the administrator's own ID + if (id) choose('student', { studentId: id }); + } function choose(role: typeof active, pick?: { studentId?: string; facultyId?: string }) { + if (role === 'student' && !pick && mine) pick = { studentId: mine.id }; // viewing as a student defaults to yourself when you have a record setViewAs(role === 'admin' ? null : { ...defaultView(db, role), ...pick }); setOpen(false); nav('/'); @@ -44,9 +52,10 @@ export function RoleSwitcher() { {active === r && } {r === 'student' && ( - chooseStudent(e.target.value)}> - {db.students.map((s) => )} + {mine ? : } + {db.students.filter((s) => s.id !== realUser?.studentId).map((s) => )} )} {r === 'faculty' && ( diff --git a/src/data/store.tsx b/src/data/store.tsx index 052d22f..0901d0c 100644 --- a/src/data/store.tsx +++ b/src/data/store.tsx @@ -57,6 +57,7 @@ interface StoreApi { realUser: User | null; // always the signed-in user viewAs: ViewAs | null; setViewAs(v: ViewAs | null): void; + createMyStudentRecord(): string | null; // administrators: a student record under their own ID, to view the site as themselves session: Session; mode: StoreMode; syncError: string | null; @@ -301,6 +302,19 @@ export function StoreProvider({ children }: { children: ReactNode }) { try { if (next) sessionStorage.setItem(VIEW_KEY, JSON.stringify(next)); else sessionStorage.removeItem(VIEW_KEY); } catch { /* not remembered across reloads */ } }, + createMyStudentRecord() { + if (!realUser || !canSwitchRoles(realUser)) return null; + const id = realUser.studentId ?? realUser.id; + if (realUser.studentId && db.students.some((s) => s.id === id)) return id; + if (db.students.some((s) => s.id === id)) return null; // that ID already belongs to someone else's record + commit((d) => stamp({ + ...d, + users: d.users.map((u) => (u.id === realUser.id ? { ...u, studentId: id } : u)), + students: d.students.some((s) => s.id === id) ? d.students : [...d.students, skeletonStudent(id, realUser.name)], + }, 'FRAMEWORK', `Created a student record for ${realUser.name} (${id})`, realUser.name)); + return id; + }, + signOut() { try { sessionStorage.removeItem(VIEW_KEY); } catch { /* nothing stored */ } void clerk.signOut({ redirectUrl: '/login' }); }, resetDemo() { diff --git a/src/screens/CourseSearch.tsx b/src/screens/CourseSearch.tsx index c679453..1d12330 100644 --- a/src/screens/CourseSearch.tsx +++ b/src/screens/CourseSearch.tsx @@ -21,7 +21,7 @@ const COLS = '84px 1fr 44px 130px 112px 92px'; // seats fits "3 seats left" and export default function CourseSearch() { const { db, user, register, requestWaiver } = useStore(); const toast = useToast(); - const student = db.students.find((s) => s.id === user?.studentId); + const student = user?.role === 'student' ? db.students.find((s) => s.id === user.studentId) : undefined; // staff accounts may also own a student record; it applies only when acting as a student const [q, setQ] = useState(''); const [dept, setDept] = useState(''); const [openOnly, setOpenOnly] = useState(true); diff --git a/src/screens/Dashboard.tsx b/src/screens/Dashboard.tsx index fd0a2bf..2363ad8 100644 --- a/src/screens/Dashboard.tsx +++ b/src/screens/Dashboard.tsx @@ -15,7 +15,7 @@ import { formatDate, formatTimeRange, shortName } from '../lib/format'; export default function Dashboard() { const { db, user, register, drop } = useStore(); const toast = useToast(); - const student = db.students.find((s) => s.id === user?.studentId); + const student = user?.role === 'student' ? db.students.find((s) => s.id === user.studentId) : undefined; // staff accounts may also own a student record; it applies only when acting as a student if (!user) return null; if (!student) return ;