Skip to content

Commit

Permalink
Merge pull request #35 from azvyae/fix/timezone-issue
Browse files Browse the repository at this point in the history
Fix problem that jsonata cannot determine timezone offset
  • Loading branch information
azvyae authored Oct 10, 2024
2 parents 68cfe32 + 0507fd2 commit e683405
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "optimisticoder",
"version": "1.1.0",
"version": "1.1.1",
"private": false,
"contributors": [
{
Expand All @@ -24,7 +24,6 @@
"decrypt-all": "env-decrypt local && env-decrypt production"
},
"dependencies": {
"@types/js-cookie": "^3.0.6",
"@vercel/analytics": "^1.3.1",
"@vercel/speed-insights": "^1.0.12",
"client-only": "^0.0.1",
Expand Down Expand Up @@ -54,6 +53,7 @@
"sharp": "^0.33.5"
},
"devDependencies": {
"@types/js-cookie": "^3.0.6",
"@tailwindcss/typography": "^0.5.15",
"@types/fs-extra": "^11.0.4",
"@types/node": "^20.16.11",
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/app/stories/sections.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { FilteringHandler } from '@/app/stories/components';
import { StoryCard } from '@/components/common/story-card';
import { STORIES_URL } from '@/config/common';
import { sfetch } from '@/helpers/common';
import { sfetch, timezoneOffset } from '@/helpers/common';
import type { StoriesIndexEntry, StoriesMeta } from '@/types/common';
import type { CommonResponse } from '@/types/responses';
import Stars from '@public/static/svg/stars.svg';
import jsonata from 'jsonata';
import { cookies } from 'next/headers';
import Image from 'next/image';
import Link from 'next/link';
import { redirect } from 'next/navigation';
Expand Down Expand Up @@ -60,9 +61,9 @@ async function listStories(
} else if (key === 'search') {
query = `$[$contains(title,/${value}/i) or keywords[$contains($,/${value}/i)]][]`;
} else if (key === 'date') {
const timezoneOffsetMillis =
new Date().getTimezoneOffset() * 60 * 1000 * -1;
query = `$[$contains($fromMillis($toMillis(date)+${timezoneOffsetMillis}),"${value}")][]`;
const tz = cookies().get('tz')?.value ?? 'Asia/Jakarta';
const timezoneOffsetMillis = timezoneOffset(tz);
query = `$[$contains($fromMillis($toMillis(date)${timezoneOffsetMillis >= 0 ? '+' : ''}${timezoneOffsetMillis}),"${value}")][]`;
}
const expression = jsonata(query);
const result: StoriesIndexEntry[] = await expression.evaluate(indexStories);
Expand Down
22 changes: 17 additions & 5 deletions src/components/navigation/components.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use client';

import { NavLink } from '@/components/link';
import { links } from '@/config/common';
import { APP_ENV, links } from '@/config/common';
import Cookies from 'js-cookie';
import { usePathname } from 'next/navigation';
import { useEffect, useState } from 'react';
Expand Down Expand Up @@ -130,10 +130,16 @@ function DarkModeSelector({
if (!defaultTheme) {
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.add('dark');
Cookies.set('theme', 'dark');
Cookies.set('theme', 'dark', {
expires: 365,
secure: APP_ENV === 'production',
});
} else {
document.documentElement.classList.remove('dark');
Cookies.set('theme', 'light');
Cookies.set('theme', 'light', {
expires: 365,
secure: APP_ENV === 'production',
});
}
}
setTheme(Cookies.get('theme') as Theme);
Expand All @@ -144,7 +150,10 @@ function DarkModeSelector({
switch (t) {
case 'dark':
document.documentElement.classList.remove('dark');
Cookies.set('theme', 'light');
Cookies.set('theme', 'light', {
expires: 365,
secure: APP_ENV === 'production',
});
return 'light';
case 'light':
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
Expand All @@ -156,7 +165,10 @@ function DarkModeSelector({
return;
default:
document.documentElement.classList.add('dark');
Cookies.set('theme', 'dark');
Cookies.set('theme', 'dark', {
expires: 365,
secure: APP_ENV === 'production',
});
return 'dark';
}
});
Expand Down
25 changes: 25 additions & 0 deletions src/helpers/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ function generateFallbackImage(initials: string) {
return `/static/fallback/${randomNum}.jpg`;
}

function timezoneOffset(ianaTimeZone: string) {
const now = new Date();
now.setSeconds(0, 0);

// Format current time in `ianaTimeZone` as `M/DD/YYYY, HH:MM:SS`:
const tzDateString = now.toLocaleString('en-US', {
timeZone: ianaTimeZone,
hourCycle: 'h23',
});

// Parse formatted date string:
const match = /(\d+)\/(\d+)\/(\d+), (\d+):(\d+)/.exec(tzDateString);
if (!match) {
return 25200000;
}
const [, month, day, year, hour, min] = match.map(Number);

// Change date string's time zone to UTC and get timestamp:
const tzTime = Date.UTC(year, month - 1, day, hour, min);

// Return the offset between UTC and target time zone:
return Math.floor(tzTime - now.getTime());
}

function isMobileBrowser() {
if (!window) {
return false;
Expand All @@ -69,4 +93,5 @@ export {
generateFallbackImage,
isMobileBrowser,
sfetch,
timezoneOffset,
};
14 changes: 14 additions & 0 deletions src/providers/client-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
'use client';

import { APP_ENV } from '@/config/common';
import Cookies from 'js-cookie';
import { AppProgressBar as ProgressBar } from 'next-nprogress-bar';
import { usePathname } from 'next/navigation';
import { useEffect } from 'react';

const nprogressStyle = `
#nprogress {
Expand Down Expand Up @@ -45,6 +49,16 @@ const nprogressStyle = `
}
`;
function ClientProvider() {
const pathname = usePathname();
useEffect(() => {
const tz = Cookies.get('tz');
if (!tz) {
Cookies.set('tz', Intl.DateTimeFormat().resolvedOptions().timeZone, {
expires: 1,
secure: APP_ENV === 'production',
});
}
}, [pathname]);
return (
<>
<ProgressBar
Expand Down

0 comments on commit e683405

Please sign in to comment.