sidebar fix
This commit is contained in:
@ -7,8 +7,8 @@ import {
|
||||
IconStarFilled
|
||||
} from '@tabler/icons-react';
|
||||
import { FunctionList } from './components/FunctionList';
|
||||
import { FunctionForm } from './components/FunctionForm';
|
||||
import { ExecutionModal } from './components/ExecutionModal';
|
||||
import { FunctionSidebar } from './components/FunctionSidebar';
|
||||
import { ExecutionSidebar } from './components/ExecutionSidebar';
|
||||
import ExecutionList from './components/ExecutionList';
|
||||
import { FunctionDefinition } from './types';
|
||||
|
||||
@ -24,8 +24,8 @@ const App: React.FC = () => {
|
||||
const [currentRoute, setCurrentRoute] = useState(getCurrentRoute());
|
||||
const [isFavorited, setIsFavorited] = useState(false);
|
||||
const [selectedColor, setSelectedColor] = useState('');
|
||||
const [functionFormOpened, setFunctionFormOpened] = useState(false);
|
||||
const [executionModalOpened, setExecutionModalOpened] = useState(false);
|
||||
const [functionSidebarOpened, setFunctionSidebarOpened] = useState(false);
|
||||
const [executionSidebarOpened, setExecutionSidebarOpened] = useState(false);
|
||||
const [editingFunction, setEditingFunction] = useState<FunctionDefinition | null>(null);
|
||||
const [executingFunction, setExecutingFunction] = useState<FunctionDefinition | null>(null);
|
||||
const [refreshKey, setRefreshKey] = useState(0);
|
||||
@ -57,30 +57,30 @@ const App: React.FC = () => {
|
||||
|
||||
const handleCreateFunction = () => {
|
||||
setEditingFunction(null);
|
||||
setFunctionFormOpened(true);
|
||||
setFunctionSidebarOpened(true);
|
||||
};
|
||||
|
||||
const handleEditFunction = (func: FunctionDefinition) => {
|
||||
setEditingFunction(func);
|
||||
setFunctionFormOpened(true);
|
||||
setFunctionSidebarOpened(true);
|
||||
};
|
||||
|
||||
const handleExecuteFunction = (func: FunctionDefinition) => {
|
||||
setExecutingFunction(func);
|
||||
setExecutionModalOpened(true);
|
||||
setExecutionSidebarOpened(true);
|
||||
};
|
||||
|
||||
const handleFormSuccess = () => {
|
||||
setRefreshKey(prev => prev + 1);
|
||||
};
|
||||
|
||||
const handleFormClose = () => {
|
||||
setFunctionFormOpened(false);
|
||||
const handleSidebarClose = () => {
|
||||
setFunctionSidebarOpened(false);
|
||||
setEditingFunction(null);
|
||||
};
|
||||
|
||||
const handleExecutionClose = () => {
|
||||
setExecutionModalOpened(false);
|
||||
setExecutionSidebarOpened(false);
|
||||
setExecutingFunction(null);
|
||||
};
|
||||
|
||||
@ -124,8 +124,15 @@ const App: React.FC = () => {
|
||||
};
|
||||
|
||||
return (
|
||||
<Box w="100%" pos="relative">
|
||||
<Stack gap="lg">
|
||||
<>
|
||||
<Stack
|
||||
gap="lg"
|
||||
style={{
|
||||
transition: 'margin-right 0.3s ease',
|
||||
marginRight: (functionSidebarOpened || executionSidebarOpened) ?
|
||||
(functionSidebarOpened ? '500px' : '600px') : '0',
|
||||
}}
|
||||
>
|
||||
<div>
|
||||
<Group justify="space-between" align="flex-start">
|
||||
<div>
|
||||
@ -186,19 +193,19 @@ const App: React.FC = () => {
|
||||
</Tabs>
|
||||
</Stack>
|
||||
|
||||
<FunctionForm
|
||||
opened={functionFormOpened}
|
||||
onClose={handleFormClose}
|
||||
<FunctionSidebar
|
||||
opened={functionSidebarOpened}
|
||||
onClose={handleSidebarClose}
|
||||
onSuccess={handleFormSuccess}
|
||||
editFunction={editingFunction}
|
||||
/>
|
||||
|
||||
<ExecutionModal
|
||||
opened={executionModalOpened}
|
||||
<ExecutionSidebar
|
||||
opened={executionSidebarOpened}
|
||||
onClose={handleExecutionClose}
|
||||
function={executingFunction}
|
||||
/>
|
||||
</Box>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
457
faas/web/src/components/ExecutionSidebar.tsx
Normal file
457
faas/web/src/components/ExecutionSidebar.tsx
Normal file
@ -0,0 +1,457 @@
|
||||
import React, { useState, useEffect, useRef } from 'react';
|
||||
import {
|
||||
Paper,
|
||||
Button,
|
||||
Group,
|
||||
Stack,
|
||||
Text,
|
||||
Textarea,
|
||||
Switch,
|
||||
Alert,
|
||||
Badge,
|
||||
Divider,
|
||||
JsonInput,
|
||||
Loader,
|
||||
ActionIcon,
|
||||
Tooltip,
|
||||
Title,
|
||||
ScrollArea,
|
||||
Box,
|
||||
} from '@mantine/core';
|
||||
import { IconPlayerPlay, IconPlayerStop, IconRefresh, IconCopy, IconX } from '@tabler/icons-react';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import { functionApi, executionApi } from '../services/apiService';
|
||||
import { FunctionDefinition, ExecuteFunctionResponse, FunctionExecution } from '../types';
|
||||
|
||||
interface ExecutionSidebarProps {
|
||||
opened: boolean;
|
||||
onClose: () => void;
|
||||
function: FunctionDefinition | null;
|
||||
}
|
||||
|
||||
export const ExecutionSidebar: React.FC<ExecutionSidebarProps> = ({
|
||||
opened,
|
||||
onClose,
|
||||
function: func,
|
||||
}) => {
|
||||
const [input, setInput] = useState('{}');
|
||||
const [async, setAsync] = useState(false);
|
||||
const [executing, setExecuting] = useState(false);
|
||||
const [result, setResult] = useState<ExecuteFunctionResponse | null>(null);
|
||||
const [execution, setExecution] = useState<FunctionExecution | null>(null);
|
||||
const [logs, setLogs] = useState<string[]>([]);
|
||||
const [loadingLogs, setLoadingLogs] = useState(false);
|
||||
const [autoRefreshLogs, setAutoRefreshLogs] = useState(false);
|
||||
const pollIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
||||
const logsPollIntervalRef = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
const stopLogsAutoRefresh = () => {
|
||||
if (logsPollIntervalRef.current) {
|
||||
clearInterval(logsPollIntervalRef.current);
|
||||
logsPollIntervalRef.current = null;
|
||||
}
|
||||
setAutoRefreshLogs(false);
|
||||
};
|
||||
|
||||
// Cleanup intervals on unmount or when sidebar closes
|
||||
useEffect(() => {
|
||||
if (!opened) {
|
||||
// Stop auto-refresh when sidebar closes
|
||||
stopLogsAutoRefresh();
|
||||
if (pollIntervalRef.current) {
|
||||
clearTimeout(pollIntervalRef.current);
|
||||
}
|
||||
}
|
||||
}, [opened]);
|
||||
|
||||
// Cleanup intervals on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (pollIntervalRef.current) {
|
||||
clearTimeout(pollIntervalRef.current);
|
||||
}
|
||||
if (logsPollIntervalRef.current) {
|
||||
clearInterval(logsPollIntervalRef.current);
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
if (!func) return null;
|
||||
|
||||
const handleExecute = async () => {
|
||||
try {
|
||||
setExecuting(true);
|
||||
setResult(null);
|
||||
setExecution(null);
|
||||
setLogs([]);
|
||||
|
||||
let parsedInput;
|
||||
try {
|
||||
parsedInput = input.trim() ? JSON.parse(input) : undefined;
|
||||
} catch (e) {
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: 'Invalid JSON input',
|
||||
color: 'red',
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await functionApi.execute(func.id, {
|
||||
input: parsedInput,
|
||||
async,
|
||||
});
|
||||
|
||||
setResult(response.data);
|
||||
|
||||
if (async) {
|
||||
// Poll for execution status and start auto-refreshing logs
|
||||
pollExecution(response.data.execution_id);
|
||||
} else {
|
||||
// For synchronous executions, load logs immediately
|
||||
if (response.data.execution_id) {
|
||||
loadLogs(response.data.execution_id);
|
||||
}
|
||||
}
|
||||
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
message: `Function ${async ? 'invoked' : 'executed'} successfully`,
|
||||
color: 'green',
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Execution error:', error);
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: 'Failed to execute function',
|
||||
color: 'red',
|
||||
});
|
||||
} finally {
|
||||
setExecuting(false);
|
||||
}
|
||||
};
|
||||
|
||||
const pollExecution = async (executionId: string) => {
|
||||
// Start auto-refreshing logs immediately for async executions
|
||||
startLogsAutoRefresh(executionId);
|
||||
|
||||
const poll = async () => {
|
||||
try {
|
||||
const response = await executionApi.getById(executionId);
|
||||
setExecution(response.data);
|
||||
|
||||
if (response.data.status === 'running' || response.data.status === 'pending') {
|
||||
pollIntervalRef.current = setTimeout(poll, 2000); // Poll every 2 seconds
|
||||
} else {
|
||||
// Execution completed, stop auto-refresh and load final logs
|
||||
stopLogsAutoRefresh();
|
||||
loadLogs(executionId);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error polling execution:', error);
|
||||
stopLogsAutoRefresh();
|
||||
}
|
||||
};
|
||||
|
||||
poll();
|
||||
};
|
||||
|
||||
const loadLogs = async (executionId: string) => {
|
||||
try {
|
||||
console.debug(`[ExecutionSidebar] Loading logs for execution ${executionId}`);
|
||||
setLoadingLogs(true);
|
||||
const response = await executionApi.getLogs(executionId);
|
||||
console.debug(`[ExecutionSidebar] Loaded logs for execution ${executionId}:`, {
|
||||
logCount: response.data.logs?.length || 0,
|
||||
logs: response.data.logs
|
||||
});
|
||||
setLogs(response.data.logs || []);
|
||||
} catch (error) {
|
||||
console.error(`[ExecutionSidebar] Error loading logs for execution ${executionId}:`, error);
|
||||
} finally {
|
||||
setLoadingLogs(false);
|
||||
}
|
||||
};
|
||||
|
||||
const startLogsAutoRefresh = (executionId: string) => {
|
||||
console.debug(`[ExecutionSidebar] Starting auto-refresh for execution ${executionId}`);
|
||||
|
||||
// Clear any existing interval
|
||||
if (logsPollIntervalRef.current) {
|
||||
clearInterval(logsPollIntervalRef.current);
|
||||
}
|
||||
|
||||
setAutoRefreshLogs(true);
|
||||
|
||||
// Load logs immediately
|
||||
loadLogs(executionId);
|
||||
|
||||
// Set up auto-refresh every 2 seconds
|
||||
logsPollIntervalRef.current = setInterval(async () => {
|
||||
try {
|
||||
console.debug(`[ExecutionSidebar] Auto-refreshing logs for execution ${executionId}`);
|
||||
const response = await executionApi.getLogs(executionId);
|
||||
console.debug(`[ExecutionSidebar] Auto-refresh got logs for execution ${executionId}:`, {
|
||||
logCount: response.data.logs?.length || 0,
|
||||
logs: response.data.logs
|
||||
});
|
||||
setLogs(response.data.logs || []);
|
||||
} catch (error) {
|
||||
console.error(`[ExecutionSidebar] Error auto-refreshing logs for execution ${executionId}:`, error);
|
||||
}
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
const handleCancel = async () => {
|
||||
if (result && async) {
|
||||
try {
|
||||
await executionApi.cancel(result.execution_id);
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
message: 'Execution canceled',
|
||||
color: 'orange',
|
||||
});
|
||||
// Refresh execution status
|
||||
if (result.execution_id) {
|
||||
const response = await executionApi.getById(result.execution_id);
|
||||
setExecution(response.data);
|
||||
}
|
||||
} catch (error) {
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: 'Failed to cancel execution',
|
||||
color: 'red',
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const getStatusColor = (status: string) => {
|
||||
switch (status) {
|
||||
case 'completed': return 'green';
|
||||
case 'failed': return 'red';
|
||||
case 'running': return 'blue';
|
||||
case 'pending': return 'yellow';
|
||||
case 'canceled': return 'orange';
|
||||
case 'timeout': return 'red';
|
||||
default: return 'gray';
|
||||
}
|
||||
};
|
||||
|
||||
const copyToClipboard = (text: string) => {
|
||||
navigator.clipboard.writeText(text);
|
||||
notifications.show({
|
||||
title: 'Copied',
|
||||
message: 'Copied to clipboard',
|
||||
color: 'green',
|
||||
});
|
||||
};
|
||||
|
||||
return (
|
||||
<Paper
|
||||
style={{
|
||||
position: 'fixed',
|
||||
top: 60, // Below header
|
||||
right: opened ? 0 : '-600px',
|
||||
bottom: 0,
|
||||
width: '600px',
|
||||
zIndex: 1000,
|
||||
borderRadius: 0,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
borderLeft: '1px solid var(--mantine-color-gray-3)',
|
||||
backgroundColor: 'var(--mantine-color-body)',
|
||||
transition: 'right 0.3s ease',
|
||||
}}
|
||||
>
|
||||
{/* Header */}
|
||||
<Group justify="space-between" p="md" style={{ borderBottom: '1px solid var(--mantine-color-gray-3)' }}>
|
||||
<Title order={4}>
|
||||
Execute Function: {func.name}
|
||||
</Title>
|
||||
<ActionIcon
|
||||
variant="subtle"
|
||||
color="gray"
|
||||
onClick={onClose}
|
||||
>
|
||||
<IconX size={18} />
|
||||
</ActionIcon>
|
||||
</Group>
|
||||
|
||||
{/* Content */}
|
||||
<ScrollArea style={{ flex: 1 }}>
|
||||
<Box p="md">
|
||||
<Stack gap="md">
|
||||
{/* Function Info */}
|
||||
<Paper withBorder p="sm" bg="gray.0">
|
||||
<Group justify="space-between">
|
||||
<div>
|
||||
<Text size="sm" fw={500}>{func.name}</Text>
|
||||
<Text size="xs" c="dimmed">{func.runtime} • {func.memory}MB • {func.timeout}</Text>
|
||||
</div>
|
||||
<Badge variant="light">
|
||||
{func.runtime}
|
||||
</Badge>
|
||||
</Group>
|
||||
</Paper>
|
||||
|
||||
{/* Input */}
|
||||
<JsonInput
|
||||
label="Function Input (JSON)"
|
||||
placeholder='{"key": "value"}'
|
||||
value={input}
|
||||
onChange={setInput}
|
||||
minRows={4}
|
||||
maxRows={8}
|
||||
validationError="Invalid JSON"
|
||||
/>
|
||||
|
||||
{/* Execution Options */}
|
||||
<Group justify="space-between">
|
||||
<Switch
|
||||
label="Asynchronous execution"
|
||||
description="Execute in background"
|
||||
checked={async}
|
||||
onChange={(event) => setAsync(event.currentTarget.checked)}
|
||||
/>
|
||||
<Group>
|
||||
<Button
|
||||
leftSection={<IconPlayerPlay size={16} />}
|
||||
onClick={handleExecute}
|
||||
loading={executing}
|
||||
disabled={executing}
|
||||
>
|
||||
{async ? 'Invoke' : 'Execute'}
|
||||
</Button>
|
||||
{result && async && execution?.status === 'running' && (
|
||||
<Button
|
||||
leftSection={<IconPlayerStop size={16} />}
|
||||
color="orange"
|
||||
variant="light"
|
||||
onClick={handleCancel}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
)}
|
||||
</Group>
|
||||
</Group>
|
||||
|
||||
{/* Results */}
|
||||
{result && (
|
||||
<>
|
||||
<Divider label="Execution Result" labelPosition="center" />
|
||||
|
||||
<Paper withBorder p="md">
|
||||
<Group justify="space-between" mb="sm">
|
||||
<Text fw={500}>Execution #{result.execution_id.slice(0, 8)}...</Text>
|
||||
<Group gap="xs">
|
||||
<Badge color={getStatusColor(execution?.status || result.status)}>
|
||||
{execution?.status || result.status}
|
||||
</Badge>
|
||||
{result.duration && (
|
||||
<Badge variant="light">
|
||||
{result.duration}ms
|
||||
</Badge>
|
||||
)}
|
||||
{result.memory_used && (
|
||||
<Badge variant="light">
|
||||
{result.memory_used}MB
|
||||
</Badge>
|
||||
)}
|
||||
</Group>
|
||||
</Group>
|
||||
|
||||
{/* Output */}
|
||||
{(result.output || execution?.output) && (
|
||||
<div>
|
||||
<Group justify="space-between" mb="xs">
|
||||
<Text size="sm" fw={500}>Output:</Text>
|
||||
<Tooltip label="Copy output">
|
||||
<ActionIcon
|
||||
variant="light"
|
||||
size="sm"
|
||||
onClick={() => copyToClipboard(JSON.stringify(result.output || execution?.output, null, 2))}
|
||||
>
|
||||
<IconCopy size={14} />
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
</Group>
|
||||
<Paper bg="gray.1" p="sm">
|
||||
<Text size="sm" component="pre" style={{ whiteSpace: 'pre-wrap' }}>
|
||||
{JSON.stringify(result.output || execution?.output, null, 2)}
|
||||
</Text>
|
||||
</Paper>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Error */}
|
||||
{(result.error || execution?.error) && (
|
||||
<Alert color="red" mt="sm">
|
||||
<Text size="sm">{result.error || execution?.error}</Text>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
{/* Logs */}
|
||||
<div style={{ marginTop: '1rem' }}>
|
||||
<Group justify="space-between" mb="xs">
|
||||
<Group gap="xs">
|
||||
<Text size="sm" fw={500}>Logs:</Text>
|
||||
{autoRefreshLogs && (
|
||||
<Badge size="xs" color="blue" variant="light">
|
||||
Auto-refreshing
|
||||
</Badge>
|
||||
)}
|
||||
</Group>
|
||||
<Group gap="xs">
|
||||
{result.execution_id && (
|
||||
<Button
|
||||
size="xs"
|
||||
variant={autoRefreshLogs ? "filled" : "light"}
|
||||
color={autoRefreshLogs ? "red" : "blue"}
|
||||
leftSection={<IconRefresh size={12} />}
|
||||
onClick={() => {
|
||||
if (autoRefreshLogs) {
|
||||
stopLogsAutoRefresh();
|
||||
} else {
|
||||
startLogsAutoRefresh(result.execution_id);
|
||||
}
|
||||
}}
|
||||
>
|
||||
{autoRefreshLogs ? 'Stop Auto-refresh' : 'Auto-refresh'}
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
size="xs"
|
||||
variant="light"
|
||||
leftSection={<IconRefresh size={12} />}
|
||||
onClick={() => result.execution_id && loadLogs(result.execution_id)}
|
||||
loading={loadingLogs}
|
||||
disabled={autoRefreshLogs}
|
||||
>
|
||||
Manual Refresh
|
||||
</Button>
|
||||
</Group>
|
||||
</Group>
|
||||
<Paper bg="gray.9" p="sm" mah={200} style={{ overflow: 'auto' }}>
|
||||
{loadingLogs ? (
|
||||
<Group justify="center">
|
||||
<Loader size="sm" />
|
||||
</Group>
|
||||
) : (logs.length > 0 || (execution?.logs && execution.logs.length > 0)) ? (
|
||||
<Text size="xs" c="white" component="pre">
|
||||
{(execution?.logs || logs).join('\n')}
|
||||
</Text>
|
||||
) : (
|
||||
<Text size="xs" c="gray.5">No logs available</Text>
|
||||
)}
|
||||
</Paper>
|
||||
</div>
|
||||
</Paper>
|
||||
</>
|
||||
)}
|
||||
</Stack>
|
||||
</Box>
|
||||
</ScrollArea>
|
||||
</Paper>
|
||||
);
|
||||
};
|
||||
502
faas/web/src/components/FunctionSidebar.tsx
Normal file
502
faas/web/src/components/FunctionSidebar.tsx
Normal file
@ -0,0 +1,502 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import {
|
||||
Paper,
|
||||
TextInput,
|
||||
Select,
|
||||
NumberInput,
|
||||
Button,
|
||||
Group,
|
||||
Stack,
|
||||
Text,
|
||||
Divider,
|
||||
JsonInput,
|
||||
Box,
|
||||
Title,
|
||||
ActionIcon,
|
||||
ScrollArea,
|
||||
} from '@mantine/core';
|
||||
import { IconX } from '@tabler/icons-react';
|
||||
import { useForm } from '@mantine/form';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import Editor from '@monaco-editor/react';
|
||||
import { functionApi, runtimeApi } from '../services/apiService';
|
||||
import { FunctionDefinition, CreateFunctionRequest, UpdateFunctionRequest, RuntimeType } from '../types';
|
||||
|
||||
interface FunctionSidebarProps {
|
||||
opened: boolean;
|
||||
onClose: () => void;
|
||||
onSuccess: () => void;
|
||||
editFunction?: FunctionDefinition;
|
||||
}
|
||||
|
||||
export const FunctionSidebar: React.FC<FunctionSidebarProps> = ({
|
||||
opened,
|
||||
onClose,
|
||||
onSuccess,
|
||||
editFunction,
|
||||
}) => {
|
||||
const isEditing = !!editFunction;
|
||||
const [runtimeOptions, setRuntimeOptions] = useState<Array<{value: string; label: string}>>([]);
|
||||
|
||||
// Default images for each runtime
|
||||
const DEFAULT_IMAGES: Record<string, string> = {
|
||||
'nodejs18': 'node:18-alpine',
|
||||
'python3.9': 'python:3.9-alpine',
|
||||
'go1.20': 'golang:1.20-alpine',
|
||||
};
|
||||
|
||||
// Map runtime to Monaco editor language
|
||||
const getEditorLanguage = (runtime: string): string => {
|
||||
const languageMap: Record<string, string> = {
|
||||
'nodejs18': 'javascript',
|
||||
'python3.9': 'python',
|
||||
'go1.20': 'go',
|
||||
};
|
||||
return languageMap[runtime] || 'javascript';
|
||||
};
|
||||
|
||||
// Get default code template based on runtime
|
||||
const getDefaultCode = (runtime: string): string => {
|
||||
const templates: Record<string, string> = {
|
||||
'nodejs18': `exports.handler = async (event, context) => {
|
||||
console.log('Event:', JSON.stringify(event, null, 2));
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: JSON.stringify({
|
||||
message: 'Hello from Node.js!',
|
||||
timestamp: new Date().toISOString()
|
||||
})
|
||||
};
|
||||
};`,
|
||||
'python3.9': `import json
|
||||
from datetime import datetime
|
||||
|
||||
def handler(event, context):
|
||||
print('Event:', json.dumps(event, indent=2))
|
||||
|
||||
return {
|
||||
'statusCode': 200,
|
||||
'body': json.dumps({
|
||||
'message': 'Hello from Python!',
|
||||
'timestamp': datetime.now().isoformat()
|
||||
})
|
||||
}`,
|
||||
'go1.20': `package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Event map[string]interface{}
|
||||
type Response struct {
|
||||
StatusCode int \`json:"statusCode"\`
|
||||
Body string \`json:"body"\`
|
||||
}
|
||||
|
||||
func Handler(ctx context.Context, event Event) (Response, error) {
|
||||
eventJSON, _ := json.MarshalIndent(event, "", " ")
|
||||
log.Printf("Event: %s", eventJSON)
|
||||
|
||||
body := map[string]interface{}{
|
||||
"message": "Hello from Go!",
|
||||
"timestamp": time.Now().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
bodyJSON, _ := json.Marshal(body)
|
||||
|
||||
return Response{
|
||||
StatusCode: 200,
|
||||
Body: string(bodyJSON),
|
||||
}, nil
|
||||
}`
|
||||
};
|
||||
return templates[runtime] || templates['nodejs18'];
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
// Fetch available runtimes from backend
|
||||
const fetchRuntimes = async () => {
|
||||
try {
|
||||
const response = await runtimeApi.getRuntimes();
|
||||
setRuntimeOptions(response.data.runtimes || []);
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch runtimes:', error);
|
||||
// Fallback to default options
|
||||
setRuntimeOptions([
|
||||
{ value: 'nodejs18', label: 'Node.js 18.x' },
|
||||
{ value: 'python3.9', label: 'Python 3.9' },
|
||||
{ value: 'go1.20', label: 'Go 1.20' },
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
if (opened) {
|
||||
fetchRuntimes();
|
||||
}
|
||||
}, [opened]);
|
||||
|
||||
// Update form values when editFunction changes
|
||||
useEffect(() => {
|
||||
if (editFunction) {
|
||||
form.setValues({
|
||||
name: editFunction.name || '',
|
||||
app_id: editFunction.app_id || 'default',
|
||||
runtime: editFunction.runtime || 'nodejs18' as RuntimeType,
|
||||
image: editFunction.image || DEFAULT_IMAGES['nodejs18'] || '',
|
||||
handler: editFunction.handler || 'index.handler',
|
||||
code: editFunction.code || '',
|
||||
environment: editFunction.environment ? JSON.stringify(editFunction.environment, null, 2) : '{}',
|
||||
timeout: editFunction.timeout || '30s',
|
||||
memory: editFunction.memory || 128,
|
||||
owner: {
|
||||
type: editFunction.owner?.type || 'team' as const,
|
||||
name: editFunction.owner?.name || 'FaaS Team',
|
||||
owner: editFunction.owner?.owner || 'admin@example.com',
|
||||
},
|
||||
});
|
||||
} else {
|
||||
// Reset to default values when not editing
|
||||
form.setValues({
|
||||
name: '',
|
||||
app_id: 'default',
|
||||
runtime: 'nodejs18' as RuntimeType,
|
||||
image: DEFAULT_IMAGES['nodejs18'] || '',
|
||||
handler: 'index.handler',
|
||||
code: getDefaultCode('nodejs18'),
|
||||
environment: '{}',
|
||||
timeout: '30s',
|
||||
memory: 128,
|
||||
owner: {
|
||||
type: 'team' as const,
|
||||
name: 'FaaS Team',
|
||||
owner: 'admin@example.com',
|
||||
},
|
||||
});
|
||||
}
|
||||
}, [editFunction, opened]);
|
||||
|
||||
const form = useForm({
|
||||
initialValues: {
|
||||
name: '',
|
||||
app_id: 'default',
|
||||
runtime: 'nodejs18' as RuntimeType,
|
||||
image: DEFAULT_IMAGES['nodejs18'] || '',
|
||||
handler: 'index.handler',
|
||||
code: getDefaultCode('nodejs18'),
|
||||
environment: '{}',
|
||||
timeout: '30s',
|
||||
memory: 128,
|
||||
owner: {
|
||||
type: 'team' as const,
|
||||
name: 'FaaS Team',
|
||||
owner: 'admin@example.com',
|
||||
},
|
||||
},
|
||||
validate: {
|
||||
name: (value) => value.length < 1 ? 'Name is required' : null,
|
||||
app_id: (value) => value.length < 1 ? 'App ID is required' : null,
|
||||
runtime: (value) => !value ? 'Runtime is required' : null,
|
||||
image: (value) => value.length < 1 ? 'Image is required' : null,
|
||||
handler: (value) => value.length < 1 ? 'Handler is required' : null,
|
||||
timeout: (value) => !value.match(/^\d+[smh]$/) ? 'Timeout must be in format like 30s, 5m, 1h' : null,
|
||||
memory: (value) => value < 64 || value > 3008 ? 'Memory must be between 64 and 3008 MB' : null,
|
||||
},
|
||||
});
|
||||
|
||||
const handleRuntimeChange = (runtime: string | null) => {
|
||||
if (runtime && DEFAULT_IMAGES[runtime]) {
|
||||
form.setFieldValue('image', DEFAULT_IMAGES[runtime]);
|
||||
}
|
||||
form.setFieldValue('runtime', runtime as RuntimeType);
|
||||
|
||||
// If creating a new function and no code is set, provide default template
|
||||
if (!isEditing && runtime && (!form.values.code || form.values.code.trim() === '')) {
|
||||
form.setFieldValue('code', getDefaultCode(runtime));
|
||||
}
|
||||
};
|
||||
|
||||
const handleSubmit = async (values: typeof form.values) => {
|
||||
console.log('handleSubmit called with values:', values);
|
||||
console.log('Form validation errors:', form.errors);
|
||||
console.log('Is form valid?', form.isValid());
|
||||
|
||||
// Check each field individually
|
||||
const fieldNames = ['name', 'app_id', 'runtime', 'image', 'handler', 'timeout', 'memory'];
|
||||
fieldNames.forEach(field => {
|
||||
const error = form.validateField(field);
|
||||
console.log(`Field ${field} error:`, error);
|
||||
});
|
||||
|
||||
if (!form.isValid()) {
|
||||
console.log('Form is not valid, validation errors:', form.errors);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
// Parse environment variables JSON
|
||||
let parsedEnvironment;
|
||||
try {
|
||||
parsedEnvironment = values.environment ? JSON.parse(values.environment) : undefined;
|
||||
} catch (error) {
|
||||
console.error('Error parsing environment variables:', error);
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: 'Invalid JSON in environment variables',
|
||||
color: 'red',
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (isEditing && editFunction) {
|
||||
const updateData: UpdateFunctionRequest = {
|
||||
name: values.name,
|
||||
runtime: values.runtime,
|
||||
image: values.image,
|
||||
handler: values.handler,
|
||||
code: values.code || undefined,
|
||||
environment: parsedEnvironment,
|
||||
timeout: values.timeout,
|
||||
memory: values.memory,
|
||||
owner: values.owner,
|
||||
};
|
||||
await functionApi.update(editFunction.id, updateData);
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
message: 'Function updated successfully',
|
||||
color: 'green',
|
||||
});
|
||||
} else {
|
||||
const createData: CreateFunctionRequest = {
|
||||
name: values.name,
|
||||
app_id: values.app_id,
|
||||
runtime: values.runtime,
|
||||
image: values.image,
|
||||
handler: values.handler,
|
||||
code: values.code || undefined,
|
||||
environment: parsedEnvironment,
|
||||
timeout: values.timeout,
|
||||
memory: values.memory,
|
||||
owner: values.owner,
|
||||
};
|
||||
await functionApi.create(createData);
|
||||
notifications.show({
|
||||
title: 'Success',
|
||||
message: 'Function created successfully',
|
||||
color: 'green',
|
||||
});
|
||||
}
|
||||
onSuccess();
|
||||
onClose();
|
||||
form.reset();
|
||||
} catch (error) {
|
||||
console.error('Error saving function:', error);
|
||||
notifications.show({
|
||||
title: 'Error',
|
||||
message: `Failed to ${isEditing ? 'update' : 'create'} function`,
|
||||
color: 'red',
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Paper
|
||||
style={{
|
||||
position: 'fixed',
|
||||
top: 60, // Below header
|
||||
right: opened ? 0 : '-500px',
|
||||
bottom: 0,
|
||||
width: '500px',
|
||||
zIndex: 1000,
|
||||
borderRadius: 0,
|
||||
display: 'flex',
|
||||
flexDirection: 'column',
|
||||
borderLeft: '1px solid var(--mantine-color-gray-3)',
|
||||
backgroundColor: 'var(--mantine-color-body)',
|
||||
transition: 'right 0.3s ease',
|
||||
}}
|
||||
>
|
||||
{/* Header */}
|
||||
<Group justify="space-between" p="md" style={{ borderBottom: '1px solid var(--mantine-color-gray-3)' }}>
|
||||
<Title order={4}>
|
||||
{isEditing ? 'Edit Function' : 'Create Function'}
|
||||
</Title>
|
||||
<ActionIcon
|
||||
variant="subtle"
|
||||
color="gray"
|
||||
onClick={onClose}
|
||||
>
|
||||
<IconX size={18} />
|
||||
</ActionIcon>
|
||||
</Group>
|
||||
|
||||
{/* Content */}
|
||||
<ScrollArea style={{ flex: 1 }}>
|
||||
<Box p="md">
|
||||
<form onSubmit={(e) => {
|
||||
console.log('Form submit event triggered');
|
||||
console.log('Form values:', form.values);
|
||||
console.log('Form errors:', form.errors);
|
||||
console.log('Is form valid?', form.isValid());
|
||||
const result = form.onSubmit(handleSubmit)(e);
|
||||
console.log('Form onSubmit result:', result);
|
||||
return result;
|
||||
}}>
|
||||
<Stack gap="md">
|
||||
<Group grow>
|
||||
<TextInput
|
||||
label="Function Name"
|
||||
placeholder="my-function"
|
||||
required
|
||||
{...form.getInputProps('name')}
|
||||
/>
|
||||
<TextInput
|
||||
label="App ID"
|
||||
placeholder="my-app"
|
||||
required
|
||||
disabled={isEditing}
|
||||
{...form.getInputProps('app_id')}
|
||||
/>
|
||||
</Group>
|
||||
|
||||
<Group grow>
|
||||
<Select
|
||||
label="Runtime"
|
||||
placeholder="Select runtime"
|
||||
required
|
||||
data={runtimeOptions}
|
||||
{...form.getInputProps('runtime')}
|
||||
onChange={handleRuntimeChange}
|
||||
/>
|
||||
<NumberInput
|
||||
label="Memory (MB)"
|
||||
placeholder="128"
|
||||
required
|
||||
min={64}
|
||||
max={3008}
|
||||
{...form.getInputProps('memory')}
|
||||
/>
|
||||
</Group>
|
||||
|
||||
<Group grow>
|
||||
<TextInput
|
||||
label="Timeout"
|
||||
placeholder="30s"
|
||||
required
|
||||
{...form.getInputProps('timeout')}
|
||||
/>
|
||||
</Group>
|
||||
|
||||
<TextInput
|
||||
label="Handler"
|
||||
description="The entry point for your function (e.g., 'index.handler' means handler function in index file)"
|
||||
placeholder="index.handler"
|
||||
required
|
||||
{...form.getInputProps('handler')}
|
||||
/>
|
||||
|
||||
<Box>
|
||||
<Text size="sm" fw={500} mb={5}>
|
||||
Function Code
|
||||
</Text>
|
||||
<Box
|
||||
style={{
|
||||
border: '1px solid #dee2e6',
|
||||
borderRadius: '4px',
|
||||
overflow: 'hidden'
|
||||
}}
|
||||
>
|
||||
<Editor
|
||||
height="300px"
|
||||
language={getEditorLanguage(form.values.runtime)}
|
||||
value={form.values.code}
|
||||
onChange={(value) => form.setFieldValue('code', value || '')}
|
||||
options={{
|
||||
minimap: { enabled: false },
|
||||
scrollBeyondLastLine: false,
|
||||
fontSize: 12,
|
||||
lineNumbers: 'on',
|
||||
roundedSelection: false,
|
||||
scrollbar: {
|
||||
vertical: 'visible',
|
||||
horizontal: 'visible'
|
||||
},
|
||||
automaticLayout: true,
|
||||
wordWrap: 'on',
|
||||
tabSize: 2,
|
||||
insertSpaces: true,
|
||||
folding: true,
|
||||
lineDecorationsWidth: 0,
|
||||
lineNumbersMinChars: 3,
|
||||
renderLineHighlight: 'line',
|
||||
selectOnLineNumbers: true,
|
||||
theme: 'vs-light'
|
||||
}}
|
||||
loading={<Text ta="center" p="xl">Loading editor...</Text>}
|
||||
/>
|
||||
</Box>
|
||||
{form.errors.code && (
|
||||
<Text size="xs" c="red" mt={5}>
|
||||
{form.errors.code}
|
||||
</Text>
|
||||
)}
|
||||
</Box>
|
||||
|
||||
<JsonInput
|
||||
label="Environment Variables"
|
||||
description="JSON object with key-value pairs that will be available in your function runtime"
|
||||
placeholder={`{
|
||||
"NODE_ENV": "production",
|
||||
"API_URL": "https://api.example.com"
|
||||
}`}
|
||||
validationError="Invalid JSON - please check your syntax"
|
||||
formatOnBlur
|
||||
autosize
|
||||
minRows={3}
|
||||
{...form.getInputProps('environment')}
|
||||
/>
|
||||
|
||||
<Paper withBorder p="md" bg="gray.0">
|
||||
<Text size="sm" fw={500} mb="xs">Owner Information</Text>
|
||||
<Group grow>
|
||||
<Select
|
||||
label="Owner Type"
|
||||
data={[
|
||||
{ value: 'individual', label: 'Individual' },
|
||||
{ value: 'team', label: 'Team' },
|
||||
]}
|
||||
{...form.getInputProps('owner.type')}
|
||||
/>
|
||||
<TextInput
|
||||
label="Owner Name"
|
||||
placeholder="Team Name"
|
||||
{...form.getInputProps('owner.name')}
|
||||
/>
|
||||
</Group>
|
||||
<TextInput
|
||||
label="Owner Email"
|
||||
placeholder="owner@example.com"
|
||||
mt="xs"
|
||||
{...form.getInputProps('owner.owner')}
|
||||
/>
|
||||
</Paper>
|
||||
|
||||
<Divider />
|
||||
|
||||
<Group justify="flex-end">
|
||||
<Button variant="light" onClick={onClose}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit">
|
||||
{isEditing ? 'Update' : 'Create'} Function
|
||||
</Button>
|
||||
</Group>
|
||||
</Stack>
|
||||
</form>
|
||||
</Box>
|
||||
</ScrollArea>
|
||||
</Paper>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user