File size: 5,159 Bytes
15c3607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<script lang="ts">
	import * as Card from '$lib/components/ui/card';
	import { Badge } from '$lib/components/ui/badge';
	import { Skeleton } from '$lib/components/ui/skeleton';
	import { Switch } from '$lib/components/ui/switch';
	import * as Tooltip from '$lib/components/ui/tooltip';
	import { McpServerIdentity } from '$lib/components/app/mcp';
	import { mcpStore } from '$lib/stores/mcp.svelte';
	import { HealthCheckStatus } from '$lib/enums';
	import type { MCPServerDisplayInfo, HealthCheckState, MCPServerSettingsEntry } from '$lib/types';
	import { onMount } from 'svelte';
	import { MCP_CARD_VISIBLE_TOOL_LIMIT, NEWLINE } from '$lib/constants';

	interface Props {
		server: MCPServerDisplayInfo & { description?: string };
		enabled?: boolean;
		onToggle?: (enabled: boolean) => void;
	}

	let { server, enabled = false, onToggle }: Props = $props();

	onMount(() => {
		const state = mcpStore.getHealthCheckState(server.id);

		if (state.status === HealthCheckStatus.IDLE) {
			mcpStore.runHealthCheck(server as MCPServerSettingsEntry).catch(() => {});
		}
	});

	let healthState = $derived<HealthCheckState>(mcpStore.getHealthCheckState(server.id));
	let displayName = $derived(mcpStore.getServerLabel(server));
	let faviconUrl = $derived(mcpStore.getServerFavicon(server.id));
	let isIdle = $derived(healthState.status === HealthCheckStatus.IDLE);
	let isHealthChecking = $derived(healthState.status === HealthCheckStatus.CONNECTING);
	let isError = $derived(healthState.status === HealthCheckStatus.ERROR);
	let errorMessage = $derived(
		healthState.status === HealthCheckStatus.ERROR ? healthState.message : undefined
	);
	let serverInfo = $derived(
		healthState.status === HealthCheckStatus.SUCCESS ? healthState.serverInfo : undefined
	);
	let tools = $derived(healthState.status === HealthCheckStatus.SUCCESS ? healthState.tools : []);
	let instructions = $derived(
		healthState.status === HealthCheckStatus.SUCCESS ? healthState.instructions : undefined
	);
	let showSkeleton = $derived(isIdle || isHealthChecking);

	// Curated descriptions get two lines; instructions fallback is one line so the
	// compact card stays scannable.
	let description = $derived.by(() => {
		if (server.description) {
			return { text: server.description, lines: 2 };
		}
		if (!instructions) return null;
		const firstLine = instructions.split(NEWLINE).find((line: string) => line.trim().length > 0);
		const trimmed = firstLine?.trim();
		return trimmed ? { text: trimmed, lines: 1 } : null;
	});

	let visibleTools = $derived(tools.slice(0, MCP_CARD_VISIBLE_TOOL_LIMIT));
	let hiddenTools = $derived(tools.slice(MCP_CARD_VISIBLE_TOOL_LIMIT));
	let hiddenToolCount = $derived(hiddenTools.length);

	function handleToggle(checked: boolean) {
		onToggle?.(checked);
	}
</script>

<Card.Root class="!gap-3 bg-muted/30 p-4">
	<div class="flex items-start justify-between gap-3">
		<div class="min-w-0 flex-1">
			{#if showSkeleton}
				<span class="flex min-w-0 items-center gap-1.5">
					<Skeleton class="h-5 w-5 rounded" />
					<Skeleton class="h-4 w-32" />
				</span>
			{:else}
				<McpServerIdentity
					{displayName}
					{faviconUrl}
					{serverInfo}
					iconClass="h-5 w-5"
					iconRounded="rounded"
					nameClass="font-medium"
				/>
			{/if}
		</div>

		<Switch checked={enabled} disabled={isError || showSkeleton} onCheckedChange={handleToggle} />
	</div>

	{#if isError && errorMessage}
		<p class="text-xs text-destructive">{errorMessage}</p>
	{/if}

	{#if showSkeleton}
		<div class="space-y-1.5">
			<Skeleton class="h-3 w-full max-w-md" />
		</div>

		<div class="flex flex-wrap items-center gap-1.5">
			<Skeleton class="h-5 w-16 rounded-full" />
			<Skeleton class="h-5 w-20 rounded-full" />
			<Skeleton class="h-5 w-24 rounded-full" />
			<Skeleton class="h-5 w-14 rounded-full" />
		</div>
	{:else}
		{#if description}
			{#if description.lines === 2}
				<p class="line-clamp-2 text-xs text-muted-foreground" title={description.text}>
					{description.text}
				</p>
			{:else}
				<p class="line-clamp-1 truncate text-xs text-muted-foreground" title={description.text}>
					{description.text}
				</p>
			{/if}
		{/if}

		{#if tools.length > 0}
			<div class="flex flex-wrap items-center gap-1.5">
				{#each visibleTools as tool (tool.name)}
					<Tooltip.Root>
						<Tooltip.Trigger>
							<Badge variant="secondary" class="h-5 max-w-40 px-2 text-[11px]">
								<span class="block min-w-0 flex-1 truncate">{tool.name}</span>
							</Badge>
						</Tooltip.Trigger>

						<Tooltip.Content>
							<p class="max-w-xs text-xs">
								{tool.description ?? 'No description'}
							</p>
						</Tooltip.Content>
					</Tooltip.Root>
				{/each}

				{#if hiddenToolCount > 0}
					<Tooltip.Root>
						<Tooltip.Trigger>
							<Badge variant="secondary" class="h-5 px-2 text-[11px] text-muted-foreground">
								+ {hiddenToolCount} more tools
							</Badge>
						</Tooltip.Trigger>

						<Tooltip.Content class="max-w-md">
							<p class="text-xs">
								{hiddenTools.map((tool) => tool.name).join(', ')}
							</p>
						</Tooltip.Content>
					</Tooltip.Root>
				{/if}
			</div>
		{/if}
	{/if}
</Card.Root>