All files / components/JobPanel useFreshness.ts

95.77% Statements 68/71
76.47% Branches 13/17
100% Functions 3/3
95.77% Lines 68/71

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 721x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1086x 1086x 1086x 1086x 1086x 1086x 131x 1086x 1086x 1086x 107x 1x 1x 1x 1x 1x 1x 1x 1x 1x 107x 107x 1086x 1086x 1086x 1086x 1x 1x 1x 914x 914x 914x 79x 6x 6x 6x 6x 6x     6x 6x 6x 6x 6x 6x 6x 6x 6x 6x   6x 6x 6x 6x 914x 914x 914x 914x  
import { useEffect, useState } from "react";
import { rescoreCostLine } from "../../../shared/fitView";
import { fetchAccount } from "../../services/accountService";
import { FINGERPRINT_KEY } from "../../services/profileStorage";
 
/**
 * The current profile fingerprint, kept live: it starts from what the
 * background reported with the result and follows `profile:fingerprint` in
 * session storage, which the options page writes after a profile save
 * (FR-007). `undefined` = not known yet, `null` = the account has no profile.
 */
export function useLiveFingerprint(
  reported: string | null | undefined
): string | null | undefined {
  const [live, setLive] = useState<string | null | undefined>(undefined);
 
  // A new result carries a fresher answer than any earlier live event.
  useEffect(() => {
    setLive(undefined);
  }, [reported]);
 
  useEffect(() => {
    const listener = (
      changes: Record<string, chrome.storage.StorageChange>,
      area: string
    ): void => {
      if (area !== "session") return;
      const change = changes[FINGERPRINT_KEY];
      if (!change) return;
      const next = change.newValue as { value: string | null } | undefined;
      if (next) setLive(next.value);
    };
    chrome.storage.onChanged.addListener(listener);
    return () => chrome.storage.onChanged.removeListener(listener);
  }, []);
 
  return live !== undefined ? live : reported;
}
 
/** FR-004a: the cost line shown beside a Rescore action; `null` until known. */
export function useRescoreCostLine(enabled: boolean): string | null {
  const [line, setLine] = useState<string | null>(null);
 
  useEffect(() => {
    if (!enabled) return;
    let cancelled = false;
    fetchAccount()
      .then(({ usage }) => {
        if (cancelled) return;
        if (usage.rescoresUsed === undefined || usage.rescoresLimit === undefined) {
          return;
        }
        setLine(
          rescoreCostLine({
            rescoresUsed: usage.rescoresUsed,
            rescoresLimit: usage.rescoresLimit,
            count: usage.count,
            limit: usage.limit,
          })
        );
      })
      .catch(() => {
        // The cost line is a courtesy; a failed fetch just omits it.
      });
    return () => {
      cancelled = true;
    };
  }, [enabled]);
 
  return line;
}