168 lines
5.5 KiB
TypeScript
168 lines
5.5 KiB
TypeScript
import type {
|
|
GamePluginResponse,
|
|
JobResponse,
|
|
RunEndpointResponse,
|
|
ServerDeploymentMode,
|
|
ServerInstanceResponse,
|
|
ServerInstanceState
|
|
} from "../api/types";
|
|
|
|
export type ServerWorkflowViewState = "api" | "local" | "saving";
|
|
export type ServerLifecycleActionLabel = "create" | "start" | "stop" | "refresh";
|
|
export type ServerRemovalAction = "delete";
|
|
|
|
export interface ServerCreateFormState {
|
|
id: string;
|
|
name: string;
|
|
pluginId: string;
|
|
deploymentTargetId: string;
|
|
runEndpointId: string;
|
|
profileKey: string;
|
|
bindings: Record<string, string>;
|
|
createInputs: Record<string, string>;
|
|
deploymentMode: ServerDeploymentMode;
|
|
serverRoot: string;
|
|
workingDirectory: string;
|
|
installCommand: string;
|
|
startCommand: string;
|
|
stopCommand: string;
|
|
statusCommand: string;
|
|
shell: "" | "posix-sh" | "powershell" | "cmd";
|
|
}
|
|
|
|
export interface RuntimeBindingField {
|
|
key: string;
|
|
required: boolean;
|
|
sensitive: boolean;
|
|
}
|
|
|
|
export interface ServerWorkflowActionState {
|
|
label: ServerLifecycleActionLabel;
|
|
serverInstanceId?: string;
|
|
success: boolean;
|
|
message: string;
|
|
}
|
|
|
|
export interface ServerMetadataFormState {
|
|
name: string;
|
|
}
|
|
|
|
export interface ServerRemovalConfirmationState {
|
|
action: ServerRemovalAction;
|
|
serverInstanceId: string;
|
|
name: string;
|
|
state: ServerInstanceState;
|
|
}
|
|
|
|
export interface ServerManagementSummary {
|
|
total: number;
|
|
running: number;
|
|
pendingJobs: number;
|
|
failed: number;
|
|
}
|
|
|
|
export const emptyServerCreateForm: ServerCreateFormState = {
|
|
id: "",
|
|
name: "",
|
|
pluginId: "",
|
|
deploymentTargetId: "",
|
|
runEndpointId: "",
|
|
profileKey: "",
|
|
bindings: {},
|
|
createInputs: {},
|
|
deploymentMode: "guided-install",
|
|
serverRoot: "",
|
|
workingDirectory: "",
|
|
installCommand: "",
|
|
startCommand: "",
|
|
stopCommand: "",
|
|
statusCommand: "",
|
|
shell: ""
|
|
};
|
|
|
|
export function summarizeServerManagement(instances: ServerInstanceResponse[], jobs: JobResponse[]): ServerManagementSummary {
|
|
return {
|
|
total: instances.length,
|
|
running: instances.filter((instance) => instance.state === "running").length,
|
|
pendingJobs: jobs.filter((job) => isPendingJobState(job.state)).length,
|
|
failed: instances.filter((instance) => instance.state === "failed").length
|
|
};
|
|
}
|
|
|
|
export function pendingJobsForServer(jobs: JobResponse[], serverInstanceId: string): JobResponse[] {
|
|
return jobs.filter((job) => job.serverInstanceId === serverInstanceId && isPendingJobState(job.state));
|
|
}
|
|
|
|
export function pluginLabel(plugin: GamePluginResponse | undefined, pluginId: string): string {
|
|
if (!plugin) {
|
|
return pluginId;
|
|
}
|
|
return plugin.serverDisplayName || plugin.name || plugin.id;
|
|
}
|
|
|
|
export function endpointLabel(endpoint: RunEndpointResponse | undefined, runEndpointId: string): string {
|
|
if (!endpoint) {
|
|
return runEndpointId;
|
|
}
|
|
return endpoint.displayName || endpoint.id;
|
|
}
|
|
|
|
export function canStartServer(state: ServerInstanceState): boolean {
|
|
return state === "ready" || state === "stopped";
|
|
}
|
|
|
|
export function canStopServer(state: ServerInstanceState): boolean {
|
|
return state === "running";
|
|
}
|
|
|
|
export function isPendingJobState(state: JobResponse["state"]): boolean {
|
|
return state === "queued" || state === "accepted" || state === "running" || state === "retrying";
|
|
}
|
|
|
|
export function defaultServerCreateForm(plugins: GamePluginResponse[], endpoints: RunEndpointResponse[]): ServerCreateFormState {
|
|
const plugin = plugins[0];
|
|
return {
|
|
...emptyServerCreateForm,
|
|
pluginId: plugin?.id ?? "",
|
|
profileKey: plugin?.runtimeProfiles?.lifecycleProfiles?.[0]?.key ?? "",
|
|
runEndpointId: "",
|
|
createInputs: pluginCreateInputDefaults(plugin)
|
|
};
|
|
}
|
|
|
|
export function pluginCreateInputDefaults(plugin: GamePluginResponse | undefined): Record<string, string> {
|
|
return Object.fromEntries((plugin?.createFields ?? []).map((field) => [field.key, field.defaultValue ?? ""]));
|
|
}
|
|
|
|
export function runtimeBindingFields(plugin: GamePluginResponse | undefined, profileKey: string): RuntimeBindingField[] {
|
|
const profiles = plugin?.runtimeProfiles;
|
|
const lifecycle = profiles?.lifecycleProfiles?.find((profile) => profile.key === profileKey);
|
|
if (!profiles || !lifecycle) return [];
|
|
const fields = new Map<string, RuntimeBindingField>();
|
|
const add = (key: string | undefined, required: boolean) => {
|
|
if (!key) return;
|
|
const current = fields.get(key);
|
|
fields.set(key, { key, required: required || current?.required === true, sensitive: runtimeBindingKeyIsSensitive(key) });
|
|
};
|
|
profiles.discovery?.forEach((probe) => add(probe.targetKey, probe.required === true));
|
|
profiles.dependencyProbes?.forEach((probe) => add(probe.targetKey, probe.required === true));
|
|
profiles.logSources?.forEach((source) => add(source.targetKey, Boolean(source.targetKey)));
|
|
profiles.installPlans?.forEach((plan) => plan.steps.forEach((step) => add(step.targetKey, false)));
|
|
profiles.transportProfiles?.filter((transport) => lifecycle.transportKeys?.includes(transport.key)).forEach((transport) => add(transport.targetKey || transport.key, true));
|
|
add(lifecycle.clientManagerRef, Boolean(lifecycle.clientManagerRef));
|
|
return [...fields.values()].sort((left, right) => left.key.localeCompare(right.key));
|
|
}
|
|
|
|
export function runtimeBindingKeyIsSensitive(key: string): boolean {
|
|
const normalized = key.toLowerCase();
|
|
return ["password", "credential", "secret", "token", "dsn"].some((part) => normalized.includes(part));
|
|
}
|
|
|
|
export function serverMetadataFormFromInstance(instance: ServerInstanceResponse): ServerMetadataFormState {
|
|
return { name: instance.name };
|
|
}
|
|
|
|
export function canDeleteServer(state: ServerInstanceState): boolean {
|
|
return state !== "deleted";
|
|
}
|