Skip to content

About

IP Address Tracker Built Using React

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

24 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 

Repository files navigation

Project Objective

Rebuild IP Address Tracker but with React as a SPA and make it responsive for all screen sizes. Have a default fall back value for ip address or domain if the user enters search button without entering a value. Add validation if user enters an input which does not match the pattern of an ip or domain.

Reliance on Google and AI

Medium - I used AI for generating Readme, understanding architecture, google for figuring out (how to make api key private with setting up Vercel Serverless function) and approach to take for certain > parts of the application as well as syntax help where I struggled

Project Features

  • Search by domain or ip
  • Load map pointing to the ip or domain => used google for looking up how to organize my map rendering code within useEffects
  • Search field validation
  • Accessibility support

Possible Issues from last implementation

  • Search field validation missing
  • Accessibility support missing
  • Bring message bubble to the top (front) instead of hiding it behind the map
Project Run Instructions
  1. cd ip-address-tracker/ip-tracker-react
  2. npm install
  3. npm run dev

Original Architecture Design (Vanilla JS Project)

Step-by-Step Communication Flow
  1. The Trigger: User Input (index.html ──► index.ts)

• What happened: HTML file rendered the search input box and a submit button. • The communication: index.ts targets input elements using document.getElementById("searchIPAddress") and document.getElementById("submitBtn"). When a user clicked the button, index.ts captured that event and grabbed the text from the search field.

  1. The Request: Handing off to the Module (index.ts ──► fetch-ip-details.ts)

• What happened: Once index.ts got the IP address string, it didn't know how to talk to the internet directly. • The communication: index.ts imported fetchIPDetails. index.ts executed this imported function, passing the user's searched IP address along as an argument. • fetchIPDetails function inspects what was typed to figure out whether it's an IP address or a domain name so that it can append the correct query parameters (?domain= or ?ipAddress=).

  1. The Middleman: Hiding the API Key (fetch-ip-details.ts ──► api/ip-details.ts)

• What happened: browser-side code inside fetch-ip-details.ts fired off a network fetch() request. • The communication: Instead of calling the real geolocation service directly (which would expose private API key), it sent a local request to Vercel serverless environment at /api/ip-details?ipAddress=....

  1. The Backend Fetch: Contacting the Real Server (api/ip-details.ts ──► Third-Party API) (Contacting Ipify)

• What happened: Vercel spun up serverless node function securely in the cloud. • The communication: api/ip-details.ts took the IP address from the incoming request, attached secret .env key, and performed the final backend-to-backend fetch request to the external geolocation provider.

  1. The Response: Updating the Visuals (Data travels all the way back to index.ts)

• What happened: The third-party API responded to Vercel function, Vercel function responded to fetch-ip-details.ts, and fetch-ip-details.ts returned that data object back to index.ts. • The communication: Once index.ts received the final IPObject data, it updated the text layout dynamically and used Leaflet to re-center the map markers (Map, TileLayer, Marker) directly onto the page.

  1. Fallback Architecture: If a user hits submit with a completely empty input bar, code gracefully recovers by routing a search request for "microsoft.com" rather than breaking or crashing the network query.

JS Vanilla (original) Application Architecture Visualization

Application Architecture

flowchart TD
    A["User types IP & clicks button"]
    B["1. UI / DOM Event<br/>index.html ↔ index.ts"]
    C["2. Data Fetching Trigger<br/>index.ts calls fetchIPDetails()"]
    D["3. Client Request<br/>src/modules/fetch-ip-details.ts<br/>Calls /api/ip-details"]
    E["4. Secure Serverless Proxy<br/>api/ip-details.ts<br/>Attaches secret .env key"]
    F["5. Third-Party Geolocation API<br/>ipify / ipapi"]

    A --> B
    B --> C
    C --> D
    D --> E
    E --> F
Loading

How The Previous Architecture Translates In React Application

Project Planning

  • index.ts (controller) will be replaced by components and state

    • broken up into ui components
  • Variables become state (single source of truth)

    • update variables as API returns a new IP object and react re-renders the ui when state changes
  • Laflet shifts from Direct Control to Lifecycle Events

    • Because React controls the DOM canvas, I don't touch the map directly. I use a lifecycle hook (like useEffect) talks to Leaflet: and tells it to redraw the map when state changes
    • Alternatively, I can use a library like react-leaflet to turn the map tiles and markers into standard React components (e.g., ). But I am not sure at the moment if I will.
  • Original Pages To React Component Planning [x] - index.html => breaks into => SearchBar, StatsDisplay, MapContainer, Footer, HeaderComponent

  • Component Props or/and State Planning [x] - index.ts => gives me the following stateVariables => fetchedDetails = null, ipDetailsField, ipLocationField, ipTimeZoneField, ipISPField, mapAreaDiv (maybe) [x] - index.ts => gives me the following variables which will be calculated at rendering => latitude = 0.0, longitude = 0.0, countryVar, regionVar, cityVar, zipVar, ipVar [might change them to stateVariables as needed] [x] - fetch-ip-details.ts => it stays a async/await utility function and gets transferred to the file src/services/ipService.ts. It remains pretty much the same. It gets called inside the useEffect hook or by a handler function when the app loads or user submits a new search. This utility function is called in the SearchBar component which returns the raw ip data object which I manage using a stateVariable in that component. [x] - ip-details.ts => Originally being the vercel serverless function (it will remain the same and run on Vercel's backend server) keeping the api_key hidden. I will just modify how the react frontend calls it namely the SearchBar component. The form submission in the searchbar component will make an HTTP request directly to the vercel route. During local development, Vite is configured via vite.config.ts to proxy (substitute) /api requests to the local Vercel development server, preventing cross-origin (CORS) and 404 errors.

Steps To Make Vercel Serverless function work with Vite (Production Deployment)
  1. Made ip-details.ts file in the api/ directory in the root of the project.
  2. Created a .env file at the root with the API key. It does not contain the VITE_ prefix to keep it strictly hidden on the backend.
  3. Added the IPIFY_API_KEY directly to the Vercel Dashboard Environment Variables so the live serverless function can access it securely in production.
  4. Updated the SearchBar component's form submission handler to fetch from the relative URL /api/ip-details?search=... so it dynamically targets the deployed Vercel domain.
  • Actions / Functions Planning [x] - index.ts => gives me the following actions/functions => renderIpDetails(ipDetails: IPObject), Map object and its respective functions, Marker and TileLayer class objects from leaflet

  • Context Planning [x] - DataFetchingContext - handles both maintaining the searchcriteria and ipFetchresponse across all the components

  • Type Definitions and Interface Planning [x] - LocationData, IPObject

  • Routes Planning [x] - This app has only 1 page, so i didnt use routing

React Application Architecture Visualization

Application Architecture

flowchart TD
    A["User enters IP address"]
    B["React Component<br/>User interaction"]
    C["React State<br/>Single source of truth"]
    D["API Service<br/>fetchIPDetails()"]
    E["Serverless Proxy<br/>/api/ip-details"]
    F["External Geolocation API"]
    G["API Response<br/>Update React State"]
    H["React Re-render<br/>Updated UI"]
    I["useEffect<br/>Synchronize Leaflet Map"]

    A --> B
    B --> D
    D --> E
    E --> F
    F --> G
    G --> C
    C --> H
    C --> I
Loading

About

IP Address Tracker Built Using React

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages