diff --git a/app/Filament/Pages/Settings/GeneralPage.php b/app/Filament/Pages/Settings/GeneralPage.php index da0683143..ed04622b8 100644 --- a/app/Filament/Pages/Settings/GeneralPage.php +++ b/app/Filament/Pages/Settings/GeneralPage.php @@ -50,9 +50,21 @@ public function form(Form $form): Form Forms\Components\TextInput::make('site_name') ->maxLength(50) ->required() - ->columnSpan(['md' => 2]), + ->columnSpanFull(), + Forms\Components\Toggle::make('public_dashboard_enabled') + ->label('Public dashboard'), + ]) + ->compact() + ->columns([ + 'default' => 1, + 'md' => 2, + ]), + + Forms\Components\Section::make('Time Zone Settings') + ->schema([ Forms\Components\Select::make('timezone') ->label('Time zone') + ->hint(new HtmlString('🔗Docs')) ->options(TimeZoneHelper::list()) ->searchable() ->required(), @@ -61,6 +73,9 @@ public function form(Form $form): Form ->placeholder('M j, Y G:i:s') ->maxLength(25) ->required(), + Forms\Components\Toggle::make('db_has_timezone') + ->label('Database has time zone') + ->helperText(new HtmlString('Enable if your database has a time zone already set.')), ]) ->compact() ->columns([ @@ -113,18 +128,6 @@ public function form(Form $form): Form 'default' => 1, 'md' => 2, ]), - - Forms\Components\Section::make('Public Dashboard Settings') - ->schema([ - Forms\Components\Toggle::make('public_dashboard_enabled') - ->label('Enable') - ->columnSpan(2), - ]) - ->compact() - ->columns([ - 'default' => 1, - 'md' => 2, - ]), ]) ->columnSpan('full'), ]); diff --git a/app/Filament/Resources/ResultResource.php b/app/Filament/Resources/ResultResource.php index 395a9e807..e41059c9e 100644 --- a/app/Filament/Resources/ResultResource.php +++ b/app/Filament/Resources/ResultResource.php @@ -4,6 +4,7 @@ use App\Exports\ResultsSelectedBulkExport; use App\Filament\Resources\ResultResource\Pages; +use App\Helpers\TimeZoneHelper; use App\Models\Result; use App\Settings\GeneralSettings; use Carbon\Carbon; @@ -142,7 +143,7 @@ public static function table(Table $table): Table TextColumn::make('created_at') ->label('Created') ->dateTime($settings->time_format ?? 'M j, Y G:i:s') - ->timezone($settings->timezone ?? 'UTC') + ->timezone(TimeZoneHelper::displayTimeZone($settings)) ->sortable(), ]) ->filters([ diff --git a/app/Filament/Widgets/RecentJitterChartWidget.php b/app/Filament/Widgets/RecentJitterChartWidget.php index e300c2ba1..f7d9d2bad 100644 --- a/app/Filament/Widgets/RecentJitterChartWidget.php +++ b/app/Filament/Widgets/RecentJitterChartWidget.php @@ -2,6 +2,7 @@ namespace App\Filament\Widgets; +use App\Helpers\TimeZoneHelper; use App\Models\Result; use App\Settings\GeneralSettings; use Filament\Widgets\ChartWidget; @@ -77,7 +78,7 @@ protected function getData(): array 'tension' => 0.4, ], ], - 'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')), + 'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')), ]; } diff --git a/app/Filament/Widgets/RecentPingChartWidget.php b/app/Filament/Widgets/RecentPingChartWidget.php index 2b83a1d7e..873f1f9f8 100644 --- a/app/Filament/Widgets/RecentPingChartWidget.php +++ b/app/Filament/Widgets/RecentPingChartWidget.php @@ -2,6 +2,7 @@ namespace App\Filament\Widgets; +use App\Helpers\TimeZoneHelper; use App\Models\Result; use App\Settings\GeneralSettings; use Filament\Widgets\ChartWidget; @@ -59,7 +60,7 @@ protected function getData(): array 'tension' => 0.4, ], ], - 'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')), + 'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')), ]; } diff --git a/app/Filament/Widgets/RecentSpeedChartWidget.php b/app/Filament/Widgets/RecentSpeedChartWidget.php index 767dd5edf..9be0032e5 100644 --- a/app/Filament/Widgets/RecentSpeedChartWidget.php +++ b/app/Filament/Widgets/RecentSpeedChartWidget.php @@ -2,6 +2,7 @@ namespace App\Filament\Widgets; +use App\Helpers\TimeZoneHelper; use App\Models\Result; use App\Settings\GeneralSettings; use Filament\Widgets\ChartWidget; @@ -68,7 +69,7 @@ protected function getData(): array 'tension' => 0.4, ], ], - 'labels' => $results->map(fn ($item) => $item->created_at->timezone($settings->timezone)->format('M d - G:i')), + 'labels' => $results->map(fn ($item) => $item->created_at->timezone(TimeZoneHelper::displayTimeZone($settings))->format('M d - G:i')), ]; } diff --git a/app/Helpers/TimeZoneHelper.php b/app/Helpers/TimeZoneHelper.php index 7a7ecfafd..a965bf889 100644 --- a/app/Helpers/TimeZoneHelper.php +++ b/app/Helpers/TimeZoneHelper.php @@ -2,12 +2,27 @@ namespace App\Helpers; +use App\Settings\GeneralSettings; use DateTimeZone; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Str; class TimeZoneHelper { + /** + * Returns the display timezone + */ + public static function displayTimeZone(GeneralSettings $settings): string + { + // Don't translate to local time if the database already returns it. + if ($settings->db_has_timezone) { + return 'UTC'; + } + + return $settings->timezone + ?? 'UTC'; + } + /** * Returns a collection of time zones with their offset from UTC. */ diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index fbc92749f..c00b2efb1 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -28,7 +28,19 @@ public function __invoke(Request $request) return view('get-started'); } + /** + * This jank needs to happen because some people like + * to watch the world burn by setting a time zone + * in their database instances. + */ + if ($settings->db_has_timezone) { + date_default_timezone_set($settings->timezone ?? 'UTC'); + } + + $diff = $latestResult->created_at->diffForHumans(); + return view('dashboard', [ + 'diff' => $diff, 'latestResult' => $latestResult, ]); } diff --git a/app/Settings/GeneralSettings.php b/app/Settings/GeneralSettings.php index a7a4ed50a..b2f22d3a3 100644 --- a/app/Settings/GeneralSettings.php +++ b/app/Settings/GeneralSettings.php @@ -19,6 +19,8 @@ class GeneralSettings extends Settings public string $timezone; + public bool $db_has_timezone; + public bool $public_dashboard_enabled; public static function group(): string diff --git a/database/settings/2023_12_12_234839_add_db_has_time_zone_to_general_settings.php b/database/settings/2023_12_12_234839_add_db_has_time_zone_to_general_settings.php new file mode 100644 index 000000000..caff13154 --- /dev/null +++ b/database/settings/2023_12_12_234839_add_db_has_time_zone_to_general_settings.php @@ -0,0 +1,11 @@ +migrator->add('general.db_has_timezone', false); + } +}; diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 4aeb32c9c..fe5219f90 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -4,11 +4,11 @@ @livewire(\App\Filament\Widgets\StatsOverviewWidget::class) - @if ($latestResult) + @isset($latestResult)
- Latest result: {{ $latestResult?->created_at->diffForHumans() }} + Latest result:
- @endif + @endisset
@livewire(\App\Filament\Widgets\RecentSpeedChartWidget::class) diff --git a/resources/views/livewire/debug/timezone.blade.php b/resources/views/livewire/debug/timezone.blade.php index 18f5eb92f..930db4c4e 100644 --- a/resources/views/livewire/debug/timezone.blade.php +++ b/resources/views/livewire/debug/timezone.blade.php @@ -62,7 +62,7 @@