Skip to content

Commit

Permalink
assert populated draft state in useDraftObservation()
Browse files Browse the repository at this point in the history
  • Loading branch information
achou11 committed Dec 3, 2024
1 parent 9d6f3ab commit dd81877
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/frontend/hooks/draftObservation.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
import {useStore} from 'zustand';

import {useDraftObservationContext} from '../contexts/DraftObservationContext';
import {DraftState} from '../hooks/persistedState/usePersistedDraftObservationNew';
import {
DraftState,
DraftStatePopulated,

Check failure on line 6 in src/frontend/hooks/draftObservation.ts

View workflow job for this annotation

GitHub Actions / frontend

Module '"../hooks/persistedState/usePersistedDraftObservationNew"' declares 'DraftStatePopulated' locally, but it is not exported.
} from '../hooks/persistedState/usePersistedDraftObservationNew';
import {useCallback} from 'react';

function defaultSelector(state: DraftState) {
function defaultSelector(state: DraftStatePopulated) {
return state;
}

export function useDraftObservation<S = DraftState>(
selector: (state: DraftState) => S = defaultSelector as any,
export function useDraftObservation<S = DraftStatePopulated>(
selector: (state: DraftStatePopulated) => S = defaultSelector as any,
) {
const {store} = useDraftObservationContext();
const draftObservation = useStore(store, selector);
return draftObservation;

const assertPopulatedStateSelector = useCallback(
(state: DraftState) => {
if (!state.value) {
throw new Error('No observation to read');
}
return selector(state);
},
[selector],
);

const value = useStore(store, assertPopulatedStateSelector);

return value;
}

export function useDraftObservationActions() {
Expand Down

0 comments on commit dd81877

Please sign in to comment.