-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
701 changed files
with
23,448 additions
and
28,602 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.