'use client'; import { useState } from "react"; import { useParams, useRouter } from "next/navigation"; import { SettingsTab } from "~/components/studies/settings-tab"; import { UsersTab } from "~/components/studies/users-tab"; import { useEffect } from "react"; import { PERMISSIONS } from "~/lib/permissions-client"; import { Button } from "~/components/ui/button"; import { Settings2Icon, UsersIcon } from "lucide-react"; import { cn } from "~/lib/utils"; import { getApiUrl } from "~/lib/fetch-utils"; import { Card, CardContent } from "~/components/ui/card"; import { Skeleton } from "~/components/ui/skeleton"; interface Study { id: number; title: string; description: string | null; permissions: string[]; } export default function StudySettings() { const [study, setStudy] = useState(null); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [activeTab, setActiveTab] = useState<'settings' | 'users'>('settings'); const { id } = useParams(); const router = useRouter(); useEffect(() => { const fetchStudy = async () => { try { const response = await fetch(getApiUrl(`/api/studies/${id}`)); if (!response.ok) { if (response.status === 403) { router.push('/dashboard/studies'); return; } throw new Error("Failed to fetch study"); } const data = await response.json(); // Check if user has any required permissions const requiredPermissions = [PERMISSIONS.EDIT_STUDY, PERMISSIONS.MANAGE_ROLES]; const hasAccess = data.data.permissions.some(p => requiredPermissions.includes(p)); if (!hasAccess) { router.push('/dashboard/studies'); return; } setStudy(data.data); } catch (error) { console.error("Error fetching study:", error); setError(error instanceof Error ? error.message : "Failed to load study"); } finally { setIsLoading(false); } }; fetchStudy(); }, [id, router]); if (isLoading) { return (
); } if (error || !study) { return
Error: {error}
; } return (

Settings

Manage study settings and team members

); }