Untitled

 avatar
unknown
plain_text
16 days ago
8.9 kB
6
Indexable
export default function EnvironmentalImpact() {
  const { metrics, loading, error } = useMetrics();
  const { settings } = useSettings();
  const { t } = useTranslation(settings?.language as SupportedLanguages || 'EN');

  if (loading) return <div className="p-4">Loading...</div>;
  if (error) return <div className="p-4">Error: {error}</div>;
  if (!metrics) return null;

  const { environmental_impact: impact } = metrics;

  const carKmAvoided = impact.co2_saved * 4;
  const flightsAvoided = Math.round((impact.co2_saved / 90) * 100) / 100;
  const phoneCharges = Math.round(impact.co2_saved * 1000);
  const lightbulbHours = Math.round(impact.co2_saved * 100);
  const oxygenForPeople = Math.round(impact.trees_saved * 2);
  const paperSheetsSaved = Math.round(impact.trees_saved * 8333);

  const totalMaterials = impact.paper_weight_recycled + impact.plastic_weight_recycled + impact.organic_weight_processed;

  const materialsData = [
    { 
      browser: "paper",
      label: t('bins.Papier'),
      visitors: Math.round(impact.paper_weight_recycled * 100) / 100,
      fill: materialChartConfig.paper.color
    },
    {
      browser: "plastic",
      label: t('bins.Gelber Sack'),
      visitors: Math.round(impact.plastic_weight_recycled * 100) / 100,
      fill: materialChartConfig.plastic.color
    },
    {
      browser: "organic",
      label: t('bins.Biomüll'),
      visitors: Math.round(impact.organic_weight_processed * 100) / 100,
      fill: materialChartConfig.organic.color
    }
  ].filter(item => item.visitors > 0);

  return (
    <SidebarProvider>
      <AppSidebar />
      <SidebarInset>
        <header className="flex shrink-0 items-center gap-2 border-b px-4 py-6">
          <SidebarTrigger className="-ml-1" />
          <Separator orientation="vertical" className="mr-2 h-4" />
          <Breadcrumb>
            <BreadcrumbList>
              <BreadcrumbItem className="hidden md:block">
                <BreadcrumbLink href="/">{t('navigation.home')}</BreadcrumbLink>
              </BreadcrumbItem>
              <BreadcrumbSeparator className="hidden md:block" />
              <BreadcrumbItem>
                <BreadcrumbPage>{t('navigation.impact')}</BreadcrumbPage>
              </BreadcrumbItem>
            </BreadcrumbList>
          </Breadcrumb>
        </header>
        
        <main className="p-4 space-y-4">
          <div className="flex justify-between items-center">
            <h1 className="text-2xl font-bold">{t('navigation.impact')}</h1>
          </div>

          <Alert>
            <AlertCircle className="h-4 w-4" />
            <AlertDescription>
              {t('environmentalImpact.customBinWarning')}
            </AlertDescription>
          </Alert>

          <div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
            <Card className="xl:col-span-2">
              <CardHeader className="pb-2">
                <CardTitle className="flex items-center gap-2 text-lg">
                  <Leaf className="h-5 w-5 text-green-500" />
                  {t('environmentalImpact.co2Impact')}
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="mt-2 space-y-4">
                  <div className="text-3xl font-bold text-green-500">
                    {impact.co2_saved.toFixed(2)} {t('environmentalImpact.co2Saved')}
                  </div>
                  <div className="grid grid-cols-2 gap-4">
                    <div className="flex items-center gap-2 text-sm">
                      <Car className="h-4 w-4 text-blue-500" />
                      <span>{carKmAvoided.toFixed(1)} {t('environmentalImpact.drivingAvoided')}</span>
                    </div>
                    <div className="flex items-center gap-2 text-sm">
                      <PlaneTakeoff className="h-4 w-4 text-blue-500" />
                      <span>{t('environmentalImpact.flightsAvoided').replace('{}', flightsAvoided.toString())}</span>
                    </div>
                    <div className="flex items-center gap-2 text-sm">
                      <Smartphone className="h-4 w-4 text-blue-500" />
                      <span>{t('environmentalImpact.phoneCharges').replace('{}', phoneCharges.toString())}</span>
                    </div>
                    <div className="flex items-center gap-2 text-sm">
                      <LightbulbOff className="h-4 w-4 text-blue-500" />
                      <span>{t('environmentalImpact.ledBulbUsage').replace('{}', lightbulbHours.toString())}</span>
                    </div>
                  </div>
                </div>
              </CardContent>
            </Card>

            <Card>
              <CardHeader className="pb-2">
                <CardTitle className="flex items-center gap-2 text-lg">
                  <TreePine className="h-5 w-5 text-green-700" />
                  {t('environmentalImpact.treeImpact')}
                </CardTitle>
              </CardHeader>
              <CardContent>
                <div className="mt-2 space-y-4">
                  <div className="text-3xl font-bold text-green-700">
                    {impact.trees_saved.toFixed(3)} {t('environmentalImpact.treesPreserved')}
                  </div>
                  <div className="space-y-2">
                    <div className="text-sm">
                      {t('environmentalImpact.oxygenProvided').replace('{}', oxygenForPeople.toString())}
                    </div>
                    <div className="text-sm">
                      {t('environmentalImpact.paperSheetsSaved').replace('{}', paperSheetsSaved.toLocaleString())}
                    </div>
                  </div>
                </div>
              </CardContent>
            </Card>

            <Card className="flex flex-col">
              <CardHeader className="items-center pb-0">
                <CardTitle>{t('environmentalImpact.materialsRecycled')}</CardTitle>
                <CardDescription>{t('common.currentFillLevels')}</CardDescription>
              </CardHeader>
              <CardContent className="flex-1 pb-0">
                <ChartContainer
                  config={materialChartConfig}
                  className="mx-auto aspect-square max-h-[250px] [&_.recharts-text]:fill-background"
                >
                  <PieChart>
                    <ChartTooltip
                      content={({ active, payload }) => {
                        if (active && payload && payload.length) {
                          return (
                            <div className="rounded-lg border bg-background p-2 shadow-sm">
                              <div className="grid grid-cols-2 gap-2">
                                <div className="flex items-center gap-2">
                                  <div
                                    className="h-2 w-2 rounded"
                                    style={{
                                      background: payload[0].payload.fill,
                                    }}
                                  />
                                  <span className="font-medium">
                                    {payload[0].payload.label}
                                  </span>
                                </div>
                                <div>{payload[0].value.toFixed(2)} {t('environmentalImpact.kg')}</div>
                              </div>
                            </div>
                          )
                        }
                        return null
                      }}
                    />
                    <Pie data={materialsData} dataKey="visitors">
                      <LabelList
                        dataKey="browser"
                        className="fill-background"
                        stroke="none"
                        fontSize={12}
                        formatter={(value: keyof typeof materialChartConfig) =>
                          materialChartConfig[value]?.label
                        }
                      />
                    </Pie>
                  </PieChart>
                </ChartContainer>
              </CardContent>
              <CardFooter className="flex-col gap-2 text-sm">
                <div className="leading-none text-muted-foreground">
                  {t('environmentalImpact.totalProcessed').replace('{}', totalMaterials.toFixed(2))}
                </div>
              </CardFooter>
            </Card>
            
            <Card className="xl:col-span-2">
                <TreeVisualization impact={impact} />
            </Card>            
          </div>
        </main>
      </SidebarInset>
    </SidebarProvider>
  );
}
Editor is loading...
Leave a Comment