Skip to content

Commit

Permalink
Merge pull request #768 from seungineer/main
Browse files Browse the repository at this point in the history
[view] Remote Git config에서 owner, repo 정보를 View에서 받는 방식 수정
  • Loading branch information
seungineer authored Oct 15, 2024
2 parents 155907d + 961be97 commit 22a6783
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 67 deletions.
23 changes: 5 additions & 18 deletions packages/view/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,32 @@ import type IDEPort from "ide/IDEPort";
import { useAnalayzedData } from "hooks";
import { RefreshButton } from "components/RefreshButton";
import type { IDESentEvents } from "types/IDESentEvents";
import type { RemoteGitHubInfo } from "types/RemoteGitHubInfo";
import { useBranchStore, useDataStore, useLoadingStore, useOwnerStore, useRepoStore } from "store";
import { useBranchStore, useDataStore, useGithubInfo, useLoadingStore } from "store";

const App = () => {
const initRef = useRef<boolean>(false);
const { handleChangeAnalyzedData } = useAnalayzedData();
const filteredData = useDataStore((state) => state.filteredData);
const { handleChangeBranchList } = useBranchStore();
const { handleGithubInfo } = useGithubInfo();
const { loading, setLoading } = useLoadingStore();
const { setOwner } = useOwnerStore();
const { setRepo } = useRepoStore();
const ideAdapter = container.resolve<IDEPort>("IDEAdapter");

useEffect(() => {
if (initRef.current === false) {
const callbacks: IDESentEvents = {
handleChangeAnalyzedData,
handleChangeBranchList,
handleGithubInfo,
};
setLoading(true);
ideAdapter.addIDESentEventListener(callbacks);
ideAdapter.sendFetchAnalyzedDataMessage();
ideAdapter.sendFetchBranchListMessage();
ideAdapter.sendFetchGithubInfo();
initRef.current = true;
}
}, [handleChangeAnalyzedData, handleChangeBranchList, ideAdapter, setLoading]);

useEffect(() => {
const handleMessage = (event: MessageEvent<RemoteGitHubInfo>) => {
const message = event.data;
if (message.data) {
setOwner(message.data.owner);
setRepo(message.data.repo);
}
};

window.addEventListener("message", handleMessage);
return () => window.removeEventListener("message", handleMessage);
}, []);
}, [handleChangeAnalyzedData, handleChangeBranchList, handleGithubInfo, ideAdapter, setLoading]);

if (loading) {
return (
Expand Down
5 changes: 2 additions & 3 deletions packages/view/src/components/Detail/Detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
import { Tooltip } from "@mui/material";

import { Author } from "components/@common/Author";
import { useOwnerStore, useRepoStore } from "store";
import { useGithubInfo } from "store";

import { useCommitListHide } from "./Detail.hook";
import { getCommitListDetail } from "./Detail.util";
Expand Down Expand Up @@ -56,8 +56,7 @@ const Detail = ({ selectedData, clusterId, authSrcMap }: DetailProps) => {
const commitNodeListInCluster =
selectedData?.filter((selected) => selected.commitNodeList[0].clusterId === clusterId)[0].commitNodeList ?? [];
const { commitNodeList, toggle, handleToggle } = useCommitListHide(commitNodeListInCluster);
const { owner } = useOwnerStore();
const { repo } = useRepoStore();
const { owner, repo } = useGithubInfo();
const isShow = commitNodeListInCluster.length > FIRST_SHOW_NUM;
const handleCommitIdCopy = (id: string) => async () => {
navigator.clipboard.writeText(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { useEffect, useState } from "react";
import ArrowDropDownCircleRoundedIcon from "@mui/icons-material/ArrowDropDownCircleRounded";

import { useOwnerStore, useRepoStore } from "store";
import { useGithubInfo } from "store";

import type { ContentProps } from "../Summary.type";

const Content = ({ content, clusterId, selectedClusterId }: ContentProps) => {
const { owner } = useOwnerStore();
const { repo } = useRepoStore();
const { owner, repo } = useGithubInfo();
const [linkedStr, setLinkedStr] = useState<React.ReactNode[]>([]);

useEffect(() => {
Expand Down
9 changes: 9 additions & 0 deletions packages/view/src/ide/FakeIDEAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export default class FakeIDEAdapter implements IDEPort {
return events.handleChangeAnalyzedData(payload ? JSON.parse(payload) : undefined);
case "fetchBranchList":
return events.handleChangeBranchList(payload ? JSON.parse(payload) : undefined);
case "fetchGithubInfo":
return events.handleGithubInfo(payload ? JSON.parse(payload) : undefined);
default:
console.log("Unknown Message");
}
Expand Down Expand Up @@ -54,6 +56,13 @@ export default class FakeIDEAdapter implements IDEPort {
this.sendMessageToMe(message);
}

public sendFetchGithubInfo() {
const message: IDEMessage = {
command: "fetchGithubInfo",
};
this.sendMessageToMe(message);
}

public setCustomTheme(color: string) {
sessionStorage.setItem("PRIMARY_COLOR", color);
const message: IDEMessage = {
Expand Down
1 change: 1 addition & 0 deletions packages/view/src/ide/IDEPort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default interface IDEPort {
sendRefreshDataMessage: (payload?: string) => void;
sendFetchAnalyzedDataMessage: (payload?: string) => void;
sendFetchBranchListMessage: () => void;
sendFetchGithubInfo: () => void;
setCustomTheme: (theme: string) => void;
}
9 changes: 9 additions & 0 deletions packages/view/src/ide/VSCodeIDEAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export default class VSCodeIDEAdapter implements IDEPort {
return events.handleChangeAnalyzedData(payloadData);
case "fetchBranchList":
return events.handleChangeBranchList(payloadData);
case "fetchGithubInfo":
return events.handleGithubInfo(payloadData);
default:
console.log("Unknown Message");
}
Expand Down Expand Up @@ -49,6 +51,13 @@ export default class VSCodeIDEAdapter implements IDEPort {
this.sendMessageToIDE(message);
}

public sendFetchGithubInfo() {
const message: IDEMessage = {
command: "fetchGithubInfo",
};
this.sendMessageToIDE(message);
}

public setCustomTheme(theme: string) {
const message: IDEMessage = {
command: "updateCustomTheme",
Expand Down
20 changes: 20 additions & 0 deletions packages/view/src/store/githubInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { create } from "zustand";

export type githubInfo = {
owner: string;
repo: string;
};

export const useGithubInfo = create<
githubInfo & {
handleGithubInfo: (repoInfo: githubInfo) => void;
}
>((set) => ({
owner: "githru",
repo: "githru-vscode-ext",
handleGithubInfo: (repoInfo: githubInfo) => {
if (repoInfo) {
set({ owner: repoInfo.owner, repo: repoInfo.repo });
}
},
}));
3 changes: 1 addition & 2 deletions packages/view/src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from "./loading";
export * from "./filteredRange";
export * from "./branch";
export * from "./repo";
export * from "./owner";
export * from "./githubInfo";
export * from "./data";
11 changes: 0 additions & 11 deletions packages/view/src/store/owner.ts

This file was deleted.

11 changes: 0 additions & 11 deletions packages/view/src/store/repo.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/view/src/types/IDEMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export type IDEMessageCommandNames =
| "fetchAnalyzedData"
| "fetchBranchList"
| "fetchCurrentBranch"
| "fetchGithubInfo"
| "updateCustomTheme";
3 changes: 2 additions & 1 deletion packages/view/src/types/IDESentEvents.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { BranchListPayload } from "store";
import type { BranchListPayload, githubInfo } from "store";
import type { ClusterNode } from "types";

// triggered by ide response
export type IDESentEvents = {
handleChangeAnalyzedData: (analyzedData: ClusterNode[]) => void;
handleChangeBranchList: (branches: BranchListPayload) => void;
handleGithubInfo: (repoInfo: githubInfo) => void;
};
6 changes: 0 additions & 6 deletions packages/view/src/types/RemoteGitHubInfo.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export async function activate(context: vscode.ExtensionContext) {
}

const fetchBranches = async () => await getBranches(gitPath, currentWorkspacePath);
const gitConfig = await getGitConfig(gitPath, currentWorkspacePath, "origin");
const fetchGithubInfo = async () => getRepo(gitConfig);

const fetchCurrentBranch = async () => {
let branchName;
Expand All @@ -77,10 +79,7 @@ export async function activate(context: vscode.ExtensionContext) {
const initialBaseBranchName = await fetchCurrentBranch();
const fetchClusterNodes = async (baseBranchName = initialBaseBranchName) => {
const gitLog = await getGitLog(gitPath, currentWorkspacePath);
const gitConfig = await getGitConfig(gitPath, currentWorkspacePath, "origin");

const { owner, repo: initialRepo } = getRepo(gitConfig);
webLoader.setGlobalOwnerAndRepo(owner, initialRepo);
const repo = initialRepo[0];
const engine = new AnalysisEngine({
isDebugMode: true,
Expand All @@ -100,6 +99,7 @@ export async function activate(context: vscode.ExtensionContext) {
fetchClusterNodes,
fetchBranches,
fetchCurrentBranch,
fetchGithubInfo,
});

currentPanel = webLoader.getPanel();
Expand Down
19 changes: 10 additions & 9 deletions packages/vscode/src/webview-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class WebviewLoader implements vscode.Disposable {
context: vscode.ExtensionContext,
fetcher: GithruFetcherMap
) {
const { fetchClusterNodes, fetchBranches, fetchCurrentBranch } = fetcher;
const { fetchClusterNodes, fetchBranches, fetchCurrentBranch, fetchGithubInfo } = fetcher;
const viewColumn = vscode.ViewColumn.One;

//캐시 초기화
Expand Down Expand Up @@ -76,6 +76,14 @@ export default class WebviewLoader implements vscode.Disposable {
payload: branches,
});
}

if (command === "fetchGithubInfo") {
const githubInfo = await fetchGithubInfo();
await this.respondToMessage({
...message,
payload: githubInfo,
});
}

if (command === "updateCustomTheme") {
const colorCode = payload && JSON.parse(payload);
Expand Down Expand Up @@ -147,19 +155,12 @@ export default class WebviewLoader implements vscode.Disposable {
}
}

public setGlobalOwnerAndRepo(owner: string, repo: string) {
if (this._panel) {
this._panel.webview.postMessage({
command: "setGlobalOwnerAndRepo",
data: { owner, repo },
});
}
}
}

type GithruFetcher<D = unknown, P extends unknown[] = []> = (...params: P) => Promise<D>;
type GithruFetcherMap = {
fetchClusterNodes: GithruFetcher<ClusterNode[], [string]>;
fetchBranches: GithruFetcher<{ branchList: string[]; head: string | null }>;
fetchCurrentBranch: GithruFetcher<string>;
fetchGithubInfo: GithruFetcher<{ owner: string; repo: string }>;
};

0 comments on commit 22a6783

Please sign in to comment.