import { useState } from 'react';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { RefreshCw, Download, Circle, Copy, Check } from 'lucide-react';
import { toast } from 'sonner';
import { Button } from '@/components/ui/button';
import { useClient } from 'hooks/useClient';
type AppStatus = {
id: string;
name: string;
description: string;
installed: boolean;
version: string | null;
running: boolean | null;
hasInstall: boolean;
hasUpdate: boolean;
manualInstallCommand: string | null;
manualUpdateCommand: string | null;
};
const CopyCommand = ({ command }: { command: string }) => {
const [copied, setCopied] = useState(false);
const copy = () => {
navigator.clipboard.writeText(command);
setCopied(true);
setTimeout(() => setCopied(false), 1500);
};
return (
{command}
);
};
export const Applications = () => {
const client = useClient();
const queryClient = useQueryClient();
const [actionInProgress, setActionInProgress] = useState(null);
const { data: apps, isLoading } = useQuery({
queryKey: ['APPLICATIONS'],
queryFn: () => client.get('/server-settings/applications'),
});
const runAction = async (id: string, action: 'install' | 'update') => {
setActionInProgress(id);
try {
await client.post(`/server-settings/applications/${id}/${action}`);
await queryClient.invalidateQueries({ queryKey: ['APPLICATIONS'] });
toast.success(`${action === 'install' ? 'Installed' : 'Updated'} successfully`);
} catch (err) {
const message = err instanceof Error ? err.message : `${action} failed`;
toast.error(message);
} finally {
setActionInProgress(null);
}
};
const getManualCommand = (app: AppStatus): string | null => {
if (!app.installed) return app.manualInstallCommand;
return app.manualUpdateCommand ?? app.manualInstallCommand;
};
const hasAutoAction = (app: AppStatus): boolean => {
if (!app.installed) return app.hasInstall && !app.manualInstallCommand;
return app.hasUpdate && !(app.manualUpdateCommand ?? app.manualInstallCommand);
};
return (
Applications
{isLoading &&
Checking applications...
}
{apps && (
{apps.map((app: AppStatus) => {
const manualCmd = getManualCommand(app);
const canAutoRun = hasAutoAction(app);
return (
{app.name}
{app.installed && (
{app.version}
)}
{!app.installed && (
Not installed
)}
{app.running !== null && (
)}
{app.description}
{canAutoRun && !app.installed && (
)}
{canAutoRun && app.installed && (
)}
{manualCmd && (
{app.installed ? 'Update' : 'Install'} manually:
)}
);
})}
)}
);
};