Skip to content

Commit

Permalink
fix some issues in a previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alirezamirian committed Nov 24, 2023
1 parent 122bb42 commit de50337
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ const StyledErrorMessage = styled.div`
position: sticky; // prevents commit message scroll jump when message is toggled on and off
color: ${({ theme }) => theme.commonColors.red};
`;
const commitSuccessfulMessage = new IntlMessageFormat(
`{count, plural,
=1 {1 file committed}
other {# files committed}
}`,
"en-US"
);

/**
* Commit message and commit buttons shown in the bottom/right split view of the commit toolwindow
Expand All @@ -85,7 +78,7 @@ export function CommitView({
}
}, [commitMessage]);

const commitSelectedChanges = useRecoilCallback(({ snapshot, set }) => {
const commitSelectedChanges = useRecoilCallback(({ snapshot }) => {
const includedChanges = snapshot
.getLoadable(includedChangesState)
.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import {
import { Change, useRefreshChanges } from "../change-lists.state";
import { useRunTask } from "../../../tasks";
import { commitTaskIdState } from "./ChangesView.state";
import { IntlMessageFormat } from "intl-messageformat";
import { useBalloonManager } from "@intellij-platform/core";
import React from "react";

const commitSuccessfulMessage = new IntlMessageFormat(
`{count, plural,
=1 {1 file committed}
other {# files committed}
}`,
"en-US"
);

/**
* Returns a commit callback to be used to commit a bunch of changes.
Expand All @@ -20,6 +31,7 @@ import { commitTaskIdState } from "./ChangesView.state";
export function useCommitChanges() {
const refreshChanges = useRefreshChanges();
const refreshFileStatus = useUpdateVcsFileStatuses();
const balloonManager = useBalloonManager();
const runTask = useRunTask();

return useRecoilCallback(
Expand Down Expand Up @@ -79,14 +91,40 @@ export function useCommitChanges() {
});
}
)
).then(
() => {
// TODO: file status state and changes state separately use statusMatrix. It can be refactored for
// changes to be based on file status state.
refreshChanges().catch(console.error);
refreshFileStatus().catch(console.error);

balloonManager.show({
icon: "Info",
body: (
<>
{`${commitSuccessfulMessage.format({
count: changes.length,
})}: `}
{commitMessage
.split("\n")
.flatMap((part, index) => [<br key={index} />, part])
.slice(1)}
</>
),
});
},
(e) => {
balloonManager.show({
icon: "Error",
title: "Commit failed!",
body: "Could not commit files.",
});
console.error("Commit error", e);
}
);
},
onFinished: () => {
set(commitTaskIdState, null);
// TODO: file status state and changes state separately use statusMatrix. It can be refactored for
// changes to be based on file status state.
refreshChanges().catch(console.error);
refreshFileStatus().catch(console.error);
},
}
);
Expand Down
45 changes: 29 additions & 16 deletions packages/example-app/src/VersionControl/file-status.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
selector,
selectorFamily,
useRecoilCallback,
useRecoilRefresher_UNSTABLE,
useResetRecoilState,
} from "recoil";
import { sampleRepos } from "../Project/project.state";
import { findRoot, status, statusMatrix } from "isomorphic-git";
Expand All @@ -18,27 +20,38 @@ import { useEffect } from "react";
import * as path from "path";
import { asyncFilter } from "../async-utils";

const temporaryVcsMappingsDefault = async (): Promise<
VcsDirectoryMapping[]
> => {
return asyncFilter(
({ dir }) => fs.promises.stat(dir).then(Boolean),
Object.values(sampleRepos).map(({ path }) => ({
dir: path,
vcs: "git",
}))
);
};

// This should be refactored to have the configuration file(s) as the source of truth.
const temporaryVcsMappingsDefaultState = selector({
key: "vcsRoots/temporaryDefault",
get: async (): Promise<VcsDirectoryMapping[]> => {
return asyncFilter(
({ dir }) => fs.promises.stat(dir).then(Boolean),
Object.values(sampleRepos).map(({ path }) => ({
dir: path,
vcs: "git",
}))
);
},
});
export const vcsRootsState = atom<VcsDirectoryMapping[]>({
key: "vcsRoots",
default: selector({
key: "vcsRoots/temporaryDefault",
get: temporaryVcsMappingsDefault,
}),
default: temporaryVcsMappingsDefaultState,
});

/**
* temporary(?) hook to refresh vcs roots
*/
export const useRefreshVcsRoots = () => {
const refreshTemporaryVcsMappingsDefault = useRecoilRefresher_UNSTABLE(
temporaryVcsMappingsDefaultState
);
const refreshVcsRoots = useResetRecoilState(vcsRootsState);
return () => {
refreshTemporaryVcsMappingsDefault();
refreshVcsRoots();
};
};

/**
* Given the absolute path of a file, returns the relevant VCS root path, if any.
*/
Expand Down
133 changes: 71 additions & 62 deletions packages/example-app/src/VersionControl/useShowGitTipIfNeeded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,74 +10,83 @@ import {
} from "../Project/project.state";
import { cloneRepo, isSuccessfullyCloned } from "../SampleRepoInitializer";
import React from "react";
import { useRefreshVcsRoots } from "./file-status.state";
import { useRecoilCallback } from "recoil";

let notified = false;
export const useShowGitTipIfNeeded = () => {
const balloonManager = useBalloonManager();
const runTask = useRunTask();
const refreshProjectFiles = useRefreshCurrentProjectFiles();
const refreshVcsRoots = useRefreshVcsRoots();

return async () => {
const cloned = await isSuccessfullyCloned(sampleRepos.ExampleRepo.path);
if (!cloned && !notified) {
notified = true;
balloonManager.showSticky({
title: "Clone another repo",
icon: <PlatformIcon icon="actions/quickfixOffBulb.svg" />,
body: "Git integration in the example app supports multiple repos within the same project. See it in action by cloning another repo",
actions: (
<>
<BalloonActionLink
onPress={() => {
runTask(
{
title: `Cloning source repository ${sampleRepos.ExampleRepo.url}`,
},
{
run: async ({
setIndeterminate,
setFraction,
setSecondaryText,
}) => {
await cloneRepo({
dir: sampleRepos.ExampleRepo.path,
url: sampleRepos.ExampleRepo.url,
onProgress: (progress) => {
if (progress.total) {
const fraction = progress.loaded / progress.total;
setFraction(fraction);
setSecondaryText(
`${progress.phase}: ${Math.round(
fraction * 100
)}% ${progress.loaded}/${progress.total}`
);
} else {
setSecondaryText(progress.phase);
setIndeterminate(true);
}
return useRecoilCallback(
({ reset }) =>
async () => {
const cloned = await isSuccessfullyCloned(sampleRepos.ExampleRepo.path);
if (!cloned && !notified) {
notified = true;
balloonManager.showSticky({
title: "Clone another repo",
icon: <PlatformIcon icon="actions/quickfixOffBulb.svg" />,
body: "Git integration in the example app supports multiple repos within the same project. See it in action by cloning another repo",
actions: (
<>
<BalloonActionLink
onPress={() => {
runTask(
{
title: `Cloning source repository ${sampleRepos.ExampleRepo.url}`,
},
{
run: async ({
setIndeterminate,
setFraction,
setSecondaryText,
}) => {
await cloneRepo({
dir: sampleRepos.ExampleRepo.path,
url: sampleRepos.ExampleRepo.url,
onProgress: (progress) => {
if (progress.total) {
const fraction =
progress.loaded / progress.total;
setFraction(fraction);
setSecondaryText(
`${progress.phase}: ${Math.round(
fraction * 100
)}% ${progress.loaded}/${progress.total}`
);
} else {
setSecondaryText(progress.phase);
setIndeterminate(true);
}
},
}).then(
() => {
refreshVcsRoots();
refreshProjectFiles(); // FIXME: flashes the whole page
},
(e) => {
balloonManager.show({
icon: "Error",
title: "Clone failed",
body: `Failed to clone git repo "${sampleRepos.ExampleRepo.url} into ${sampleRepos.ExampleRepo.path}: ${e}"`,
});
}
);
},
}).then(
() => {
refreshProjectFiles(); // FIXME: flashes the whole page
},
(e) => {
balloonManager.show({
icon: "Error",
title: "Clone failed",
body: `Failed to clone git repo "${sampleRepos.ExampleRepo.url} into ${sampleRepos.ExampleRepo.path}: ${e}"`,
});
}
);
},
}
);
}}
>
Clone example-repo
</BalloonActionLink>
</>
),
});
}
};
}
);
}}
>
Clone example-repo
</BalloonActionLink>
</>
),
});
}
},
[]
);
};

0 comments on commit de50337

Please sign in to comment.