11import { getTab , listTabs , sendMsg2Tab } from "@api/chrome/tab"
22import { getWindow } from "@api/chrome/window"
33import optionHolder from "@service/components/option-holder"
4- import itemService from "@service/item-service"
4+ import whitelistHolder from "@service/components/whitelist-holder"
5+ import itemService , { type ItemIncContext } from "@service/item-service"
56import limitService from "@service/limit-service"
67import periodService from "@service/period-service"
78import { IS_ANDROID } from "@util/constant/environment"
@@ -10,11 +11,12 @@ import { formatTimeYMD, getStartOfDay, MILL_PER_DAY } from "@util/time"
1011import badgeManager from "./badge-manager"
1112import MessageDispatcher from "./message-dispatcher"
1213
13- async function handleTime ( host : string , url : string , dateRange : [ number , number ] , tabId : number | undefined ) : Promise < number > {
14- const [ start , end ] = dateRange
14+ async function handleTime ( context : ItemIncContext , timeRange : [ number , number ] , tabId : number | undefined ) : Promise < number > {
15+ const { host, url } = context
16+ const [ start , end ] = timeRange
1517 const focusTime = end - start
1618 // 1. Save async
17- await itemService . addFocusTime ( host , url , focusTime )
19+ await itemService . addFocusTime ( context , focusTime )
1820 // 2. Process limit
1921 const { limited, reminder } = await limitService . addFocusTime ( host , url , focusTime )
2022 // If time limited after this operation, send messages
@@ -28,15 +30,18 @@ async function handleTime(host: string, url: string, dateRange: [number, number]
2830
2931async function handleTrackTimeEvent ( event : timer . core . Event , sender : ChromeMessageSender ) : Promise < void > {
3032 const { url, start, end, ignoreTabCheck } = event
31- const { id : tabId , windowId } = sender ?. tab || { }
33+ const { id : tabId , windowId, groupId } = sender ?. tab || { }
3234 if ( ! ignoreTabCheck ) {
3335 if ( await windowNotFocused ( windowId ) ) return
3436 if ( await tabNotActive ( tabId ) ) return
3537 }
3638 const { protocol, host } = extractHostname ( url ) || { }
3739 const option = await optionHolder . get ( )
40+
3841 if ( protocol === "file" && ! option ?. countLocalFiles ) return
39- await handleTime ( host , url , [ start , end ] , tabId )
42+ if ( whitelistHolder . contains ( host , url ) ) return
43+
44+ await handleTime ( { host, url, groupId } , [ start , end ] , tabId )
4045 if ( tabId ) {
4146 const winTabs = await listTabs ( { active : true , windowId } )
4247 const firstActiveTab = winTabs ?. [ 0 ]
@@ -73,19 +78,21 @@ async function sendLimitedMessage(items: timer.limit.Item[]) {
7378 }
7479}
7580
76- async function handleVisit ( host : string , url : string ) {
77- await itemService . increaseVisit ( host , url )
81+ async function handleVisit ( context : ItemIncContext ) {
82+ await itemService . increaseVisit ( context )
83+ const { host, url } = context
7884 const metLimits = await limitService . incVisit ( host , url )
7985 // If time limited after this operation, send messages
8086 metLimits ?. length && sendLimitedMessage ( metLimits )
8187}
8288
83- async function handleIncVisitEvent ( param : { host : string , url : string } ) : Promise < void > {
89+ async function handleIncVisitEvent ( param : { host : string , url : string } , sender : ChromeMessageSender ) : Promise < void > {
8490 const { host, url } = param || { }
91+ const { groupId } = sender ?. tab ?? { }
8592 const { protocol } = extractHostname ( url ) || { }
8693 const option = await optionHolder . get ( )
8794 if ( protocol === "file" && ! option . countLocalFiles ) return
88- await handleVisit ( host , url )
95+ await handleVisit ( { host, url, groupId } )
8996}
9097
9198function splitRunTime ( start : number , end : number ) : Record < string , number > {
@@ -108,14 +115,28 @@ async function handleTrackRunTimeEvent(event: timer.core.Event): Promise<void> {
108115 const realStart = Math . max ( RUN_TIME_END_CACHE [ host ] ?? 0 , start )
109116 const byDate = splitRunTime ( realStart , end )
110117 if ( ! Object . keys ( byDate ) . length ) return
111- await itemService . addRunTime ( host , url , byDate )
118+ await itemService . addRunTime ( host , byDate )
112119 RUN_TIME_END_CACHE [ host ] = Math . max ( end , realStart )
113120}
114121
122+ function handleTabGroupRemove ( group : chrome . tabGroups . TabGroup ) {
123+ itemService . batchDeleteGroupById ( group . id )
124+ }
125+
126+ function handleTabGroupEnabled ( ) {
127+ try {
128+ chrome . tabGroups . onRemoved . removeListener ( handleTabGroupRemove )
129+ chrome . tabGroups . onRemoved . addListener ( handleTabGroupRemove )
130+ } catch ( e ) {
131+ console . warn ( 'failed to handle event: enableTabGroup' , e )
132+ }
133+ }
134+
115135export default function initTrackServer ( messageDispatcher : MessageDispatcher ) {
116136 messageDispatcher
117137 . register < timer . core . Event , void > ( 'cs.trackTime' , handleTrackTimeEvent )
118138 . register < timer . core . Event , void > ( 'cs.trackRunTime' , handleTrackRunTimeEvent )
119139 . register < { host : string , url : string } , void > ( 'cs.incVisitCount' , handleIncVisitEvent )
120140 . register < string , timer . core . Result > ( 'cs.getTodayInfo' , host => itemService . getResult ( host , new Date ( ) ) )
141+ . register < void , void > ( 'enableTabGroup' , handleTabGroupEnabled )
121142}
0 commit comments