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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14x 9x 14x 14x 14x 14x 1x 1x 48x 26x 48x 48x 48x 48x 1x 1x 3x 3x 3x 3x 1x 318x 318x 318x 23x 318x 318x 318x 318x 1x 1x 318x 1x 173x 173x 173x 173x 1x 9x 9x 9x 9x 1x 317x 21x 21x 21x 21x 1x 1x 1x 1x 1x 1x 600x 600x 600x 600x 600x 600x 600x 600x 600x 600x 600x 82x 82x 82x 82x 82x 600x 600x 600x 600x 600x 1x 1x 1x 1x 5x 5x 5x 5x 5x 1x 5x 5x 1x 1x 11x 11x 1x 1x 1x 1x 91x 82x 82x 91x 91x 91x 91x 91x 91x 91x 91x 91x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 618x 618x 618x 618x 618x 318x 318x 318x 318x 318x 318x 318x 4x 318x 1x 1x 1x 3x 3x 618x 618x 163x 163x 163x 163x 163x 618x 618x 8x 8x 8x 8x 8x 618x 618x 618x 252x 252x 252x 250x 250x 250x 252x 1x 1x 1x 1x 252x 252x 252x 618x 618x 7x 7x 7x 7x 618x 618x 618x | /**
* Remembered filter state for the Table and Matches tabs (SB-1112).
*
* Both tabs are `v-if` in App.vue, so they unmount on leave and re-mount on
* their defaults — set U15 / Northeast / Flex, glance at the other tab, and
* you are back on U14. This stores what the viewer last had and hands it back
* on the next mount.
*
* Two rules shape the whole thing:
*
* 1. **Stored ids are claims about data that may have moved.** A saved
* division from a season the viewer no longer has, restored blindly,
* strands them on an empty list with nothing on screen explaining why —
* worse than the reset this replaces. Nothing is applied without being
* checked against the lists the component actually fetched, which is why
* this module reads but never restores on its own.
* 2. **localStorage is allowed to fail.** Safari's private mode throws on
* write, and a remembered filter is never worth a blank tab. Every access
* is wrapped; a failure degrades to "no memory", not to an error.
*
* Signed in, the same memory is also kept on the account (SB-1715) so it
* follows the viewer to another device. The browser copy is still written
* first and still read first; the server copy wins only when it was saved
* later (`savedAt`, client clock). Server writes are debounced — a burst of
* chip clicks is one request — and never block or fail the tab.
*/
import { getApiBaseUrl } from '../config/api';
import { useAuthStore } from '../stores/auth';
// Bump when the stored shape changes: a stale entry is then dropped whole
// rather than half-applied.
const VERSION = 'v1';
const PREFIX = 'mt.filters';
/** Per viewer — two people on one iPad must not inherit each other's filters. */
const storageKey = (view, userKey) =>
`${PREFIX}.${VERSION}.${view}.${userKey || 'anon'}`;
// Panel open/closed (SB-1714). Per viewer since SB-1715; the per-browser key
// it used before is still read once, so nobody's choice resets on upgrade.
const PANEL_PREFIX = 'mt.filterPanel.v1';
const panelKey = (view, userKey) =>
`${PANEL_PREFIX}.${view}.${userKey || 'anon'}`;
const legacyPanelKey = view => `${PANEL_PREFIX}.${view}`;
/** How long a burst of filter changes waits before one server write. */
export const SYNC_DEBOUNCE_MS = 1500;
/**
* Keep only the ids that exist in `available`, as numbers.
*
* Used for the division chips, where dropping the unknown ones and keeping
* the rest is right: all of them unknown lands on `[]`, which is the "every
* division" state (SB-1007), not an empty table.
*/
export const keepKnownIds = (saved, available) => {
if (!Array.isArray(saved)) return [];
const known = new Set(
(available || []).map(item => Number(item?.id ?? item))
);
return saved.map(Number).filter(id => known.has(id));
};
/** The saved id when the list still has it, otherwise null. */
export const knownId = (saved, available) => {
if (saved === null || saved === undefined || saved === '') return null;
const known = new Set(
(available || []).map(item => Number(item?.id ?? item))
);
return known.has(Number(saved)) ? Number(saved) : null;
};
/** The saved name when the list still has it, otherwise null. Case-exact. */
export const knownName = (saved, available) => {
if (!saved) return null;
const known = new Set((available || []).map(item => item?.name ?? item));
return known.has(saved) ? saved : null;
};
const readJson = key => {
try {
const raw = window.localStorage.getItem(key);
if (!raw) return null;
const parsed = JSON.parse(raw);
return parsed && typeof parsed === 'object' && !Array.isArray(parsed)
? parsed
: null;
} catch {
return null;
}
};
const write = (key, value) => {
try {
window.localStorage.setItem(key, value);
} catch {
// Private mode, a full quota, storage disabled. The tab still works.
}
};
const remove = key => {
try {
window.localStorage.removeItem(key);
} catch {
// Nothing to do, and nothing worth failing a render over.
}
};
const withoutStamp = stored => {
if (!stored) return null;
const filters = { ...stored };
delete filters.savedAt;
return filters;
};
/**
* The account side, over the auth store: the profile `/api/auth/me` loaded
* (`filter_memory`), and a PUT per view. Signed out, there is no account
* side — `entry` is undefined and `put` is never called.
*/
export const accountRemote = () => {
let store = null;
try {
store = useAuthStore();
} catch {
return null;
}
const profile = () => store?.state?.profile ?? null;
return {
active: () => Boolean(store?.isAuthenticated?.value && profile()),
entry: view => profile()?.filter_memory?.[view],
// Keep the loaded profile in step, so a re-mount in this session reads
// what was just saved rather than what /me said at sign-in.
remember: (view, patch) => {
const p = profile();
if (!p) return;
const memory = { ...(p.filter_memory || {}) };
memory[view] = { ...(memory[view] || {}), ...patch };
p.filter_memory = memory;
},
put: (view, body) =>
store.apiRequest(`${getApiBaseUrl()}/api/auth/filter-memory/${view}`, {
method: 'PUT',
body: JSON.stringify(body),
keepalive: true,
}),
};
};
// One pending server write per view and viewer, shared by every caller, so
// Table and its FilterPanel merge into one request instead of racing.
const pending = new Map();
const send = (id, job) => {
pending.delete(id);
clearTimeout(job.timer);
Promise.resolve()
.then(() => job.remote.put(job.view, job.body))
.catch(() => {
// The browser copy already has it; the next change tries again.
});
};
/** Send every queued write now — the tab is going away. */
export const flushFilterMemory = () => {
for (const [id, job] of [...pending]) send(id, job);
};
if (typeof window !== 'undefined') {
window.addEventListener('pagehide', flushFilterMemory);
}
const queue = (remote, view, userKey, patch) => {
if (!remote?.active?.() || !userKey) return;
const id = `${view}.${userKey}`;
const prior = pending.get(id);
clearTimeout(prior?.timer);
const job = {
remote,
view,
body: { ...(prior?.body || {}), ...patch, saved_at: Date.now() },
};
job.timer = setTimeout(() => send(id, job), SYNC_DEBOUNCE_MS);
pending.set(id, job);
};
/**
* Read and write one tab's remembered filters.
*
* `userKey` is read at call time rather than captured, so a sign-in or
* sign-out moves subsequent reads and writes to the other viewer's key
* without the component re-mounting.
*
* `remote` is the account side; tests pass their own, `null` turns it off.
*/
export const useFilterMemory = (view, userKey, { remote } = {}) => {
const account = remote === undefined ? accountRemote() : remote;
const user = () => userKey?.();
const key = () => storageKey(view, user());
/** The newer of this browser's copy and the account's, without its stamp. */
const load = () => {
const local = readJson(key());
const entry = user() ? account?.entry?.(view) : undefined;
const server = entry && 'filters' in entry ? entry.filters : undefined;
if (server === undefined) return withoutStamp(local);
const localAt = Number(local?.savedAt) || 0;
const serverAt = Number(server?.savedAt ?? entry.saved_at) || 0;
if (serverAt <= localAt) return withoutStamp(local);
// The account is newer: adopt it here too, or forget if it was cleared.
if (server === null || typeof server !== 'object') {
remove(key());
return null;
}
write(key(), JSON.stringify(server));
return withoutStamp(server);
};
const save = filters => {
const stamped = { ...filters, savedAt: Date.now() };
write(key(), JSON.stringify(stamped));
if (!user()) return;
account?.remember?.(view, { filters: stamped });
queue(account, view, user(), { filters: stamped });
};
const clear = () => {
remove(key());
if (!user()) return;
const forgotten = { filters: null, saved_at: Date.now() };
account?.remember?.(view, forgotten);
queue(account, view, user(), { filters: null });
};
/** Open until first closed; this browser's choice, then the account's. */
const loadPanel = () => {
try {
const mine = window.localStorage.getItem(panelKey(view, user()));
if (mine === 'open' || mine === 'closed') return mine === 'open';
// Pre-SB-1715 per-browser choice: the first viewer to read it takes
// it, so a second person on this browser starts from the default.
const legacy = window.localStorage.getItem(legacyPanelKey(view));
if (legacy === 'open' || legacy === 'closed') {
window.localStorage.setItem(panelKey(view, user()), legacy);
window.localStorage.removeItem(legacyPanelKey(view));
return legacy === 'open';
}
} catch {
// Unreadable storage: fall through to the account, then the default.
}
const remembered = user() ? account?.entry?.(view)?.panel_open : undefined;
return typeof remembered === 'boolean' ? remembered : true;
};
const savePanel = open => {
write(panelKey(view, user()), open ? 'open' : 'closed');
if (!user()) return;
account?.remember?.(view, { panel_open: open });
queue(account, view, user(), { panel_open: open });
};
return { load, save, clear, loadPanel, savePanel };
};
|