Skip to content

Commit

Permalink
Codestyle updates and bugfixes (#31)
Browse files Browse the repository at this point in the history
* Stashed changes. Don't remember what I was doing half a year ago

* Stashed changes. Don't remember what I was doing half a year ago

* merged package-lock

* Fixed an issue where app would get stuck without record and refused to create a new one

* Removed dashboard index file
  • Loading branch information
PsychoSanchez authored Feb 3, 2024
1 parent d2357ef commit d8a5b8f
Show file tree
Hide file tree
Showing 83 changed files with 3,002 additions and 2,312 deletions.
11 changes: 6 additions & 5 deletions .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
"arrowParens": "always",
"bracketSpacing": true,
"printWidth": 80,
"plugins": [
"@trivago/prettier-plugin-sort-imports"
],
"importOrder": [
"^[a-z]",
"^../../",
"^../[a-z]*",
"^\\./.*(?<!css)$",
"css$"
"<THIRD_PARTY_MODULES>",
"^@shared/(.*)$",
"^[./]"
],
"importOrderSeparation": true
}
3,775 changes: 2,191 additions & 1,584 deletions package-lock.json

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/PsychoSanchez/web-activity-plugin#readme",
"devDependencies": {
"@trivago/prettier-plugin-sort-imports": "^2.0.2",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/chrome": "^0.0.203",
"@types/debounce": "^1.2.0",
"@types/jest": "^29.2.3",
Expand All @@ -44,26 +44,26 @@
"@types/react-dom": "^17.0.5",
"@types/throttle": "^1.0.0",
"@types/throttle-debounce": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"@typescript-eslint/eslint-plugin": "^6.12.0",
"@typescript-eslint/parser": "^6.12.0",
"autoprefixer": "^10.4.12",
"copy-webpack-plugin": "^8.1.1",
"css-loader": "^5.2.4",
"eslint": "^8.28.0",
"eslint-plugin-react": "^7.31.11",
"eslint": "^8.54.0",
"eslint-plugin-react": "^7.33.2",
"eslint-plugin-react-hooks": "^4.6.0",
"husky": "^8.0.0",
"jest": "^29.3.1",
"mocha": "^10.1.0",
"postcss": "^8.4.18",
"postcss-loader": "^7.0.1",
"prettier": "^2.3.0",
"prettier": "^3.1.0",
"react": "^17.0.2",
"style-loader": "^3.3.1",
"tailwindcss": "^3.1.8",
"ts-jest": "^29.0.3",
"ts-loader": "^9.4.1",
"typescript": "^4.8.4",
"typescript": "^5.3.2",
"web-ext-types": "^3.2.1",
"webpack": "^5.76.0",
"webpack-cli": "^4.7.0"
Expand All @@ -76,6 +76,7 @@
"react-github-contribution-calendar": "^2.0.0",
"react-tooltip": "^4.2.21",
"tailwind-merge": "^1.6.2",
"throttle-debounce": "^3.0.1"
"throttle-debounce": "^3.0.1",
"ts-pattern": "^4.2.1"
}
}
7 changes: 4 additions & 3 deletions src/background.index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import { Tab } from '@shared/browser-api.types';
import { WAKE_UP_BACKGROUND } from '@shared/messages';
import { logMessage } from '@shared/tables/logs';

import { getTabInfo } from './background/browser-api/tabs';
import { handleStateChange } from './background/controller';
import {
Expand All @@ -7,9 +11,6 @@ import {
handleTabUpdate,
handleWindowFocusChange,
} from './background/services/state-service';
import { logMessage } from './shared/tables/logs';
import { Tab } from './shared/browser-api.types';
import { WAKE_UP_BACKGROUND } from './shared/messages';

const ASYNC_POLL_ALARM_NAME = 'async-poll';
const ASYNC_POLL_INTERVAL_MINUTES = 1;
Expand Down
8 changes: 4 additions & 4 deletions src/background/browser-api/badge.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ignore } from '../../shared/utils/errors';
import { ignore } from '@shared/utils/errors';

import { isTabNotExistError } from './errors';

Expand All @@ -19,22 +19,22 @@ export const setActionBadge = async ({

export const hideBadge = async (tabId: number) => {
await Promise.all([chromeActionSetBadgeText(tabId, '')]).catch(
ignore(isTabNotExistError)
ignore(isTabNotExistError),
);
};

function chromeActionSetBadgeColor(tabId: number, color: string) {
return new Promise<void>((resolve) =>
chrome.action?.setBadgeBackgroundColor
? chrome.action.setBadgeBackgroundColor({ color, tabId }, resolve)
: resolve()
: resolve(),
);
}

function chromeActionSetBadgeText(tabId: number, text: string) {
return new Promise<void>((resolve) =>
chrome.action?.setBadgeText
? chrome.action.setBadgeText({ text, tabId }, resolve)
: resolve()
: resolve(),
);
}
4 changes: 2 additions & 2 deletions src/background/browser-api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function isTabNotExistError(error: unknown): error is Error {
}

export function isCouldNotEstablishConnectionError(
error: unknown
error: unknown,
): error is Error {
return (
error instanceof Error &&
Expand All @@ -29,7 +29,7 @@ export function isUserDraggingWindowError(error: unknown): error is Error {
}

export function isExtensionContextInvalidatedError(
error: unknown
error: unknown,
): error is Error {
return (
error instanceof Error &&
Expand Down
8 changes: 4 additions & 4 deletions src/background/browser-api/tabs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActiveTabState } from '../../shared/db/types';
import { LIMIT_EXCEEDED, LIMIT_OK } from '../../shared/messages';
import { ignore } from '../../shared/utils/errors';
import { ActiveTabState } from '@shared/db/types';
import { LIMIT_EXCEEDED, LIMIT_OK } from '@shared/messages';
import { ignore } from '@shared/utils/errors';

import {
isCouldNotEstablishConnectionError,
Expand Down Expand Up @@ -45,7 +45,7 @@ export const getFocusedTab = async () => {

export const getTabFromFocusedWindow = async (
windowId: number,
tabId: number
tabId: number,
): Promise<Partial<ActiveTabState>> => {
const activeTabWindow = await chrome.windows.get(windowId);
if (!activeTabWindow.focused) {
Expand Down
15 changes: 6 additions & 9 deletions src/background/controller/active.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { Tab } from '../../shared/browser-api.types';
import { TimelineRecord } from '../../shared/db/types';
import {
getActiveTabRecord,
setActiveTabRecord,
} from '../../shared/tables/state';
import { getIsoDate } from '../../shared/utils/dates-helper';
import { getHostNameFromUrl } from '../../shared/utils/url';
import { Tab } from '@shared/browser-api.types';
import { TimelineRecord } from '@shared/db/types';
import { getActiveTabRecord, setActiveTabRecord } from '@shared/tables/state';
import { getIsoDate } from '@shared/utils/dates-helper';
import { getHostNameFromUrl } from '@shared/utils/url';

export class ActiveTimelineRecordDao {
private record: null | Promise<TimelineRecord | null> = null;
Expand All @@ -32,7 +29,7 @@ export class ActiveTimelineRecordDao {

export async function createNewActiveRecord(
timestamp: number,
focusedActiveTab: Tab
focusedActiveTab: Tab,
) {
if (!focusedActiveTab.id) {
return;
Expand Down
14 changes: 7 additions & 7 deletions src/background/controller/badge.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Tab } from '../../shared/browser-api.types';
import { getCurrentHostTime } from '../../shared/db/sync-storage';
import { TimelineRecord } from '../../shared/db/types';
import { presentHoursOrMinutesFromMinutes } from '../../shared/utils/dates-helper';
import { getHostNameFromUrl } from '../../shared/utils/url';
import { Tab } from '@shared/browser-api.types';
import { getCurrentHostTime } from '@shared/db/sync-storage';
import { TimelineRecord } from '@shared/db/types';
import { presentHoursOrMinutesFromMinutes } from '@shared/utils/dates-helper';
import { getHostNameFromUrl } from '@shared/utils/url';

import { setActionBadge, hideBadge } from '../browser-api/badge';

export async function updateTimeOnBadge(
focusedActiveTab: Tab | null,
currentTimelineRecord: TimelineRecord | null,
isEnabled: boolean
isEnabled: boolean,
) {
if (!focusedActiveTab?.id) {
return;
Expand All @@ -30,7 +30,7 @@ export async function updateTimeOnBadge(
(currentTimelineRecord?.activityPeriodStart ?? Date.now());

const currentHostTimeInMinutes = Math.floor(
(committedHostTime + notCommittedHostTime) / 1000 / 60
(committedHostTime + notCommittedHostTime) / 1000 / 60,
);

if (currentHostTimeInMinutes > 0) {
Expand Down
6 changes: 3 additions & 3 deletions src/background/controller/domain-info.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tab } from '../../shared/browser-api.types';
import { upsertDomainInfo } from '../../shared/tables/domain-info';
import { getHostNameFromUrl } from '../../shared/utils/url';
import { Tab } from '@shared/browser-api.types';
import { upsertDomainInfo } from '@shared/tables/domain-info';
import { getHostNameFromUrl } from '@shared/utils/url';

export const updateDomainInfo = async (focusedActiveTab: Tab | null) => {
if (
Expand Down
38 changes: 19 additions & 19 deletions src/background/controller/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ActiveTabState, TimelineRecord } from '../../shared/db/types';
import { getSettings } from '../../shared/preferences';
import { setActiveTabRecord } from '../../shared/tables/state';
import { getIsoDate, getMinutesInMs } from '../../shared/utils/dates-helper';
import { isInvalidUrl } from '../../shared/utils/url';
import { ActiveTabState, TimelineRecord } from '@shared/db/types';
import { getSettings } from '@shared/preferences';
import { setActiveTabRecord } from '@shared/tables/state';
import { getIsoDate, getMinutesInMs } from '@shared/utils/dates-helper';
import { isInvalidUrl } from '@shared/utils/url';

import { ActiveTimelineRecordDao, createNewActiveRecord } from './active';
import { updateTimeOnBadge } from './badge';
Expand All @@ -14,7 +14,7 @@ import { saveTimelineRecord } from './timeline';
const FIVE_MINUTES = getMinutesInMs(5);
export const handleStateChange = async (
activeTabState: ActiveTabState,
timestamp: number = Date.now()
timestamp: number = Date.now(),
) => {
const preferences = await getSettings();
const activeTimeline = new ActiveTimelineRecordDao();
Expand All @@ -41,15 +41,15 @@ export const handleStateChange = async (
}

const isDomainIgnored = preferences.ignoredHosts.includes(
currentTimelineRecord?.hostname ?? ''
currentTimelineRecord?.hostname ?? '',
);

const updatePageLimits = () => {
if (!isDomainIgnored) {
handlePageLimitExceed(
preferences.limits,
focusedActiveTab,
currentTimelineRecord
currentTimelineRecord,
);
}
};
Expand All @@ -59,27 +59,27 @@ export const handleStateChange = async (
updateTimeOnBadge(
focusedActiveTab,
currentTimelineRecord,
preferences.displayTimeOnBadge && !isDomainIgnored
preferences.displayTimeOnBadge && !isDomainIgnored,
),
updateDomainInfo(focusedActiveTab),
updatePageLimits(),
]);

const isValidUrl = !isInvalidUrl(focusedActiveTab?.url);
const isUrlChanged = currentTimelineRecord?.url !== focusedActiveTab?.url;

if (
isLocked ||
isNotFocused ||
isIdleAndNotAudible ||
isImpossiblyLongEvent ||
isInvalidUrl(focusedActiveTab?.url)
isUrlChanged
) {
await commitTabActivity(await activeTimeline.get());
await commitTabActivity(currentTimelineRecord);

return;
}

if (currentTimelineRecord?.url !== focusedActiveTab.url) {
await commitTabActivity(await activeTimeline.get());
await createNewActiveRecord(timestamp, focusedActiveTab);
if (focusedActiveTab && isValidUrl && isUrlChanged) {
await createNewActiveRecord(timestamp, focusedActiveTab);
}
}
};

Expand All @@ -96,11 +96,11 @@ async function commitTabActivity(currentTimelineRecord: TimelineRecord | null) {
// previous day's total time as well.
// Dates in the array should be different in this case.
const dates = Array.from(
new Set([currentIsoDate, currentTimelineRecord.date])
new Set([currentIsoDate, currentTimelineRecord.date]),
);

await Promise.all(
dates.map((date) => updateTotalTime(date, currentTimelineRecord.hostname))
dates.map((date) => updateTotalTime(date, currentTimelineRecord.hostname)),
);

await setActiveTabRecord(null);
Expand Down
12 changes: 6 additions & 6 deletions src/background/controller/limits.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { Tab } from '../../shared/browser-api.types';
import { getTotalActivity } from '../../shared/db/sync-storage';
import { TimelineRecord } from '../../shared/db/types';
import { getIsoDate } from '../../shared/utils/dates-helper';
import { getHostNameFromUrl } from '../../shared/utils/url';
import { Tab } from '@shared/browser-api.types';
import { getTotalActivity } from '@shared/db/sync-storage';
import { TimelineRecord } from '@shared/db/types';
import { getIsoDate } from '@shared/utils/dates-helper';
import { getHostNameFromUrl } from '@shared/utils/url';

import { greyOutTab, unGreyOutTab } from '../browser-api/tabs';

export async function handlePageLimitExceed(
limits: Record<string, number>,
focusedTab: Tab | null,
activeTimeline: TimelineRecord | null
activeTimeline: TimelineRecord | null,
) {
if (!focusedTab?.url || !focusedTab?.id || !activeTimeline) {
return;
Expand Down
7 changes: 3 additions & 4 deletions src/background/controller/overall.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { setTotalDailyHostTime } from '../../shared/db/sync-storage';

import { getActivityTimeline } from '../../shared/tables/activity-timeline';
import { setTotalDailyHostTime } from '@shared/db/sync-storage';
import { getActivityTimeline } from '@shared/tables/activity-timeline';

export async function updateTotalTime(
currentIsoDate: string,
hostname: string
hostname: string,
) {
const timeline = await getActivityTimeline(currentIsoDate);
const timeOnRecord = timeline
Expand Down
7 changes: 3 additions & 4 deletions src/background/controller/timeline.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { TimelineRecord } from '../../shared/db/types';

import { putActivityTimelineRecord } from '../../shared/tables/activity-timeline';
import { TimelineRecord } from '@shared/db/types';
import { putActivityTimelineRecord } from '@shared/tables/activity-timeline';

export async function saveTimelineRecord(
currentTimelineRecord: TimelineRecord,
currentIsoDate: string
currentIsoDate: string,
) {
if (currentTimelineRecord.date === currentIsoDate) {
await putActivityTimelineRecord(currentTimelineRecord);
Expand Down
Loading

0 comments on commit d8a5b8f

Please sign in to comment.