Skip to content

Commit

Permalink
fix: apply view service module, add coverage about detached head
Browse files Browse the repository at this point in the history
  • Loading branch information
ss-won committed Sep 19, 2023
1 parent 1cb3103 commit a80e141
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 27 deletions.
12 changes: 5 additions & 7 deletions packages/view/src/components/BranchSelector/BranchSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ import { type ChangeEventHandler } from "react";
import "./BranchSelector.scss";

import { useGlobalData } from "hooks";
import { container } from "tsyringe";
import type IDEPort from "ide/IDEPort";
import { sendFetchAnalyzedDataCommand } from "services";

const BranchSelector = () => {
const { branchList, baseBranch, setBaseBranch, setLoading } = useGlobalData();
const { branchList, selectedBranch, setSelectedBranch, setLoading } = useGlobalData();

const handleChangeSelect: ChangeEventHandler<HTMLSelectElement> = (e) => {
setBaseBranch(e.target.value);
const ideAdapter = container.resolve<IDEPort>("IDEAdapter");
setSelectedBranch(e.target.value);
setLoading(true);
ideAdapter.sendFetchAnalyzedDataMessage(e.target.value);
sendFetchAnalyzedDataCommand(e.target.value);
};

return (
Expand All @@ -21,7 +19,7 @@ const BranchSelector = () => {
<select
className="select-box"
onChange={handleChangeSelect}
value={baseBranch}
value={selectedBranch}
>
{branchList?.map((option) => (
<option
Expand Down
10 changes: 3 additions & 7 deletions packages/view/src/components/RefreshButton/RefreshButton.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import "reflect-metadata";
import cn from "classnames";
import { container } from "tsyringe";
import { FiRefreshCcw } from "react-icons/fi";

import { throttle } from "utils";
import type IDEPort from "ide/IDEPort";
import { useGlobalData } from "hooks";

import "./RefreshButton.scss";
import { sendRefreshDataCommand } from "services";

const RefreshButton = () => {
const { loading, setLoading, baseBranch } = useGlobalData();
const { loading, setLoading, selectedBranch } = useGlobalData();

const refreshHandler = throttle(() => {
setLoading(true);

const ideAdapter = container.resolve<IDEPort>("IDEAdapter");
ideAdapter.sendFetchAnalyzedDataMessage(baseBranch);
ideAdapter.sendFetchBranchListMessage();
sendRefreshDataCommand(selectedBranch);
}, 3000);

return (
Expand Down
10 changes: 5 additions & 5 deletions packages/view/src/context/GlobalDataProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ export const GlobalDataProvider = ({ children }: PropsWithChildren) => {
const [filteredRange, setFilteredRange] = useState<DateFilterRange>(undefined);

const [branchList, setBranchList] = useState<string[]>([]);
const [baseBranch, setBaseBranch] = useState<string>(branchList?.[0]);
const [selectedBranch, setSelectedBranch] = useState<string>(branchList?.[0]);

const handleChangeBranchList = (branches: { branchList: string[]; head: string | null }) => {
setBaseBranch((prev) => (!prev && branches.head ? branches.head : prev));
setSelectedBranch((prev) => (!prev && branches.head ? branches.head : prev));
setBranchList(branches.branchList);
};

Expand All @@ -40,12 +40,12 @@ export const GlobalDataProvider = ({ children }: PropsWithChildren) => {
setLoading,
branchList,
setBranchList,
baseBranch,
setBaseBranch,
selectedBranch,
setSelectedBranch,
handleChangeAnalyzedData,
handleChangeBranchList,
}),
[data, filteredRange, filteredData, selectedData, branchList, baseBranch, loading]
[data, filteredRange, filteredData, selectedData, branchList, selectedBranch, loading]
);

return <GlobalDataContext.Provider value={value}>{children}</GlobalDataContext.Provider>;
Expand Down
4 changes: 2 additions & 2 deletions packages/view/src/hooks/useGlobalData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type GlobalDataState = {
setLoading: Dispatch<SetStateAction<boolean>>;
branchList: string[];
setBranchList: Dispatch<SetStateAction<string[]>>;
baseBranch: string;
setBaseBranch: Dispatch<SetStateAction<string>>;
selectedBranch: string;
setSelectedBranch: Dispatch<SetStateAction<string>>;
} & IDESentEvents;

export const GlobalDataContext = createContext<GlobalDataState | undefined>(undefined);
Expand Down
14 changes: 10 additions & 4 deletions packages/view/src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@ export const setPrimaryColor = (color: string) => {
ideAdapter.setPrimaryColor(color);
};

export const sendFetchAnalyzedDataCommand = () => {
export const sendFetchAnalyzedDataCommand = (selectedBranch?: string) => {
const ideAdapter = container.resolve<IDEPort>("IDEAdapter");
ideAdapter.sendFetchAnalyzedDataMessage();
ideAdapter.sendFetchAnalyzedDataMessage(selectedBranch);
};

export const sendRefreshDataCommand = () => {
export const sendRefreshDataCommand = (selectedBranch?: string) => {
const ideAdapter = container.resolve<IDEPort>("IDEAdapter");
ideAdapter.sendRefreshDataMessage();
ideAdapter.sendRefreshDataMessage(selectedBranch);
ideAdapter.sendFetchBranchListMessage();
};

export const sendFetchBranchListCommand = () => {
const ideAdapter = container.resolve<IDEPort>("IDEAdapter");
ideAdapter.sendFetchBranchListMessage();
};
19 changes: 17 additions & 2 deletions packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import { Credentials } from "./credentials";
import { GithubTokenUndefinedError, WorkspacePathUndefinedError } from "./errors/ExtensionError";
import { getGithubToken, setGithubToken } from "./setting-repository";
import { mapClusterNodesFrom } from "./utils/csm.mapper";
import { findGit, getBranches, getCurrentBranchName, getGitConfig, getGitLog, getRepo } from "./utils/git.util";
import {
findGit,
getBranches,
getCurrentBranchName,
getDefaultBranchName,
getGitConfig,
getGitLog,
getRepo,
} from "./utils/git.util";
import WebviewLoader from "./webview-loader";

let myStatusBarItem: vscode.StatusBarItem;
Expand Down Expand Up @@ -39,7 +47,14 @@ export async function activate(context: vscode.ExtensionContext) {
}

const fetchBranches = async () => await getBranches(gitPath, currentWorkspacePath);
const fetchCurrentBranch = async () => await getCurrentBranchName(gitPath, currentWorkspacePath);
const fetchCurrentBranch = async () => {
let branchName = await getCurrentBranchName(gitPath, currentWorkspacePath);
if (!branchName) {
const branchList = (await fetchBranches()).branchList;
branchName = getDefaultBranchName(branchList);
}
return branchName;
};

const initialBaseBranchName = await fetchCurrentBranch();
const fetchClusterNodes = async (baseBranchName = initialBaseBranchName) => {
Expand Down
3 changes: 3 additions & 0 deletions packages/vscode/src/utils/git.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,15 @@ export async function getBranches(
.replace(/(.*) -> (?:.*)/g, "$1")
.replace("remotes/", "");
if (branch.startsWith("* ")) {
if (branch.includes("HEAD detached")) continue;
branch = branch.replace("* ", "");
head = branch;
}
branchList.push(branch);
}

if (!head) head = getDefaultBranchName(branchList);

return { branchList, head };
}

Expand Down

0 comments on commit a80e141

Please sign in to comment.