Skip to content

Commit

Permalink
fix: offline
Browse files Browse the repository at this point in the history
Now, rather than crashing, there's just no default.
  • Loading branch information
lishaduck committed Mar 6, 2024
1 parent 98e82d2 commit cb7ba54
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/islands/StateSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { IconCheck, IconChevronDown } from "../utils/icons.ts";
import { tw } from "../utils/tailwind.ts";

export interface StateSelectorProps {
currentState: State;
currentState?: State | undefined;
}

export function StateSelector({
Expand Down
8 changes: 4 additions & 4 deletions src/routes/calculator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { Meta } from "../components/Meta.tsx";
import { StateSelector } from "../islands/StateSelector.tsx";
import { type State, type StateData, stateData } from "../utils/calc.ts";
import type { FreshContextHelper } from "../utils/handlers.ts";
import { getIPLocation } from "../utils/ip.ts";
import { getIpLocation } from "../utils/ip.ts";
import { isKey } from "../utils/type-helpers.ts";

export type CalculatorProps = CalculatorSearchProps | CalculatorShowProps;

export interface CalculatorSearchProps {
state: "search";
region: State;
region: State | undefined;
}

export interface CalculatorShowProps {
Expand All @@ -31,14 +31,14 @@ export const handler: Handlers = {
req: Request,
ctx: FreshContextHelper<CalculatorProps>,
): Promise<Response> {
const visitor = await getIPLocation(ctx.remoteAddr.hostname);
const visitor = await getIpLocation(ctx.remoteAddr.hostname);

const url = new URL(req.url);
const state = url.searchParams.get("region");
if (state === null) {
return ctx.render({
state: "search",
region: visitor.region,
region: visitor?.region,
});
}

Expand Down
24 changes: 14 additions & 10 deletions src/utils/ip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ let serverIp: string | undefined;
/**
* Get details about an IP from the <https://ipapi.co> API.
*/
export async function getIPLocation(ip?: string): Promise<Geo> {
let currentIP: string | undefined;
if (["127.0.0.1", undefined].includes(ip)) {
if (serverIp === undefined) {
serverIp = (await getIp()).ip;
export async function getIpLocation(ip?: string): Promise<Geo | undefined> {
try {
let currentIP: string | undefined;
if (["127.0.0.1", undefined].includes(ip)) {
if (serverIp === undefined) {
serverIp = (await getIp()).ip;
}
currentIP = serverIp;
} else {
currentIP = ip;
}
currentIP = serverIp;
} else {
currentIP = ip;
}

return await makeRequest(`${geoEndpoint}/${currentIP}/json/`, geoSchema);
return makeRequest(`${geoEndpoint}/${currentIP}/json/`, geoSchema);
} catch {
return undefined;
}
}

/**
Expand Down

0 comments on commit cb7ba54

Please sign in to comment.