diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/data-context.ts | 65 | ||||
-rw-r--r-- | lib/utils.ts | 21 | ||||
-rw-r--r-- | lib/wmocodes.json | 282 |
3 files changed, 368 insertions, 0 deletions
diff --git a/lib/data-context.ts b/lib/data-context.ts new file mode 100644 index 0000000..f4c09c7 --- /dev/null +++ b/lib/data-context.ts @@ -0,0 +1,65 @@ +import { createContext } from "react"; +import wmocodes from "./wmocodes.json"; + +interface Weather { + temperature: number; + description: string; + image: string; +} + +interface WhoisUpdate { + name: string; + time: number; +} + +export type Data = { + weather?: Weather; + whois?: WhoisUpdate[]; +}; + +export const DataContext = createContext<Data>({}); + +export const fetchWeather = async (): Promise<Weather> => { + const [latitude, longitude] = await new Promise<[number, number]>((res) => + navigator.geolocation.getCurrentPosition((position) => { + res([position.coords.latitude, position.coords.longitude]); + }) + ); + + const params = new URLSearchParams({ + latitude: latitude.toString(), + longitude: longitude.toString(), + current: ["temperature_2m", "weather_code", "is_day"].join(","), + }).toString(); + const url = `https://api.open-meteo.com/v1/forecast?${params}`; + + return fetch(url) + .then((r) => r.json()) + .then(({ current: { temperature_2m: temp, is_day, weather_code } }) => ({ + temp, + wmo: wmocodes[weather_code as keyof typeof wmocodes][ + is_day ? "day" : "night" + ], + })) + .then(({ wmo, temp }) => ({ + temperature: temp as number, + description: wmo.description, + image: wmo.image, + })); +}; + +export const fetchWhois = async (): Promise<WhoisUpdate[]> => + fetch("https://whois.simponic.xyz/updates") + .then((response) => response.json()) + .then( + (data) => + data + .map((data: WhoisUpdate) => ({ + ...data, + time: new Date(data.time).getTime(), + })) + .sort((a: WhoisUpdate, b: WhoisUpdate) => a.time - b.time) // time should go positive from left to right + .filter((x: WhoisUpdate, i: number, arr: WhoisUpdate[]) => + i > 0 ? x.name !== arr[i - 1].name : true + ) // dedupe updates with the same name + ); diff --git a/lib/utils.ts b/lib/utils.ts new file mode 100644 index 0000000..e70557a --- /dev/null +++ b/lib/utils.ts @@ -0,0 +1,21 @@ +import { clsx, type ClassValue } from "clsx"; +import { twMerge } from "tailwind-merge"; + +export function cn(...inputs: ClassValue[]) { + return twMerge(clsx(inputs)); +} + +export function greet(name: string, time: Date) { + const messages: [number, number, string][] = [ + [0, 4, "🌕 Good night"], + [5, 11, "🌤️ Good morning"], //Store messages in an array + [12, 17, "🌷͙ Good afternoon"], + [18, 23, "🌕 Good night"], + ]; + + const message = messages.find( + ([start, end]) => time.getHours() >= start && time.getHours() <= end + ); + + return (message ? message[2] : "Hello") + ", " + name + "."; +} diff --git a/lib/wmocodes.json b/lib/wmocodes.json new file mode 100644 index 0000000..545589b --- /dev/null +++ b/lib/wmocodes.json @@ -0,0 +1,282 @@ +{ + "0": { + "day": { + "description": "Sunny", + "image": "http://openweathermap.org/img/wn/01d@2x.png" + }, + "night": { + "description": "Clear", + "image": "http://openweathermap.org/img/wn/01n@2x.png" + } + }, + "1": { + "day": { + "description": "Mainly Sunny", + "image": "http://openweathermap.org/img/wn/01d@2x.png" + }, + "night": { + "description": "Mainly Clear", + "image": "http://openweathermap.org/img/wn/01n@2x.png" + } + }, + "2": { + "day": { + "description": "Partly Cloudy", + "image": "http://openweathermap.org/img/wn/02d@2x.png" + }, + "night": { + "description": "Partly Cloudy", + "image": "http://openweathermap.org/img/wn/02n@2x.png" + } + }, + "3": { + "day": { + "description": "Cloudy", + "image": "http://openweathermap.org/img/wn/03d@2x.png" + }, + "night": { + "description": "Cloudy", + "image": "http://openweathermap.org/img/wn/03n@2x.png" + } + }, + "45": { + "day": { + "description": "Foggy", + "image": "http://openweathermap.org/img/wn/50d@2x.png" + }, + "night": { + "description": "Foggy", + "image": "http://openweathermap.org/img/wn/50n@2x.png" + } + }, + "48": { + "day": { + "description": "Rime Fog", + "image": "http://openweathermap.org/img/wn/50d@2x.png" + }, + "night": { + "description": "Rime Fog", + "image": "http://openweathermap.org/img/wn/50n@2x.png" + } + }, + "51": { + "day": { + "description": "Light Drizzle", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Light Drizzle", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "53": { + "day": { + "description": "Drizzle", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Drizzle", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "55": { + "day": { + "description": "Heavy Drizzle", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Heavy Drizzle", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "56": { + "day": { + "description": "Light Freezing Drizzle", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Light Freezing Drizzle", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "57": { + "day": { + "description": "Freezing Drizzle", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Freezing Drizzle", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "61": { + "day": { + "description": "Light Rain", + "image": "http://openweathermap.org/img/wn/10d@2x.png" + }, + "night": { + "description": "Light Rain", + "image": "http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "63": { + "day": { + "description": "Rain", + "image": "http://openweathermap.org/img/wn/10d@2x.png" + }, + "night": { + "description": "Rain", + "image": "http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "65": { + "day": { + "description": "Heavy Rain", + "image": "http://openweathermap.org/img/wn/10d@2x.png" + }, + "night": { + "description": "Heavy Rain", + "image": "http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "66": { + "day": { + "description": "Light Freezing Rain", + "image": "http://openweathermap.org/img/wn/10d@2x.png" + }, + "night": { + "description": "Light Freezing Rain", + "image": "http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "67": { + "day": { + "description": "Freezing Rain", + "image": "http://openweathermap.org/img/wn/10d@2x.png" + }, + "night": { + "description": "Freezing Rain", + "image": "http://openweathermap.org/img/wn/10n@2x.png" + } + }, + "71": { + "day": { + "description": "Light Snow", + "image": "http://openweathermap.org/img/wn/13d@2x.png" + }, + "night": { + "description": "Light Snow", + "image": "http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "73": { + "day": { + "description": "Snow", + "image": "http://openweathermap.org/img/wn/13d@2x.png" + }, + "night": { + "description": "Snow", + "image": "http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "75": { + "day": { + "description": "Heavy Snow", + "image": "http://openweathermap.org/img/wn/13d@2x.png" + }, + "night": { + "description": "Heavy Snow", + "image": "http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "77": { + "day": { + "description": "Snow Grains", + "image": "http://openweathermap.org/img/wn/13d@2x.png" + }, + "night": { + "description": "Snow Grains", + "image": "http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "80": { + "day": { + "description": "Light Showers", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Light Showers", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "81": { + "day": { + "description": "Showers", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Showers", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "82": { + "day": { + "description": "Heavy Showers", + "image": "http://openweathermap.org/img/wn/09d@2x.png" + }, + "night": { + "description": "Heavy Showers", + "image": "http://openweathermap.org/img/wn/09n@2x.png" + } + }, + "85": { + "day": { + "description": "Light Snow Showers", + "image": "http://openweathermap.org/img/wn/13d@2x.png" + }, + "night": { + "description": "Light Snow Showers", + "image": "http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "86": { + "day": { + "description": "Snow Showers", + "image": "http://openweathermap.org/img/wn/13d@2x.png" + }, + "night": { + "description": "Snow Showers", + "image": "http://openweathermap.org/img/wn/13n@2x.png" + } + }, + "95": { + "day": { + "description": "Thunderstorm", + "image": "http://openweathermap.org/img/wn/11d@2x.png" + }, + "night": { + "description": "Thunderstorm", + "image": "http://openweathermap.org/img/wn/11n@2x.png" + } + }, + "96": { + "day": { + "description": "Light Thunderstorms With Hail", + "image": "http://openweathermap.org/img/wn/11d@2x.png" + }, + "night": { + "description": "Light Thunderstorms With Hail", + "image": "http://openweathermap.org/img/wn/11n@2x.png" + } + }, + "99": { + "day": { + "description": "Thunderstorm With Hail", + "image": "http://openweathermap.org/img/wn/11d@2x.png" + }, + "night": { + "description": "Thunderstorm With Hail", + "image": "http://openweathermap.org/img/wn/11n@2x.png" + } + } +} |