@arcblock/ux 2.13.26 → 2.13.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,9 +6,9 @@ export interface RelativeTimeProps {
6
6
  withoutSuffix?: false | true;
7
7
  from?: string | number;
8
8
  to?: string | number;
9
- type?: 'relative' | 'absolute';
9
+ type?: 'relative' | 'absolute' | 'utc' | 'all';
10
10
  tz?: string;
11
11
  relativeRange?: number;
12
12
  enableTooltip?: boolean;
13
13
  }
14
- export default function RelativeTime({ value, locale, withoutSuffix, from, to, type, tz, relativeRange, enableTooltip, ...rest }: RelativeTimeProps): "-" | import("react/jsx-runtime").JSX.Element;
14
+ export default function RelativeTime({ value, locale, withoutSuffix, from, to, type, tz, relativeRange, enableTooltip, ...rest }: RelativeTimeProps): import("react/jsx-runtime").JSX.Element;
@@ -1,8 +1,9 @@
1
- import { jsx as _jsx } from "react/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Box, Tooltip } from '@mui/material';
3
+ import { useState } from 'react';
2
4
  import relativeTime from 'dayjs/plugin/relativeTime';
3
5
  import dayjs from 'dayjs';
4
6
  import 'dayjs/locale/zh-cn';
5
- import { Tooltip } from '@mui/material';
6
7
  import localizedFormat from 'dayjs/plugin/localizedFormat';
7
8
  import utc from 'dayjs/plugin/utc';
8
9
  import timezone from 'dayjs/plugin/timezone';
@@ -33,7 +34,15 @@ dayjs.updateLocale('zh-cn', {
33
34
  });
34
35
  // FIXME: @@ 此处不能真正的将 relativeTime 设置为支持中文
35
36
  setDateTool(dayjs);
36
- export default function RelativeTime({
37
+ const translations = {
38
+ en: {
39
+ utc: 'Your Timezone'
40
+ },
41
+ zh: {
42
+ utc: '你所在时区'
43
+ }
44
+ };
45
+ function useRelativeTime({
37
46
  value,
38
47
  locale = 'en',
39
48
  withoutSuffix = false,
@@ -41,17 +50,31 @@ export default function RelativeTime({
41
50
  to = '',
42
51
  type = 'relative',
43
52
  tz,
44
- relativeRange,
45
- enableTooltip = true,
46
- ...rest
53
+ relativeRange
47
54
  }) {
55
+ const date = new Date();
56
+ const timeZoneOffset = date.getTimezoneOffset();
57
+ const sign = timeZoneOffset > 0 ? '-' : '+';
58
+ const hoursOffset = Math.abs(timeZoneOffset) / 60;
59
+ const isLocalUtc = timeZoneOffset === 0;
60
+ const [isUtc, setIsUtc] = useState(isLocalUtc);
48
61
  if (!value) {
49
- return '-';
62
+ return {
63
+ innerContent: '-',
64
+ popContent: '-',
65
+ isUtc,
66
+ setIsUtc,
67
+ sign,
68
+ hoursOffset
69
+ };
50
70
  }
51
71
  const localeOption = locale === 'zh' ? 'zh-cn' : 'en-us';
52
- const datetime = dayjs(value).locale(localeOption);
72
+ let datetime = dayjs(value).locale(localeOption);
73
+ if (type === 'utc') {
74
+ datetime = dayjs(value).utc().locale(localeOption);
75
+ }
53
76
  if (tz) {
54
- datetime.tz(tz);
77
+ datetime = datetime.tz(tz);
55
78
  }
56
79
  const absoluteString = formatToDatetime(value, {
57
80
  locale: localeOption,
@@ -77,13 +100,150 @@ export default function RelativeTime({
77
100
  innerContent = absoluteString;
78
101
  popContent = relativeString;
79
102
  }
103
+ if (type === 'utc') {
104
+ if (isUtc) {
105
+ innerContent = formatToDatetime(value, {
106
+ locale: localeOption,
107
+ tz,
108
+ isUtc: true
109
+ });
110
+ popContent = formatToDatetime(value, {
111
+ locale: localeOption,
112
+ tz,
113
+ isUtc: true
114
+ });
115
+ } else {
116
+ innerContent = absoluteString;
117
+ popContent = relativeString;
118
+ }
119
+ }
120
+ return {
121
+ innerContent,
122
+ popContent,
123
+ isUtc,
124
+ setIsUtc,
125
+ sign,
126
+ hoursOffset,
127
+ relativeString,
128
+ absoluteString
129
+ };
130
+ }
131
+ function UTCChip({
132
+ locale,
133
+ isUtc,
134
+ sign,
135
+ hoursOffset,
136
+ setIsUtc
137
+ }) {
138
+ return /*#__PURE__*/_jsx(Box, {
139
+ component: "span",
140
+ sx: {
141
+ color: 'inherit',
142
+ cursor: 'pointer',
143
+ border: '1px solid',
144
+ fontSize: '0.8rem',
145
+ borderColor: 'divider',
146
+ borderRadius: '20px',
147
+ padding: '0px 8px'
148
+ },
149
+ onClick: () => setIsUtc(r => !r),
150
+ children: `${translations[locale].utc}: ${isUtc ? 'UTC' : `UTC${sign}${hoursOffset}`}`
151
+ });
152
+ }
153
+ export default function RelativeTime({
154
+ value,
155
+ locale = 'en',
156
+ withoutSuffix = false,
157
+ from = '',
158
+ to = '',
159
+ type = 'relative',
160
+ tz,
161
+ relativeRange,
162
+ enableTooltip = true,
163
+ ...rest
164
+ }) {
165
+ const {
166
+ innerContent,
167
+ popContent,
168
+ isUtc,
169
+ setIsUtc,
170
+ sign,
171
+ hoursOffset,
172
+ relativeString
173
+ } = useRelativeTime({
174
+ value,
175
+ locale,
176
+ withoutSuffix,
177
+ from,
178
+ to,
179
+ type: type === 'all' ? 'utc' : type,
180
+ tz,
181
+ relativeRange
182
+ });
183
+ if (type === 'all') {
184
+ return /*#__PURE__*/_jsx(Tooltip, {
185
+ title: undefined,
186
+ placement: "top-end",
187
+ enterTouchDelay: 0,
188
+ children: /*#__PURE__*/_jsxs(Box, {
189
+ display: "flex",
190
+ alignItems: "center",
191
+ gap: 0.5,
192
+ width: "max-content",
193
+ ...rest,
194
+ children: [/*#__PURE__*/_jsx(Box, {
195
+ component: "span",
196
+ ...rest,
197
+ sx: {},
198
+ children: innerContent
199
+ }), /*#__PURE__*/_jsx(Box, {
200
+ component: "span",
201
+ sx: {
202
+ color: 'inherit'
203
+ },
204
+ children: "\xB7"
205
+ }), /*#__PURE__*/_jsx(Box, {
206
+ component: "span",
207
+ sx: {
208
+ color: 'text.secondary'
209
+ },
210
+ children: relativeString
211
+ }), /*#__PURE__*/_jsx(Box, {
212
+ component: "span",
213
+ sx: {
214
+ color: 'inherit'
215
+ },
216
+ children: "\xB7"
217
+ }), /*#__PURE__*/_jsx(UTCChip, {
218
+ locale: locale,
219
+ isUtc: isUtc,
220
+ sign: sign,
221
+ hoursOffset: hoursOffset,
222
+ setIsUtc: setIsUtc
223
+ })]
224
+ })
225
+ });
226
+ }
80
227
  return /*#__PURE__*/_jsx(Tooltip, {
81
228
  title: enableTooltip ? popContent : undefined,
82
229
  placement: "top-end",
83
230
  enterTouchDelay: 0,
84
- children: /*#__PURE__*/_jsx("span", {
85
- ...rest,
86
- children: innerContent
231
+ children: /*#__PURE__*/_jsxs(Box, {
232
+ display: "flex",
233
+ alignItems: "center",
234
+ gap: 1,
235
+ width: "max-content",
236
+ children: [/*#__PURE__*/_jsx(Box, {
237
+ component: "span",
238
+ ...rest,
239
+ children: innerContent
240
+ }), type === 'utc' && /*#__PURE__*/_jsx(UTCChip, {
241
+ locale: locale,
242
+ isUtc: isUtc,
243
+ sign: sign,
244
+ hoursOffset: hoursOffset,
245
+ setIsUtc: setIsUtc
246
+ })]
87
247
  })
88
248
  });
89
249
  }
@@ -48,9 +48,10 @@ export declare function formatToDate(date: string | number | Date, { locale, tz
48
48
  * Ensure that the setDateTool() function is called first to set the time tool library.
49
49
  * @returns formatted date string
50
50
  */
51
- export declare function formatToDatetime(date: string | number | Date, { locale, tz }?: {
51
+ export declare function formatToDatetime(date: string | number | Date, { locale, tz, isUtc }?: {
52
52
  locale?: Locale;
53
53
  tz?: string;
54
+ isUtc?: boolean;
54
55
  }): any;
55
56
  export declare function detectWalletExtension(): any;
56
57
  export declare function openWebWallet({ webWalletUrl, action, locale, url, windowFeatures, appInfo, memberAppInfo, }: {
package/lib/Util/index.js CHANGED
@@ -189,7 +189,8 @@ export function getDateTool() {
189
189
  }
190
190
  const createDateFormatter = format => (date, {
191
191
  locale,
192
- tz
192
+ tz,
193
+ isUtc
193
194
  } = {}) => {
194
195
  if (dateTool === null) {
195
196
  throw new Error('call setDateTool to set the date tool library, such as: setDateTool(dayjs)');
@@ -201,6 +202,9 @@ const createDateFormatter = format => (date, {
201
202
  if (tz) {
202
203
  instance = instance.tz(tz);
203
204
  }
205
+ if (isUtc) {
206
+ instance = instance.utc();
207
+ }
204
208
  if (typeof locale !== 'undefined') {
205
209
  instance = instance.locale(locale);
206
210
  }
@@ -229,11 +233,13 @@ export function formatToDate(date, {
229
233
  */
230
234
  export function formatToDatetime(date, {
231
235
  locale = 'en',
232
- tz
236
+ tz,
237
+ isUtc = false
233
238
  } = {}) {
234
239
  return createDateFormatter('lll')(date, {
235
240
  locale,
236
- tz
241
+ tz,
242
+ isUtc
237
243
  });
238
244
  }
239
245
  export function detectWalletExtension() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arcblock/ux",
3
- "version": "2.13.26",
3
+ "version": "2.13.27",
4
4
  "description": "Common used react components for arcblock products",
5
5
  "keywords": [
6
6
  "react",
@@ -71,14 +71,14 @@
71
71
  "react": ">=18.2.0",
72
72
  "react-router-dom": ">=6.22.3"
73
73
  },
74
- "gitHead": "bf72ea918c9caeddeac9bcd6cb4352a415e7a29c",
74
+ "gitHead": "06abd7d8f9068ebca145a411b4a9b5b7f329535a",
75
75
  "dependencies": {
76
76
  "@arcblock/did-motif": "^1.1.13",
77
- "@arcblock/icons": "^2.13.26",
78
- "@arcblock/nft-display": "^2.13.26",
79
- "@arcblock/react-hooks": "^2.13.26",
77
+ "@arcblock/icons": "^2.13.27",
78
+ "@arcblock/nft-display": "^2.13.27",
79
+ "@arcblock/react-hooks": "^2.13.27",
80
80
  "@babel/plugin-syntax-dynamic-import": "^7.8.3",
81
- "@blocklet/theme": "^2.13.26",
81
+ "@blocklet/theme": "^2.13.27",
82
82
  "@fontsource/roboto": "~5.1.1",
83
83
  "@fontsource/ubuntu-mono": "^5.0.18",
84
84
  "@iconify-icons/logos": "^1.2.36",
@@ -1,7 +1,8 @@
1
+ import { Box, Tooltip } from '@mui/material';
2
+ import { useState } from 'react';
1
3
  import relativeTime from 'dayjs/plugin/relativeTime';
2
4
  import dayjs from 'dayjs';
3
5
  import 'dayjs/locale/zh-cn';
4
- import { Tooltip } from '@mui/material';
5
6
  import localizedFormat from 'dayjs/plugin/localizedFormat';
6
7
  import utc from 'dayjs/plugin/utc';
7
8
  import timezone from 'dayjs/plugin/timezone';
@@ -35,19 +36,28 @@ dayjs.updateLocale('zh-cn', {
35
36
  // FIXME: @@ 此处不能真正的将 relativeTime 设置为支持中文
36
37
  setDateTool(dayjs);
37
38
 
39
+ const translations: Record<Locale, { utc: string }> = {
40
+ en: {
41
+ utc: 'Your Timezone',
42
+ },
43
+ zh: {
44
+ utc: '你所在时区',
45
+ },
46
+ };
47
+
38
48
  export interface RelativeTimeProps {
39
49
  value: string | number;
40
50
  locale?: Locale;
41
51
  withoutSuffix?: false | true;
42
52
  from?: string | number;
43
53
  to?: string | number;
44
- type?: 'relative' | 'absolute';
54
+ type?: 'relative' | 'absolute' | 'utc' | 'all';
45
55
  tz?: string;
46
56
  relativeRange?: number;
47
57
  enableTooltip?: boolean;
48
58
  }
49
59
 
50
- export default function RelativeTime({
60
+ function useRelativeTime({
51
61
  value,
52
62
  locale = 'en',
53
63
  withoutSuffix = false,
@@ -56,18 +66,36 @@ export default function RelativeTime({
56
66
  type = 'relative',
57
67
  tz,
58
68
  relativeRange,
59
- enableTooltip = true,
60
- ...rest
61
- }: RelativeTimeProps) {
69
+ }: {
70
+ value: string | number;
71
+ locale?: Locale;
72
+ withoutSuffix?: boolean;
73
+ from?: string | number;
74
+ to?: string | number;
75
+ type?: 'relative' | 'absolute' | 'utc';
76
+ tz?: string;
77
+ relativeRange?: number;
78
+ }) {
79
+ const date = new Date();
80
+ const timeZoneOffset = date.getTimezoneOffset();
81
+ const sign = timeZoneOffset > 0 ? '-' : '+';
82
+ const hoursOffset = Math.abs(timeZoneOffset) / 60;
83
+ const isLocalUtc = timeZoneOffset === 0;
84
+ const [isUtc, setIsUtc] = useState(isLocalUtc);
85
+
62
86
  if (!value) {
63
- return '-';
87
+ return { innerContent: '-', popContent: '-', isUtc, setIsUtc, sign, hoursOffset };
64
88
  }
65
89
 
66
90
  const localeOption = locale === 'zh' ? 'zh-cn' : 'en-us';
67
- const datetime = dayjs(value).locale(localeOption);
91
+ let datetime = dayjs(value).locale(localeOption);
92
+
93
+ if (type === 'utc') {
94
+ datetime = dayjs(value).utc().locale(localeOption);
95
+ }
68
96
 
69
97
  if (tz) {
70
- datetime.tz(tz);
98
+ datetime = datetime.tz(tz);
71
99
  }
72
100
 
73
101
  const absoluteString = formatToDatetime(value, { locale: localeOption, tz });
@@ -97,9 +125,110 @@ export default function RelativeTime({
97
125
  popContent = relativeString;
98
126
  }
99
127
 
128
+ if (type === 'utc') {
129
+ if (isUtc) {
130
+ innerContent = formatToDatetime(value, { locale: localeOption, tz, isUtc: true });
131
+ popContent = formatToDatetime(value, { locale: localeOption, tz, isUtc: true });
132
+ } else {
133
+ innerContent = absoluteString;
134
+ popContent = relativeString;
135
+ }
136
+ }
137
+
138
+ return { innerContent, popContent, isUtc, setIsUtc, sign, hoursOffset, relativeString, absoluteString };
139
+ }
140
+
141
+ function UTCChip({
142
+ locale,
143
+ isUtc,
144
+ sign,
145
+ hoursOffset,
146
+ setIsUtc,
147
+ }: {
148
+ locale: Locale;
149
+ isUtc?: boolean;
150
+ sign: string;
151
+ hoursOffset: number;
152
+ setIsUtc: (data: any) => void;
153
+ }) {
154
+ return (
155
+ <Box
156
+ component="span"
157
+ sx={{
158
+ color: 'inherit',
159
+ cursor: 'pointer',
160
+ border: '1px solid',
161
+ fontSize: '0.8rem',
162
+ borderColor: 'divider',
163
+ borderRadius: '20px',
164
+ padding: '0px 8px',
165
+ }}
166
+ onClick={() => setIsUtc((r: any) => !r)}>
167
+ {`${translations[locale].utc}: ${isUtc ? 'UTC' : `UTC${sign}${hoursOffset}`}`}
168
+ </Box>
169
+ );
170
+ }
171
+
172
+ export default function RelativeTime({
173
+ value,
174
+ locale = 'en',
175
+ withoutSuffix = false,
176
+ from = '',
177
+ to = '',
178
+ type = 'relative',
179
+ tz,
180
+ relativeRange,
181
+ enableTooltip = true,
182
+ ...rest
183
+ }: RelativeTimeProps) {
184
+ const { innerContent, popContent, isUtc, setIsUtc, sign, hoursOffset, relativeString } = useRelativeTime({
185
+ value,
186
+ locale,
187
+ withoutSuffix,
188
+ from,
189
+ to,
190
+ type: type === 'all' ? 'utc' : type,
191
+ tz,
192
+ relativeRange,
193
+ });
194
+
195
+ if (type === 'all') {
196
+ return (
197
+ <Tooltip title={undefined} placement="top-end" enterTouchDelay={0}>
198
+ <Box display="flex" alignItems="center" gap={0.5} width="max-content" {...rest}>
199
+ <Box component="span" {...rest} sx={{}}>
200
+ {innerContent}
201
+ </Box>
202
+
203
+ <Box component="span" sx={{ color: 'inherit' }}>
204
+ ·
205
+ </Box>
206
+
207
+ <Box component="span" sx={{ color: 'text.secondary' }}>
208
+ {relativeString}
209
+ </Box>
210
+
211
+ <Box component="span" sx={{ color: 'inherit' }}>
212
+ ·
213
+ </Box>
214
+
215
+ <UTCChip locale={locale} isUtc={isUtc} sign={sign} hoursOffset={hoursOffset} setIsUtc={setIsUtc} />
216
+ </Box>
217
+ </Tooltip>
218
+ );
219
+ }
220
+
100
221
  return (
101
222
  <Tooltip title={enableTooltip ? popContent : undefined} placement="top-end" enterTouchDelay={0}>
102
- <span {...rest}>{innerContent}</span>
223
+ <Box display="flex" alignItems="center" gap={1} width="max-content">
224
+ <Box component="span" {...rest}>
225
+ {innerContent}
226
+ </Box>
227
+
228
+ {type === 'utc' && (
229
+ <UTCChip locale={locale} isUtc={isUtc} sign={sign} hoursOffset={hoursOffset} setIsUtc={setIsUtc} />
230
+ )}
231
+ </Box>
103
232
  </Tooltip>
104
233
  );
105
234
  }
package/src/Util/index.ts CHANGED
@@ -246,7 +246,7 @@ export function getDateTool() {
246
246
 
247
247
  const createDateFormatter =
248
248
  (format: string) =>
249
- (date: string | number | Date, { locale, tz }: { locale?: Locale; tz?: string } = {}) => {
249
+ (date: string | number | Date, { locale, tz, isUtc }: { locale?: Locale; tz?: string; isUtc?: boolean } = {}) => {
250
250
  if (dateTool === null) {
251
251
  throw new Error('call setDateTool to set the date tool library, such as: setDateTool(dayjs)');
252
252
  }
@@ -261,6 +261,10 @@ const createDateFormatter =
261
261
  instance = instance.tz(tz);
262
262
  }
263
263
 
264
+ if (isUtc) {
265
+ instance = instance.utc();
266
+ }
267
+
264
268
  if (typeof locale !== 'undefined') {
265
269
  instance = instance.locale(locale);
266
270
  }
@@ -287,9 +291,9 @@ export function formatToDate(
287
291
  */
288
292
  export function formatToDatetime(
289
293
  date: string | number | Date,
290
- { locale = 'en', tz }: { locale?: Locale; tz?: string } = {}
294
+ { locale = 'en', tz, isUtc = false }: { locale?: Locale; tz?: string; isUtc?: boolean } = {}
291
295
  ) {
292
- return createDateFormatter('lll')(date, { locale, tz });
296
+ return createDateFormatter('lll')(date, { locale, tz, isUtc });
293
297
  }
294
298
 
295
299
  export function detectWalletExtension() {