page.tsx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. 'use client';
  2. import { useState, useEffect, useCallback } from 'react';
  3. import { FileText, Fuel } from 'lucide-react';
  4. import { Card, CardContent } from '@/components/ui/card';
  5. import { getImportsByLayoutName } from '@/app/actions/imports';
  6. import { TerraTechImportsTable } from '@/app/components/terratech/TerraTechImportsTable';
  7. import { TerraTechSummaryDialog } from '@/app/components/terratech/TerraTechSummaryDialog';
  8. import { useToast } from '@/hooks/use-toast';
  9. interface Import {
  10. id: number;
  11. name: string;
  12. importDate: string;
  13. layoutId: number;
  14. layout: {
  15. id: number;
  16. name: string;
  17. };
  18. }
  19. interface RawImportData {
  20. id: number;
  21. name: string;
  22. importDate: Date | string;
  23. layoutId: number;
  24. layout: {
  25. id: number;
  26. name: string;
  27. };
  28. }
  29. const LAYOUT_NAME = 'TerraTech - GasOilWater Summary';
  30. export default function TerraTechFacilitySummariesPage() {
  31. const [imports, setImports] = useState<Import[]>([]);
  32. const [loading, setLoading] = useState(true);
  33. const [summaryDialogOpen, setSummaryDialogOpen] = useState(false);
  34. const [selectedImportId, setSelectedImportId] = useState<number | null>(null);
  35. const { toast } = useToast();
  36. const loadImports = useCallback(async () => {
  37. try {
  38. const result = await getImportsByLayoutName(LAYOUT_NAME);
  39. if (result.success && result.data) {
  40. const transformedImports = result.data.map((item: RawImportData) => ({
  41. id: item.id,
  42. name: item.name,
  43. importDate: item.importDate instanceof Date
  44. ? item.importDate.toISOString()
  45. : String(item.importDate),
  46. layoutId: item.layoutId,
  47. layout: {
  48. id: item.layout.id,
  49. name: item.layout.name,
  50. },
  51. }));
  52. setImports(transformedImports);
  53. } else {
  54. toast({
  55. title: 'Error',
  56. description: result.error || 'Failed to load imports',
  57. variant: 'destructive',
  58. });
  59. setImports([]);
  60. }
  61. } catch {
  62. toast({
  63. title: 'Error',
  64. description: 'Failed to load imports',
  65. variant: 'destructive',
  66. });
  67. } finally {
  68. setLoading(false);
  69. }
  70. }, [toast]);
  71. useEffect(() => {
  72. loadImports();
  73. }, [loadImports]);
  74. function handleViewSummary(importRecord: Import) {
  75. setSelectedImportId(importRecord.id);
  76. setSummaryDialogOpen(true);
  77. }
  78. if (loading) {
  79. return (
  80. <div className="min-h-screen bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800">
  81. <div className="container mx-auto px-4 py-8">
  82. <div className="flex justify-center">
  83. <div className="animate-spin rounded-full h-8 w-8 border-b-2 border-primary"></div>
  84. </div>
  85. </div>
  86. </div>
  87. );
  88. }
  89. return (
  90. <div className="min-h-screen bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800">
  91. <div className="container mx-auto px-4 py-8">
  92. <div className="flex justify-between items-center mb-8">
  93. <div className="flex items-center gap-4">
  94. <div className="bg-amber-500 w-12 h-12 rounded-full flex items-center justify-center text-white">
  95. <Fuel className="w-6 h-6" />
  96. </div>
  97. <div>
  98. <h1 className="text-3xl font-bold text-gray-900 dark:text-white">
  99. TerraTech Facility Summaries
  100. </h1>
  101. <p className="text-gray-600 dark:text-gray-300">
  102. View Gas, Oil, and Water production summaries for imported data
  103. </p>
  104. </div>
  105. </div>
  106. </div>
  107. {imports.length === 0 ? (
  108. <Card className="dark:bg-gray-800 dark:border-gray-700">
  109. <CardContent className="flex flex-col items-center justify-center py-12">
  110. <FileText className="h-12 w-12 text-muted-foreground mb-4" />
  111. <h3 className="text-lg font-semibold mb-2 text-gray-900 dark:text-white">No imports found</h3>
  112. <p className="text-muted-foreground mb-4 dark:text-gray-300 text-center">
  113. No imports using the &quot;{LAYOUT_NAME}&quot; layout configuration were found.
  114. <br />
  115. Import data using this layout to see it here.
  116. </p>
  117. </CardContent>
  118. </Card>
  119. ) : (
  120. <div className="w-full">
  121. <TerraTechImportsTable
  122. data={imports}
  123. onViewSummary={handleViewSummary}
  124. />
  125. </div>
  126. )}
  127. <TerraTechSummaryDialog
  128. open={summaryDialogOpen}
  129. onOpenChange={setSummaryDialogOpen}
  130. importId={selectedImportId || 0}
  131. />
  132. </div>
  133. </div>
  134. );
  135. }