Convert src/ JavaScript modules to TypeScript
Renames every .js module under src/ to .ts (api, useResource, adminSchema, navConfig, adminNav and src/data/*) and points imports, comments and the seed script's module paths at the new names. Their types now come from inference; no .d.ts files and no shape interfaces. tsc stays strict (noImplicitAny off). The errors inference leaves behind get the lightest fix that clears them: `any` on empty state, contexts and list defaults, `: any` on components with optional or spread props, class fields on ApiError, and option shapes on get/useResource. Kept as real types, since they belong to modules that were already TypeScript and later features import them: PageShell's ShellSection and props, useContent's corrected record types (website/email/ instagram are bare strings) and EventListItem, and TimelineRef's orgKind. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
5cedc68fd7
commit
22d5328885
43 changed files with 195 additions and 135 deletions
|
|
@ -1128,7 +1128,7 @@ const frontPage = {
|
|||
],
|
||||
};
|
||||
|
||||
export const ADMIN_ENTITIES = {
|
||||
export const ADMIN_ENTITIES: Record<string, any> = {
|
||||
organizations,
|
||||
events,
|
||||
people,
|
||||
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
import { createContext, useContext, useEffect } from "react";
|
||||
|
||||
export const AdminTitleContext = createContext(null);
|
||||
export const AdminTitleContext = createContext<any>(null);
|
||||
|
||||
/* Publish the name of whatever this page is showing. Clears on
|
||||
unmount, so navigating away can't leave a stale record name in
|
||||
|
|
|
|||
|
|
@ -25,7 +25,10 @@ const DEFAULT_TTL = 60_000;
|
|||
const cache = new Map(); // path → { at, promise }
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(message, { status, fields } = {}) {
|
||||
status;
|
||||
fields;
|
||||
|
||||
constructor(message, { status, fields }: { status?: number; fields?: any } = {}) {
|
||||
super(message);
|
||||
this.name = "ApiError";
|
||||
this.status = status;
|
||||
|
|
@ -33,7 +36,7 @@ export class ApiError extends Error {
|
|||
}
|
||||
}
|
||||
|
||||
async function request(path, options = {}) {
|
||||
async function request(path, options: RequestInit = {}) {
|
||||
const response = await fetch(`${BASE}${path}`, {
|
||||
headers: { Accept: "application/json", ...options.headers },
|
||||
...options,
|
||||
|
|
@ -68,7 +71,7 @@ async function request(path, options = {}) {
|
|||
get("/events", { fallback }) → fallback on any failure
|
||||
───────────────────────────────────────────────────────────── */
|
||||
|
||||
export function get(path, { ttl = DEFAULT_TTL, fallback } = {}) {
|
||||
export function get(path, { ttl = DEFAULT_TTL, fallback }: { ttl?: number; fallback?: any } = {}) {
|
||||
const hit = cache.get(path);
|
||||
|
||||
if (hit && Date.now() - hit.at < ttl) return hit.promise;
|
||||
|
|
@ -16,9 +16,9 @@
|
|||
import { createContext, useCallback, useContext, useEffect, useState } from "react";
|
||||
import { Navigate, Outlet, useLocation } from "react-router-dom";
|
||||
|
||||
import { get, post, ApiError } from "./api.js";
|
||||
import { get, post, ApiError } from "./api.ts";
|
||||
|
||||
const AuthContext = createContext(null);
|
||||
const AuthContext = createContext<any>(null);
|
||||
|
||||
export function AuthProvider({ children }) {
|
||||
const [user, setUser] = useState(null);
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
table, so there is one answer to "where does an event live" and
|
||||
changing it is changing EVENT_BASE below.
|
||||
|
||||
navConfig.js stays the source of truth for the *nav*: these are
|
||||
navConfig.ts stays the source of truth for the *nav*: these are
|
||||
record routes, which never appear in it.
|
||||
|
||||
── On missing ids ──
|
||||
|
|
|
|||
|
|
@ -23,6 +23,8 @@
|
|||
* and the public/ layout stay the frontend's business.
|
||||
*/
|
||||
|
||||
import type { OrgKind } from './hrefs.ts'
|
||||
|
||||
export type DatePrecision = 'year' | 'month' | 'day'
|
||||
|
||||
/** What an entry is about. Drives the marker and the body layout. */
|
||||
|
|
@ -41,6 +43,8 @@ export type TimelineRef = {
|
|||
kind: RefKind
|
||||
/** The row's TEXT primary key — an event id, org slug, team slug. */
|
||||
id: string
|
||||
/** Only on organizations: history.js copies v_timeline.org_kind. */
|
||||
orgKind?: OrgKind
|
||||
}
|
||||
|
||||
/** Filename plus the table it came from; the directory is derived
|
||||
|
|
|
|||
|
|
@ -106,12 +106,22 @@ export type EventRecord = {
|
|||
hosts: EventHost[]
|
||||
description: string[]
|
||||
links: Link[]
|
||||
instagram?: Link | null
|
||||
/** The instagram link's label, the handle. See splitLinks in shape.js. */
|
||||
instagram?: string | null
|
||||
blocks: ContentBlock[]
|
||||
people: EventPerson[]
|
||||
awards: EventAward[]
|
||||
}
|
||||
|
||||
/** One row of GET /events: shapeEvent without the detail-only
|
||||
* blocks, people and awards. */
|
||||
export type EventListItem = Omit<EventRecord, 'blocks' | 'people' | 'awards'>
|
||||
|
||||
/** An event_sections row, as GET /events sends it beside the list. */
|
||||
export type EventSection = { id: string; name: string; sort_order: number }
|
||||
|
||||
export type EventsResponse = { sections: EventSection[]; events: EventListItem[] }
|
||||
|
||||
export const useEvent = (id?: string): Resource<EventRecord> =>
|
||||
useRecord<EventRecord>(detailPath('/events', id), 'event')
|
||||
|
||||
|
|
@ -157,6 +167,17 @@ export type OrgEvent = {
|
|||
color?: string | null
|
||||
}
|
||||
|
||||
/** regions.scope's CHECK values. */
|
||||
export type RegionScope = 'domestic' | 'international' | 'virtual'
|
||||
|
||||
/** A region_areas row as attachRegionDetails sends it. */
|
||||
export type RegionArea = {
|
||||
area_code: string
|
||||
share: number
|
||||
edge: 'top' | 'bottom' | null
|
||||
note: string | null
|
||||
}
|
||||
|
||||
export type OrganizationRecord = {
|
||||
id: string
|
||||
kind: 'national' | 'region' | 'chapter' | 'partner'
|
||||
|
|
@ -176,14 +197,16 @@ export type OrganizationRecord = {
|
|||
blocks: ContentBlock[]
|
||||
links: Link[]
|
||||
socials: Link[]
|
||||
website?: Link | null
|
||||
email?: Link | null
|
||||
instagram?: Link | null
|
||||
/* splitLinks in shape.js lifts these out as bare strings: the
|
||||
website's url, the email's label, the instagram handle. */
|
||||
website?: string | null
|
||||
email?: string | null
|
||||
instagram?: string | null
|
||||
/** Shape depends on `kind`; empty object for national and partner. */
|
||||
details: {
|
||||
scope?: string | null
|
||||
scope?: RegionScope | null
|
||||
map_note?: string | null
|
||||
areas?: Array<{ area_code: string; share?: number | null; edge?: string | null; note?: string | null }>
|
||||
areas?: RegionArea[]
|
||||
chapters?: Array<{ id: string; name: string; location_label?: string | null; logo?: string | null }>
|
||||
region_id?: string | null
|
||||
region_name?: string | null
|
||||
|
|
@ -197,6 +220,14 @@ export type OrganizationRecord = {
|
|||
events: OrgEvent[]
|
||||
}
|
||||
|
||||
/** One row of GET /organizations: the card surface and details,
|
||||
* without the sections only the org's own page loads. */
|
||||
export type OrganizationListItem = Omit<OrganizationRecord, 'teams' | 'awards' | 'events'> & {
|
||||
sort_order: number
|
||||
}
|
||||
|
||||
export type OrganizationsResponse = { organizations: OrganizationListItem[] }
|
||||
|
||||
export const useOrganization = (id?: string): Resource<OrganizationRecord> =>
|
||||
useRecord<OrganizationRecord>(detailPath('/organizations', id), 'organization')
|
||||
|
||||
|
|
@ -214,7 +245,8 @@ export type TeamRecord = {
|
|||
description: string[]
|
||||
links: Link[]
|
||||
socials: Link[]
|
||||
instagram?: Link | null
|
||||
/** The instagram handle. See splitLinks in shape.js. */
|
||||
instagram?: string | null
|
||||
blocks: ContentBlock[]
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@
|
|||
*/
|
||||
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { get, invalidate, ApiError } from './api.js'
|
||||
import { get, invalidate, ApiError } from './api.ts'
|
||||
import type { TimelineItem } from './timeline'
|
||||
|
||||
const PATH = '/history'
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
* pattern.
|
||||
*
|
||||
* Named for what it returns, and deliberately not useResource:
|
||||
* src/lib/useResource.js is a different hook — it hands back the
|
||||
* src/lib/useResource.ts is a different hook — it hands back the
|
||||
* whole response body and takes an options object — and a .ts file
|
||||
* of the same name would sit one extension away from it. An import
|
||||
* whose target goes missing then resolves to the other file without
|
||||
|
|
@ -26,7 +26,7 @@
|
|||
* · `reload` invalidates before it refetches. Without that, the
|
||||
* retry button inside the 60s TTL hands back the same settled
|
||||
* promise and looks like it did nothing. A *failed* request is
|
||||
* already evicted by api.js, so this matters for the refresh
|
||||
* already evicted by api.ts, so this matters for the refresh
|
||||
* case rather than the error case.
|
||||
*
|
||||
* · no AbortController. `get` shares one promise between callers,
|
||||
|
|
@ -47,7 +47,7 @@
|
|||
*/
|
||||
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import { get, invalidate, ApiError } from './api.js'
|
||||
import { get, invalidate, ApiError } from './api.ts'
|
||||
|
||||
export type Resource<T> = {
|
||||
data: T | null
|
||||
|
|
|
|||
|
|
@ -18,13 +18,13 @@
|
|||
═══════════════════════════════════════════════════════════════ */
|
||||
|
||||
import { useEffect, useState } from "react";
|
||||
import { get } from "./api.js";
|
||||
import { get } from "./api.ts";
|
||||
|
||||
export function useResource(path, { ttl, fallback } = {}) {
|
||||
export function useResource(path, { ttl, fallback }: { ttl?: number; fallback?: any } = {}) {
|
||||
// Seed with the fallback so the first paint has content when one
|
||||
// is available, rather than flashing a spinner and then the same
|
||||
// data a moment later.
|
||||
const [state, setState] = useState(() => ({
|
||||
const [state, setState] = useState<any>(() => ({
|
||||
data: fallback,
|
||||
error: null,
|
||||
loading: true,
|
||||
Loading…
Add table
Add a link
Reference in a new issue