File size: 1,313 Bytes
af1bee9
fb43301
af1bee9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { Button, Stack, Text } from '@mantine/core';
import { IconLock } from '@/components/icons';
import type { ReactNode } from 'react';
import { Link } from 'react-router-dom';
import { useAuth } from '../auth/AuthContext';
import { PageHeader } from './common';

/** Admin pages: everyone else gets a proper page that says why and leads back to Home. */
export function AdminOnly({ children, title = 'Settings' }: { children: ReactNode; title?: string }) {
  const { can, user } = useAuth();
  if (can('admin')) return <>{children}</>;
  return (
    <Stack gap="md">

      <PageHeader crumbs={[{ label: 'Home', to: '/' }, { label: title }]} title={title} />

      <div className="fpt-card" role="alert" style={{ padding: 20, display: 'flex', gap: 14, alignItems: 'flex-start' }} data-testid="admin-only">

        <span className="trio-tile" aria-hidden><IconLock size={19} stroke={1.7} /></span>

        <div>

          <Text fw={600}>This page needs the admin role</Text>

          <Text size="sm" c="dimmed" mt={2}>You are signed in as {user?.username ?? 'a user'} ({user?.role ?? 'no role'}). Ask an administrator if you need access.</Text>

          <Button component={Link} to="/" variant="default" mt="sm">Back to Home</Button>

        </div>

      </div>

    </Stack>
  );
}