60f68b178d
* Fold type, fold page, query for fold contracts * Tsconfig: target esnext, nounused locals: false * Store tags in field on contract. Script to update contract tags * Show tags on fold page * Load all fold comments server-side to serve better feed * Fix the annoying firebase already initialized error! * Add links to /edit and /leaderboards for fold * Page with list of folds * UI for creating a fold * Create a fold * Edit fold page
42 lines
947 B
TypeScript
42 lines
947 B
TypeScript
import { db } from './init'
|
|
import {
|
|
doc,
|
|
getDoc,
|
|
getDocs,
|
|
onSnapshot,
|
|
Query,
|
|
DocumentReference,
|
|
} from 'firebase/firestore'
|
|
|
|
export const getValue = async <T>(doc: DocumentReference) => {
|
|
const snap = await getDoc(doc)
|
|
return snap.exists() ? (snap.data() as T) : null
|
|
}
|
|
|
|
export const getValues = async <T>(query: Query) => {
|
|
const snap = await getDocs(query)
|
|
return snap.docs.map((doc) => doc.data() as T)
|
|
}
|
|
|
|
export function listenForValue<T>(
|
|
docRef: DocumentReference,
|
|
setValue: (value: T | null) => void
|
|
) {
|
|
return onSnapshot(docRef, (snapshot) => {
|
|
const value = snapshot.exists() ? (snapshot.data() as T) : null
|
|
setValue(value)
|
|
})
|
|
}
|
|
|
|
export function listenForValues<T>(
|
|
query: Query,
|
|
setValues: (values: T[]) => void
|
|
) {
|
|
return onSnapshot(query, (snapshot) => {
|
|
if (snapshot.metadata.fromCache) return
|
|
|
|
const values = snapshot.docs.map((doc) => doc.data() as T)
|
|
setValues(values)
|
|
})
|
|
}
|