Skip to content

Commit

Permalink
Fixed an issue where app would get stuck without record and refused t…
Browse files Browse the repository at this point in the history
…o create a new one
  • Loading branch information
PsychoSanchez committed Feb 3, 2024
1 parent 931de71 commit 80a7e2c
Showing 1 changed file with 19 additions and 19 deletions.
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

0 comments on commit 80a7e2c

Please sign in to comment.