Skip to content

User Settings: Add Shell selector dropdown to the Preferences tab #1216

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

Merged
merged 4 commits into from
Apr 16, 2025
Merged
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
57 changes: 57 additions & 0 deletions src/components/terminal-picker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { SelectControl } from '@wordpress/components';
import { useI18n } from '@wordpress/react-i18n';
import { useEffect, useState } from 'react';
import { SupportedTerminal, supportedTerminalNames } from 'src/lib/terminal';

interface TerminalPickerProps {
value: SupportedTerminal;
onChange: ( value: SupportedTerminal ) => void;
}

export const TerminalPicker = ( { value, onChange }: TerminalPickerProps ) => {
const { __ } = useI18n();
const [ installedTerminals, setInstalledTerminals ] = useState< {
iterm: boolean | null;
terminal: boolean | null;
} >( {
iterm: null,
terminal: null,
} );

useEffect( () => {
const fetchInstalledTerminals = async () => {
try {
const terminals = { iterm: true, terminal: true }; // TODO:: Fetch installed terminals from the API
setInstalledTerminals( terminals );
} catch ( error ) {
console.error( 'Failed to fetch installed terminals:', error );
}
};

fetchInstalledTerminals();
}, [] );

const options = Object.entries( supportedTerminalNames ).map( ( [ terminal, label ] ) => {
const terminalKey = terminal as SupportedTerminal;
const isInstalled = installedTerminals[ terminalKey as keyof typeof installedTerminals ];

return {
value: terminalKey,
label,
disabled: ! isInstalled,
};
} );

return (
<div className="flex gap-5 flex-col">
<h2 className="a8c-subtitle-small">{ __( 'Shell' ) }</h2>
<SelectControl
value={ value }
onChange={ onChange }
options={ options }
__nextHasNoMarginBottom
className="mb-2"
/>
</div>
);
};
26 changes: 26 additions & 0 deletions src/components/tests/terminal-picker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { render, screen, fireEvent } from '@testing-library/react';
import { TerminalPicker } from 'src/components/terminal-picker';

describe( 'TerminalPicker', () => {
const mockOnChange = jest.fn();

beforeEach( () => {
jest.clearAllMocks();
} );

it( 'renders correctly with initial props', () => {
render( <TerminalPicker value="terminal" onChange={ mockOnChange } /> );

expect( screen.getByText( 'Shell' ) ).toBeVisible();
expect( screen.getByRole( 'combobox' ) ).toBeVisible();
} );

it( 'calls onChange when selecting a different terminal', async () => {
render( <TerminalPicker value="terminal" onChange={ mockOnChange } /> );

const select = screen.getByRole( 'combobox' );
fireEvent.change( select, { target: { value: 'iterm' } } );

expect( mockOnChange ).toHaveBeenCalledWith( 'iterm', expect.anything() );
} );
} );
1 change: 1 addition & 0 deletions src/components/tests/user-settings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe( 'UserSettings', () => {
fireEvent.click( screen.getByText( 'Preferences' ) );
expect( screen.getByText( 'Preferences' ) ).toHaveAttribute( 'aria-selected', 'true' );
expect( screen.getByText( 'Language' ) ).toBeVisible();
expect( screen.getByText( 'Shell' ) ).toBeVisible();

// Switch to Usage tab
fireEvent.click( screen.getByText( 'Usage' ) );
Expand Down
15 changes: 14 additions & 1 deletion src/components/user-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ import { useIpcListener } from 'src/hooks/use-ipc-listener';
import { useOffline } from 'src/hooks/use-offline';
import { usePromptUsage } from 'src/hooks/use-prompt-usage';
import { useSnapshots } from 'src/hooks/use-snapshots';
import { useTerminalData } from 'src/hooks/use-terminal-data';
import { cx } from 'src/lib/cx';
import { getIpcApi } from 'src/lib/get-ipc-api';
import { TerminalPicker } from './terminal-picker';

const UserInfo = ( {
user,
Expand Down Expand Up @@ -227,24 +229,35 @@ const PreferencesTab = ( { onClose }: { onClose: () => void } ) => {
const { editor, handleEditorChange, saveEditorPreference, resetEditor, hasEditorChanges } =
useEditorData();

const {
terminal,
handleTerminalChange,
saveTerminalPreference,
resetTerminal,
hasTerminalChanges,
} = useTerminalData();

const savePreferences = async () => {
setSavedLocale( locale );
await saveEditorPreference();
await saveTerminalPreference();
onClose();
};

const cancelChanges = () => {
setLocale( savedLocale );
resetEditor();
resetTerminal();
onClose();
};

const hasChanges = locale !== savedLocale || hasEditorChanges;
const hasChanges = locale !== savedLocale || hasEditorChanges || hasTerminalChanges;

return (
<>
<LanguagePicker value={ locale } onChange={ setLocale } />
<EditorPicker value={ editor } onChange={ handleEditorChange } />
<TerminalPicker value={ terminal } onChange={ handleTerminalChange } />
<div className="mt-auto pt-6 flex justify-end gap-3">
<Button variant="tertiary" onClick={ cancelChanges }>
{ __( 'Cancel' ) }
Expand Down
50 changes: 50 additions & 0 deletions src/hooks/use-terminal-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useState, useEffect } from 'react';
import { SupportedTerminal, DEFAULT_TERMINAL } from 'src/lib/terminal';

/**
* Hook to manage terminal preferences
*/
export function useTerminalData() {
const [ terminal, setTerminal ] = useState< SupportedTerminal >( DEFAULT_TERMINAL );
const [ savedTerminalValue, setSavedTerminalValue ] =
useState< SupportedTerminal >( DEFAULT_TERMINAL );

const getSavedTerminal = async () => {
// TODO::Get the actual terminal from the IPC API
return DEFAULT_TERMINAL;
};

// Load the saved terminal value
useEffect( () => {
const loadSavedTerminal = async () => {
const terminal = await getSavedTerminal();
setSavedTerminalValue( terminal );
setTerminal( terminal );
};
loadSavedTerminal();
}, [] );

const handleTerminalChange = ( newTerminal: SupportedTerminal ) => {
setTerminal( newTerminal );
};

const saveTerminalPreference = async () => {
//TODO:: Save the terminal preference to the IPC API
alert( terminal );
};

const resetTerminal = () => {
setTerminal( savedTerminalValue );
};

const hasTerminalChanges = terminal !== savedTerminalValue;

return {
terminal,
savedTerminalValue,
handleTerminalChange,
saveTerminalPreference,
resetTerminal,
hasTerminalChanges,
};
}
8 changes: 8 additions & 0 deletions src/lib/terminal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type SupportedTerminal = 'terminal' | 'iterm';

export const supportedTerminalNames: Record< SupportedTerminal, string > = {
terminal: 'Terminal',
iterm: 'iTerm',
};

export const DEFAULT_TERMINAL: SupportedTerminal = 'terminal';
Loading