-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappwrite-hooks.tsx
147 lines (129 loc) · 4.16 KB
/
appwrite-hooks.tsx
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import { Account, AppwriteException, Client, Databases, Models, RealtimeResponseEvent } from "appwrite";
import { useEffect, useState } from "react";
/*
Get the given collection, live-updated.
*/
export function useCollection(
appwriteClient: Client,
databaseAppwriteClient: Databases,
databaseId: string,
collectionId: string,
queries?: string[] | undefined,
) {
const [ documents, setDocuments ] = useState<Models.DocumentList<Models.Document>>({
total: 0, documents: [],
})
const [ loaded, setLoaded ] = useState<boolean>(false)
const [ error, setError ] = useState<AppwriteException | null>(null)
/*
Fetch the collection.
*/
const syncCollection = () => {
databaseAppwriteClient.listDocuments(
databaseId,
collectionId,
queries,
)
.then((documents: Models.DocumentList<Models.Document>) => {
setDocuments(documents)
setLoaded(true)
})
.catch(setError)
}
useEffect(() => {
syncCollection()
}, [JSON.stringify(queries)])
useEffect(() => {
// Subscribe to live changes
const unsubscribe = appwriteClient.subscribe(
`databases.${databaseId}.collections.${collectionId}.documents`,
(event) => {
if (event.events.indexOf("databases.*.collections.*.documents.*.update") >= 0) {
const updatedDocuments = [...documents.documents];
updatedDocuments.forEach((doc, index) => {
if (doc.$id === (event.payload as Models.Document).$id) {
updatedDocuments[index] = (event.payload as Models.Document);
}
});
setDocuments({total: documents.total, documents: updatedDocuments});
} else {
syncCollection()
}
},
)
return () => {
unsubscribe()
}
}, [documents]);
return { documents, loaded, error }
}
/*
Get the current logged user, live-updated.
*/
export function useUser(
appwriteClient: Client,
accountAppwriteClient: Account
) {
const [ account, setAccount ] = useState<Models.User<Models.Preferences> | null>(null)
const [ loaded, setLoaded ] = useState<boolean>(false)
const [ error, setError ] = useState<AppwriteException | null>(null)
let unsubscribe: () => void = () => {}
const syncUser = () => {
accountAppwriteClient.get()
.then((account: Models.User<Models.Preferences>) => {
setAccount(account)
setLoaded(true)
})
.catch(setError)
}
useEffect(() => {
syncUser()
unsubscribe = appwriteClient.subscribe(`account`, () => syncUser())
return () => {
unsubscribe()
}
}, [])
return { account, loaded, error }
}
/*
Get the given document, live-updated.
*/
export function useDocument(
appwriteClient: Client,
databaseAppwriteClient: Databases,
databaseId: string,
collectionId: string,
documentId: string,
) {
const [ document, setDocument ] = useState<Models.Document | null>(null)
const [ loaded, setLoaded ] = useState<boolean>(false)
const [ error, setError ] = useState<AppwriteException | null>(null)
/*
Fetch the collection.
*/
const syncDocument = () => {
databaseAppwriteClient.getDocument(
databaseId,
collectionId,
documentId,
)
.then((document: Models.Document) => {
setDocument(document)
setLoaded(true)
})
.catch(setError)
}
useEffect(() => {
if (!documentId) return () => {};
syncDocument()
// Subscribe to live changes
const unsubscribe = appwriteClient.subscribe(
`databases.${databaseId}.collections.${collectionId}.documents.${documentId}`,
(event: RealtimeResponseEvent<Models.Document>) => setDocument(event.payload),
)
return () => {
unsubscribe()
}
}, [documentId])
return { document, loaded, error }
}