@@ -15,6 +15,7 @@ import {
1515 toRecipeUpdateDto ,
1616 addToList ,
1717} from 'src/shared/CookTime' ;
18+ import { UnitPreference } from 'src/shared/units' ;
1819
1920export type PendingImage = {
2021 id : string ;
@@ -35,6 +36,7 @@ interface RecipeContextState {
3536 imageOperationInProgress : boolean ;
3637 edit : boolean ;
3738 units : MeasureUnit [ ] ;
39+ unitPreference : UnitPreference ;
3840 newServings : number ;
3941 errorMessage : string | null ;
4042 operationInProgress : boolean ;
@@ -51,6 +53,7 @@ interface RecipeContextActions {
5153 setErrorMessage : ( message : string | null ) => void ;
5254 setNewServings : ( servings : number ) => void ;
5355 setToastMessage : ( message : string | null ) => void ;
56+ setUnitPreference : ( preference : UnitPreference ) => void ;
5457
5558 // Recipe updates
5659 updateRecipe : ( updates : Partial < MultiPartRecipe > ) => void ;
@@ -129,6 +132,7 @@ export function RecipeProvider({ recipeId, generatedRecipe, children }: RecipePr
129132 const [ imageOperationInProgress , setImageOperationInProgress ] = useState ( false ) ;
130133 const [ edit , setEdit ] = useState ( false ) ;
131134 const [ units , setUnits ] = useState < MeasureUnit [ ] > ( [ ] ) ;
135+ const [ unitPreference , setUnitPreference ] = useState < UnitPreference > ( 'recipe' ) ;
132136 const [ newServings , setNewServings ] = useState ( 1 ) ;
133137 const [ errorMessage , setErrorMessage ] = useState < string | null > ( null ) ;
134138 const [ operationInProgress , setOperationInProgress ] = useState ( false ) ;
@@ -175,6 +179,11 @@ export function RecipeProvider({ recipeId, generatedRecipe, children }: RecipePr
175179
176180 // Load initial data
177181 useEffect ( ( ) => {
182+ const savedPreference = window . localStorage . getItem ( 'cooktime.unitPreference' ) ;
183+ if ( savedPreference === 'recipe' || savedPreference === 'imperial' || savedPreference === 'metric' ) {
184+ setUnitPreference ( savedPreference ) ;
185+ }
186+
178187 // Fetch units
179188 fetch ( '/api/recipe/units' )
180189 . then ( ( res ) => res . json ( ) )
@@ -223,6 +232,10 @@ export function RecipeProvider({ recipeId, generatedRecipe, children }: RecipePr
223232 . then ( ( result ) => setNutritionFacts ( result as RecipeNutritionFacts ) ) ;
224233 } , [ recipeId , generatedRecipe , applyGeneratedRecipe ] ) ;
225234
235+ useEffect ( ( ) => {
236+ window . localStorage . setItem ( 'cooktime.unitPreference' , unitPreference ) ;
237+ } , [ unitPreference ] ) ;
238+
226239 // Recipe updates
227240 const updateRecipe = useCallback ( ( updates : Partial < MultiPartRecipe > ) => {
228241 setRecipe ( ( prev ) => ( { ...prev , ...updates } ) ) ;
@@ -521,6 +534,7 @@ export function RecipeProvider({ recipeId, generatedRecipe, children }: RecipePr
521534 imageOperationInProgress,
522535 edit,
523536 units,
537+ unitPreference,
524538 newServings,
525539 errorMessage,
526540 operationInProgress,
@@ -534,6 +548,7 @@ export function RecipeProvider({ recipeId, generatedRecipe, children }: RecipePr
534548 setErrorMessage,
535549 setNewServings,
536550 setToastMessage,
551+ setUnitPreference,
537552 updateRecipe,
538553 updateComponent,
539554 appendIngredientToComponent,
0 commit comments