Skip to content

Only re-render CharmList when connection status actually changes #1038

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions jumble/src/contexts/SyncStatusContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { useCharmManager } from "@/contexts/CharmManagerContext.tsx";
interface SyncStatusContextType {
isSyncing: boolean;
lastSyncTime: Date | null;
hasConnected: boolean;
}

const SyncStatusContext = createContext<SyncStatusContextType | undefined>(
Expand All @@ -27,7 +28,8 @@ export function SyncStatusProvider({
intervalMs = 50,
}: SyncStatusProviderProps) {
const [isSyncing, setIsSyncing] = useState(true);
const [lastSyncTime, setLastSyncTime] = useState<Date | null>(null);
const lastSyncTimeRef = useRef<Date | null>(null);
const [hasConnected, setHasConnected] = useState(false);
const { charmManager } = useCharmManager();
const isCheckingSyncRef = useRef(false);

Expand All @@ -44,7 +46,10 @@ export function SyncStatusProvider({
await charmManager.synced();

if (isMounted) {
setLastSyncTime(new Date());
lastSyncTimeRef.current = new Date();
if (!hasConnected) {
setHasConnected(true);
}
}
} catch (error) {
console.error("Sync error:", error);
Expand All @@ -68,7 +73,7 @@ export function SyncStatusProvider({
};
}, [charmManager, intervalMs]);

const value = { isSyncing, lastSyncTime };
const value = { isSyncing, lastSyncTime: lastSyncTimeRef.current, hasConnected };

return (
<SyncStatusContext.Provider value={value}>
Expand Down
4 changes: 2 additions & 2 deletions jumble/src/views/CharmList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export default function CharmList() {
const [charms] = useCell(charmManager.getCharms());
const [trash] = useCell(charmManager.getTrash());

const { lastSyncTime } = useSyncedStatus();
const { hasConnected } = useSyncedStatus();

if (lastSyncTime == null) {
if (!hasConnected) {
return (
<div className="flex flex-col items-center justify-center h-[70vh] text-center p-8">
<div className="mb-6">
Expand Down