refactor
This commit is contained in:
@@ -49,15 +49,13 @@ interface UseAssetNavigatorOptions {
|
||||
defaultOrdinal?: number;
|
||||
}
|
||||
|
||||
type SetOrdinalArg = number | ((prev: number) => number);
|
||||
|
||||
interface AssetNavigatorReturn {
|
||||
document: DocumentLike | null | undefined;
|
||||
documentId: Identifier | null;
|
||||
asset: AssetLike | null;
|
||||
assetType: string;
|
||||
ordinal: number;
|
||||
setOrdinal: (next: SetOrdinalArg) => void;
|
||||
setOrdinal: (next: number) => void;
|
||||
goPrev: () => void;
|
||||
goNext: () => void;
|
||||
canGoPrev: boolean;
|
||||
@@ -90,7 +88,7 @@ export const useAssetNavigator = ({
|
||||
const documentId = (document?.id ?? null) as Identifier | null;
|
||||
|
||||
const asset = useMemo<AssetLike | null>(() => {
|
||||
if (!document || typeof getAsset !== 'function') {
|
||||
if (!document || !getAsset) {
|
||||
return null;
|
||||
}
|
||||
return getAsset(document, assetType) || null;
|
||||
@@ -109,17 +107,19 @@ export const useAssetNavigator = ({
|
||||
}, [documentId, assetType, defaultOrdinal]);
|
||||
|
||||
const setOrdinal = useCallback(
|
||||
(next: SetOrdinalArg) => {
|
||||
setOrdinalInternal((prev) => {
|
||||
const target = typeof next === 'function' ? next(prev) : next;
|
||||
return clampOrdinalValue(target, cardinality, defaultOrdinal);
|
||||
});
|
||||
(next: number) => {
|
||||
setOrdinalInternal(clampOrdinalValue(next, cardinality, defaultOrdinal));
|
||||
},
|
||||
[cardinality, defaultOrdinal],
|
||||
);
|
||||
|
||||
const goPrev = useCallback(() => setOrdinal((value) => value - 1), [setOrdinal]);
|
||||
const goNext = useCallback(() => setOrdinal((value) => value + 1), [setOrdinal]);
|
||||
const goPrev = useCallback(() => {
|
||||
setOrdinalInternal((prev) => clampOrdinalValue(prev - 1, cardinality, defaultOrdinal));
|
||||
}, [cardinality, defaultOrdinal]);
|
||||
|
||||
const goNext = useCallback(() => {
|
||||
setOrdinalInternal((prev) => clampOrdinalValue(prev + 1, cardinality, defaultOrdinal));
|
||||
}, [cardinality, defaultOrdinal]);
|
||||
|
||||
const objects = view.getObjects();
|
||||
const currentObject = view.getObject(ordinal);
|
||||
|
||||
Reference in New Issue
Block a user