Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added Initial Autocomplete for Node Commands #13

Draft
wants to merge 1 commit into
base: chore--update-readme
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/node/nodeControls/Styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export const TerminalOutput = styled.pre`
margin: 0;
padding: 5px;
font-size: 12px;
max-height: 500px;
`;

export const TerminalForm = styled.form`
Expand Down
152 changes: 145 additions & 7 deletions src/components/node/nodeControls/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ const NodeControls: React.FC<NodeControlsProps> = ({ ...props }) => {
type: MessageType.INFO,
});
const { handleGetNodeOutput } = useNodeManagement();
const [suggestions, setSuggestions] = useState<string[]>([]);
const [showSuggestions, setShowSuggestions] = useState(false);
const [selectedSuggestionIndex, setSelectedSuggestionIndex] = useState<number>(-1);

useEffect(() => {
if (props.action) {
Expand Down Expand Up @@ -200,6 +203,105 @@ const NodeControls: React.FC<NodeControlsProps> = ({ ...props }) => {
}
};

const handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
if (!showSuggestions) return;

switch (e.key) {
case 'ArrowDown':
e.preventDefault();
setSelectedSuggestionIndex(prev =>
prev < suggestions.length - 1 ? prev + 1 : prev
);
break;
case 'ArrowUp':
e.preventDefault();
setSelectedSuggestionIndex(prev =>
prev > 0 ? prev - 1 : prev
);
break;
case 'Enter':
e.preventDefault();
if (selectedSuggestionIndex >= 0) {
const suggestion = suggestions[selectedSuggestionIndex];
const parts = suggestion.split(' ');
if (parts.length > 1 && !parts[1].startsWith('[')) {
// If it's a subcommand suggestion, preserve the main command
const mainCommand = input.split(' ')[0];
setInput(mainCommand + ' ' + parts[0]);
} else {
// If it's a main command or has no subcommands, use only the first word
setInput(parts[0]);
}
setShowSuggestions(false);
setSelectedSuggestionIndex(-1);
}
break;
case 'Escape':
setShowSuggestions(false);
setSelectedSuggestionIndex(-1);
break;
}
};

const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const value = e.target.value;
setInput(value);

const commandStructure = {
'call': ['<Context ID> <Method> <JSON Payload> <Executor Public Key>'],
'peers': [],
'pool': [],
'gc': [],
'store': [],
'context': ['ls', 'join', 'leave', 'invite', 'create', 'delete', 'state', 'identity'],
'application': ['ls', 'install']
};

// Handle both cases: with and without leading '/'
const parts = value.startsWith('/') ? value.slice(1).split(' ') : value.split(' ');
const mainCommand = parts[0].toLowerCase();

if (parts.length === 1) {
// Show main commands
const commands = Object.keys(commandStructure).map(cmd => {
const subcommands = commandStructure[cmd];
return subcommands.length > 0 ? `${cmd} [${subcommands.join('|')}]` : cmd;
});

const filtered = commands.filter(cmd =>
cmd.toLowerCase().startsWith(mainCommand)
);
setSuggestions(filtered);
setShowSuggestions(filtered.length > 0);
} else if (parts.length === 2) {
// Get the base command without any parameters
const baseCommand = mainCommand.split(' ')[0];
const subcommands = commandStructure[baseCommand] || [];
if (subcommands.length > 0) {
const subcommandTerm = parts[1].toLowerCase();
const filtered = subcommands.filter(cmd =>
cmd.toLowerCase().startsWith(subcommandTerm)
);
setSuggestions(filtered);
setShowSuggestions(filtered.length > 0);
}
}
setSelectedSuggestionIndex(-1);
};

const handleSuggestionClick = (suggestion: string) => {
const parts = suggestion.split(' ');
if (parts.length > 1 && !parts[1].startsWith('[')) {
// If it's a subcommand suggestion, preserve the main command
const mainCommand = input.split(' ')[0];
setInput(mainCommand + ' ' + parts[0]);
} else {
// If it's a main command or has no subcommands, use only the first word
setInput(parts[0]);
}
setShowSuggestions(false);
};

return (
<ControlsContainer>
<ButtonGroup>
Expand All @@ -213,13 +315,49 @@ const NodeControls: React.FC<NodeControlsProps> = ({ ...props }) => {
<TerminalContainer>
<TerminalOutput ref={outputRef}>{output}</TerminalOutput>
<TerminalForm onSubmit={handleInputSubmit}>
<TerminalInput
type="text"
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Enter command..."
disabled={!isRunning}
/>
<div style={{ position: 'relative', width: '100%' }}>
<TerminalInput
type="text"
value={input}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
placeholder="Enter command... (type / for commands)"
disabled={!isRunning}
/>
{showSuggestions && (
<div
style={{
position: 'absolute',
top: '100%',
left: 0,
right: 0,
backgroundColor: '#1e1e1e',
border: '1px solid #333',
borderRadius: '4px',
maxHeight: '150px',
overflowY: 'auto',
zIndex: 1000
}}
>
{suggestions.map((suggestion, index) => (
<div
key={index}
onClick={() => handleSuggestionClick(suggestion)}
style={{
padding: '8px 12px',
cursor: 'pointer',
backgroundColor: index === selectedSuggestionIndex ? '#2c2c2c' : 'transparent',
'&:hover': {
backgroundColor: '#2c2c2c'
}
}}
>
{suggestion}
</div>
))}
</div>
)}
</div>
</TerminalForm>
</TerminalContainer>
<MessagePopup
Expand Down