Files
pybricks-code/src/fileStorage/hooks.ts
T
David Lechner 0b84b825b6 fileStorage/hooks: fix deps on useFileStorageUuid
This way if the path changes, we get the correct result.
2022-12-16 17:54:36 -06:00

45 lines
1.3 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
import { useLiveQuery } from 'dexie-react-hooks';
import { useContext } from 'react';
import { FileStorageContext } from './context';
import { FileMetadata, UUID } from '.';
/**
* Gets all file metadata for all files currently in storage.
*
* The returned array is sorted by the path.
*/
export function useFileStorageMetadata(): FileMetadata[] | undefined {
const db = useContext(FileStorageContext);
return useLiveQuery(() => db.metadata.orderBy('path').toArray());
}
/**
* Gets the file path for a file UUID.
*
* If the file is renamed, the returned value will be automatically updated.
*/
export function useFileStoragePath(uuid: UUID): string | undefined {
const db = useContext(FileStorageContext);
return useLiveQuery(() => db.metadata.get(uuid, (x) => x?.path), [uuid]);
}
/**
* Gets the file path for a file UUID.
*
* If the file is renamed, the returned value will be automatically updated.
*/
export function useFileStorageUuid(path: string): UUID | undefined {
const db = useContext(FileStorageContext);
return useLiveQuery(
() =>
db.metadata
.where('path')
.equals(path)
.first((x) => x?.uuid),
[path],
);
}