diff --git a/packages/view/src/App.tsx b/packages/view/src/App.tsx index 29e5da71..53632d8a 100644 --- a/packages/view/src/App.tsx +++ b/packages/view/src/App.tsx @@ -16,6 +16,7 @@ import type IDEPort from "ide/IDEPort"; import { useGlobalData } from "hooks"; import { RefreshButton } from "components/RefreshButton"; import type { IDESentEvents } from "types/IDESentEvents"; +import type { RemoteGitHubInfo } from "types/RemoteGitHubInfo"; const App = () => { const initRef = useRef(false); @@ -39,6 +40,18 @@ const App = () => { } }, [handleChangeAnalyzedData, handleChangeBranchList, ideAdapter, setLoading]); + const { setOwner, setRepo } = useGlobalData(); + useEffect(() => { + const handleMessage = (event: MessageEvent) => { + const message = event.data; + setOwner(message.data.owner); + setRepo(message.data.repo); + }; + + window.addEventListener("message", handleMessage); + return () => window.removeEventListener("message", handleMessage); + }, []); + if (loading) { return ( { - const str: string = content.message; - const regex = /^(\(#[0-9]+\)|#[0-9]+)/g; - const tobeStr: string[] = str.split(" "); + const { owner, repo } = useGlobalData(); + const [linkedStr, setLinkedStr] = useState([]); + + useEffect(() => { + const str: string = content.message; + const regex = /^(\(#[0-9]+\)|#[0-9]+)/g; + const tobeStr: string[] = str.split(" "); - const linkedStr = tobeStr.reduce((acc: React.ReactNode[], tokenStr: string) => { - const matches = tokenStr.match(regex); // #num 으로 결과가 나옴 ()가 결과에 포함되지 않음 - if (matches) { - const matchedStr = matches[0]; - const matchedStrNum: string = matchedStr.substring(1); - const linkIssues = `https://github.com/githru/githru-vscode-ext/issues/${matchedStrNum}`; - acc.push( - - {matchedStr} - - ); - acc.push(" "); - } else { - acc.push(tokenStr); - acc.push(" "); - } - return acc; + const newLinkedStr = tobeStr.reduce((acc: React.ReactNode[], tokenStr: string) => { + const matches = tokenStr.match(regex); // #num 으로 결과가 나옴 ()가 결과에 포함되지 않음 + if (matches) { + const matchedStr = matches[0]; + const matchedStrNum: string = matchedStr.substring(1); + const linkIssues = `https://github.com/${owner}/${repo}/issues/${matchedStrNum}`; + acc.push( + + {matchedStr} + + ); + acc.push(" "); + } else { + acc.push(tokenStr); + acc.push(" "); + } + return acc; + }, []); + setLinkedStr(newLinkedStr); }, []); return ( diff --git a/packages/view/src/context/GlobalDataProvider.tsx b/packages/view/src/context/GlobalDataProvider.tsx index 6a3ace09..22040f25 100644 --- a/packages/view/src/context/GlobalDataProvider.tsx +++ b/packages/view/src/context/GlobalDataProvider.tsx @@ -14,6 +14,8 @@ export const GlobalDataProvider = ({ children }: PropsWithChildren) => { const [branchList, setBranchList] = useState([]); const [selectedBranch, setSelectedBranch] = useState(branchList?.[0]); + const [owner, setOwner] = useState(""); + const [repo, setRepo] = useState(""); const handleChangeBranchList = (branches: { branchList: string[]; head: string | null }) => { setSelectedBranch((prev) => (!prev && branches.head ? branches.head : prev)); @@ -44,8 +46,12 @@ export const GlobalDataProvider = ({ children }: PropsWithChildren) => { setSelectedBranch, handleChangeAnalyzedData, handleChangeBranchList, + owner, + setOwner, + repo, + setRepo, }), - [data, filteredRange, filteredData, selectedData, branchList, selectedBranch, loading] + [data, filteredRange, filteredData, selectedData, branchList, selectedBranch, loading, owner, repo] ); return {children}; diff --git a/packages/view/src/hooks/useGlobalData.ts b/packages/view/src/hooks/useGlobalData.ts index 06021703..7401cd4e 100644 --- a/packages/view/src/hooks/useGlobalData.ts +++ b/packages/view/src/hooks/useGlobalData.ts @@ -24,6 +24,10 @@ type GlobalDataState = { setBranchList: Dispatch>; selectedBranch: string; setSelectedBranch: Dispatch>; + owner: string; + setOwner: Dispatch>; + repo: string; + setRepo: Dispatch>; } & IDESentEvents; export const GlobalDataContext = createContext(undefined); diff --git a/packages/view/src/types/RemoteGitHubInfo.ts b/packages/view/src/types/RemoteGitHubInfo.ts new file mode 100644 index 00000000..c2564690 --- /dev/null +++ b/packages/view/src/types/RemoteGitHubInfo.ts @@ -0,0 +1,6 @@ +export interface RemoteGitHubInfo { + data: { + owner: string; + repo: string; + }; +} diff --git a/packages/vscode/src/extension.ts b/packages/vscode/src/extension.ts index 2fc0c8e6..a046b354 100644 --- a/packages/vscode/src/extension.ts +++ b/packages/vscode/src/extension.ts @@ -4,7 +4,7 @@ import * as vscode from "vscode"; import { COMMAND_LAUNCH, COMMAND_LOGIN_WITH_GITHUB, COMMAND_RESET_GITHUB_AUTH } from "./commands"; import { Credentials } from "./credentials"; import { GithubTokenUndefinedError, WorkspacePathUndefinedError } from "./errors/ExtensionError"; -import { deleteGithubToken, getGithubToken, setGithubToken, } from "./setting-repository"; +import { deleteGithubToken, getGithubToken, setGithubToken } from "./setting-repository"; import { mapClusterNodesFrom } from "./utils/csm.mapper"; import { findGit, @@ -18,7 +18,7 @@ import { import WebviewLoader from "./webview-loader"; let myStatusBarItem: vscode.StatusBarItem; -const projectName = 'githru'; +const projectName = "githru"; function normalizeFsPath(fsPath: string) { return fsPath.replace(/\\/g, "/"); @@ -58,12 +58,11 @@ export async function activate(context: vscode.ExtensionContext) { const fetchBranches = async () => await getBranches(gitPath, currentWorkspacePath); const fetchCurrentBranch = async () => { - let branchName; try { - branchName = await getCurrentBranchName(gitPath, currentWorkspacePath) + branchName = await getCurrentBranchName(gitPath, currentWorkspacePath); } catch (error) { - console.error(error); + console.error(error); } if (!branchName) { @@ -77,7 +76,9 @@ export async function activate(context: vscode.ExtensionContext) { const fetchClusterNodes = async (baseBranchName = initialBaseBranchName) => { const gitLog = await getGitLog(gitPath, currentWorkspacePath); const gitConfig = await getGitConfig(gitPath, currentWorkspacePath, "origin"); - const { owner, repo } = getRepo(gitConfig); + const { owner, repo: initialRepo } = getRepo(gitConfig); + webLoader.setGlobalOwnerAndRepo(owner, initialRepo); + const repo = initialRepo[0]; const engine = new AnalysisEngine({ isDebugMode: true, gitLog, diff --git a/packages/vscode/src/utils/git.util.ts b/packages/vscode/src/utils/git.util.ts index cd1fc5f9..25a4ab22 100644 --- a/packages/vscode/src/utils/git.util.ts +++ b/packages/vscode/src/utils/git.util.ts @@ -208,7 +208,7 @@ export async function getGitConfig( export const getRepo = (gitRemoteConfig: string) => { const gitRemoteConfigPattern = - /(?:https?|git)(?::\/\/(?:\w+@)?|@)(?:github\.com)(?:\/|:)(?:(?.+?)\/(?.+?))(?:\.git|\/)?(\S*)$/m; + /(?:https?|git)(?::\/\/(?:\w+@)?|@)(?:github\.com)(?:\/|:)(?:(?[^/]+?)\/(?[^/.]+))(?:\.git|\/)?(\S*)$/m; const gitRemote = gitRemoteConfig.match(gitRemoteConfigPattern)?.groups; if (!gitRemote) { throw new Error("git remote config should be: [https?://|git@]${domain}/${owner}/${repo}.git"); diff --git a/packages/vscode/src/webview-loader.ts b/packages/vscode/src/webview-loader.ts index 3715fae3..7453d91e 100644 --- a/packages/vscode/src/webview-loader.ts +++ b/packages/vscode/src/webview-loader.ts @@ -111,6 +111,14 @@ export default class WebviewLoader implements vscode.Disposable { `; return returnString; } + public setGlobalOwnerAndRepo(owner: string, repo: string) { + if (this._panel) { + this._panel.webview.postMessage({ + command: "setGlobalOwnerAndRepo", + data: { owner, repo }, + }); + } + } } type GithruFetcher = (...params: P) => Promise;