perf: memo GridCard, fuse save fns, fix slash tab active color
This commit is contained in:
@@ -110,3 +110,58 @@ export function openCookiePreferences(): void {
|
||||
if (!isBrowser()) return
|
||||
window.dispatchEvent(new CustomEvent(OPEN_COOKIE_PREFERENCES_EVENT))
|
||||
}
|
||||
|
||||
/**
|
||||
* Save consent locally and sync to database for authenticated users.
|
||||
* This is the preferred method for updating consent from UI components.
|
||||
*
|
||||
* Note: For server-side sync, import and call saveCookieConsent action from your component.
|
||||
*/
|
||||
export function saveConsentWithSync(analytics: boolean, marketing: boolean = false): ConsentRecord {
|
||||
const record = setConsent({ analytics, marketing })
|
||||
|
||||
// Trigger server-side sync in the background
|
||||
// The import() is dynamic to avoid "use server" import issues in client code
|
||||
if (isBrowser()) {
|
||||
import('@/app/actions/cookie-consent').then(({ saveCookieConsent }) => {
|
||||
saveCookieConsent(record).catch((err) => {
|
||||
console.warn('[saveConsentWithSync] Server sync failed (local consent saved):', err)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
return record
|
||||
}
|
||||
|
||||
/**
|
||||
* Load consent preferring DB value when local storage is empty.
|
||||
* For authenticated users on a new device, this syncs their existing consent.
|
||||
*
|
||||
* Returns the consent record that should be used, or null if no preference exists.
|
||||
*/
|
||||
export async function loadConsentWithDBSync(): Promise<ConsentRecord | null> {
|
||||
// First check local storage (fast, always available)
|
||||
const local = getConsent()
|
||||
if (local) {
|
||||
return local // User already has local preference, use it
|
||||
}
|
||||
|
||||
// No local preference — try to load from DB for authenticated users
|
||||
if (isBrowser()) {
|
||||
try {
|
||||
const { getCookieConsentFromDB } = await import('@/app/actions/cookie-consent')
|
||||
const dbConsent = await getCookieConsentFromDB()
|
||||
|
||||
if (dbConsent) {
|
||||
// Sync DB value to local storage for future use
|
||||
localStorage.setItem(CONSENT_STORAGE_KEY, JSON.stringify(dbConsent))
|
||||
writeCookie(dbConsent)
|
||||
return dbConsent
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('[loadConsentWithDBSync] Failed to load from DB:', error)
|
||||
}
|
||||
}
|
||||
|
||||
return null // No preference anywhere
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user