Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Opeyem1a committed Sep 23, 2024
1 parent 80ca92a commit d10f96f
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/commands/changes/commit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import ErrorDisplay from '../../components/error-display.js';
import React, { useEffect, useState } from 'react';
import { CommandConfig, CommandProps } from '../../types.js';
import { Text } from 'ink';
import { useGit } from '../../hooks/use-git.js';

function ChangesCommit({ input }: CommandProps) {
const [, , message] = input;
const result = useChangesCommit({ message });

if (result.isError) {
return <ErrorDisplay error={result.error} />;
}

if (result.isLoading) {
return <Text color="cyan">Loading...</Text>;
}

return (
<Text bold color="green">
Committed all changes
</Text>
);
}

type Action = { isLoading: boolean } & (
| {
isError: false;
}
| {
isError: true;
error: Error;
}
);

type State =
| {
type: 'LOADING';
}
| {
type: 'COMPLETE';
}
| {
type: 'ERROR';
error: Error;
};

const useChangesCommit = ({ message }: { message: string }): Action => {
const git = useGit();
const [state, setState] = useState<State>({ type: 'LOADING' });

useEffect(() => {
git.commit({ message })
.then(() => setState({ type: 'COMPLETE' }))
.catch((e: Error) => {
setState({ type: 'ERROR', error: e });
});
}, []);

if (state.type === 'ERROR') {
return {
isLoading: false,
isError: true,
error: state.error,
};
}

return {
isLoading: state.type === 'LOADING',
isError: false,
};
};

export const changesCommitConfig: CommandConfig = {
description: 'Stage and commit all changes.',
usage: 'changes commit "<message>"',
key: 'commit',
aliases: ['c'],
validateProps: (props) => {
const { input } = props;
const [, , message] = input;

if (!message)
return {
valid: false,
errors: ['Please provide a commit message'],
};

return { valid: true };
},
};

export default ChangesCommit;

0 comments on commit d10f96f

Please sign in to comment.