Skip to content

Commit

Permalink
chore: prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
evereq committed Oct 25, 2023
1 parent 3a25f66 commit af6c2d9
Show file tree
Hide file tree
Showing 701 changed files with 23,448 additions and 28,602 deletions.
178 changes: 82 additions & 96 deletions apps/extensions/background.ts
Original file line number Diff line number Diff line change
@@ -1,108 +1,94 @@
import browser from "~misc/browser"
import {
MessageTypesFromBackgroundEnum,
MessageTypesToBackgroundEnum
} from "~typescript/enums/MessageTypesEnum"
import { TimerStateEnum } from "~typescript/enums/TimerStateEnum"
import type { IPostMessage } from "~typescript/interfaces/PostMessage"
import type { ITimerTask } from "~typescript/types/Tasks"
import browser from '~misc/browser';
import { MessageTypesFromBackgroundEnum, MessageTypesToBackgroundEnum } from '~typescript/enums/MessageTypesEnum';
import { TimerStateEnum } from '~typescript/enums/TimerStateEnum';
import type { IPostMessage } from '~typescript/interfaces/PostMessage';
import type { ITimerTask } from '~typescript/types/Tasks';

let tasks: ITimerTask[] = []
let activeTaskIndex: number | null = null
let timeout: ReturnType<typeof setTimeout> | null = null
let runState: TimerStateEnum = TimerStateEnum.stopped
let interval = 1000 // ms
let workPort: chrome.runtime.Port | null = null
let tasks: ITimerTask[] = [];
let activeTaskIndex: number | null = null;
let timeout: ReturnType<typeof setTimeout> | null = null;
let runState: TimerStateEnum = TimerStateEnum.stopped;
let interval = 1000; // ms
let workPort: chrome.runtime.Port | null = null;

browser.runtime.onConnect.addListener((port) => {
workPort = port
let expected = Date.now() + interval
workPort = port;
let expected = Date.now() + interval;

function postUpdateTask() {
const activeTask = tasks[activeTaskIndex]
if (workPort) {
const totalWorked = tasks.reduce((a, v) => v.timer + a, 0)
workPort.postMessage({
type: MessageTypesFromBackgroundEnum.taskUpdate,
payload: {
id: activeTask ? activeTask.id : null,
timer: activeTask ? activeTask.timer : 0,
runState: activeTask ? runState : TimerStateEnum.paused,
totalWorked
}
})
}
}
function postUpdateTask() {
const activeTask = tasks[activeTaskIndex];
if (workPort) {
const totalWorked = tasks.reduce((a, v) => v.timer + a, 0);
workPort.postMessage({
type: MessageTypesFromBackgroundEnum.taskUpdate,
payload: {
id: activeTask ? activeTask.id : null,
timer: activeTask ? activeTask.timer : 0,
runState: activeTask ? runState : TimerStateEnum.paused,
totalWorked
}
});
}
}

if (tasks.length > 0 && activeTaskIndex !== null && tasks[activeTaskIndex]) {
postUpdateTask()
}
if (tasks.length > 0 && activeTaskIndex !== null && tasks[activeTaskIndex]) {
postUpdateTask();
}

function step() {
if (
tasks.length > 0 &&
activeTaskIndex !== null &&
tasks[activeTaskIndex]
) {
let dt = Date.now() - expected // the drift (positive for overshooting)
if (dt > interval) {
// something awful happened. Maybe the browser (tab) was inactive?
// possibly special handling to avoid futile "catch up" run
}
tasks[activeTaskIndex].timer += 1
postUpdateTask()
function step() {
if (tasks.length > 0 && activeTaskIndex !== null && tasks[activeTaskIndex]) {
let dt = Date.now() - expected; // the drift (positive for overshooting)
if (dt > interval) {
// something awful happened. Maybe the browser (tab) was inactive?
// possibly special handling to avoid futile "catch up" run
}
tasks[activeTaskIndex].timer += 1;
postUpdateTask();

expected += interval
timeout = setTimeout(step, interval) // take into account drift
}
}
expected += interval;
timeout = setTimeout(step, interval); // take into account drift
}
}

port.onDisconnect.addListener(() => {
workPort = null
timeout = null
})
port.onDisconnect.addListener(() => {
workPort = null;
timeout = null;
});

port.onMessage.addListener((msg: IPostMessage) => {
if (activeTaskIndex !== null) {
if (
msg.type === MessageTypesToBackgroundEnum.startTimer &&
runState !== TimerStateEnum.running
) {
timeout = setTimeout(step, interval)
runState = TimerStateEnum.running
}
if (msg.type === MessageTypesToBackgroundEnum.pauseTimer) {
clearTimeout(timeout)
timeout = null
runState = TimerStateEnum.paused
}
if (msg.type === MessageTypesToBackgroundEnum.stopTimer) {
clearTimeout(timeout)
timeout = null
tasks[activeTaskIndex].timer = 0
runState = TimerStateEnum.stopped
}
}
port.onMessage.addListener((msg: IPostMessage) => {
if (activeTaskIndex !== null) {
if (msg.type === MessageTypesToBackgroundEnum.startTimer && runState !== TimerStateEnum.running) {
timeout = setTimeout(step, interval);
runState = TimerStateEnum.running;
}
if (msg.type === MessageTypesToBackgroundEnum.pauseTimer) {
clearTimeout(timeout);
timeout = null;
runState = TimerStateEnum.paused;
}
if (msg.type === MessageTypesToBackgroundEnum.stopTimer) {
clearTimeout(timeout);
timeout = null;
tasks[activeTaskIndex].timer = 0;
runState = TimerStateEnum.stopped;
}
}

if (
msg.type === MessageTypesToBackgroundEnum.updateTasks &&
msg.payload &&
Array.isArray(msg.payload)
) {
tasks = msg.payload.map((x) => {
const element = tasks.find((y) => y.id === x.id)
if (msg.type === MessageTypesToBackgroundEnum.updateTasks && msg.payload && Array.isArray(msg.payload)) {
tasks = msg.payload.map((x) => {
const element = tasks.find((y) => y.id === x.id);

return element ? element : { id: x.id, timer: 0 }
})
}
if (msg.type == MessageTypesToBackgroundEnum.updateActiveTaskIndex) {
const index = tasks.findIndex((x) => x.id === msg.payload.id)
return element ? element : { id: x.id, timer: 0 };
});
}
if (msg.type == MessageTypesToBackgroundEnum.updateActiveTaskIndex) {
const index = tasks.findIndex((x) => x.id === msg.payload.id);

if (index !== -1) {
activeTaskIndex = index
postUpdateTask()
}
}
})
})
export {}
if (index !== -1) {
activeTaskIndex = index;
postUpdateTask();
}
}
});
});
export {};
66 changes: 32 additions & 34 deletions apps/extensions/components/popup/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,39 @@
import classNames from "classnames"
import logoImg from "data-base64:~assets/logo/gauzy-teams-dark.png"
import React, { FC, useState } from "react"
import classNames from 'classnames';
import logoImg from 'data-base64:~assets/logo/gauzy-teams-dark.png';
import React, { FC, useState } from 'react';

import AppDropdown from "~components/shared/AppDropdown"
import { textEllipsis } from "~misc/tailwindClasses"
import type { ITeam } from "~typescript/types/Team"
import AppDropdown from '~components/shared/AppDropdown';
import { textEllipsis } from '~misc/tailwindClasses';
import type { ITeam } from '~typescript/types/Team';

const teams: ITeam[] = [
{ id: 1, title: "Team" }
]
const teams: ITeam[] = [{ id: 1, title: 'Team' }];

const Header: FC = () => {
const [team, setTeam] = useState<ITeam>(teams[0])
const [team, setTeam] = useState<ITeam>(teams[0]);

const onTeamSelect = (team: ITeam) => {
setTeam(team)
}
const onTeamSelect = (team: ITeam) => {
setTeam(team);
};

return (
<div className="flex items-center justify-between mb-4">
<img className="w-32 h-full" src={logoImg}></img>
<div className="flex items-center min-w-[180px] text-end">
<span className="mr-2">Team:</span>
<AppDropdown
buttonTitle={team.title}
buttonClassNames={classNames(
"text-white bg-slate-800 hover:bg-slate-700 focus:outline-none font-medium rounded-lg text-sm px-4 py-2.5 text-center",
textEllipsis,
"w-36"
)}
options={teams}
onOptionSelect={onTeamSelect}
optionContainerClassNames="-ml-8"
/>
</div>
</div>
)
}
return (
<div className="flex items-center justify-between mb-4">
<img className="w-32 h-full" src={logoImg}></img>
<div className="flex items-center min-w-[180px] text-end">
<span className="mr-2">Team:</span>
<AppDropdown
buttonTitle={team.title}
buttonClassNames={classNames(
'text-white bg-slate-800 hover:bg-slate-700 focus:outline-none font-medium rounded-lg text-sm px-4 py-2.5 text-center',
textEllipsis,
'w-36'
)}
options={teams}
onOptionSelect={onTeamSelect}
optionContainerClassNames="-ml-8"
/>
</div>
</div>
);
};

export default Header
export default Header;
Loading

0 comments on commit af6c2d9

Please sign in to comment.