mirror of
https://github.com/jason5ng32/MyIP.git
synced 2026-11-04 08:55:47 +00:00
common/ had become the backend's library: 30 of its 41 modules were imported by nothing in the frontend, so the name no longer said what belonged there. It now holds only what the frontend or the build imports too; backend-only code moved to server/, flat except for datasets/ (the dataset engine and its readers). The *-db/ dataset directories stay in common/: self-hosting docs and Docker volumes mount those paths. frontend/utils/ keeps its app-wide modules at the root and gains ip/ (IP / ASN shaping and validation), report/ (the report pipeline) and features/ (the pure layer of a single feature). Paths only — no logic changes. The Dockerfile copies server/, and the AGENTS.md files say where a new module goes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
85 lines
3.1 KiB
JavaScript
85 lines
3.1 KiB
JavaScript
// use-ip-history.js — records every IP the app detects (store.allIPs) into a
|
||
// localStorage history grouped by day, and exposes it reactively for the
|
||
// IPHistory panel. Local-only by design: the record never syncs to the user's
|
||
// account. Gated by user preferences: ipHistoryEnabled stops recording,
|
||
// ipHistoryDays (1–30) sets retention. Pure logic lives in utils/ip/ip-history.js.
|
||
import { ref, computed, watch } from 'vue';
|
||
import { emitAppEvent } from '@/utils/app-events.js';
|
||
import {
|
||
IP_HISTORY_STORAGE_KEY,
|
||
parseHistory,
|
||
mergeIntoHistory,
|
||
pruneHistory,
|
||
createEmptyHistory,
|
||
sortedHistoryDays,
|
||
localDayKey,
|
||
clampRetentionDays,
|
||
distinctCountryCount,
|
||
} from '@/utils/ip/ip-history.js';
|
||
|
||
export const useIpHistory = ({ store }) => {
|
||
const enabled = computed(() => store.userPreferences.ipHistoryEnabled !== false);
|
||
const retentionDays = computed(() => clampRetentionDays(store.userPreferences.ipHistoryDays));
|
||
|
||
const persist = (history) => {
|
||
try {
|
||
localStorage.setItem(IP_HISTORY_STORAGE_KEY, JSON.stringify(history));
|
||
} catch {
|
||
// Quota exceeded / privacy mode — the history is best-effort.
|
||
}
|
||
};
|
||
|
||
const loadInitial = () => {
|
||
const parsed = parseHistory(localStorage.getItem(IP_HISTORY_STORAGE_KEY));
|
||
const { history, changed } = pruneHistory(parsed, localDayKey(), retentionDays.value);
|
||
if (changed) persist(history);
|
||
return history;
|
||
};
|
||
|
||
const history = ref(loadInitial());
|
||
|
||
// Domain event for the achievement engine: distinct-country snapshot of
|
||
// the whole history. Emitted once at mount (records accumulated in past
|
||
// sessions count too) and again whenever a merge lands new entries.
|
||
const emitSnapshot = (h) => {
|
||
emitAppEvent('iphistory:updated', { countryCount: distinctCountryCount(h) });
|
||
};
|
||
emitSnapshot(history.value);
|
||
|
||
// Fold newly detected IPs into today's bucket. `immediate` covers IPs that
|
||
// landed in the store before this composable mounted.
|
||
watch(() => store.allIPs, (ips) => {
|
||
if (!enabled.value) return;
|
||
const { history: next, changed } = mergeIntoHistory(history.value, ips, localDayKey());
|
||
if (changed) {
|
||
history.value = next;
|
||
persist(next);
|
||
emitSnapshot(next);
|
||
}
|
||
}, { immediate: true });
|
||
|
||
// Re-prune when the user shrinks the retention window. Preferences commits
|
||
// the slider value on release, so this never fires mid-drag.
|
||
watch(retentionDays, (days) => {
|
||
const { history: next, changed } = pruneHistory(history.value, localDayKey(), days);
|
||
if (changed) {
|
||
history.value = next;
|
||
persist(next);
|
||
}
|
||
});
|
||
|
||
const sortedDays = computed(() => sortedHistoryDays(history.value));
|
||
const hasHistory = computed(() => sortedDays.value.length > 0);
|
||
|
||
const clearHistory = () => {
|
||
history.value = createEmptyHistory();
|
||
try {
|
||
localStorage.removeItem(IP_HISTORY_STORAGE_KEY);
|
||
} catch {
|
||
// Same best-effort stance as persist().
|
||
}
|
||
};
|
||
|
||
return { enabled, sortedDays, hasHistory, clearHistory };
|
||
};
|