authentication fix
This commit is contained in:
+30
-22
@@ -3,7 +3,10 @@ import { createSignal, createEffect, createRoot } from "solid-js";
|
||||
import { pb, TASGRID_COLLECTION } from "@/lib/pocketbase";
|
||||
import { toast } from "solid-sonner";
|
||||
|
||||
const STORAGE_KEY = "tasgrid_data_v1";
|
||||
const getStorageKey = () => {
|
||||
const userId = pb.authStore.model?.id;
|
||||
return userId ? `tasgrid_data_${userId}` : null;
|
||||
};
|
||||
|
||||
export const [now, setNow] = createSignal(Date.now());
|
||||
export const [activeTaskId, setActiveTaskId] = createSignal<string | null>(null);
|
||||
@@ -30,30 +33,21 @@ interface TaskStore {
|
||||
prefId?: string; // ID of the user_preferences record
|
||||
}
|
||||
|
||||
// Synchronous hydration from localStorage for "instant" first paint
|
||||
const getInitialState = (): TaskStore => {
|
||||
const saved = localStorage.getItem(STORAGE_KEY);
|
||||
if (saved) {
|
||||
try {
|
||||
return JSON.parse(saved);
|
||||
} catch (e) {
|
||||
console.error("Failed to parse cached data", e);
|
||||
}
|
||||
}
|
||||
return {
|
||||
tasks: [],
|
||||
pWeight: 1.0,
|
||||
uWeight: 1.0,
|
||||
matrixScaleDays: 30,
|
||||
};
|
||||
};
|
||||
|
||||
export const [store, setStore] = createStore<TaskStore>(getInitialState());
|
||||
// Initial empty state
|
||||
export const [store, setStore] = createStore<TaskStore>({
|
||||
tasks: [],
|
||||
pWeight: 1.0,
|
||||
uWeight: 1.0,
|
||||
matrixScaleDays: 30,
|
||||
});
|
||||
|
||||
// Auto-persist changes to localStorage (wrapped in root to avoid console warning)
|
||||
createRoot(() => {
|
||||
createEffect(() => {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(store));
|
||||
const key = getStorageKey();
|
||||
if (key) {
|
||||
localStorage.setItem(key, JSON.stringify(store));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -100,6 +94,19 @@ let heartbeatInterval: number | undefined;
|
||||
export const initStore = async () => {
|
||||
if (!pb.authStore.isValid) return;
|
||||
|
||||
// 0. Synchronous hydration from localStorage for "instant" first paint
|
||||
const key = getStorageKey();
|
||||
if (key) {
|
||||
const saved = localStorage.getItem(key);
|
||||
if (saved) {
|
||||
try {
|
||||
setStore(JSON.parse(saved));
|
||||
} catch (e) {
|
||||
console.error("Failed to parse cached data", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Heartbeat - Ensure only one interval is running
|
||||
if (heartbeatInterval) clearInterval(heartbeatInterval);
|
||||
heartbeatInterval = setInterval(() => setNow(Date.now()), 60000);
|
||||
@@ -127,8 +134,9 @@ export const initStore = async () => {
|
||||
console.warn("Failed to load preferences from user profile:", prefErr);
|
||||
}
|
||||
|
||||
// 2. Fetch Tasks
|
||||
// 2. Fetch Tasks - Explicitly filtered by user and sorted
|
||||
const records = await pb.collection(TASGRID_COLLECTION).getFullList({
|
||||
filter: `user = "${pb.authStore.model?.id}"`,
|
||||
sort: '-created',
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user