@@ -3,7 +3,6 @@ import { getCurrentIdtTime } from './dateTime'
33import type { Env } from '../env'
44import type { Flight , InlineKeyboardButton , InlineKeyboardMarkup , DOProps } from '../types'
55
6- // Helper function to format time from timestamp
76export const formatTimeFromTimestamp = ( timestamp : number ) => {
87 const date = new Date ( timestamp )
98 return date . toLocaleTimeString ( 'en-GB' , {
@@ -12,8 +11,13 @@ export const formatTimeFromTimestamp = (timestamp: number) => {
1211 hour12 : false ,
1312 } )
1413}
14+ export const formatDateFromTimestamp = ( timestamp : number ) => {
15+ const date = new Date ( timestamp )
16+ return date . toLocaleString ( 'en-GB' , {
17+ hour12 : false ,
18+ } )
19+ }
1520
16- // Helper function to get day label from timestamp
1721export const getDayLabelFromTimestamp = ( timestamp : number , ctx : DurableObjectState < DOProps > ) => {
1822 const date = new Date ( timestamp )
1923 const todayIdt = getCurrentIdtTime ( ctx )
@@ -34,16 +38,37 @@ export const getDayLabelFromTimestamp = (timestamp: number, ctx: DurableObjectSt
3438 }
3539}
3640
37- export const formatTrackingList = async ( userFlights : string [ ] , env : Env ) => {
38- if ( userFlights . length === 0 ) return "You're not tracking any flights.\nUse /track LY086 to start!"
41+ export const formatTimeAgo = ( timestamp : number , ctx : DurableObjectState < DOProps > ) : string => {
42+ if ( ! timestamp || timestamp === 0 ) {
43+ return '- not updated'
44+ }
45+
46+ const now = getCurrentIdtTime ( ctx ) . getTime ( )
47+ const diffMs = now - timestamp
48+ const diffMinutes = Math . floor ( diffMs / 60000 )
49+
50+ if ( diffMinutes < 1 ) {
51+ return 'just now'
52+ } else if ( diffMinutes < 60 ) {
53+ return `${ diffMinutes } minute${ diffMinutes > 1 ? 's' : '' } ago`
54+ } else if ( diffMinutes < 1440 ) {
55+ const hours = Math . floor ( diffMinutes / 60 )
56+ return `${ hours } hour${ hours > 1 ? 's' : '' } ago`
57+ } else {
58+ const days = Math . floor ( diffMinutes / 1440 )
59+ return `${ days } day${ days > 1 ? 's' : '' } ago`
60+ }
61+ }
62+
63+ export const formatTimestampForDisplay = ( timestamp : number ) : string => {
64+ if ( ! timestamp || timestamp === 0 ) {
65+ return '- not updated'
66+ }
3967
40- // Use the optimized function to get flight data in a single query
41- const chatId = parseInt ( userFlights [ 0 ] . split ( '_' ) [ 0 ] ) // Extract chat ID from first flight ID (this is a hack, need to pass chatId properly)
42- // Actually, let's modify the function signature to accept chatId instead of userFlights array
43- return 'Function needs to be called with chatId parameter for optimization'
68+ const date = new Date ( timestamp )
69+ return date . toISOString ( ) . split ( 'T' ) [ 0 ]
4470}
4571
46- // New optimized version that takes chatId directly
4772export const formatTrackingListOptimized = (
4873 chatId : number ,
4974 env : Env ,
0 commit comments