@polybase/react
Advanced tools
Sorry, the diff of this file is not supported yet
| export * from './PolybaseProvider' | ||
| export * from './useCollection' | ||
| export * from './useCollectionOnce' | ||
| export * from './useRecord' | ||
| export * from './useRecordOnce' | ||
| export * from './usePolybase' |
| import React, { createContext, ReactNode } from 'react' | ||
| import { Polybase } from '@polybase/client' | ||
| export const PolybaseContext = createContext<Polybase>(new Polybase()) | ||
| export interface PolybaseProviderProps { | ||
| polybase: Polybase | ||
| children: ReactNode|ReactNode[] | ||
| } | ||
| export function PolybaseProvider ({ children, polybase }: PolybaseProviderProps) { | ||
| return ( | ||
| <PolybaseContext.Provider value={polybase}> | ||
| {children} | ||
| </PolybaseContext.Provider> | ||
| ) | ||
| } |
| import { useEffect, useState } from 'react' | ||
| import { Collection, CollectionList, Query, PolybaseError } from '@polybase/client' | ||
| export interface UseCollectionReturnValue<T> { | ||
| error: PolybaseError|null | ||
| data: CollectionList<T>|null | ||
| loading: boolean | ||
| } | ||
| export function useCollection<T=any> (collection?: Collection<T>|Query<T>|null): UseCollectionReturnValue<T> { | ||
| const [res, setResult] = useState<UseCollectionReturnValue<T>>({ error: null, data: null, loading: true }) | ||
| const key = collection?.key() | ||
| useEffect(() => { | ||
| if (!collection) return | ||
| setResult({ ...res, loading: true }) | ||
| const unsub = collection.onSnapshot((data) => { | ||
| setResult({ data, error: null, loading: false }) | ||
| }, (err) => { | ||
| setResult({ data: res.data, error: err, loading: false }) | ||
| }) | ||
| return unsub | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [key]) | ||
| return res | ||
| } |
| import { useEffect, useState } from 'react' | ||
| import { Collection, Query } from '@polybase/client' | ||
| import { UseCollectionReturnValue } from './useCollection' | ||
| export function useCollectionOnce<T=any> (collection?: Collection<T>|Query<T>|null): UseCollectionReturnValue<T> { | ||
| const [res, setResult] = useState<UseCollectionReturnValue<T>>({ error: null, data: null, loading: true }) | ||
| const key = collection?.key() | ||
| useEffect(() => { | ||
| if (!collection) return | ||
| setResult({ ...res, loading: true }) | ||
| collection.get().then((data) => { | ||
| setResult({ error: null, data, loading: false }) | ||
| }).catch((e) => { | ||
| setResult({ ...res, error: e, loading: false }) | ||
| }) | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [key]) | ||
| return res | ||
| } |
| import { useContext } from 'react' | ||
| import { PolybaseContext } from './PolybaseProvider' | ||
| export function usePolybase () { | ||
| return useContext(PolybaseContext) | ||
| } |
| import { useEffect, useState } from 'react' | ||
| import { CollectionRecord, PolybaseError, CollectionRecordResponse } from '@polybase/client' | ||
| export interface UseRecordReturnValue<T> { | ||
| error: PolybaseError|null | ||
| data: CollectionRecordResponse<T>|null | ||
| loading: boolean | ||
| } | ||
| export function useRecord<T=any> (record?: CollectionRecord<T>|null): UseRecordReturnValue<T> { | ||
| const [res, setResult] = useState<UseRecordReturnValue<T>>({ error: null, data: null, loading: true }) | ||
| const key = record?.key() | ||
| useEffect(() => { | ||
| if (!record) return | ||
| setResult({ ...res, loading: true }) | ||
| const unsub = record.onSnapshot((data) => { | ||
| setResult({ data, error: null, loading: false }) | ||
| }, (err) => { | ||
| setResult({ data: res.data, error: err, loading: false }) | ||
| }) | ||
| return unsub | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [key]) | ||
| return res | ||
| } | ||
| export const useDocument = useRecord |
| import { useEffect, useState } from 'react' | ||
| import { CollectionRecord } from '@polybase/client' | ||
| import { UseRecordReturnValue } from './useRecord' | ||
| export function useRecordOnce<T=any> (record?: CollectionRecord<T>|null): UseRecordReturnValue<T> { | ||
| const [res, setResult] = useState<UseRecordReturnValue<T>>({ error: null, data: null, loading: true }) | ||
| const key = record?.key() | ||
| useEffect(() => { | ||
| if (!record) return | ||
| setResult({ ...res, loading: true }) | ||
| record.get().then((data) => { | ||
| setResult({ error: null, data, loading: false }) | ||
| }).catch((e) => { | ||
| setResult({ ...res, error: e, loading: false }) | ||
| }) | ||
| // eslint-disable-next-line react-hooks/exhaustive-deps | ||
| }, [key]) | ||
| return res | ||
| } | ||
| export const useRecordONce = useRecordOnce |
+15
-14
| { | ||
| "name": "@polybase/react", | ||
| "description": "React wrapper for @polybase/client", | ||
| "version": "0.3.6", | ||
| "version": "0.3.12", | ||
| "main": "./dist/index.js", | ||
@@ -10,3 +10,4 @@ "types": "./dist/index.d.ts", | ||
| "files": [ | ||
| "dist/*" | ||
| "dist/*", | ||
| "src/*" | ||
| ], | ||
@@ -30,20 +31,20 @@ "scripts": { | ||
| "devDependencies": { | ||
| "@sinonjs/fake-timers": "^9.1.2", | ||
| "@types/jest": "^29.2.2", | ||
| "@types/react": "^18.0.25", | ||
| "@sinonjs/fake-timers": "^10.0.2", | ||
| "@types/jest": "^29.2.4", | ||
| "@types/react": "^18.0.26", | ||
| "@types/sinonjs__fake-timers": "^8.1.2", | ||
| "@typescript-eslint/eslint-plugin": "^5.42.0", | ||
| "@typescript-eslint/parser": "^5.42.0", | ||
| "eslint": "^8.26.0", | ||
| "@typescript-eslint/eslint-plugin": "^5.47.0", | ||
| "@typescript-eslint/parser": "^5.47.0", | ||
| "eslint": "^8.30.0", | ||
| "eslint-config-standard": "^17.0.0", | ||
| "eslint-plugin-import": "^2.26.0", | ||
| "eslint-plugin-jest": "^27.1.3", | ||
| "eslint-plugin-n": "^15.4.0", | ||
| "eslint-plugin-jest": "^27.1.7", | ||
| "eslint-plugin-n": "^15.6.0", | ||
| "eslint-plugin-promise": "^6.1.1", | ||
| "eslint-plugin-react": "^7.31.10", | ||
| "eslint-plugin-react": "^7.31.11", | ||
| "eslint-plugin-react-hooks": "^4.6.0", | ||
| "jest": "^29.2.2", | ||
| "jest": "^29.3.1", | ||
| "rimraf": "^3.0.2", | ||
| "ts-jest": "^29.0.3", | ||
| "typescript": "^4.8.4" | ||
| "typescript": "^4.9.4" | ||
| }, | ||
@@ -54,3 +55,3 @@ "peerDependencies": { | ||
| }, | ||
| "gitHead": "88129f382befc3f7485b8acaeeef0c75c0b5f7b2" | ||
| "gitHead": "3936b6e311e759c4d6cd973f547d958522176a33" | ||
| } |
30694
50.79%38
26.67%343
46.58%