Доставка файлов приложения из каталога (data_files) до compose up, APP_PASSWORD/default_credentials для Umbrel, сворачивание диалогов долгих операций (MinimizableDialog), фильтрация pull-шума в ошибках. Исправления багов: - teardown: отсутствующий в Proxmox CT считается удалённым — orphaned-приложение теперь можно удалить из БД - install: сдвиг VMID мимо занятых записей (nextid не резервирует номер, retry мог подхватить чужой контейнер) - retry: параметры установки сохраняются в installed_apps.install_params (cores/memory/disk/storage/bridge/ip_config/port_explicit, миграция JSONB) вместо жёстких дефолтов - reconcile: недоступный сервер больше не помечает все приложения orphaned - update: реально опубликованный порт фиксируется в БД (url/health) - .env: значения с '#' кавычатся (dotenv-парсер обрезал их как inline-комментарий) - порт 80, выбранный явно в мастере, публикуется как есть - useCatalog: available_only включён в queryKey (коллизия кэша) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
257 lines
9.0 KiB
TypeScript
257 lines
9.0 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
|
|
import { apiClient } from '@/lib/api-client';
|
|
import { getTasksWebSocket } from '@/lib/websocket';
|
|
import type {
|
|
AppOperation, CatalogApp, CatalogListResponse, CatalogMeta,
|
|
InstalledApp, InstallResponse,
|
|
} from '@/types/appstore';
|
|
|
|
export const appstoreKeys = {
|
|
catalog: (q?: string, category?: string, source?: string, availableOnly = false) =>
|
|
['appstore-catalog', q ?? '', category ?? '', source ?? '', availableOnly] as const,
|
|
catalogMeta: ['appstore-catalog-meta'] as const,
|
|
catalogApp: (id: string) => ['appstore-catalog-app', id] as const,
|
|
installed: ['appstore-installed'] as const,
|
|
installedApp: (id: number) => ['appstore-installed', id] as const,
|
|
operations: (id: number) => ['appstore-operations', id] as const,
|
|
};
|
|
|
|
// ── Каталог ────────────────────────────────────────────────────────────────
|
|
|
|
export function useCatalog(q?: string, category?: string, source?: string, availableOnly = false) {
|
|
const params = new URLSearchParams();
|
|
if (q) params.set('q', q);
|
|
if (category) params.set('category', category);
|
|
if (source) params.set('source', source);
|
|
if (availableOnly) params.set('available_only', 'true');
|
|
const qs = params.toString();
|
|
return useQuery({
|
|
queryKey: appstoreKeys.catalog(q, category, source, availableOnly),
|
|
queryFn: () => apiClient.get<CatalogListResponse>(`/api/appstore/catalog${qs ? `?${qs}` : ''}`),
|
|
});
|
|
}
|
|
|
|
export function useCatalogMeta() {
|
|
return useQuery({
|
|
queryKey: appstoreKeys.catalogMeta,
|
|
queryFn: () => apiClient.get<CatalogMeta>('/api/appstore/catalog/meta'),
|
|
});
|
|
}
|
|
|
|
export function useCatalogApp(appId: string) {
|
|
return useQuery({
|
|
queryKey: appstoreKeys.catalogApp(appId),
|
|
queryFn: () => apiClient.get<CatalogApp>(`/api/appstore/catalog/${appId}`),
|
|
enabled: !!appId,
|
|
});
|
|
}
|
|
|
|
export function useSyncCatalog() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: () => apiClient.post<{ success: boolean; stats: { total: number } }>('/api/appstore/catalog/sync'),
|
|
onSuccess: () => {
|
|
qc.invalidateQueries({ queryKey: ['appstore-catalog'] });
|
|
qc.invalidateQueries({ queryKey: appstoreKeys.catalogMeta });
|
|
},
|
|
});
|
|
}
|
|
|
|
// ── Золотой шаблон (Workstream B) ────────────────────────────────────────────
|
|
|
|
export interface GoldenTemplateStatus {
|
|
configured_template: string | null;
|
|
env_template: string | null;
|
|
saved_template: string | null;
|
|
last_operation: AppOperation | null;
|
|
}
|
|
|
|
export interface GoldenBuildArgs {
|
|
server_id: number;
|
|
node?: string;
|
|
template_storage?: string;
|
|
rootfs_storage?: string;
|
|
bridge?: string;
|
|
force?: boolean;
|
|
}
|
|
|
|
// vztmpl — файл на storage конкретного Proxmox-сервера: шаблон сервера A не
|
|
// существует на сервере B, поэтому статус всегда запрашивается по serverId.
|
|
export function useGoldenTemplateStatus(serverId?: number, pollMs?: number) {
|
|
return useQuery({
|
|
queryKey: ['appstore-golden-status', serverId],
|
|
queryFn: () => apiClient.get<GoldenTemplateStatus>(
|
|
`/api/appstore/golden-template/status?server_id=${serverId}`,
|
|
),
|
|
enabled: !!serverId,
|
|
refetchInterval: pollMs && pollMs > 0 ? pollMs : false,
|
|
});
|
|
}
|
|
|
|
export function useBuildGoldenTemplate() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: GoldenBuildArgs) =>
|
|
apiClient.post<{ success: boolean; operation_id: number }>('/api/appstore/golden-template', data),
|
|
onSuccess: (_data, variables) =>
|
|
qc.invalidateQueries({ queryKey: ['appstore-golden-status', variables.server_id] }),
|
|
});
|
|
}
|
|
|
|
// ── Установленные ──────────────────────────────────────────────────────────
|
|
|
|
export function useInstalledApps() {
|
|
return useQuery({
|
|
queryKey: appstoreKeys.installed,
|
|
queryFn: () => apiClient.get<{ items: InstalledApp[] }>('/api/appstore/apps').then((r) => r.items),
|
|
});
|
|
}
|
|
|
|
export function useInstalledApp(id: number) {
|
|
return useQuery({
|
|
queryKey: appstoreKeys.installedApp(id),
|
|
queryFn: () => apiClient.get<InstalledApp>(`/api/appstore/apps/${id}`),
|
|
enabled: id > 0,
|
|
});
|
|
}
|
|
|
|
export function useAppOperations(id: number, pollMs?: number) {
|
|
return useQuery({
|
|
queryKey: appstoreKeys.operations(id),
|
|
queryFn: () => apiClient.get<{ items: AppOperation[] }>(`/api/appstore/apps/${id}/operations`).then((r) => r.items),
|
|
enabled: id > 0,
|
|
// Резервный опрос (когда задан) — прогресс не зависит только от WebSocket.
|
|
refetchInterval: pollMs && pollMs > 0 ? pollMs : false,
|
|
});
|
|
}
|
|
|
|
export interface InstallArgs {
|
|
app_id: string;
|
|
name: string;
|
|
server_id: number;
|
|
node?: string;
|
|
form_answers: Record<string, string>;
|
|
cores: number;
|
|
memory: number;
|
|
disk: number;
|
|
storage: string;
|
|
bridge: string;
|
|
port?: number;
|
|
ipam_network_id?: number;
|
|
ipam_pool_id?: number;
|
|
}
|
|
|
|
export function useInstallApp() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (data: InstallArgs) => apiClient.post<InstallResponse>('/api/appstore/apps/install', data),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: appstoreKeys.installed }),
|
|
});
|
|
}
|
|
|
|
export function useRetryApp() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: number) => apiClient.post(`/api/appstore/apps/${id}/retry`),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: appstoreKeys.installed }),
|
|
});
|
|
}
|
|
|
|
export function useDeleteApp() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: number) => apiClient.delete(`/api/appstore/apps/${id}`),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: appstoreKeys.installed }),
|
|
});
|
|
}
|
|
|
|
export function useUpdateApp() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: number) => apiClient.post(`/api/appstore/apps/${id}/update`),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: appstoreKeys.installed }),
|
|
});
|
|
}
|
|
|
|
export function useRollbackApp() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (id: number) => apiClient.post(`/api/appstore/apps/${id}/rollback`),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: appstoreKeys.installed }),
|
|
});
|
|
}
|
|
|
|
export type LifecycleAction = 'start' | 'stop' | 'restart';
|
|
|
|
export function useLifecycleAction() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({ id, action }: { id: number; action: LifecycleAction }) =>
|
|
apiClient.post(`/api/appstore/apps/${id}/action/${action}`),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: appstoreKeys.installed }),
|
|
});
|
|
}
|
|
|
|
export function useReconcile() {
|
|
const qc = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: () => apiClient.post<{ checked: number; changed: number }>('/api/appstore/apps/reconcile'),
|
|
onSuccess: () => qc.invalidateQueries({ queryKey: appstoreKeys.installed }),
|
|
});
|
|
}
|
|
|
|
export function useAppCredentials(id: number, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: ['appstore-credentials', id],
|
|
queryFn: () =>
|
|
apiClient.get<{ credentials: Record<string, string> }>(`/api/appstore/apps/${id}/credentials`)
|
|
.then((r) => r.credentials),
|
|
enabled: enabled && id > 0,
|
|
refetchOnWindowFocus: false,
|
|
});
|
|
}
|
|
|
|
export function useAppLogs(id: number, enabled: boolean) {
|
|
return useQuery({
|
|
queryKey: ['appstore-logs', id],
|
|
queryFn: () => apiClient.get<{ logs: string }>(`/api/appstore/apps/${id}/logs?tail=200`).then((r) => r.logs),
|
|
enabled: enabled && id > 0,
|
|
refetchOnWindowFocus: false,
|
|
});
|
|
}
|
|
|
|
// ── Realtime: инвалидация кэшей по WS-событиям + подписка на операцию ──────────
|
|
|
|
export function useAppstoreRealtime() {
|
|
const qc = useQueryClient();
|
|
useEffect(() => {
|
|
const ws = getTasksWebSocket();
|
|
ws.connect();
|
|
const off1 = ws.onType('appstore_operation_update', () => {
|
|
qc.invalidateQueries({ queryKey: appstoreKeys.installed });
|
|
});
|
|
const off2 = ws.onType('appstore_app_deleted', () => {
|
|
qc.invalidateQueries({ queryKey: appstoreKeys.installed });
|
|
});
|
|
return () => { off1(); off2(); };
|
|
}, [qc]);
|
|
}
|
|
|
|
/** Подписка на прогресс конкретной установки (по installed_app_id). */
|
|
export function useOperationProgress(
|
|
installedAppId: number | null,
|
|
onUpdate: (op: AppOperation) => void,
|
|
) {
|
|
useEffect(() => {
|
|
if (!installedAppId) return;
|
|
const ws = getTasksWebSocket();
|
|
ws.connect();
|
|
const off = ws.onType('appstore_operation_update', (data: unknown) => {
|
|
const op = (data as { task?: AppOperation }).task;
|
|
if (op && op.installed_app_id === installedAppId) onUpdate(op);
|
|
});
|
|
return () => { off(); };
|
|
}, [installedAppId, onUpdate]);
|
|
}
|