|
1 | | -'use client'; |
2 | | - |
3 | | -import { useEffect, useState, useCallback } from 'react'; |
4 | | -import { getCompanies, Company } from '@/lib/actions/companies'; |
| 1 | +import { getCompaniesForUserDashboard, ManagedCompany, InvestedCompany, OtherCompany } from '@/lib/actions/companies'; |
5 | 2 | import { Card, CardHeader, CardTitle, CardDescription, CardContent } from "@/components/ui/card"; |
6 | 3 | import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; |
7 | 4 | import { Badge } from "@/components/ui/badge"; |
8 | 5 | import { CreateCompanyDialog } from '@/components/create-company-dialog'; |
9 | 6 | import { Button } from '@/components/ui/button'; |
10 | 7 | import Link from 'next/link'; |
11 | | -import { Loader2 } from 'lucide-react'; |
12 | | - |
13 | | -export default function CompaniesPage() { |
14 | | - const [companies, setCompanies] = useState<Company[]>([]); |
15 | | - const [loading, setLoading] = useState(true); |
| 8 | +import { InvestDialog } from '@/components/invest-dialog'; |
| 9 | +import type { CompanyWithDetails } from '@/lib/actions/companies'; |
16 | 10 |
|
17 | | - const fetchCompanies = useCallback(async () => { |
18 | | - setLoading(true); |
19 | | - const companiesData = await getCompanies(); |
20 | | - setCompanies(companiesData); |
21 | | - setLoading(false); |
22 | | - }, []); |
23 | 11 |
|
24 | | - useEffect(() => { |
25 | | - fetchCompanies(); |
26 | | - }, [fetchCompanies]); |
| 12 | +function CompanyTableRow({ company, type }: { company: any, type: 'managed' | 'invested' | 'other' }) { |
| 13 | + return ( |
| 14 | + <TableRow> |
| 15 | + <TableCell> |
| 16 | + <div className="font-medium">{company.name}</div> |
| 17 | + <div className="text-sm text-muted-foreground">{company.industry}</div> |
| 18 | + </TableCell> |
| 19 | + {type === 'managed' && ( |
| 20 | + <TableCell> |
| 21 | + <Badge variant="secondary">{company.role.toUpperCase()}</Badge> |
| 22 | + </TableCell> |
| 23 | + )} |
| 24 | + {type === 'invested' && ( |
| 25 | + <> |
| 26 | + <TableCell className="font-mono">{company.sharesHeld.toFixed(4)}</TableCell> |
| 27 | + <TableCell className="font-mono">${company.sharesValue.toFixed(2)}</TableCell> |
| 28 | + </> |
| 29 | + )} |
| 30 | + {(type === 'managed' || type === 'other') && ( |
| 31 | + <TableCell className="font-mono">${company.cash.toFixed(2)}</TableCell> |
| 32 | + )} |
| 33 | + {type === 'other' && ( |
| 34 | + <TableCell className="font-mono">${company.marketCap.toLocaleString(undefined, { maximumFractionDigits: 0})}</TableCell> |
| 35 | + )} |
| 36 | + <TableCell className="text-right space-x-2"> |
| 37 | + <Button asChild variant="outline" size="sm"> |
| 38 | + <Link href={`/companies/${company.id}`}>Détails</Link> |
| 39 | + </Button> |
| 40 | + {type === 'other' && ( |
| 41 | + <InvestDialog company={company as CompanyWithDetails}> |
| 42 | + <Button size="sm">Investir</Button> |
| 43 | + </InvestDialog> |
| 44 | + )} |
| 45 | + </TableCell> |
| 46 | + </TableRow> |
| 47 | + ) |
| 48 | +} |
27 | 49 |
|
28 | | - if (loading) { |
| 50 | +function CompanyTable({ title, description, companies, type }: { title: string, description: string, companies: any[], type: 'managed' | 'invested' | 'other' }) { |
| 51 | + const headers = { |
| 52 | + managed: ["Entreprise", "Mon Rôle", "Trésorerie", ""], |
| 53 | + invested: ["Entreprise", "Parts Détenues", "Valeur des Parts", ""], |
| 54 | + other: ["Entreprise", "Trésorerie", "Cap. Boursière", ""] |
| 55 | + } |
| 56 | + |
29 | 57 | return ( |
30 | | - <div className="flex h-64 items-center justify-center"> |
31 | | - <Loader2 className="h-8 w-8 animate-spin" /> |
32 | | - </div> |
| 58 | + <Card> |
| 59 | + <CardHeader> |
| 60 | + <CardTitle>{title}</CardTitle> |
| 61 | + <CardDescription>{description}</CardDescription> |
| 62 | + </CardHeader> |
| 63 | + <CardContent> |
| 64 | + <Table> |
| 65 | + <TableHeader> |
| 66 | + <TableRow> |
| 67 | + {headers[type].map((header, i) => ( |
| 68 | + <TableHead key={i} className={i === headers[type].length - 1 ? 'text-right' : ''}>{header}</TableHead> |
| 69 | + ))} |
| 70 | + </TableRow> |
| 71 | + </TableHeader> |
| 72 | + <TableBody> |
| 73 | + {companies.length > 0 ? ( |
| 74 | + companies.map((company) => <CompanyTableRow key={company.id} company={company} type={type} />) |
| 75 | + ) : ( |
| 76 | + <TableRow> |
| 77 | + <TableCell colSpan={headers[type].length} className="h-24 text-center text-muted-foreground"> |
| 78 | + {type === 'managed' ? "Vous ne gérez aucune entreprise." : type === 'invested' ? "Vous n'avez investi dans aucune entreprise." : "Aucune autre entreprise disponible."} |
| 79 | + </TableCell> |
| 80 | + </TableRow> |
| 81 | + )} |
| 82 | + </TableBody> |
| 83 | + </Table> |
| 84 | + </CardContent> |
| 85 | + </Card> |
33 | 86 | ) |
34 | | - } |
| 87 | +} |
35 | 88 |
|
36 | | - return ( |
37 | | - <Card> |
38 | | - <CardHeader className="flex-row items-center justify-between"> |
39 | | - <div> |
40 | | - <CardTitle>Entreprises</CardTitle> |
41 | | - <CardDescription>Créez ou investissez dans des entreprises, influencez la gestion et gagnez des dividendes.</CardDescription> |
42 | | - </div> |
43 | | - <CreateCompanyDialog onCompanyCreated={fetchCompanies} /> |
44 | | - </CardHeader> |
45 | | - <CardContent> |
46 | | - <Table> |
47 | | - <TableHeader> |
48 | | - <TableRow> |
49 | | - <TableHead>Entreprise</TableHead> |
50 | | - <TableHead>Industrie</TableHead> |
51 | | - <TableHead>Trésorerie</TableHead> |
52 | | - <TableHead className="text-right">Actions</TableHead> |
53 | | - </TableRow> |
54 | | - </TableHeader> |
55 | | - <TableBody> |
56 | | - {companies.length > 0 ? ( |
57 | | - companies.map((company) => ( |
58 | | - <TableRow key={company.id}> |
59 | | - <TableCell> |
60 | | - <div className="font-medium">{company.name}</div> |
61 | | - </TableCell> |
62 | | - <TableCell> |
63 | | - <Badge variant="secondary">{company.industry}</Badge> |
64 | | - </TableCell> |
65 | | - <TableCell className="font-mono">${parseFloat(company.cash).toFixed(2)}</TableCell> |
66 | | - <TableCell className="text-right space-x-2"> |
67 | | - <Button asChild variant="outline" size="sm"> |
68 | | - <Link href={`/companies/${company.id}`}>Détails</Link> |
69 | | - </Button> |
70 | | - </TableCell> |
71 | | - </TableRow> |
72 | | - )) |
73 | | - ) : ( |
74 | | - <TableRow> |
75 | | - <TableCell colSpan={4} className="h-24 text-center text-muted-foreground"> |
76 | | - Aucune entreprise n'a encore été créée. Soyez le premier ! |
77 | | - </TableCell> |
78 | | - </TableRow> |
| 89 | + |
| 90 | +export default async function CompaniesPage() { |
| 91 | + const { managedCompanies, investedCompanies, otherCompanies } = await getCompaniesForUserDashboard(); |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="space-y-6"> |
| 95 | + <div className="flex items-center justify-between"> |
| 96 | + <div> |
| 97 | + <h1 className="text-2xl font-bold tracking-tight">Espace Entreprises</h1> |
| 98 | + <p className="text-muted-foreground">Créez, gérez et investissez dans des entreprises dirigées par des joueurs.</p> |
| 99 | + </div> |
| 100 | + <CreateCompanyDialog /> |
| 101 | + </div> |
| 102 | + |
| 103 | + {managedCompanies.length > 0 && ( |
| 104 | + <CompanyTable |
| 105 | + title="Mes Entreprises (Dirigeant)" |
| 106 | + description="Les entreprises que vous gérez directement." |
| 107 | + companies={managedCompanies} |
| 108 | + type="managed" |
| 109 | + /> |
79 | 110 | )} |
80 | | - </TableBody> |
81 | | - </Table> |
82 | | - </CardContent> |
83 | | - </Card> |
84 | | - ); |
| 111 | + |
| 112 | + {investedCompanies.length > 0 && ( |
| 113 | + <CompanyTable |
| 114 | + title="Mes Investissements" |
| 115 | + description="Les entreprises dans lesquelles vous détenez des parts." |
| 116 | + companies={investedCompanies} |
| 117 | + type="invested" |
| 118 | + /> |
| 119 | + )} |
| 120 | + |
| 121 | + <CompanyTable |
| 122 | + title="Marché des Entreprises" |
| 123 | + description="Toutes les entreprises disponibles à l'investissement." |
| 124 | + companies={otherCompanies} |
| 125 | + type="other" |
| 126 | + /> |
| 127 | + </div> |
| 128 | + ); |
85 | 129 | } |
0 commit comments