Skip to content

Commit

Permalink
Merge pull request #45 from rohitkumbhar/richer-airport-info
Browse files Browse the repository at this point in the history
Showing full name and timezone info for airports
  • Loading branch information
rohitkumbhar authored Feb 2, 2025
2 parents d99cae5 + 80f7e9a commit 68f0b52
Show file tree
Hide file tree
Showing 17 changed files with 279 additions and 80 deletions.
11 changes: 11 additions & 0 deletions backend/app/surmai.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"backend/hooks"
"backend/jobs"
R "backend/routes"
"github.com/pocketbase/pocketbase"
Expand Down Expand Up @@ -80,3 +81,13 @@ func (surmai *SurmaiApp) StartDemoMode() {
})
}
}

func (surmai *SurmaiApp) BindEventHooks() {
surmai.Pb.OnRecordCreate("trips").BindFunc(func(e *core.RecordEvent) error {
return hooks.AddTimezoneToDestinations(e, surmai.TimezoneFinder)
})

surmai.Pb.OnRecordUpdate("trips").BindFunc(func(e *core.RecordEvent) error {
return hooks.AddTimezoneToDestinations(e, surmai.TimezoneFinder)
})
}
6 changes: 5 additions & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ require (
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect
Expand Down Expand Up @@ -75,8 +76,11 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20250127172529-29210b9bc287 // indirect
google.golang.org/grpc v1.70.0 // indirect
google.golang.org/protobuf v1.36.4 // indirect
modernc.org/gc/v3 v3.0.0-20250105121824-520be1a3aee6 // indirect
modernc.org/libc v1.61.10 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.8.2 // indirect
modernc.org/sqlite v1.34.5 // indirect
modernc.org/sqlite v1.34.4 // indirect
modernc.org/strutil v1.2.1 // indirect
modernc.org/token v1.1.0 // indirect
)
8 changes: 6 additions & 2 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.4 h1:XYIDZApgAnrN1c855gT
github.com/googleapis/enterprise-certificate-proxy v0.3.4/go.mod h1:YKe7cfqYXjKGpGvmSg28/fFvhNzinZQm8DGnaburhGA=
github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q=
github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand Down Expand Up @@ -411,6 +413,8 @@ modernc.org/fileutil v1.3.0 h1:gQ5SIzK3H9kdfai/5x41oQiKValumqNTDXMvKo62HvE=
modernc.org/fileutil v1.3.0/go.mod h1:XatxS8fZi3pS8/hKG2GH/ArUogfxjpEKs3Ku3aK4JyQ=
modernc.org/gc/v2 v2.6.2 h1:YBXi5Kqp6aCK3fIxwKQ3/fErvawVKwjOLItxj1brGds=
modernc.org/gc/v2 v2.6.2/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
modernc.org/gc/v3 v3.0.0-20250105121824-520be1a3aee6 h1:JoKwHjIFumiKrjMbp1cNbC5E9UyCgA/ZcID0xOWQ2N8=
modernc.org/gc/v3 v3.0.0-20250105121824-520be1a3aee6/go.mod h1:LG5UO1Ran4OO0JRKz2oNiXhR5nNrgz0PzH7UKhz0aMU=
modernc.org/libc v1.61.10 h1:zPPaT7/dnMkTzG8b9HjIsvxWr4Ixk3Ce/WPuxakHj7Q=
modernc.org/libc v1.61.10/go.mod h1:HHX+srFdn839oaJRd0W8hBM3eg+mieyZCAjWwB08/nM=
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
Expand All @@ -421,8 +425,8 @@ modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
modernc.org/sqlite v1.34.5 h1:Bb6SR13/fjp15jt70CL4f18JIN7p7dnMExd+UFnF15g=
modernc.org/sqlite v1.34.5/go.mod h1:YLuNmX9NKs8wRNK2ko1LW1NGYcc9FkBO69JOt1AR9JE=
modernc.org/sqlite v1.34.4 h1:sjdARozcL5KJBvYQvLlZEmctRgW9xqIZc2ncN7PU0P8=
modernc.org/sqlite v1.34.4/go.mod h1:3QQFCG2SEMtc2nv+Wq4cQCH7Hjcg+p/RMlS1XK+zwbk=
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
Expand Down
49 changes: 49 additions & 0 deletions backend/hooks/add_timezone_to_destinations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package hooks

import (
"backend/trips"
"encoding/json"
"github.com/pocketbase/pocketbase/core"
"github.com/ringsaturn/tzf"
"strconv"
)

func AddTimezoneToDestinations(e *core.RecordEvent, finder tzf.F) error {

record := e.Record
destinations := record.GetString("destinations")

var payload []trips.Destination
err := json.Unmarshal([]byte((destinations)), &payload)

var updatedDestinations = make([]trips.Destination, len(payload))

if err != nil {
return err
}

for i, destination := range payload {
if destination.Latitude != "" && destination.Longitude != "" {

timezone := destination.TimeZone
if timezone == "" {
lat, _ := strconv.ParseFloat(destination.Latitude, 64)
long, _ := strconv.ParseFloat(destination.Longitude, 64)
timezone = finder.GetTimezoneName(long, lat)
}

updatedDestinations[i] = trips.Destination{
Id: destination.Id,
Name: destination.Name,
StateName: destination.StateName,
CountryName: destination.CountryName,
Latitude: destination.Latitude,
Longitude: destination.Longitude,
TimeZone: timezone,
}
}
}

record.Set("destinations", updatedDestinations)
return e.Next()
}
1 change: 1 addition & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func main() {
surmai.BuildTimezoneFinder()
surmai.BindMigrations(isGoRun)
surmai.BindRoutes()
surmai.BindEventHooks()
surmai.StartDemoMode()

if err := surmai.Pb.Start(); err != nil {
Expand Down
39 changes: 39 additions & 0 deletions backend/migrations/1738518698_add_timezone_field_to_airports.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package migrations

import (
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
)

func init() {
m.Register(func(app core.App) error {

airports, err := app.FindCollectionByNameOrId("airports")
if err != nil {
return err
}

timezone := airports.Fields.GetByName("timezone")
if timezone == nil {
airports.Fields.Add(
&core.TextField{
Name: "timezone",
})
}

return app.Save(airports)
}, func(app core.App) error {

airports, err := app.FindCollectionByNameOrId("airports")
if err != nil {
return err
}

timezone := airports.Fields.GetByName("timezone")
if timezone != nil {
airports.Fields.RemoveByName("timezone")
}

return nil
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package migrations

import (
"github.com/pocketbase/pocketbase/core"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/ringsaturn/tzf"
"log"
"strconv"
)

func init() {
m.Register(func(app core.App) error {
finder, err := tzf.NewDefaultFinder()
if err != nil {
panic(err)
}

records, err := app.FindAllRecords("airports")
if err != nil {
panic(err)
}

for _, airport := range records {
latitude := airport.GetString("latitude")
longitude := airport.GetString("longitude")

if latitude != "" && longitude != "" {
lat, _ := strconv.ParseFloat(latitude, 64)
long, _ := strconv.ParseFloat(longitude, 64)
timezone := finder.GetTimezoneName(long, lat)
airport.Set("timezone", timezone)
err = app.Save(airport)
if err != nil {
log.Printf("Error saving airport to %s: %s", timezone, err)
}
}
}

return nil
}, func(app core.App) error {
return nil
})
}
8 changes: 7 additions & 1 deletion backend/trips/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ type UploadedFile struct {
}

type Destination struct {
Name string `json:"name"`
Id string `json:"id"`
Name string `json:"name"`
StateName string `json:"stateName"`
CountryName string `json:"countryName"`
TimeZone string `json:"timezone"`
Latitude string `json:"latitude"`
Longitude string `json:"longitude"`
}

type Participant struct {
Expand Down
23 changes: 4 additions & 19 deletions src/components/destinations/DestinationSelect.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MutableRefObject, useEffect, useRef, useState } from 'react';
import { Combobox, Group, Pill, PillsInput, Text, useCombobox } from '@mantine/core';
import { useClickOutside, useDebouncedState } from '@mantine/hooks';
import { getTimezone, searchPlaces } from '../../lib/api';
import { searchPlaces } from '../../lib/api';
import { nanoid } from 'nanoid';
import { UseFormReturnType } from '@mantine/form';
import { Destination } from '../../types/trips.ts';
Expand Down Expand Up @@ -57,26 +57,11 @@ export function DestinationSelect({ propName, form }: { propName: string; form:
const selection = searchResults.find((item) => item.id === val);
const existing = values.find((v) => v.id === val);
if (!existing && selection) {
if (selection.latitude && selection.longitude) {
getTimezone(selection.latitude, selection.longitude)
.then((timezone) => {
const updatedValues = [...values, { ...selection, timezone }];
setValues(updatedValues);
form.setFieldValue(propName, updatedValues);
})
.catch(() => {
const updatedValues = [...values, selection];
setValues(updatedValues);
form.setFieldValue(propName, updatedValues);
});
} else {
const updatedValues = [...values, selection];
setValues(updatedValues);
form.setFieldValue(propName, updatedValues);
}
const updatedValues = [...values, { ...selection }];
setValues(updatedValues);
form.setFieldValue(propName, updatedValues);
}
}

combobox.closeDropdown();
};

Expand Down
34 changes: 3 additions & 31 deletions src/components/trip/basic/DestinationCard.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
import { Destination, Trip } from '../../../types/trips.ts';
import { IconClock, IconMapPin } from '@tabler/icons-react';
import { Anchor, Badge, Card, Group, HoverCard, Stack, Text } from '@mantine/core';
import { Anchor, Badge, Card, Group, HoverCard, Text } from '@mantine/core';
import classes from './DestinationCard.module.css';
import { useCurrentUser } from '../../../auth/useCurrentUser.ts';
import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { calculateTimezoneDifference } from '../../../lib/time.ts';
import { getMapsUrl } from '../../../lib/places.ts';
import { TimezoneInfo } from '../../util/TimezoneInfo.tsx';

export const DestinationCard = ({ destination }: { destination: Destination; trip: Trip }) => {
const { user } = useCurrentUser();
const { t } = useTranslation();
const [timezoneDiff, setTimezoneDiff] = useState<number>(0);

useEffect(() => {
if (destination.timezone) {
setTimezoneDiff(calculateTimezoneDifference(user, destination.timezone));
}
}, [user, destination]);

return (
<Card withBorder radius="xs" className={classes.card} p={'xs'}>
<Group justify="space-between">
Expand All @@ -42,24 +31,7 @@ export const DestinationCard = ({ destination }: { destination: Destination; tri
</Badge>
</HoverCard.Target>
<HoverCard.Dropdown>
<Stack gap={'md'}>
{!destination.timezone && <Text size={'sm'}>{t('no_tz', 'Timezone information is unavailable')}</Text>}
{destination.timezone && timezoneDiff === 0 && (
<Text size={'sm'}>
{t('tz_no_diff', 'There is no difference in your timezone and the timezone at this destination')}
</Text>
)}
{timezoneDiff < 0 && (
<Text size={'sm'}>
{t('tz_behind', 'Your timezone is {{diff}} hours behind', { diff: Math.abs(timezoneDiff) })}
</Text>
)}
{timezoneDiff > 0 && (
<Text size={'sm'}>
{t('tz_ahead', 'Your timezone is {{diff}} hours ahead', { diff: timezoneDiff })}
</Text>
)}
</Stack>
<TimezoneInfo user={user} timezone={destination.timezone} />
</HoverCard.Dropdown>
</HoverCard>
</Group>
Expand Down
12 changes: 10 additions & 2 deletions src/components/trip/transportation/AirportSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const AirportSelect = ({
combobox.openDropdown();
});
}
}, [search, combobox]);
}, [search]);

const options = searchResults
.filter((item) => item.iataCode && item.iataCode !== '')
Expand All @@ -59,7 +59,15 @@ export const AirportSelect = ({
const selection = searchResults.find((item) => item.id === val);
if (selection) {
setValue(selection.iataCode);
form.setFieldValue(propName, selection.iataCode);
form.setFieldValue(propName, {
iataCode: selection.iataCode,
name: selection.name,
id: selection.id,
countryCode: selection.isoCountry,
latitude: selection.latitude,
longitude: selection.longitude,
timezone: selection.timezone,
});
}
}
combobox.closeDropdown();
Expand Down
Loading

0 comments on commit 68f0b52

Please sign in to comment.