-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
189 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
// OpenSauced constants | ||
export const SUPABASE_LOGIN_URL = "https://ibcwmlhcimymasokhgvn.supabase.co/auth/v1/authorize?provider=github&redirect_to=https://insights.opensauced.pizza/"; | ||
export const SUPABASE_AUTH_COOKIE_NAME = "supabase-auth-token"; | ||
export const OPEN_SAUCED_AUTH_TOKEN_KEY = "os-access-token"; | ||
export const OPEN_SAUCED_INSIGHTS_DOMAIN = "insights.opensauced.pizza"; | ||
|
||
// API endpoints | ||
export const OPEN_SAUCED_USERS_ENDPOINT = "https://api.opensauced.pizza/v1/users"; | ||
export const OPEN_SAUCED_REPOS_ENDPOINT = "https://api.opensauced.pizza/v1/repos"; | ||
export const OPEN_SAUCED_SESSION_ENDPOINT = "https://api.opensauced.pizza/v1/auth/session"; | ||
export const OPEN_SAUCED_INSIGHTS_DOMAIN = "insights.opensauced.pizza"; | ||
|
||
// GitHub constants/selectors | ||
export const GITHUB_PROFILE_MENU_SELECTOR = ".p-nickname.vcard-username.d-block"; | ||
export const GITHUB_PROFILE_EDIT_MENU_SELECTOR = "button.js-profile-editable-edit-button"; | ||
export const GITHUB_PR_AUTHOR_USERNAME_SELECTOR = "author Link--primary text-bold css-overflow-wrap-anywhere"; | ||
export const GITHUB_LOGGED_IN_USER_USERNAME_SELECTOR = "meta[name=\"user-login\"]"; | ||
export const GITHUB_PR_COMMENT_HEADER_SELECTOR = "timeline-comment-header clearfix d-flex"; | ||
export const GITHUB_REPO_ACTIONS_SELECTOR = ".pagehead-actions"; |
36 changes: 36 additions & 0 deletions
36
src/content-scripts/components/RepoVoting/RepoUnvoteButton.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import "../../content-scripts.css"; | ||
import { createHtmlElement } from "../../../utils/createHtmlElement"; | ||
import { getAuthToken } from "../../../utils/checkAuthentication"; | ||
import { VoteRepoButton } from "./RepoVoteButton"; | ||
import { voteOrUnvoteRepo } from "../../../utils/fetchOpenSaucedApiData"; | ||
|
||
export const RepoUnvoteButton = (ownerName: string, repoName: string) => { | ||
const repoUnvoteButton = createHtmlElement("li", { | ||
className: | ||
"text-white text-center hover:shadow-button bg-gradient-to-r from-[#e67e22] to-[#d35400] btn btn-sm", | ||
innerHTML: ` | ||
<span>Unvote</span> | ||
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" class="align-middle"><g><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm-.997-6l7.07-7.071-1.414-1.414-5.656 5.657-2.829-2.829-1.414 1.414L11.003 16z"></path></g></svg> | ||
`, | ||
}); | ||
|
||
repoUnvoteButton.addEventListener("click", async () => { | ||
repoUnvoteButton.innerHTML = ` | ||
<span>Loading...</span> | ||
`; | ||
const userToken = await getAuthToken(); | ||
|
||
const unvoted = await voteOrUnvoteRepo(userToken, ownerName, repoName, false); | ||
|
||
if (unvoted) { | ||
const voteRepoButton = VoteRepoButton(ownerName, repoName); | ||
|
||
repoUnvoteButton.replaceWith(voteRepoButton); | ||
} else { | ||
console.log("Something went wrong"); | ||
} | ||
}); | ||
|
||
|
||
return repoUnvoteButton; | ||
}; |
35 changes: 35 additions & 0 deletions
35
src/content-scripts/components/RepoVoting/RepoVoteButton.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import "../../content-scripts.css"; | ||
import { createHtmlElement } from "../../../utils/createHtmlElement"; | ||
import { getAuthToken } from "../../../utils/checkAuthentication"; | ||
import { RepoUnvoteButton } from "./RepoUnvoteButton"; | ||
import { voteOrUnvoteRepo } from "../../../utils/fetchOpenSaucedApiData"; | ||
|
||
export const VoteRepoButton = (ownerName: string, repoName: string) => { | ||
const voteRepoButton = createHtmlElement("li", { | ||
className: | ||
"text-white text-center hover:shadow-button bg-gradient-to-r from-[#e67e22] to-[#d35400] btn btn-sm", | ||
innerHTML: ` | ||
<span>Upvote</span> | ||
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 24 24" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" class="align-middle"><g><path fill="none" d="M0 0h24v24H0z"></path><path d="M12 22C6.477 22 2 17.523 2 12S6.477 2 12 2s10 4.477 10 10-4.477 10-10 10zm-.997-6l7.07-7.071-1.414-1.414-5.656 5.657-2.829-2.829-1.414 1.414L11.003 16z"></path></g></svg> | ||
`, | ||
}); | ||
|
||
voteRepoButton.addEventListener("click", async () => { | ||
voteRepoButton.innerHTML = ` | ||
<span>Loading...</span> | ||
`; | ||
const userToken = await getAuthToken(); | ||
|
||
const voted = await voteOrUnvoteRepo(userToken, ownerName, repoName, false); | ||
|
||
if (voted) { | ||
const unvoteRepoButton = RepoUnvoteButton(ownerName, repoName); | ||
|
||
voteRepoButton.replaceWith(unvoteRepoButton); | ||
} else { | ||
console.log("Something went wrong"); | ||
} | ||
}); | ||
|
||
return voteRepoButton; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { GITHUB_REPO_ACTIONS_SELECTOR } from "../../constants"; | ||
import { VoteRepoButton } from "../../content-scripts/components/RepoVoting/RepoVoteButton"; | ||
import { RepoUnvoteButton } from "../../content-scripts/components/RepoVoting/RepoUnvoteButton"; | ||
import { isLoggedIn, getAuthToken } from "../checkAuthentication"; | ||
import { | ||
checkUserVotedRepo, | ||
repoExistsOnOpenSauced, | ||
} from "../fetchOpenSaucedApiData"; | ||
|
||
const injectRepoVotingButtons = async (ownerName: string, repoName: string) => { | ||
if (!(await isLoggedIn())) { | ||
return; | ||
} | ||
if (!(await repoExistsOnOpenSauced(ownerName, repoName))) { | ||
return; | ||
} | ||
const repoActions = document.querySelector(GITHUB_REPO_ACTIONS_SELECTOR); | ||
|
||
if (!repoActions) { | ||
return; | ||
} | ||
|
||
const voteRepoButton = VoteRepoButton(ownerName, repoName); | ||
const repoUnvoteButton = RepoUnvoteButton(ownerName, repoName); | ||
const userToken = await getAuthToken(); | ||
|
||
const userVotedRepo = await checkUserVotedRepo(userToken, repoName); | ||
|
||
if (userVotedRepo) { | ||
if (repoActions.lastChild?.isEqualNode(repoUnvoteButton)) { | ||
return; | ||
} | ||
repoActions.appendChild(repoUnvoteButton); | ||
} else { | ||
if (repoActions.lastChild?.isEqualNode(voteRepoButton)) { | ||
return; | ||
} | ||
repoActions.appendChild(voteRepoButton); | ||
} | ||
}; | ||
|
||
export default injectRepoVotingButtons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters