Files
cal.diy/companion/utils/queryPersister.ts
T
Dhairyashil Shinde 9203cb262c feat(companion): component improvements, migrate to path aliases, expo-haptics, expo-image (#26226)
* react compiler

* remove compilation mode 'all', it will use infer by default

* feat(companion): add theme tokens, path aliases, and component improvements

- Extend tailwind.config.js with Cal.com brand color tokens (cal.text, cal.bg, cal.border, cal.accent, cal.brand)
- Create theme/colors.ts for JS usage where Tailwind classes can't be used
- Configure path aliases in tsconfig.json (@/components, @/hooks, @/utils, etc.)
- Add expo-image and expo-haptics dependencies
- Enhance AppPressable with Reanimated animations (opacity + scale) and haptic feedback
- Extract shared logic from BookingListItem into useBookingListItemData hook and BookingListItemParts
- Extract shared logic from EventTypeListItem into useEventTypeListItemData hook and EventTypeListItemParts
- Extract shared logic from AvailabilityListItem into AvailabilityListItemParts
- Update all list item components to use new theme tokens instead of hardcoded hex values

* fix react compiler non memo issue

* feat(companion): migrate to path aliases and expo-image

- Remove theme/colors.ts and theme/index.ts (defer dark mode to future PR)
- Replace colors import with hardcoded hex values in components
- Migrate all files to use path aliases (@/components/*, @/hooks/*, etc.)
- Migrate SvgImage to use expo-image for better performance
- Update tsconfig.json to remove @/theme/* alias

* fix(companion): add path aliases to extension tsconfig

Add baseUrl and paths configuration to tsconfig.extension.json to support
path aliases in shared types used by the extension build.
2025-12-28 08:08:33 -03:00

143 lines
4.1 KiB
TypeScript

/**
* React Query Cache Persister
*
* Uses the shared storage adapter from utils/storage.ts for cross-platform support.
*/
import type { PersistedClient, Persister } from "@tanstack/react-query-persist-client";
import { CACHE_CONFIG } from "@/config/cache.config";
import { generalStorage } from "./storage";
// Use the shared general storage adapter for cache persistence
const storage = generalStorage;
/**
* Create a React Query persister that works across all platforms
*
* This persister:
* - Saves the query cache to platform-appropriate storage
* - Restores cache on app launch for instant data display
* - Handles serialization/deserialization of cache data
* - Respects cache expiration (maxAge)
*/
export const createQueryPersister = (): Persister => {
const storageKey = CACHE_CONFIG.persistence.storageKey;
const maxAge = CACHE_CONFIG.persistence.maxAge;
return {
/**
* Persist the client state to storage
*/
persistClient: async (client: PersistedClient): Promise<void> => {
try {
const serialized = JSON.stringify(client);
await storage.setItem(storageKey, serialized);
} catch (error) {
console.warn("[QueryPersister] Failed to persist client:", error);
// Fail silently - persistence is a nice-to-have, not critical
}
},
/**
* Restore the client state from storage
*/
restoreClient: async (): Promise<PersistedClient | undefined> => {
try {
const serialized = await storage.getItem(storageKey);
if (!serialized) {
return undefined;
}
const client = JSON.parse(serialized) as PersistedClient;
// Validate timestamp exists and is a valid number
if (typeof client.timestamp !== "number" || Number.isNaN(client.timestamp)) {
console.warn("[QueryPersister] Invalid or missing timestamp, discarding cache");
await storage.removeItem(storageKey);
return undefined;
}
// Check if the persisted cache has expired
const persistedAt = client.timestamp;
const now = Date.now();
if (now - persistedAt > maxAge) {
// Cache is too old, discard it
await storage.removeItem(storageKey);
return undefined;
}
return client;
} catch (error) {
console.warn("[QueryPersister] Failed to restore client:", error);
// If restoration fails, start fresh
return undefined;
}
},
/**
* Remove the persisted client state
*/
removeClient: async (): Promise<void> => {
try {
await storage.removeItem(storageKey);
} catch (error) {
console.warn("[QueryPersister] Failed to remove client:", error);
}
},
};
};
/**
* Export the storage adapter for potential direct use
*/
export { storage };
/**
* Utility to clear all query cache from storage
* Useful for logout or cache reset scenarios
*/
export const clearQueryCache = async (): Promise<void> => {
try {
await storage.removeItem(CACHE_CONFIG.persistence.storageKey);
} catch (error) {
console.warn("[QueryPersister] Failed to clear cache:", error);
}
};
/**
* Get cache metadata (for debugging/status display)
*/
export const getCacheMetadata = async (): Promise<{
exists: boolean;
timestamp?: number;
age?: number;
isExpired?: boolean;
} | null> => {
try {
const serialized = await storage.getItem(CACHE_CONFIG.persistence.storageKey);
if (!serialized) {
return { exists: false };
}
const client = JSON.parse(serialized) as PersistedClient;
// Validate timestamp exists and is a valid number
if (typeof client.timestamp !== "number" || Number.isNaN(client.timestamp)) {
return { exists: true, isExpired: true }; // Treat invalid timestamp as expired
}
const now = Date.now();
const age = now - client.timestamp;
return {
exists: true,
timestamp: client.timestamp,
age,
isExpired: age > CACHE_CONFIG.persistence.maxAge,
};
} catch (error) {
console.warn("[QueryPersister] Failed to get cache metadata:", error);
return null;
}
};