Files
eveship.fit/components/LocationHash/LocationHash.tsx
Patric Stout f9d801a1dc feat: ability to load characters and their skills from ESI (#20)
This can track multiple characters, and tries to make as few as possible
ESI calls.

In the localstorage, we keep track of all the characters and their
refresh-tokens.
Access-tokens and skills are not stored in the localstorage, which means
that every reload, this information is fetched from ESI again.
2023-11-26 15:55:22 +00:00

33 lines
891 B
TypeScript

import { type EsiFit, eveShipFitHash } from "@eveshipfit/react";
import React from "react";
async function analyzeHash(setFit: (fit: EsiFit) => void) {
const hash = window.location.hash;
window.history.replaceState(null, "", window.location.pathname + window.location.search);
if (hash.startsWith("#fit:")) {
const fitHash = hash.slice(1);
const esiFit = await eveShipFitHash(fitHash);
if (esiFit) {
setFit(esiFit);
}
}
}
export const LocationHash = ({ setFit }: { setFit: (fit: EsiFit) => void }) => {
React.useEffect(() => {
analyzeHash(setFit);
// We only want to analyze the hash on page-enter; never again after.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
if (typeof window !== "undefined") {
window.addEventListener("hashchange", async () => {
await analyzeHash(setFit);
});
}
return null;
}