types#DbQuery TypeScript Examples

The following examples show how to use types#DbQuery. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useAvailableCodeBundles.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useAvailableCodeBundles(limit = 1): DbQuery<CodeBundleDocument[]> {
  const { db, identity } = useDatabase();

  const query = useCallback(async (): Promise<CodeBundleDocument[]> => {
    return (await findOwnedCodeBundles(db, identity, limit)).sort(byDate);
  }, [db, identity, limit]);

  return useDbQuery(query);
}
Example #2
Source File: useCodeBundle.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useCodeBundle(codeHash: string): DbQuery<CodeBundle> {
  const { db } = useDatabase();

  const query = useCallback(async (): Promise<CodeBundle> => {
    if (isValidHash(codeHash)) {
      const document = await findCodeBundleByHash(db, codeHash);
      return { document };
    }
    return { document: null };
  }, [codeHash, db]);

  return useDbQuery(query, result => !!result);
}
Example #3
Source File: useContract.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useContract(address: string): DbQuery<ReturnType> {
  const { api } = useApi();
  const { db } = useDatabase();

  const query = useCallback(async (): Promise<ReturnType> => {
    const document = await findContractByAddress(db, address);

    return api && document
      ? [new Contract(api, new Abi(document.abi), address), document]
      : [null, null];
  }, [db, address, api]);

  return useDbQuery(query, result => !!result && !!result[0]);
}
Example #4
Source File: useMyCodeBundles.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useMyCodeBundles(): DbQuery<MyCodeBundles> {
  const { db, identity } = useDatabase();

  const query = useCallback((): Promise<MyCodeBundles | null> => {
    return findMyCodeBundles(db, identity);
  }, [db, identity]);

  return useDbQuery(query);
}
Example #5
Source File: useStatistics.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useStatistics(): DbQuery<DbStatistics> {
  const { db } = useDatabase();

  const query = useCallback((): Promise<DbStatistics | null> => {
    return getStatistics(db);
  }, [db]);

  return useDbQuery(query);
}
Example #6
Source File: useTopContracts.ts    From contracts-ui with GNU General Public License v3.0 6 votes vote down vote up
export function useTopContracts(): DbQuery<ContractDocument[]> {
  const { db } = useDatabase();

  const query = useCallback((): Promise<ContractDocument[] | null> => {
    return findTopContracts(db);
  }, [db]);

  return useDbQuery(query);
}
Example #7
Source File: useDbQuery.ts    From contracts-ui with GNU General Public License v3.0 5 votes vote down vote up
export function useDbQuery<T>(
  query: () => Promise<T | null>,
  validate: ValidateFn<T> = value => !!value
): DbQuery<T> {
  const { isDbReady } = useDatabase();
  const [state, setState] = useState<State<T>>(initialState);

  const fetch = useCallback((): void => {
    if (!isDbReady) return;

    query()
      .then(result => {
        setState(state => ({
          ...state,
          data: result,
          error: null,
          isLoading: false,
          isValid: validate(result),
          updated: Date.now(),
        }));
      })
      .catch((e: Error) => {
        setState(state => ({
          ...state,
          data: null,
          error: e.message || 'Error',
          isLoading: false,
          isValid: false,
          updated: Date.now(),
        }));

        console.error(e);
      });
    // validate updates too often
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [isDbReady, query]);

  const refresh = useCallback((): void => {
    setState(state => ({
      ...state,
      error: null,
      isLoading: true,
      isValid: true,
    }));

    fetch();
  }, [fetch]);

  useEffect((): void => {
    fetch();
  }, [fetch]);

  return { ...state, refresh };
}