All files / src/components/shared/Company CompanyDetailView.tsx

57.47% Statements 50/87
46.66% Branches 49/105
28% Functions 7/25
55.84% Lines 43/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652                                                                                                          27x                           6x       10x 10x 10x 10x   10x         10x 1x     10x   10x                                                                                                             6x       10x 10x 10x 10x   10x           10x 1x     10x   10x                                                                                                                                                     6x 10x   10x     9x                 10x 1x                                                                                                                                                                                                                                   6x                   15x 15x 15x 15x 15x   15x       15x 1x     15x       15x 2x                 13x 4x                           9x               9x                                                                                                                                                                                                                                                                                                                                                                                                                                                            
import React, { useState } from 'react';
import {
  Box,
  Card,
  CardContent,
  Typography,
  Button,
  Tabs,
  Tab,
  Chip,
  Skeleton,
  Alert,
  Stack,
  Avatar,
  AvatarGroup,
  Paper,
  BottomNavigation,
  BottomNavigationAction,
  useTheme,
  useMediaQuery,
} from '@mui/material';
import Grid from '@mui/material/Grid';
import {
  Edit as EditIcon,
  Delete as DeleteIcon,
  CheckCircle as CheckCircleIcon,
  Business as BusinessIcon,
  People as UsersIcon,
  RecordVoiceOver as SpeakersIcon,
  Slideshow as SessionsIcon,
} from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import { useQuery } from '@tanstack/react-query';
import { useNavigate, useLocation } from 'react-router-dom';
import apiClient from '@/services/api/apiClient';
import type { components } from '@/types/generated/company-api.types';
import type { User } from '@/types/user.types';
import DeleteCompanyDialog from '@/components/shared/Company/DeleteCompanyDialog';
import { Breadcrumbs } from '@/components/shared/Breadcrumbs/Breadcrumbs';
import { useUserList } from '@/hooks/useUserManagement';
import UserTable from '@/components/organizer/UserManagement/UserTable';
import UserCard from '@/components/organizer/UserManagement/UserCard';
import UserPagination from '@/components/organizer/UserManagement/UserPagination';
 
type CompanyDetail = components['schemas']['CompanyResponse'];
 
interface TabPanelProps {
  children?: React.ReactNode;
  index: number;
  value: number;
}
 
function TabPanel({ children, value, index }: TabPanelProps) {
  return (
    <div
      role="tabpanel"
      hidden={value !== index}
      id={`company-tabpanel-${index}`}
      aria-labelledby={`company-tab-${index}`}
    >
      <Box sx={{ pt: 3 }}>{children}</Box>
    </div>
  );
}
 
// ─── Company Users Panel ───────────────────────────────────────────────────────
 
const CompanyUsersPanel: React.FC<{ companyName: string; isMobile: boolean }> = ({
  companyName,
  isMobile,
}) => {
  const navigate = useNavigate();
  const location = useLocation();
  const [page, setPage] = useState(1);
  const [limit, setLimit] = useState(20);
 
  const { data, isLoading, isError } = useUserList({
    filters: { company: companyName },
    pagination: { page, limit },
  });
 
  if (isLoading) return <Skeleton variant="rectangular" height={300} />;
  Eif (isError) return <Alert severity="error">Failed to load users.</Alert>;
 
  const users = data?.data ?? [];
  const paginationData = data?.pagination;
 
  Iif (users.length === 0) {
    return (
      <Alert severity="info" sx={{ mt: 2 }}>
        No users associated with this company yet.
      </Alert>
    );
  }
 
  return (
    <Box sx={{ mt: 2 }}>
      {isMobile ? (
        <Stack spacing={2}>
          {users.map((user) => (
            <UserCard
              key={user.id}
              user={user}
              onClick={(u: User) =>
                navigate(`/organizer/users/${u.id}`, {
                  state: { from: location.pathname, fromLabel: companyName },
                })
              }
            />
          ))}
        </Stack>
      ) : (
        <UserTable
          users={users}
          onRowClick={(user: User) =>
            navigate(`/organizer/users/${user.id}`, {
              state: { from: location.pathname, fromLabel: companyName },
            })
          }
          onAction={(action, user: User) => {
            if (action === 'view')
              navigate(`/organizer/users/${user.id}`, {
                state: { from: location.pathname, fromLabel: companyName },
              });
          }}
        />
      )}
      {paginationData && (
        <UserPagination
          page={paginationData.page}
          totalPages={paginationData.totalPages}
          limit={paginationData.limit}
          onPageChange={setPage}
          onLimitChange={setLimit}
        />
      )}
    </Box>
  );
};
 
// ─── Company Speakers Panel ────────────────────────────────────────────────────
 
const CompanySpeakersPanel: React.FC<{ companyName: string; isMobile: boolean }> = ({
  companyName,
  isMobile,
}) => {
  const navigate = useNavigate();
  const location = useLocation();
  const [page, setPage] = useState(1);
  const [limit, setLimit] = useState(20);
 
  const { data, isLoading, isError } = useUserList({
    filters: { role: ['SPEAKER'], company: companyName },
    pagination: { page, limit },
    enabled: !!companyName,
  });
 
  if (isLoading) return <Skeleton variant="rectangular" height={300} />;
  Eif (isError) return <Alert severity="error">Failed to load speakers.</Alert>;
 
  const speakers = data?.data ?? [];
  const paginationData = data?.pagination;
 
  Iif (speakers.length === 0) {
    return (
      <Alert severity="info" sx={{ mt: 2 }}>
        No speakers from this company yet.
      </Alert>
    );
  }
 
  return (
    <Box sx={{ mt: 2 }}>
      {isMobile ? (
        <Stack spacing={2}>
          {speakers.map((user) => (
            <UserCard
              key={user.id}
              user={user}
              onClick={(u: User) =>
                navigate(`/organizer/users/${u.id}`, {
                  state: { from: location.pathname, fromLabel: companyName },
                })
              }
            />
          ))}
        </Stack>
      ) : (
        <UserTable
          users={speakers}
          onRowClick={(user: User) =>
            navigate(`/organizer/users/${user.id}`, {
              state: { from: location.pathname, fromLabel: companyName },
            })
          }
          onAction={(action, user: User) => {
            if (action === 'view')
              navigate(`/organizer/users/${user.id}`, {
                state: { from: location.pathname, fromLabel: companyName },
              });
          }}
        />
      )}
      {paginationData && (
        <UserPagination
          page={paginationData.page}
          totalPages={paginationData.totalPages}
          limit={paginationData.limit}
          onPageChange={setPage}
          onLimitChange={setLimit}
        />
      )}
    </Box>
  );
};
 
// ─── Company Sessions Panel ────────────────────────────────────────────────────
 
interface SessionSpeaker {
  username: string;
  firstName?: string;
  lastName?: string;
  company?: string;
  profilePictureUrl?: string;
}
 
interface CompanySession {
  sessionSlug: string;
  eventCode: string;
  eventTitle?: string;
  eventDate?: string;
  title: string;
  sessionType?: string;
  startTime?: string;
  room?: string;
  speakers: SessionSpeaker[];
}
 
const CompanySessionsPanel: React.FC<{ companyName: string }> = ({ companyName }) => {
  const navigate = useNavigate();
 
  const { data, isLoading, isError } = useQuery({
    queryKey: ['sessions', 'byCompany', companyName],
    queryFn: async () => {
      const res = await apiClient.get<{ data: CompanySession[] }>('/sessions', {
        params: { companyName, limit: 50 },
      });
      return res.data.data ?? [];
    },
    enabled: !!companyName,
    staleTime: 5 * 60 * 1000,
  });
 
  if (isLoading) return <Skeleton variant="rectangular" height={300} />;
  Eif (isError) return <Alert severity="error">Failed to load sessions.</Alert>;
  if (!data?.length) {
    return (
      <Alert severity="info" sx={{ mt: 2 }}>
        No sessions from this company yet.
      </Alert>
    );
  }
 
  return (
    <Stack spacing={2} sx={{ mt: 2 }}>
      {data.map((session) => {
        const companySpeakers = session.speakers.filter((s) => s.company === companyName);
        const otherSpeakers = session.speakers.filter((s) => s.company !== companyName);
        const allSpeakers = [...companySpeakers, ...otherSpeakers];
        const eventLabel = session.eventTitle
          ? `${session.eventCode} · ${session.eventTitle}`
          : session.eventCode;
        const dateLabel = session.eventDate
          ? new Date(session.eventDate).toLocaleDateString('de-CH', {
              year: 'numeric',
              month: 'short',
              day: 'numeric',
            })
          : null;
 
        return (
          <Card
            key={`${session.eventCode}-${session.sessionSlug}`}
            variant="outlined"
            onClick={() => navigate(`/events/${session.eventCode}`)}
            sx={{
              cursor: 'pointer',
              transition: 'border-color 0.15s, box-shadow 0.15s',
              '&:hover': { borderColor: 'primary.main', boxShadow: 2 },
            }}
          >
            <CardContent>
              <Stack spacing={1}>
                {/* Event badge */}
                <Stack direction="row" spacing={1} alignItems="center" flexWrap="wrap">
                  <Chip label={eventLabel} size="small" color="primary" variant="outlined" />
                  {dateLabel && (
                    <Typography variant="caption" color="text.secondary">
                      {dateLabel}
                    </Typography>
                  )}
                  {session.room && (
                    <Typography variant="caption" color="text.secondary">
                      · {session.room}
                    </Typography>
                  )}
                </Stack>
 
                {/* Session title */}
                <Typography variant="subtitle1" fontWeight="medium">
                  {session.title}
                </Typography>
 
                {/* Speakers */}
                {allSpeakers.length > 0 && (
                  <Stack direction="row" spacing={1} alignItems="center" flexWrap="wrap">
                    <AvatarGroup
                      max={6}
                      sx={{ '& .MuiAvatar-root': { width: 28, height: 28, fontSize: '0.75rem' } }}
                    >
                      {allSpeakers.map((speaker) => (
                        <Avatar
                          key={speaker.username}
                          src={speaker.profilePictureUrl ?? undefined}
                          alt={speaker.firstName}
                          sx={{
                            width: 28,
                            height: 28,
                            fontSize: '0.75rem',
                            border: speaker.company === companyName ? '2px solid' : undefined,
                            borderColor:
                              speaker.company === companyName ? 'primary.main' : undefined,
                          }}
                        >
                          {`${speaker.firstName?.charAt(0) ?? ''}${speaker.lastName?.charAt(0) ?? ''}`}
                        </Avatar>
                      ))}
                    </AvatarGroup>
                    <Typography variant="caption" color="text.secondary">
                      {allSpeakers
                        .map((s) => `${s.firstName ?? ''} ${s.lastName ?? ''}`.trim())
                        .filter(Boolean)
                        .join(', ')}
                    </Typography>
                  </Stack>
                )}
              </Stack>
            </CardContent>
          </Card>
        );
      })}
    </Stack>
  );
};
 
// ─── Main Component ────────────────────────────────────────────────────────────
 
interface CompanyDetailViewProps {
  company: CompanyDetail | null;
  isLoading?: boolean;
  error?: string;
  canEdit?: boolean;
  canDelete?: boolean;
  onEdit: (companyId: string) => void;
  onBack: () => void;
  onRetry?: () => void;
}
 
export const CompanyDetailView: React.FC<CompanyDetailViewProps> = ({
  company,
  isLoading = false,
  error,
  canEdit = true,
  canDelete = false,
  onEdit,
  onBack,
  onRetry,
}) => {
  const { t } = useTranslation('common');
  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down('sm'));
  const [activeTab, setActiveTab] = useState(0);
  const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false);
 
  const handleTabChange = (_event: React.SyntheticEvent, newValue: number) => {
    setActiveTab(newValue);
  };
 
  const handleEdit = () => {
    Eif (company) onEdit(company.name);
  };
 
  const handleDelete = () => {
    if (company) setIsDeleteDialogOpen(true);
  };
 
  if (isLoading) {
    return (
      <Box data-testid="detail-view-skeleton" sx={{ p: 3 }}>
        <Skeleton variant="rectangular" width="100%" height={200} sx={{ mb: 2 }} />
        <Skeleton variant="text" width="60%" height={40} sx={{ mb: 1 }} />
        <Skeleton variant="text" width="40%" height={30} />
      </Box>
    );
  }
 
  if (error) {
    return (
      <Box sx={{ p: 3 }}>
        <Alert severity="error" sx={{ mb: 2 }}>
          {error}
        </Alert>
        {onRetry && (
          <Button variant="contained" onClick={onRetry}>
            {t('actions.retry')}
          </Button>
        )}
      </Box>
    );
  }
 
  Iif (!company) {
    return (
      <Box sx={{ p: 3 }}>
        <Alert severity="info">{t('company.detail.noData')}</Alert>
      </Box>
    );
  }
 
  return (
    <Box data-testid="company-detail-view" sx={{ p: { xs: 1.5, sm: 3 }, pb: 8 }}>
      {/* Breadcrumbs */}
      <Breadcrumbs
        items={[
          { label: t('company.title'), path: '/organizer/companies' },
          { label: company.displayName || company.name },
        ]}
      />
 
      {/* Action buttons */}
      {(canEdit || canDelete) && (
        <Box sx={{ mb: 3, display: 'flex', justifyContent: 'flex-end' }}>
          <Stack direction={{ xs: 'column', sm: 'row' }} spacing={1}>
            {canDelete && (
              <Button
                variant="outlined"
                color="error"
                startIcon={<DeleteIcon />}
                onClick={handleDelete}
              >
                {t('actions.delete')}
              </Button>
            )}
            {canEdit && (
              <Button variant="contained" startIcon={<EditIcon />} onClick={handleEdit}>
                {t('actions.edit')}
              </Button>
            )}
          </Stack>
        </Box>
      )}
 
      {/* Company Profile Card */}
      <Card sx={{ mb: 3 }}>
        <CardContent>
          <Grid container spacing={3} component="div">
            <Grid size={{ xs: 12, sm: 3 }}>
              <Box
                sx={{
                  display: 'flex',
                  justifyContent: 'center',
                  alignItems: 'center',
                  minHeight: 180,
                }}
              >
                {company.logo?.url ? (
                  <Box
                    component="img"
                    src={company.logo.url}
                    alt={`${company.displayName || company.name} logo`}
                    sx={{ maxWidth: 150, maxHeight: 150, objectFit: 'contain' }}
                  />
                ) : (
                  <Box
                    sx={{
                      width: 150,
                      height: 150,
                      bgcolor: 'grey.200',
                      borderRadius: 1,
                      display: 'flex',
                      justifyContent: 'center',
                      alignItems: 'center',
                    }}
                  >
                    <BusinessIcon sx={{ fontSize: 60, color: 'grey.500' }} />
                  </Box>
                )}
              </Box>
            </Grid>
 
            <Grid size={{ xs: 12, sm: 9 }}>
              <Stack spacing={2}>
                <Box sx={{ display: 'flex', alignItems: 'center', gap: 1, flexWrap: 'wrap' }}>
                  <Typography
                    variant="h4"
                    component="h1"
                    sx={{ fontSize: { xs: '1.5rem', sm: '2.125rem' } }}
                  >
                    {company.displayName || company.name}
                  </Typography>
                  {company.isVerified && (
                    <Chip
                      data-testid="verified-badge"
                      icon={<CheckCircleIcon />}
                      label={t('company.badges.verified')}
                      color="success"
                      size="small"
                    />
                  )}
                </Box>
 
                {company.displayName && company.displayName !== company.name && (
                  <Typography variant="subtitle1" color="text.secondary">
                    {t('company.fields.legalName', { name: company.name })}
                  </Typography>
                )}
 
                <Grid container spacing={2} component="div">
                  {company.swissUID && (
                    <Grid size={{ xs: 12, sm: 6 }}>
                      <Typography variant="body2" color="text.secondary">
                        Swiss UID
                      </Typography>
                      <Typography variant="body1">{company.swissUID}</Typography>
                    </Grid>
                  )}
                  {company.industry && (
                    <Grid size={{ xs: 12, sm: 6 }}>
                      <Typography variant="body2" color="text.secondary">
                        Industry
                      </Typography>
                      <Typography variant="body1">{company.industry}</Typography>
                    </Grid>
                  )}
                  {company.website && (
                    <Grid size={{ xs: 12, sm: 6 }}>
                      <Typography variant="body2" color="text.secondary">
                        Website
                      </Typography>
                      <Typography variant="body1">
                        <a href={company.website} target="_blank" rel="noopener noreferrer">
                          {company.website}
                        </a>
                      </Typography>
                    </Grid>
                  )}
                  <Grid size={{ xs: 12, sm: 6 }}>
                    <Typography variant="body2" color="text.secondary">
                      Created By
                    </Typography>
                    <Typography variant="body1" sx={{ fontFamily: 'monospace' }}>
                      {company.createdBy}
                    </Typography>
                  </Grid>
                </Grid>
 
                {company.description && (
                  <Box>
                    <Typography variant="body2" color="text.secondary" gutterBottom>
                      Description
                    </Typography>
                    <Typography variant="body1">{company.description}</Typography>
                  </Box>
                )}
              </Stack>
            </Grid>
          </Grid>
        </CardContent>
      </Card>
 
      {/* Top Tabs — desktop only */}
      {!isMobile && (
        <Box sx={{ borderBottom: 1, borderColor: 'divider', mb: 2 }}>
          <Tabs
            value={activeTab}
            onChange={handleTabChange}
            aria-label={t('company.detail.tabs.ariaLabel')}
          >
            <Tab
              label={t('company.detail.tabs.users')}
              id="company-tab-0"
              aria-controls="company-tabpanel-0"
            />
            <Tab
              label={t('navigation.speakers')}
              id="company-tab-1"
              aria-controls="company-tabpanel-1"
            />
            <Tab
              label={t('labels.sessions')}
              id="company-tab-2"
              aria-controls="company-tabpanel-2"
            />
          </Tabs>
        </Box>
      )}
 
      {/* Users: all users associated with this company */}
      <TabPanel value={activeTab} index={0}>
        <CompanyUsersPanel companyName={company.name} isMobile={isMobile} />
      </TabPanel>
 
      {/* Speakers: users with role SPEAKER from this company */}
      <TabPanel value={activeTab} index={1}>
        <CompanySpeakersPanel companyName={company.name} isMobile={isMobile} />
      </TabPanel>
 
      {/* Sessions: sessions where a speaker from this company participated */}
      <TabPanel value={activeTab} index={2}>
        <CompanySessionsPanel companyName={company.name} />
      </TabPanel>
 
      {/* Delete Confirmation Dialog */}
      <DeleteCompanyDialog
        company={company}
        open={isDeleteDialogOpen}
        onClose={() => setIsDeleteDialogOpen(false)}
        onSuccess={onBack}
      />
 
      {/* Bottom Navigation — mobile only */}
      <Paper
        sx={{
          position: 'fixed',
          bottom: 0,
          left: 0,
          right: 0,
          zIndex: 1100,
          display: { xs: 'block', md: 'none' },
        }}
        elevation={3}
      >
        <BottomNavigation value={activeTab} onChange={(_, v) => setActiveTab(v)}>
          <BottomNavigationAction icon={<UsersIcon />} sx={{ minWidth: 0, flex: 1 }} />
          <BottomNavigationAction icon={<SpeakersIcon />} sx={{ minWidth: 0, flex: 1 }} />
          <BottomNavigationAction icon={<SessionsIcon />} sx={{ minWidth: 0, flex: 1 }} />
        </BottomNavigation>
      </Paper>
    </Box>
  );
};