2022-06-29 19:21:40 +00:00
|
|
|
import { getDoc, orderBy, query, setDoc, where } from 'firebase/firestore'
|
2022-06-23 08:07:52 +00:00
|
|
|
import { doc } from 'firebase/firestore'
|
|
|
|
import { Manalink } from '../../../common/manalink'
|
|
|
|
import { customAlphabet } from 'nanoid'
|
2022-06-29 19:21:40 +00:00
|
|
|
import { coll, listenForValues } from './utils'
|
2022-06-23 08:07:52 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
|
2022-06-29 19:21:40 +00:00
|
|
|
export const manalinks = coll<Manalink>('manalinks')
|
|
|
|
|
2022-06-23 08:07:52 +00:00
|
|
|
export async function createManalink(data: {
|
|
|
|
fromId: string
|
|
|
|
amount: number
|
|
|
|
expiresTime: number | null
|
|
|
|
maxUses: number | null
|
|
|
|
message: string
|
|
|
|
}) {
|
|
|
|
const { fromId, amount, expiresTime, maxUses, message } = data
|
|
|
|
|
|
|
|
// At 100 IDs per hour, using this alphabet and 8 chars, there's a 1% chance of collision in 2 years
|
|
|
|
// See https://zelark.github.io/nano-id-cc/
|
|
|
|
const nanoid = customAlphabet(
|
|
|
|
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
|
|
8
|
|
|
|
)
|
|
|
|
const slug = nanoid()
|
|
|
|
|
|
|
|
if (amount <= 0 || isNaN(amount) || !isFinite(amount)) return null
|
|
|
|
|
|
|
|
const manalink: Manalink = {
|
|
|
|
slug,
|
|
|
|
fromId,
|
|
|
|
amount,
|
|
|
|
token: 'M$',
|
|
|
|
createdTime: Date.now(),
|
|
|
|
expiresTime,
|
|
|
|
maxUses,
|
|
|
|
claimedUserIds: [],
|
|
|
|
claims: [],
|
|
|
|
message,
|
|
|
|
}
|
|
|
|
|
2022-06-29 19:21:40 +00:00
|
|
|
await setDoc(doc(manalinks, slug), manalink)
|
2022-06-23 08:07:52 +00:00
|
|
|
return slug
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: This required an index, make sure to also set up in prod
|
|
|
|
function listUserManalinks(fromId?: string) {
|
|
|
|
return query(
|
2022-06-29 19:21:40 +00:00
|
|
|
manalinks,
|
2022-06-23 08:07:52 +00:00
|
|
|
where('fromId', '==', fromId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getManalink(slug: string) {
|
2022-06-29 19:21:40 +00:00
|
|
|
return (await getDoc(doc(manalinks, slug))).data()
|
2022-06-23 08:07:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useManalink(slug: string) {
|
2022-06-29 19:21:40 +00:00
|
|
|
const [manalink, setManalink] = useState<Manalink | undefined>(undefined)
|
2022-06-23 08:07:52 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (slug) {
|
|
|
|
getManalink(slug).then(setManalink)
|
|
|
|
}
|
|
|
|
}, [slug])
|
|
|
|
return manalink
|
|
|
|
}
|
|
|
|
|
|
|
|
export function listenForUserManalinks(
|
|
|
|
fromId: string | undefined,
|
|
|
|
setLinks: (links: Manalink[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<Manalink>(listUserManalinks(fromId), setLinks)
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useUserManalinks = (fromId: string) => {
|
|
|
|
const [links, setLinks] = useState<Manalink[]>([])
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return listenForUserManalinks(fromId, setLinks)
|
|
|
|
}, [fromId])
|
|
|
|
|
|
|
|
return links
|
|
|
|
}
|