-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathcommon.ts
More file actions
74 lines (67 loc) · 2.53 KB
/
common.ts
File metadata and controls
74 lines (67 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { t } from "@app/locale"
import StatDatabase from "@db/stat-database"
import { type StatQueryParam } from "@service/stat-service"
import { formatTime } from "@util/time"
import type { ReportFilterOption } from "./types"
const statDatabase = new StatDatabase(chrome.storage.local)
export const cvtOption2Param = ({ host, dateRange, mergeDate, siteMerge, cateIds, readRemote }: ReportFilterOption): StatQueryParam => ({
host: host,
date: dateRange,
mergeDate,
mergeHost: siteMerge === 'domain',
mergeCate: siteMerge === 'cate',
inclusiveRemote: readRemote,
cateIds,
})
/**
* Compute the confirm text for one item to delete
*
* @param url item url
* @param date item date
*/
function computeSingleConfirmText(url: string, date: string): string {
return t(msg => msg.item.operation.deleteConfirmMsg, { url, date })
}
function computeRangeConfirmText(url: string, dateRange: [Date, Date] | undefined): string {
const hasDateRange = dateRange?.length === 2 && (dateRange[0] || dateRange[1])
if (!hasDateRange) {
// Delete all
return t(msg => msg.item.operation.deleteConfirmMsgAll, { url })
}
const dateFormat = t(msg => msg.calendar.dateFormat)
const startDate = dateRange[0]
const endDate = dateRange[1]
const start = formatTime(startDate, dateFormat)
const end = formatTime(endDate, dateFormat)
return start === end
// Only one day
? computeSingleConfirmText(url, start)
: t(msg => msg.item.operation.deleteConfirmMsgRange, { url, start, end })
}
export function computeDeleteConfirmMsg(row: timer.stat.Row, filterOption: ReportFilterOption): string {
const { siteKey, date } = row || {}
const host = siteKey?.host ?? 'Unknown Host'
const { mergeDate, dateRange } = filterOption || {}
return mergeDate
? computeRangeConfirmText(host, dateRange)
: computeSingleConfirmText(host, date ?? '')
}
export async function handleDelete(row: timer.stat.Row, filterOption: ReportFilterOption) {
const { siteKey, date } = row || {}
const { host } = siteKey || {}
const { mergeDate, dateRange } = filterOption || {}
if (!mergeDate) {
// Delete one day
host && date && await statDatabase.deleteByUrlAndDate(host, date)
return
}
const start = dateRange?.[0]
const end = dateRange?.[1]
if (!start && !end) {
// Delete all
host && await statDatabase.deleteByUrl(host)
return
}
// Delete by range
host && await statDatabase.deleteByUrlBetween(host, start, end)
}