|
| 1 | +import { useEffect, useReducer } from 'react' |
| 2 | +import { SupabaseRealtimePayload } from '@supabase/supabase-js' |
| 3 | + |
| 4 | +import { UseSelectState, useSelect } from '../data' |
| 5 | +import { useSubscription } from './use-subscription' |
| 6 | + |
| 7 | +export type UseRealtimeState<Data = any> = Omit< |
| 8 | + UseSelectState<Data>, |
| 9 | + 'count' |
| 10 | +> & { |
| 11 | + old?: Data[] | null |
| 12 | +} |
| 13 | + |
| 14 | +export type UseRealtimeResponse<Data = any> = [ |
| 15 | + UseRealtimeState<Data>, |
| 16 | + () => Promise<Pick<UseSelectState<Data>, 'count' | 'data' | 'error'>>, |
| 17 | +] |
| 18 | + |
| 19 | +export type UseRealtimeAction<Data = any> = |
| 20 | + | { type: 'FETCH'; payload: UseSelectState<Data> } |
| 21 | + | { type: 'SUBSCRIPTION'; payload: SupabaseRealtimePayload<Data> } |
| 22 | + |
| 23 | +export type UseRealtimeCompareFn<Data = any> = ( |
| 24 | + data: Data, |
| 25 | + payload: Data, |
| 26 | +) => boolean |
| 27 | + |
| 28 | +export function useRealtime<Data = any>( |
| 29 | + table: string, |
| 30 | + compareFn: UseRealtimeCompareFn = (a, b) => a.id === b.id, |
| 31 | +): UseRealtimeResponse<Data> { |
| 32 | + if (table === '*') |
| 33 | + throw Error( |
| 34 | + 'Must specify table or row. Cannot listen for all database changes.', |
| 35 | + ) |
| 36 | + |
| 37 | + const [result, reexecute] = useSelect<Data>(table) |
| 38 | + const [state, dispatch] = useReducer< |
| 39 | + React.Reducer<UseRealtimeState<Data>, UseRealtimeAction<Data>> |
| 40 | + >(reducer(compareFn), result) |
| 41 | + |
| 42 | + useSubscription((payload) => dispatch({ type: 'SUBSCRIPTION', payload }), { |
| 43 | + table, |
| 44 | + }) |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + dispatch({ type: 'FETCH', payload: result }) |
| 48 | + }, [result]) |
| 49 | + |
| 50 | + return [state, reexecute] |
| 51 | +} |
| 52 | + |
| 53 | +const reducer = <Data = any>(compareFn: UseRealtimeCompareFn) => ( |
| 54 | + state: UseRealtimeState<Data>, |
| 55 | + action: UseRealtimeAction<Data>, |
| 56 | +): UseRealtimeState<Data> => { |
| 57 | + const old = state.data |
| 58 | + switch (action.type) { |
| 59 | + case 'FETCH': |
| 60 | + return { ...state, old, ...action.payload } |
| 61 | + case 'SUBSCRIPTION': |
| 62 | + switch (action.payload.eventType) { |
| 63 | + case 'DELETE': |
| 64 | + return { |
| 65 | + ...state, |
| 66 | + data: state.data?.filter( |
| 67 | + (x) => !compareFn(x, action.payload.old), |
| 68 | + ), |
| 69 | + fetching: false, |
| 70 | + old, |
| 71 | + } |
| 72 | + case 'INSERT': |
| 73 | + return { |
| 74 | + ...state, |
| 75 | + data: [...(old ?? []), action.payload.new], |
| 76 | + fetching: false, |
| 77 | + old, |
| 78 | + } |
| 79 | + case 'UPDATE': { |
| 80 | + const data = old ?? [] |
| 81 | + const index = data.findIndex((x) => |
| 82 | + compareFn(x, action.payload.new), |
| 83 | + ) |
| 84 | + return { |
| 85 | + ...state, |
| 86 | + data: [ |
| 87 | + ...data.slice(0, index), |
| 88 | + action.payload.new, |
| 89 | + ...data.slice(index + 1), |
| 90 | + ], |
| 91 | + fetching: false, |
| 92 | + old, |
| 93 | + } |
| 94 | + } |
| 95 | + default: |
| 96 | + throw Error( |
| 97 | + `eventType "${action.payload.eventType}" does not exist.`, |
| 98 | + ) |
| 99 | + } |
| 100 | + default: |
| 101 | + throw Error('Action type does not exist.') |
| 102 | + } |
| 103 | +} |
0 commit comments