1+ import {
2+ ColumnTypeCompBuilder ,
3+ ColumnTypeViewFn ,
4+ } from "comps/comps/tableComp/column/columnTypeCompBuilder" ;
5+ import { ColumnValueTooltip } from "comps/comps/tableComp/column/simpleColumnTypeComps" ;
6+ import { StringControl } from "comps/controls/codeControl" ;
7+ import { withDefault } from "comps/generators" ;
8+ import { formatPropertyView } from "comps/utils/propertyUtils" ;
9+ import { trans } from "i18n" ;
10+ import {
11+ TIME_FORMAT ,
12+ formatTimestamp ,
13+ timestampToHumanReadable ,
14+ } from "util/dateTimeUtils" ;
15+ import { DateEdit } from "./columnDateComp" ;
16+
17+ const childrenMap = {
18+ text : StringControl ,
19+ format : withDefault ( StringControl , TIME_FORMAT ) ,
20+ inputFormat : withDefault ( StringControl , TIME_FORMAT ) ,
21+ } ;
22+
23+ let inputFormat = TIME_FORMAT ;
24+
25+ const getBaseValue : ColumnTypeViewFn < typeof childrenMap , string , string > = ( props ) =>
26+ props . text ;
27+
28+ export const TimeComp = ( function ( ) {
29+ return new ColumnTypeCompBuilder (
30+ childrenMap ,
31+ ( props , dispatch ) => {
32+ inputFormat = props . inputFormat ;
33+ const value = props . changeValue ?? getBaseValue ( props , dispatch ) ;
34+
35+ // Convert value to a number if it's a valid timestamp
36+ const timestamp = Number ( value ) ;
37+ if ( ! isNaN ( timestamp ) ) {
38+ return formatTimestamp ( timestamp ) ;
39+ }
40+
41+ return timestampToHumanReadable ( timestamp ) ?? value ; // Returns readable time
42+ } ,
43+ ( nodeValue ) => {
44+ const timestamp = Number ( nodeValue . text . value ) ;
45+ return ! isNaN ( timestamp )
46+ ? timestampToHumanReadable ( timestamp ) // Convert to readable format if valid timestamp
47+ : nodeValue . text . value ; // Otherwise, return original value
48+ } ,
49+ getBaseValue
50+ )
51+ . setEditViewFn ( ( props ) => (
52+ < DateEdit
53+ value = { props . value }
54+ onChange = { props . onChange }
55+ onChangeEnd = { props . onChangeEnd }
56+ showTime = { true } // Ensures only time is shown
57+ inputFormat = { inputFormat }
58+ />
59+ ) )
60+ . setPropertyViewFn ( ( children ) => (
61+ < >
62+ { children . text . propertyView ( {
63+ label : trans ( "table.columnValue" ) ,
64+ tooltip : ColumnValueTooltip ,
65+ } ) }
66+ { formatPropertyView ( { children, placeholder : TIME_FORMAT } ) }
67+ </ >
68+ ) )
69+ . build ( ) ;
70+ } ) ( ) ;
71+
0 commit comments