- Add Install button that appears when script files are loaded - Integrate with existing terminal component for script execution - Pass correct script path to terminal (scripts/ct/script.sh) - Close modal when installation starts - Add proper TypeScript handling for union types - Install button only shows when CT script is available locally
123 lines
4.2 KiB
TypeScript
123 lines
4.2 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
import { api } from '~/trpc/react';
|
|
import { ScriptCard } from './ScriptCard';
|
|
import { ScriptDetailModal } from './ScriptDetailModal';
|
|
import type { ScriptCard as ScriptCardType, Script } from '~/types/script';
|
|
|
|
interface ScriptsGridProps {
|
|
onInstallScript?: (scriptPath: string, scriptName: string) => void;
|
|
}
|
|
|
|
export function ScriptsGrid({ onInstallScript }: ScriptsGridProps) {
|
|
const [selectedSlug, setSelectedSlug] = useState<string | null>(null);
|
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
|
|
|
const { data: scriptCardsData, isLoading, error, refetch } = api.scripts.getScriptCards.useQuery();
|
|
const { data: scriptData } = api.scripts.getScriptBySlug.useQuery(
|
|
{ slug: selectedSlug ?? '' },
|
|
{ enabled: !!selectedSlug }
|
|
);
|
|
|
|
// Debug logging
|
|
console.log('ScriptsGrid render:', {
|
|
isLoading,
|
|
error: error?.message,
|
|
scriptCardsData: scriptCardsData?.success,
|
|
cardsCount: scriptCardsData?.cards?.length
|
|
});
|
|
|
|
const handleCardClick = (scriptCard: ScriptCardType) => {
|
|
setSelectedSlug(scriptCard.slug);
|
|
setIsModalOpen(true);
|
|
};
|
|
|
|
const handleCloseModal = () => {
|
|
setIsModalOpen(false);
|
|
setSelectedSlug(null);
|
|
};
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex items-center justify-center py-12">
|
|
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-600"></div>
|
|
<span className="ml-2 text-gray-600">Loading scripts...</span>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
if (error || !scriptCardsData?.success) {
|
|
return (
|
|
<div className="text-center py-12">
|
|
<div className="text-red-600 mb-4">
|
|
<svg className="w-12 h-12 mx-auto mb-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L3.732 16.5c-.77.833.192 2.5 1.732 2.5z" />
|
|
</svg>
|
|
<p className="text-lg font-medium">Failed to load scripts</p>
|
|
<p className="text-sm text-gray-500 mt-1">
|
|
{scriptCardsData?.error || 'Unknown error occurred'}
|
|
</p>
|
|
<div className="mt-4 text-xs text-gray-400">
|
|
<p>No JSON files found in scripts/json directory.</p>
|
|
<p>Use the "Resync Scripts" button to download from GitHub.</p>
|
|
</div>
|
|
</div>
|
|
<button
|
|
onClick={() => refetch()}
|
|
className="mt-4 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors"
|
|
>
|
|
Try Again
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const scripts = scriptCardsData?.cards || [];
|
|
|
|
if (!scripts || scripts.length === 0) {
|
|
return (
|
|
<div className="text-center py-12">
|
|
<div className="text-gray-500">
|
|
<svg className="w-12 h-12 mx-auto mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z" />
|
|
</svg>
|
|
<p className="text-lg font-medium">No scripts found</p>
|
|
<p className="text-sm text-gray-500 mt-1">
|
|
No script files were found in the repository.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<>
|
|
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
|
|
{scripts.map((script, index) => {
|
|
// Add validation to ensure script has required properties
|
|
if (!script || typeof script !== 'object') {
|
|
console.warn('Invalid script object at index', index, script);
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<ScriptCard
|
|
key={script.slug || `script-${index}`}
|
|
script={script}
|
|
onClick={handleCardClick}
|
|
/>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
<ScriptDetailModal
|
|
script={scriptData?.success ? scriptData.script : null}
|
|
isOpen={isModalOpen}
|
|
onClose={handleCloseModal}
|
|
onInstallScript={onInstallScript}
|
|
/>
|
|
</>
|
|
);
|
|
}
|