forked from trestleinc/replicate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervalsContext.tsx
More file actions
113 lines (99 loc) · 2.94 KB
/
Copy pathIntervalsContext.tsx
File metadata and controls
113 lines (99 loc) · 2.94 KB
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
import { createContext, useContext, useState, useEffect, type ReactNode } from 'react';
import { useLiveQuery } from '@tanstack/react-db';
import type { Materialized } from '@oneiron-dev/replicate/client';
import { intervals, type Interval } from '../collections/useIntervals';
import { comments, type Comment } from '../collections/useComments';
interface IntervalsContextValue {
collection: ReturnType<typeof intervals.get>;
intervals: Interval[];
isLoading: boolean;
}
const IntervalsContext = createContext<IntervalsContextValue | null>(null);
let persistenceInitialized = false;
interface PersistenceGateProps {
children: ReactNode;
intervalsMaterial?: Materialized<Interval>;
commentsMaterial?: Materialized<Comment>;
}
function PersistenceGate({ children, intervalsMaterial, commentsMaterial }: PersistenceGateProps) {
const [ready, setReady] = useState(persistenceInitialized);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
if (!ready) {
Promise.all([
intervals.init(intervalsMaterial ?? undefined),
comments.init(commentsMaterial ?? undefined),
])
.then(() => {
persistenceInitialized = true;
setReady(true);
})
.catch((err) => {
console.error('PersistenceGate init failed:', err);
setError(err instanceof Error ? err.message : String(err));
});
}
}, [ready, intervalsMaterial, commentsMaterial]);
if (error) {
return (
<div className="flex h-screen items-center justify-center">
<div className="max-w-md px-4 text-center">
<p className="text-destructive mb-4">{error}</p>
<button
type="button"
className="border-muted rounded border px-4 py-2 text-sm"
onClick={() => location.reload()}
>
Retry
</button>
</div>
</div>
);
}
if (!ready) {
return (
<div className="flex h-screen items-center justify-center">
<div className="text-muted-foreground">Loading...</div>
</div>
);
}
return <>{children}</>;
}
function IntervalsProviderInner({ children }: { children: ReactNode }) {
const collection = intervals.get();
const { data: intervalsData = [], isLoading } = useLiveQuery(collection);
return (
<IntervalsContext.Provider
value={{
collection,
intervals: intervalsData,
isLoading,
}}
>
{children}
</IntervalsContext.Provider>
);
}
interface IntervalsProviderProps {
children: ReactNode;
intervalsMaterial?: Materialized<Interval>;
commentsMaterial?: Materialized<Comment>;
}
export function IntervalsProvider({
children,
intervalsMaterial,
commentsMaterial,
}: IntervalsProviderProps) {
return (
<PersistenceGate intervalsMaterial={intervalsMaterial} commentsMaterial={commentsMaterial}>
<IntervalsProviderInner>{children}</IntervalsProviderInner>
</PersistenceGate>
);
}
export function useIntervalsContext() {
const ctx = useContext(IntervalsContext);
if (!ctx) {
throw new Error('useIntervalsContext must be used within IntervalsProvider');
}
return ctx;
}