Skip to content

Commit 00d7f34

Browse files
authored
feat(share): add placeholder and increment format for expire date input (#196)
* feat(share): add placeholder and increment format for expire date input * chore
1 parent efb9b35 commit 00d7f34

File tree

4 files changed

+36
-11
lines changed

4 files changed

+36
-11
lines changed

src/pages/home/toolbar/Share.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useFetch, useRouter, useT, useUtil } from "~/hooks"
22
import {
33
bus,
4+
getExpireDate,
45
handleResp,
56
makeTemplateData,
67
matchTemplate,
@@ -224,14 +225,15 @@ export const Share = () => {
224225
size="sm"
225226
invalid={!expireValid()}
226227
value={expireString()}
228+
placeholder="yyyy-MM-dd HH:mm:ss or +1w1d1H1m1s1ms"
227229
onInput={(e) => {
228230
setExpireString(e.currentTarget.value)
229231
if (e.currentTarget.value === "") {
230232
setExpireValid(true)
231233
setShare("expires", null)
232234
return
233235
}
234-
const date = new Date(e.currentTarget.value)
236+
const date = getExpireDate(e.currentTarget.value)
235237
if (isNaN(date.getTime())) {
236238
setExpireValid(false)
237239
} else {

src/pages/manage/shares/AddOrEdit.tsx

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { useFetch, useRouter, useT } from "~/hooks"
2-
import {
3-
OrderDirection,
4-
PResp,
5-
Share,
6-
ShareInfo,
7-
ShareUpdate,
8-
Type,
9-
} from "~/types"
10-
import { handleResp, notify, r, randomPwd } from "~/utils"
2+
import { PResp, Share, ShareInfo, ShareUpdate, Type } from "~/types"
3+
import { handleResp, notify, r, randomPwd, getExpireDate } from "~/utils"
114
import { createStore } from "solid-js/store"
125
import { Button, Heading } from "@hope-ui/solid"
136
import { MaybeLoading } from "~/components"
@@ -140,14 +133,15 @@ const AddOrEdit = () => {
140133
type={Type.String}
141134
value={expireString()}
142135
valid={expireValid()}
136+
placeholder="yyyy-MM-dd HH:mm:ss or +1w1d1H1m1s1ms"
143137
onChange={(e) => {
144138
setExpireString(e)
145139
if (e === "") {
146140
setExpireValid(true)
147141
setShare("expires", null)
148142
return
149143
}
150-
const date = new Date(e)
144+
const date = getExpireDate(e)
151145
if (isNaN(date.getTime())) {
152146
setExpireValid(false)
153147
} else {

src/pages/manage/shares/Item.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export type ItemProps = {
2323
options?: string
2424
options_prefix?: string
2525
valid?: boolean
26+
placeholder?: string
2627
} & (
2728
| {
2829
type: Type.Bool
@@ -79,6 +80,7 @@ const Item = (props: ItemProps) => {
7980
readOnly={props.readonly}
8081
value={props.value as string}
8182
invalid={!props.valid}
83+
placeholder={props.placeholder}
8284
onChange={
8385
props.type === Type.String
8486
? (e) => props.onChange?.(e.currentTarget.value)
@@ -106,6 +108,7 @@ const Item = (props: ItemProps) => {
106108
readOnly={props.readonly}
107109
value={props.value as number}
108110
invalid={!props.valid}
111+
placeholder={props.placeholder}
109112
onInput={
110113
props.type === Type.Number
111114
? (e) => props.onChange?.(parseInt(e.currentTarget.value))
@@ -120,6 +123,7 @@ const Item = (props: ItemProps) => {
120123
readOnly={props.readonly}
121124
value={props.value as number}
122125
invalid={!props.valid}
126+
placeholder={props.placeholder}
123127
onInput={
124128
props.type === Type.Float
125129
? (e) => props.onChange?.(parseFloat(e.currentTarget.value))
@@ -146,6 +150,7 @@ const Item = (props: ItemProps) => {
146150
readOnly={props.readonly}
147151
value={props.value as string}
148152
invalid={!props.valid}
153+
placeholder={props.placeholder}
149154
onChange={
150155
props.type === Type.Text
151156
? (e) => props.onChange?.(e.currentTarget.value)

src/utils/share.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,27 @@ export const makeTemplateData = (
2121
...other,
2222
}
2323
}
24+
25+
const expireDateIncrementRegExp =
26+
/^\+(?=\d+[wdHms])(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)H)?(?:(\d+)m)?(?:(\d+)s)?(?:(\d+)ms)?$/
27+
28+
const toInt = (s: string | undefined) => {
29+
if (s === undefined) return 0
30+
const n = Number.parseInt(s)
31+
return Number.isNaN(n) ? 0 : n
32+
}
33+
34+
export const getExpireDate = (dateStr: string) => {
35+
const match = expireDateIncrementRegExp.exec(dateStr)
36+
if (match) {
37+
const w = toInt(match[1])
38+
const d = toInt(match[2]) + w * 7
39+
const H = toInt(match[3]) + d * 24
40+
const m = toInt(match[4]) + H * 60
41+
const s = toInt(match[5]) + m * 60
42+
const ms = toInt(match[6]) + s * 1000
43+
return new Date(Date.now() + ms)
44+
} else {
45+
return new Date(dateStr)
46+
}
47+
}

0 commit comments

Comments
 (0)