|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import {INFERENCE_API_ENDPOINT, VIDEO_API_ENDPOINT} from '@/demo/DemoConfig'; |
|
|
|
|
|
export type Settings = { |
|
|
videoAPIEndpoint: string; |
|
|
inferenceAPIEndpoint: string; |
|
|
}; |
|
|
|
|
|
|
|
|
export const SAM2_SETTINGS_KEY = 'SAM2_SETTINGS_KEY'; |
|
|
|
|
|
export type Action = |
|
|
| {type: 'load-state'} |
|
|
| {type: 'change-video-api-endpoint'; url: string} |
|
|
| {type: 'change-inference-api-endpoint'; url: string}; |
|
|
|
|
|
export const DEFAULT_SETTINGS: Settings = { |
|
|
videoAPIEndpoint: VIDEO_API_ENDPOINT, |
|
|
inferenceAPIEndpoint: INFERENCE_API_ENDPOINT, |
|
|
}; |
|
|
|
|
|
export function settingsReducer(state: Settings, action: Action): Settings { |
|
|
function storeSettings(newState: Settings): void { |
|
|
localStorage.setItem(SAM2_SETTINGS_KEY, JSON.stringify(newState)); |
|
|
} |
|
|
|
|
|
switch (action.type) { |
|
|
case 'load-state': { |
|
|
try { |
|
|
const serializedSettings = localStorage.getItem(SAM2_SETTINGS_KEY); |
|
|
if (serializedSettings != null) { |
|
|
return JSON.parse(serializedSettings) as Settings; |
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
storeSettings(state); |
|
|
} |
|
|
} catch { |
|
|
|
|
|
} |
|
|
return state; |
|
|
} |
|
|
case 'change-video-api-endpoint': |
|
|
state.videoAPIEndpoint = action.url; |
|
|
break; |
|
|
case 'change-inference-api-endpoint': |
|
|
state.inferenceAPIEndpoint = action.url; |
|
|
break; |
|
|
} |
|
|
|
|
|
|
|
|
storeSettings(state); |
|
|
|
|
|
return state; |
|
|
} |
|
|
|