forked from sheepzh/time-tracker-4-browser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReason.tsx
More file actions
139 lines (130 loc) · 5.94 KB
/
Reason.tsx
File metadata and controls
139 lines (130 loc) · 5.94 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { t } from "@cs/locale"
import { useRequest } from "@hooks/useRequest"
import Flex from "@pages/components/Flex"
import { matchCond, meetLimit, meetTimeLimit, period2Str } from "@util/limit"
import { formatPeriodCommon, MILL_PER_SECOND } from "@util/time"
import { ElDescriptions, ElDescriptionsItem, ElTag } from 'element-plus'
import { computed, defineComponent } from "vue"
import { useGlobalParam, useReason, useRule } from "../context"
const renderBaseItems = (rule: timer.limit.Rule | null, url: string) => <>
<ElDescriptionsItem label={t(msg => msg.limit.item.name)} labelAlign="right">
{rule?.name ?? '-'}
</ElDescriptionsItem>
<ElDescriptionsItem label={t(msg => msg.limit.item.condition)} labelAlign='right'>
{matchCond(rule?.cond ?? [], url)?.join(', ')}
</ElDescriptionsItem>
</>
const TimeDescriptions = defineComponent({
props: {
// Seconds
time: Number,
// Milliseconds
waste: Number,
count: Number,
visit: Number,
ruleLabel: String,
dataLabel: String,
},
setup(props) {
const rule = useRule()
const reason = useReason()
const { url } = useGlobalParam()
const timeLimited = computed(() => meetTimeLimit(props.time ?? 0, props.waste ?? 0, !!reason.value?.allowDelay, reason.value?.delayCount ?? 0))
const visitLimited = computed(() => meetLimit(props.count ?? 0, props.visit ?? 0))
return () => (
<ElDescriptions border column={1} labelWidth={130}>
{renderBaseItems(rule.value, url)}
<ElDescriptionsItem label={props.ruleLabel} labelAlign="right">
<Flex gap={5} width={200}>
<ElTag v-show={!!props.time}>{formatPeriodCommon((props.time ?? 0) * MILL_PER_SECOND)}</ElTag>
<ElTag v-show={!!props.count}>{`${props.count ?? 0} ${t(msg => msg.limit.item.visits)}`}</ElTag>
</Flex>
</ElDescriptionsItem>
<ElDescriptionsItem label={props.dataLabel} labelAlign="right">
<Flex gap={5} width={200}>
<ElTag
v-show={!!props.waste || !!props.time}
type={timeLimited.value ? 'danger' : 'info'}
>
{formatPeriodCommon(props.waste ?? 0)}
</ElTag>
<ElTag
v-show={!!props.count || !!props.visit}
type={visitLimited.value ? 'danger' : 'info'}
>
{`${props.visit ?? 0} ${t(msg => msg.limit.item.visits)}`}
</ElTag>
</Flex>
</ElDescriptionsItem>
<ElDescriptionsItem
v-show={!!reason.value?.allowDelay && !!props.time}
label={t(msg => msg.limit.item.delayCount)}
labelAlign="right"
>
{reason.value?.delayCount ?? 0}
</ElDescriptionsItem>
</ElDescriptions>
)
},
})
const _default = defineComponent(() => {
const reason = useReason()
const rule = useRule()
const { url } = useGlobalParam()
const type = computed(() => reason.value?.type)
const { data: browsingTime, refresh: refreshBrowsingTime } = useRequest(() => {
const { getVisitTime, type } = reason.value || {}
if (type !== 'VISIT') return
return getVisitTime?.() || 0
})
setInterval(refreshBrowsingTime, 1000)
return () => (
<div class="reason-container">
<TimeDescriptions
v-show={type.value === 'DAILY'}
time={rule.value?.time}
count={rule.value?.count}
waste={rule.value?.waste}
visit={rule.value?.visit}
ruleLabel={t(msg => msg.limit.item.daily)}
dataLabel={t(msg => msg.calendar.range.today)}
/>
<TimeDescriptions
v-show={type.value === 'WEEKLY'}
time={rule.value?.weekly}
count={rule.value?.weeklyCount}
waste={rule.value?.weeklyWaste}
visit={rule.value?.weeklyVisit}
ruleLabel={t(msg => msg.limit.item.weekly)}
dataLabel={t(msg => msg.calendar.range.thisWeek)}
/>
<ElDescriptions border column={1} v-show={type.value === 'VISIT'}>
{renderBaseItems(rule.value, url)}
<ElDescriptionsItem label={t(msg => msg.limit.item.visitTime)} labelAlign="right">
{formatPeriodCommon((rule.value?.visitTime ?? 0) * MILL_PER_SECOND) || '-'}
</ElDescriptionsItem>
<ElDescriptionsItem label={t(msg => msg.modal.browsingTime)} labelAlign="right">
{browsingTime.value ? formatPeriodCommon(browsingTime.value) : '-'}
</ElDescriptionsItem>
<ElDescriptionsItem
v-show={!!reason.value?.allowDelay || !!reason.value?.delayCount}
label={t(msg => msg.limit.item.delayCount)} labelAlign="right">
{reason.value?.delayCount ?? 0}
</ElDescriptionsItem>
</ElDescriptions>
<ElDescriptions border column={1} v-show={type.value === 'PERIOD'}>
{renderBaseItems(rule.value, url)}
<ElDescriptionsItem label={t(msg => msg.limit.item.period)} labelAlign="right">
{
rule.value?.periods?.length
? <div>
{rule.value?.periods.map(p => <span style={{ display: "block" }}>{period2Str(p)}</span>)}
</div>
: '-'
}
</ElDescriptionsItem>
</ElDescriptions>
</div>
)
})
export default _default