File size: 9,629 Bytes
f52d137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import {
  Logout as LogoutIcon,
  Refresh as RefreshIcon,
  UploadFile as UploadFileIcon,
  Warning as WarningIcon,
} from "@mui/icons-material";
import {
  Alert,
  AppBar,
  Box,
  Button,
  CircularProgress,
  Collapse,
  Divider,
  Stack,
  Toolbar,
  Tooltip,
  Typography,
} from "@mui/material";
import React, { useCallback, useEffect, useState } from "react";
import { FormData } from "../types";
import { ModelProviderSelector } from "./ModelProviderSelector";
import { McpConfigurationWarningDialog } from "./dialogs";
import { ServerLoadingIndicator } from "./ServerLoadingIndicator";

interface DemoViewProps {
  iframeUrl: string;
  iframeLoading: boolean;
  healthCheckProgress?: {
    attempt: number;
    maxAttempts: number;
  } | null;
  formData: FormData;
  defaultMcpFile: File | null;
  onIframeLoad: () => void;
  onRestart: () => void;
  onFormChange: (formData: FormData) => void;
  onSettingsRestart: () => void;
}

export const DemoView: React.FC<DemoViewProps> = ({
  iframeUrl,
  iframeLoading,
  healthCheckProgress,
  formData,
  onIframeLoad,
  onRestart,
  onFormChange,
  onSettingsRestart,
}) => {
  const [showWarning, setShowWarning] = useState(false);
  const [originalFormData, setOriginalFormData] = useState<FormData>(formData);
  const [dialogOpen, setDialogOpen] = useState(false);
  const [pendingFile, setPendingFile] = useState<File | null>(null);
  const [isMcpTooltipOpen, setMcpTooltipOpen] = useState(false);

  // Form validation
  const isFormValid = Boolean(
    formData.model.trim() && formData.provider.trim(),
  );

  // Update original form data when component first loads or when iframe URL changes (successful restart)
  useEffect(() => {
    setOriginalFormData(formData);
  }, [iframeUrl]); // Update when iframe URL changes, indicating successful restart

  const handleModelChange = useCallback(
    (value: string) => {
      onFormChange({ ...formData, model: value });
      setShowWarning(true);
    },
    [formData, onFormChange],
  );

  const handleProviderChange = useCallback(
    (value: string) => {
      // When provider changes, clear the model as well to avoid stale selections
      onFormChange({ ...formData, provider: value, model: "" });
      setShowWarning(true);
    },
    [formData, onFormChange],
  );

  const handleFileChange = useCallback(
    (event: React.ChangeEvent<HTMLInputElement>) => {
      const file = event.target.files?.[0] || null;
      if (file) {
        setPendingFile(file);
        setDialogOpen(true);
      } else {
        onFormChange({ ...formData, mcpFile: null });
        setShowWarning(true);
      }
    },
    [formData, onFormChange],
  );

  const handleDialogClose = useCallback(() => {
    setDialogOpen(false);
    setPendingFile(null);
    // Clear the file input element to reset the UI
    const fileInput = document.querySelector(
      'input[type="file"]',
    ) as HTMLInputElement;
    if (fileInput) {
      fileInput.value = "";
    }
  }, []);

  const handleDialogConfirm = useCallback(() => {
    if (pendingFile) {
      onFormChange({ ...formData, mcpFile: pendingFile });
      setShowWarning(true);
    }
    setDialogOpen(false);
    setPendingFile(null);
  }, [formData, onFormChange, pendingFile]);

  const handleSettingsRestartClick = () => {
    setShowWarning(false);
    // Update the original form data to the current form data after restart
    setOriginalFormData(formData);
    onSettingsRestart();
  };

  const handleWarningDismiss = () => {
    setShowWarning(false);
    // Reset form data back to original values
    onFormChange(originalFormData);
  };

  return (
    <Box sx={{ minHeight: "100vh", bgcolor: "background.default" }}>
      {/* Top Bar with Settings and Restart Button */}
      <AppBar position="static" color="default" elevation={1}>
        <Toolbar sx={{ px: 2, py: 1 }}>
          <Box sx={{ display: "flex", alignItems: "center", gap: 1 }}>
            <Typography
              variant="body1"
              component="div"
              sx={{ fontWeight: 500 }}
            >
              Meta Agents Research Environments
            </Typography>
          </Box>

          {/* Spacer to push form to the right */}
          <Box sx={{ flexGrow: 1 }} />

          {/* Settings Form */}
          <Box sx={{ display: "flex", alignItems: "center", gap: 0.5 }}>
            {/* Model and Provider Selection */}
            <ModelProviderSelector
              model={formData.model}
              provider={formData.provider}
              onModelChange={handleModelChange}
              onProviderChange={handleProviderChange}
              variant="toolbar"
              size="small"
              showValidation={true}
            />

            {/* MCP File Input with Tooltip */}
            <Box
              sx={{
                display: "flex",
                alignItems: "center",
                gap: 0.5,
                width: "100%",
                maxWidth: 500,
              }}
            >
              <Tooltip
                title="Upload an MCP (Model Context Protocol) file (.json) that defines the tools and capabilities for your agent."
                placement="left"
                open={isMcpTooltipOpen}
              >
                <span
                  style={{ width: "200px" }}
                  onMouseEnter={() => setMcpTooltipOpen(true)}
                  onMouseLeave={() => setMcpTooltipOpen(false)}
                  onClick={() => setMcpTooltipOpen(false)}
                >
                  <Button
                    variant="outlined"
                    component="label"
                    startIcon={<UploadFileIcon fontSize="inherit" />}
                    fullWidth
                    sx={{
                      justifyContent: "flex-start",
                      textAlign: "left",
                      height: 40,
                      borderColor: (theme) => theme.palette.grey[700],
                      "&:hover": {
                        borderColor: (theme) => theme.palette.action.active,
                      },
                    }}
                    color="inherit"
                  >
                    <Box
                      sx={{
                        overflow: "hidden",
                        textOverflow: "ellipsis",
                        whiteSpace: "nowrap",
                        width: "100%",
                      }}
                    >
                      {formData.mcpFile
                        ? `${formData.mcpFile.name}`
                        : "MCP File"}
                    </Box>
                    <input
                      type="file"
                      hidden
                      accept=".json"
                      onChange={handleFileChange}
                    />
                  </Button>
                </span>
              </Tooltip>
            </Box>
            <Divider orientation="vertical" flexItem />
            {/* Restart Button */}

            <Button
              variant="outlined"
              size="small"
              startIcon={<LogoutIcon />}
              onClick={onRestart}
              color="inherit"
              sx={{ height: 40, opacity: 0.7 }}
              fullWidth
            >
              Exit demo
            </Button>
          </Box>
        </Toolbar>
      </AppBar>

      {/* Warning Alert */}
      <Collapse in={showWarning}>
        <Alert
          severity="warning"
          variant="filled"
          icon={<WarningIcon />}
          action={
            <Stack spacing={1} direction="row" alignItems={"center"}>
              <Button
                variant="text"
                size="small"
                onClick={handleWarningDismiss}
                color="inherit"
              >
                Cancel
              </Button>
              <Button
                variant="contained"
                size="small"
                startIcon={<RefreshIcon />}
                onClick={handleSettingsRestartClick}
                color="warning"
                disabled={!isFormValid}
              >
                Restart demo with changes
              </Button>
            </Stack>
          }
          sx={{ borderTopLeftRadius: 0, borderTopRightRadius: 0, pl: 3, pr: 4 }}
        >
          You've made changes to the configuration. Click "Restart demo with
          changes" to apply them.
        </Alert>
      </Collapse>

      {/* Iframe Content */}
      <Box
        sx={{
          height: showWarning ? "calc(100vh - 112px)" : "calc(100vh - 64px)",
          position: "relative",
          transition: "height 0.3s ease",
        }}
      >
        {iframeLoading ? (
          <Box
            sx={{
              position: "absolute",
              top: "50%",
              left: "50%",
              transform: "translate(-50%, -50%)",
              zIndex: 3,
            }}
          >
            <ServerLoadingIndicator
              progress={healthCheckProgress}
              message="Waiting for server to start..."
            />
          </Box>
        ) : (
          <iframe
            src={iframeUrl}
            style={{
              width: "100%",
              height: "100%",
              border: "none",
              display: "block",
            }}
            onLoad={onIframeLoad}
            title="Demo Application"
          />
        )}
      </Box>

      {/* MCP File Upload Warning Dialog */}
      <McpConfigurationWarningDialog
        open={dialogOpen}
        onClose={handleDialogClose}
        onConfirm={handleDialogConfirm}
      />
    </Box>
  );
};