Skip to content

Commit

Permalink
feat: useful sorting first by priority then time
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Nov 29, 2023
1 parent c3fb7da commit 4b950d4
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 64 deletions.
90 changes: 57 additions & 33 deletions static/main-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,66 @@ export async function mainModule() {
if (!container) {
throw new Error("Could not find issues container");
}
await fetchIssues();

async function fetchIssues() {
try {
const cachedIssues = localStorage.getItem("githubIssues");

if (cachedIssues) {
try {
const issues = JSON.parse(cachedIssues);
const sortedIssues = sortIssuesByComments(issues);
displayIssues(container, sortedIssues);
} catch (error) {
console.error(error);
}
}
await fetchIssues(container);
}

const octokit = new Octokit();
function sortIssuesByPriority(issues: GitHubIssue[]) {
return issues.sort((a, b) => {
const priorityRegex = /Priority: (\d+)/;
const aPriorityMatch = a.labels.find((label) => priorityRegex.test(label.name));
const bPriorityMatch = b.labels.find((label) => priorityRegex.test(label.name));
const aPriority = aPriorityMatch ? parseInt(aPriorityMatch.name.match(priorityRegex)![1], 10) : 0;
const bPriority = bPriorityMatch ? parseInt(bPriorityMatch.name.match(priorityRegex)![1], 10) : 0;
return bPriority - aPriority;
});
}

const freshIssues = (await octokit.paginate("GET /repos/ubiquity/devpool-directory/issues")) as GitHubIssue[];
localStorage.setItem("githubIssues", JSON.stringify(freshIssues));
const sortedIssues = sortIssuesByComments(freshIssues);
displayIssues(container, sortedIssues);
} catch (error) {
container.innerHTML = `<p>Error loading issues: ${error}</p>`;
}
}
function sortIssuesByTime(issues: GitHubIssue[]) {
return issues.sort((a, b) => {
const aTimeValue = a.labels.reduce((acc, label) => acc + calculateLabelValue(label.name), 0);
const bTimeValue = b.labels.reduce((acc, label) => acc + calculateLabelValue(label.name), 0);
return bTimeValue - aTimeValue;
});
}

function sortIssuesByComments(issues: GitHubIssue[]) {
return issues.sort((a, b) => {
if (a.comments > b.comments) {
return -1;
}
if (a.comments < b.comments) {
return 1;
export function calculateLabelValue(label: string): number {
const matches = label.match(/\d+/);
const number = matches && matches.length > 0 ? parseInt(matches[0]) || 0 : 0;
if (label.toLowerCase().includes("priority")) return number;
// throw new Error(`Label ${label} is not a priority label`);
if (label.toLowerCase().includes("minute")) return number * 0.002;
if (label.toLowerCase().includes("hour")) return number * 0.125;
if (label.toLowerCase().includes("day")) return 1 + (number - 1) * 0.25;
if (label.toLowerCase().includes("week")) return number + 1;
if (label.toLowerCase().includes("month")) return 5 + (number - 1) * 8;
return 0;
}

async function fetchIssues(container: HTMLDivElement) {
try {
const cachedIssues = localStorage.getItem("githubIssues");

if (cachedIssues) {
try {
const issues = JSON.parse(cachedIssues);
const sortedIssuesByTime = sortIssuesByTime(issues);
const sortedIssuesByPriority = sortIssuesByPriority(sortedIssuesByTime);
displayIssues(container, sortedIssuesByPriority);
} catch (error) {
console.error(error);
}
return 0;
});
}

const octokit = new Octokit();

const freshIssues = (await octokit.paginate("GET /repos/ubiquity/devpool-directory/issues", {
state: "open",
})) as GitHubIssue[];
localStorage.setItem("githubIssues", JSON.stringify(freshIssues));
const sortedIssuesByTime = sortIssuesByTime(freshIssues);
const sortedIssuesByPriority = sortIssuesByPriority(sortedIssuesByTime);
displayIssues(container, sortedIssuesByPriority);
} catch (error) {
container.innerHTML = `<p>Error loading issues: ${error}</p>`;
}
}
37 changes: 6 additions & 31 deletions static/scripts/display-issues.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GitHubIssue } from "../github-types";

window.addEventListener("scroll", updateScale);
updateScale();

export function displayIssues(container: HTMLDivElement, issues: GitHubIssue[]) {
const existingIssueIds = new Set(Array.from(container.querySelectorAll(".issue-element-inner")).map((element) => element.getAttribute("data-issue-id")));

// container.innerHTML = "";

let delay = 0;
const baseDelay = 1000 / 15; // Base delay in milliseconds

Expand All @@ -19,8 +20,6 @@ export function displayIssues(container: HTMLDivElement, issues: GitHubIssue[])

delay += baseDelay;

// Parse organization name and repository name from the issue's URL

const urlRegex = /(https?:\/\/[^\s]+)/g;
const mirrorUrls = issue.body.match(urlRegex);

Expand Down Expand Up @@ -66,7 +65,6 @@ export function displayIssues(container: HTMLDivElement, issues: GitHubIssue[])

issueElement.addEventListener("click", () => {
console.log(issue);
// console.log(match);
window.open(match?.input, "_blank");
});

Expand All @@ -86,21 +84,7 @@ export function displayIssues(container: HTMLDivElement, issues: GitHubIssue[])
});
}

// const issueRowHeight = container.querySelector(".issue-element-wrapper")?.clientHeight || 0;
// const viewportHeight = window.innerHeight;
// const rowsInView = Math.floor(viewportHeight / issueRowHeight);

// Function to update the scale of issue elements based on their position in the viewport

// window.addEventListener("scroll", updateScale);
// updateScale();
// Append the issue element after the delay
// setTimeout(() => {
container.appendChild(issueWrapper);
// Trigger the animation by adding the 'visible' class
// issueWrapper.classList.add("active");
// updateScale();
// }, delay);
}
});
}
Expand All @@ -113,27 +97,22 @@ function updateScale() {
const elementBottom = bounds.bottom; // Get the bottom position of the element

let scale;
// , blurValue;

const SPECIAL = (viewportHeight - 32) / viewportHeight;
const OFFSET = (viewportHeight - 32) / viewportHeight;

if (elementBottom <= viewportHeight * SPECIAL) {
if (elementBottom <= viewportHeight * OFFSET) {
// If the bottom of the element is above the bottom of the viewport, it's at full scale
scale = 1;
// blurValue = 0;
} else {
// Calculate the distance from the bottom of the viewport
const distanceFromBottom = elementBottom - viewportHeight;
// Normalize the distance based on the height of the viewport
const distanceRatio = distanceFromBottom / viewportHeight;

// The scale decreases linearly from the bottom of the viewport to the bottom edge of the element
scale = SPECIAL - distanceRatio;
scale = OFFSET - distanceRatio;
// Ensure the scale does not go below 0.5
scale = Math.max(scale, 0.5);

// Blur increases as the element moves below the viewport
// blurValue = distanceRatio * 1000; // Adjust the multiplier as needed
}

// element.style.transform = `scale(${scale}) translateX(${- scale}vw)`;
Expand All @@ -147,7 +126,3 @@ function updateScale() {
}
});
}

// Call updateScale initially and on scroll
window.addEventListener("scroll", updateScale);
updateScale();

0 comments on commit 4b950d4

Please sign in to comment.