Skip to content

Commit

Permalink
Clean up patterns and logs (#36)
Browse files Browse the repository at this point in the history
* minor fixes

* remove enabled and clean select root branch pattern

* lint
  • Loading branch information
Opeyem1a authored Oct 18, 2024
1 parent e3e832a commit 8bb8b5d
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 63 deletions.
33 changes: 14 additions & 19 deletions src/commands/branch/new.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import ErrorDisplay from '../../components/error-display.js';
import React, { useCallback, useEffect, useState } from 'react';
import React, { useCallback } from 'react';
import { Action, useAction } from '../../hooks/use-action.js';
import {
CommandConfig,
Expand All @@ -14,23 +14,26 @@ import { safeBranchNameFromCommitMessage } from '../../utils/naming.js';
import { useGit } from '../../hooks/use-git.js';
import { useTree } from '../../hooks/use-tree.js';

function BranchNew(props: CommandProps) {
const BranchNew = (props: CommandProps) => {
const { rootBranchName } = useTree();

if (!rootBranchName) {
return <SelectRootBranch />;
}

return <DoBranchNew {...props} />;
};

const DoBranchNew = (props: CommandProps) => {
const args = branchNewConfig.getProps(props) as Valid<
PropSanitationResult<CommandArgs>
>;
const { commitMessage } = args.props;

const { rootBranchName } = useTree();

const result = useBranchNew({
message: commitMessage,
enabled: Boolean(rootBranchName),
});

if (!result.isEnabled) {
return <SelectRootBranch />;
}

if (result.isError) {
return <ErrorDisplay error={result.error} />;
}
Expand All @@ -44,19 +47,13 @@ function BranchNew(props: CommandProps) {
New branch created - <Text bold>{result.branchName}</Text>
</Text>
);
}
};

type UseBranchNewAction = Action & {
branchName: string;
};

const useBranchNew = ({
message,
enabled,
}: {
message: string;
enabled: boolean;
}): UseBranchNewAction => {
const useBranchNew = ({ message }: { message: string }): UseBranchNewAction => {
const git = useGit();

const branchName = safeBranchNameFromCommitMessage(message);
Expand All @@ -70,11 +67,9 @@ const useBranchNew = ({

const action = useAction({
asyncAction: performAction,
enabled,
});

return {
isEnabled: action.isEnabled,
isLoading: action.isLoading,
isError: action.isError,
error: action.error,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/changes/add.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Loading } from '../../components/loading.js';
import { Text } from 'ink';
import { useGit } from '../../hooks/use-git.js';

function ChangedAdd({}: CommandProps) {
const ChangedAdd = ({}: CommandProps) => {
const result = useChangesAdd();

if (result.isError) {
Expand All @@ -22,7 +22,7 @@ function ChangedAdd({}: CommandProps) {
Staged all changes
</Text>
);
}
};

const useChangesAdd = (): Action => {
const git = useGit();
Expand Down
30 changes: 13 additions & 17 deletions src/commands/changes/commit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ import { Text } from 'ink';
import { useGit } from '../../hooks/use-git.js';
import { useTree } from '../../hooks/use-tree.js';

function ChangesCommit(props: CommandProps) {
const ChangesCommit = (props: CommandProps) => {
const { rootBranchName } = useTree();

if (!rootBranchName) {
return <SelectRootBranch />;
}

return <DoChangesCommit {...props} />;
};

const DoChangesCommit = (props: CommandProps) => {
const args = changesCommitConfig.getProps(props) as Valid<
PropSanitationResult<CommandArgs>
>;

const { rootBranchName } = useTree();

const result = useChangesCommit({
message: args.props.message,
enabled: Boolean(rootBranchName),
});

if (!result.isEnabled) {
return <SelectRootBranch />;
}

if (result.isError) {
return <ErrorDisplay error={result.error} />;
}
Expand All @@ -42,15 +45,9 @@ function ChangesCommit(props: CommandProps) {
Committed all changes
</Text>
);
}
};

const useChangesCommit = ({
message,
enabled,
}: {
message: string;
enabled: boolean;
}): Action => {
const useChangesCommit = ({ message }: { message: string }): Action => {
const git = useGit();

const performAction = useCallback(async () => {
Expand All @@ -60,7 +57,6 @@ const useChangesCommit = ({

return useAction({
asyncAction: performAction,
enabled,
});
};

Expand Down
4 changes: 2 additions & 2 deletions src/commands/help.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import { REGISTERED_COMMANDS } from '../command-registry.js';
import { findCommand, findCommandGroup } from '../utils/commands.js';

function Help(props: CommandProps) {
const Help = (props: CommandProps) => {
const args = helpConfig.getProps(props) as Valid<
PropSanitationResult<CommandArgs>
>;
Expand Down Expand Up @@ -52,7 +52,7 @@ function Help(props: CommandProps) {
<Text>Usage - {command.config.usage}</Text>
</Box>
);
}
};

const CommandList = ({
title,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/hop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from '../types.js';
import { useGit } from '../hooks/use-git.js';

function Hop(props: CommandProps) {
const Hop = (props: CommandProps) => {
const args = hopConfig.getProps(props) as Valid<
PropSanitationResult<CommandArgs>
>;
Expand Down Expand Up @@ -115,7 +115,7 @@ function Hop(props: CommandProps) {
limit={10}
/>
);
}
};

interface CommandArgs {
searchTerm?: string;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import React from 'react';
import { CommandConfig, CommandProps } from '../types.js';
import { Text } from 'ink';

function Sync({ cli, input }: CommandProps) {
const Sync = ({ cli, input }: CommandProps) => {
console.log('Rendered', { cli, input });
return <Text>Sync command</Text>;
}
};

export const syncConfig: CommandConfig = {
description: 'Sync your local changes with the remote server',
Expand Down
4 changes: 2 additions & 2 deletions src/components/confirm-statement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ export const ConfirmStatement = ({
<Text>
{statement}
<Text>
&nbsp;(
&nbsp;[
<Text underline={intent.accept} bold={intent.accept}>
Y
</Text>
/
<Text underline={intent.deny} bold={intent.deny}>
n
</Text>
)
]:
</Text>
</Text>
<Text>{userInput.length ? userInput : '_'}</Text>
Expand Down
16 changes: 1 addition & 15 deletions src/hooks/use-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,29 @@ import { useEffect, useState } from 'react';

export const useAction = ({
asyncAction,
enabled = true,
}: {
asyncAction: () => Promise<void>;
enabled?: boolean;
}): Action => {
const [state, setState] = useState<State>({ type: 'LOADING' });

useEffect(() => {
if (!enabled) {
setState({ type: 'NOT_ENABLED' });
return;
}

setState({ type: 'LOADING' });
asyncAction()
.then(() => setState({ type: 'COMPLETE' }))
.catch((e: Error) => {
setState({ type: 'ERROR', error: e });
});
}, [asyncAction, enabled]);
}, [asyncAction]);

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

return {
isEnabled: state.type !== 'NOT_ENABLED',
isLoading: state.type === 'LOADING',
isError: false,
error: undefined,
Expand All @@ -42,22 +33,17 @@ export const useAction = ({

export type Action =
| {
isEnabled: boolean;
isLoading: boolean;
isError: false;
error: undefined;
}
| {
isEnabled: true;
isLoading: boolean;
isError: true;
error: Error;
};

export type State =
| {
type: 'NOT_ENABLED';
}
| {
type: 'LOADING';
}
Expand Down
2 changes: 0 additions & 2 deletions src/services/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ const preflight = (config: ServiceConfig): PreflightResult => {
const dir = isDev ? path.join(projectRootDir, '.local-config') : paths.data;
const dataFilePath = path.join(dir, config.filename);

console.log({ dir });

if (!existsSync(dataFilePath)) {
mkdirSync(dir, { recursive: true });
}
Expand Down

0 comments on commit 8bb8b5d

Please sign in to comment.