diff --git a/package.json b/package.json index e4b1421ba..26949c282 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "leetcode-mistake-tracker", - "version": "1.1.0", + "version": "2.0.0", "description": "Help LeetCode users keep track of the LeetCode questions they would like to re-do in the future", "license": "MIT", "repository": { diff --git a/src/helper.js b/src/helper.js index 0fd5a23cb..3bbb54641 100644 --- a/src/helper.js +++ b/src/helper.js @@ -11,3 +11,40 @@ export const isPast = (someDate) => { const today = new Date(); return someDate < today; }; + +export const getTitleFromUrl = (redoURI) => { + const spliceStart = redoURI.indexOf('problems/') + 'problems/'.length; + const spliceEnd = redoURI.indexOf('/', spliceStart); + let displayTitle = redoURI.slice(spliceStart, spliceEnd); + const lastSlash = displayTitle.lastIndexOf('/'); + displayTitle = displayTitle.slice(lastSlash + 1); + return displayTitle; +} + +/** + * + * @param {ISOString} startDate + * @param {ISOString} endDate + * @source https://stackoverflow.com/a/13904120 + * @returns difference in minutes + */ +function diffInMinutes(startDate, endDate) { + // get total seconds between the times + var delta = Math.abs(endDate - startDate) / 1000; + + // calculate (and subtract) whole days + var days = Math.floor(delta / 86400); + delta -= days * 86400; + + // calculate (and subtract) whole hours + var hours = Math.floor(delta / 3600) % 24; + delta -= hours * 3600; + + // calculate (and subtract) whole minutes + var minutes = Math.floor(delta / 60) % 60; + delta -= minutes * 60; + + // what's left is seconds + var seconds = delta % 60; // in theory the modulus is not required + return minutes; +} \ No newline at end of file diff --git a/src/pages/Background/index.js b/src/pages/Background/index.js index a7b5f7855..8b1378917 100644 --- a/src/pages/Background/index.js +++ b/src/pages/Background/index.js @@ -1,36 +1 @@ -// const main = () => { -// chrome.storage.sync.get('data', function (items) { -// let redos = items.data; -// printLine('Checking for Notifications'); -// for(let i = 0; i < redos.length; i++) { -// const reminderDate = new Date(redos[i].reminderDate); -// console.log(`isToday: `, isToday(reminderDate)); -// if (isToday(reminderDate) || isPast(reminderDate)) { -// console.log(`${redos[i].id} isToday`) -// var opt = { -// type: "basic", -// title: "Primary Title", -// message: "Primary message to display", -// iconUrl: "url_to_small_icon" -// } -// chrome.notifications.create(redos[i].uri, opt); -// return; -// } -// } -// }); -// } - -// main(); -// console.log("Please wait..."); -// chrome.runtime.onInstalled.addListener((reason) => { -// chrome.runtime.onInstalled.addListener(function(details){ -// if(details.reason == "install"){ -// //call a function to handle a first install -// console.log("install") -// }else if(details.reason == "update"){ -// //call a function to handle an update -// console.log("update") -// } -// }); -// }); diff --git a/src/pages/Content/index.js b/src/pages/Content/index.js index db33ab175..e2925034e 100644 --- a/src/pages/Content/index.js +++ b/src/pages/Content/index.js @@ -1,5 +1,5 @@ import { printLine } from './modules/print'; -import { isToday, isPast } from './../../helper'; +import { getTitleFromUrl } from './../../helper' printLine('Content Script loaded'); @@ -28,20 +28,21 @@ const timeValue = setInterval(function () { theButton.addEventListener('click', function () { chrome.storage.sync.get('data', function (items) { let redos; + const title = getTitleFromUrl(redo.uri); if (Object.keys(items).length === 0) { printLine('items is empty'); redos = []; } else { redos = items.data; for (let i = 0; i < redos.length; i++) { - console.log(redos[i].uri === redo.uri); - if (redos[i].uri === redo.uri) { + if (redos[i].id === title) { alert('You already marked this question'); return; } } } - redo.id = redos.length + 1; + // redo.id = redos.length + 1; + redo.id = title; redos.push(redo); redos.sort(function (a, b) { return new Date(a.reminderDate) - new Date(b.reminderDate); diff --git a/src/pages/Popup/Popup.jsx b/src/pages/Popup/Popup.jsx index 8c02cd085..6bfd4bb87 100644 --- a/src/pages/Popup/Popup.jsx +++ b/src/pages/Popup/Popup.jsx @@ -24,21 +24,9 @@ import AssignmentIcon from '@mui/icons-material/Assignment'; import DeleteIcon from '@mui/icons-material/Delete'; import { ListItemSecondaryAction } from '@mui/material'; -function WithoutTime(dateTime) { - var date = new Date(dateTime); - date.setHours(0, 0, 0, 0); - return date; -} - -function ISOStringToDate(ISOString) { - return ISOString.slice(0, 10); -} +// helper functions +import { getTitleFromUrl } from '../../helper'; -function diffInDays(startDate, endDate) { - const diffInMs = new Date(endDate) - new Date(startDate); - const diffInDays = diffInMs / (1000 * 60 * 60 * 24); - return Math.floor(diffInDays); -} class Popup extends Component { constructor(props) { @@ -97,17 +85,22 @@ class Popup extends Component { render() { const itemsList = this.state.data.map((redo) => { const redoURI = redo.uri; - const spliceStart = redoURI.indexOf('problems/') + 'problems/'.length; - const spliceEnd = redoURI.indexOf('/', spliceStart); - let displayTitle = redo.uri.slice(spliceStart, spliceEnd); - const lastSlash = displayTitle.lastIndexOf('/'); - displayTitle = displayTitle.slice(lastSlash + 1); - const today = new Date().toISOString(); - let daysAway = diffInDays( - ISOStringToDate(today), - ISOStringToDate(redo.reminderDate) - ); - const avatarBgColor = daysAway <= 0 ? red[700] : green[700]; + const today = new Date(); + + var delta = (new Date(redo.reminderDate) - today) / 1000; + // calculate (and subtract) whole days + var days = Math.floor(delta / 86400); + delta -= days * 86400; + // calculate (and subtract) whole hours + var hours = Math.floor(delta / 3600) % 24; + delta -= hours * 3600; + // calculate (and subtract) whole minutes + var minutes = Math.floor(delta / 60) % 60; + delta -= minutes * 60; + // what's left is seconds + var seconds = delta % 60; + + const avatarBgColor = (new Date(redo.reminderDate) - today) <= 0 ? red[700] : green[700]; return (