audiogram-beta/js/store.js

409 lines
13 KiB
JavaScript
Raw Permalink Normal View History

2022-12-10 10:27:23 -05:00
import React from "react";
2022-12-05 23:22:37 -05:00
2022-12-03 09:36:01 -05:00
const size = 1/6;
2022-12-04 16:19:22 -05:00
/** @type {Array<Store.ColumnMapping>} */
2022-11-27 23:56:37 -05:00
export const ColumnMapping = [
[ 125, size*0.0, true ],
[ 250, size*1.0, true ],
[ 500, size*2.0, true ],
[1000, size*3.0, true ],
[2000, size*4.0, true ],
[3000, size*4.5, false],
[4000, size*5.0, true ],
[6000, size*5.5, false],
[8000, size*6.0, true ]
];
2022-12-10 09:56:35 -05:00
/** Looks up a frequency in ColumnMapping
* @type {(inFrequency:number)=>Store.ColumnMapping|false} */
2022-11-27 23:56:37 -05:00
export const ColumnLookup =(inFrequency)=>
{
for(let i=0; i<ColumnMapping.length; i++)
2022-11-24 12:16:57 -05:00
{
2022-11-27 23:56:37 -05:00
const map = ColumnMapping[i];
if(map[0] == inFrequency){ return map; }
2022-11-24 12:16:57 -05:00
}
2022-11-27 23:56:37 -05:00
return false;
2022-11-28 18:42:41 -05:00
};
2022-12-10 13:24:31 -05:00
/** @type {(inDecimal:number)=>string} */
const Perc =(inDecimal)=> `${inDecimal*100}%`;
2023-06-07 19:31:10 -04:00
/** @type {(inTest:Store.Test)=>Store.Grade} */
export const Grade =(inTest)=>
{
/** @type {Store.Grade} */
const output = { Total:0, Marks:0, Score:0 };
/** @type {(inGoal:number, inResult:number)=>number} */
const Mapper =(inGoal, inResult)=>
{
const err = Math.abs(inGoal-inResult);
if(err == 0){ return 1; }
else if(err > 0 && err <= 5){ return 0.9; }
else if(err > 5 && err <= 10){ return 0.7; }
else if(err > 10 && err <= 15){ return 0.2; }
else{ return 0; }
}
for(let i=0; i<inTest.Plot.length; i++)
{
const {TestL, TestR, UserL, UserR} = inTest.Plot[i];
if(TestL)
{
output.Total ++;
if(UserL)
{
output.Marks++;
output.Score += Mapper(TestL.Stim, UserL.Stim);
}
}
if(TestR)
{
output.Total ++;
if(UserR)
{
output.Marks++;
output.Score += Mapper(TestR.Stim, UserR.Stim);
}
}
}
if(output.Marks > 0)
{
output.Score = Math.floor((output.Score/output.Marks) * 10000)/100;
}
return output;
}
2023-06-14 22:53:59 -04:00
const ErrorCol =
[30, 25, 20, 15, 10, 5, 0, -5, -10, -15 ]
const ErrorLUT = [
[0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00],
[0.00, 0.00, 0.00, 0.00, 0.10, 0.15, 0.10, 0.10, 0.00, 0.00],
[0.00, 0.00, 0.02, 0.05, 0.15, 0.20, 0.30, 0.15, 0.05, 0.00],
[0.00, 0.02, 0.05, 0.10, 0.20, 0.40, 0.60, 0.30, 0.05, 0.00]
];
/** @type {(inState:Store.State)=>void} */
const ErrorProbability =(inState)=>
{
const miss = inState.Stim.Value - (inState.Live.Mark.Test?.Stim ?? inState.Stim.Value);
inState.Live.Mark.Errs = ErrorLUT[inState.Errs]?.[ErrorCol.indexOf(miss)] ?? 0;
}
2022-12-10 09:56:35 -05:00
/** Creates a new Store.Context object that contain the current selections
* @type {(inState:Store.State, inTest?:Store.Test)=>Store.Context} */
2022-12-10 00:33:38 -05:00
const Reselect =(inState, inTest)=>
2022-11-28 18:42:41 -05:00
{
2022-12-10 00:33:38 -05:00
/** @type {Store.Context} */
const output = { Test:inTest??inState.Live.Test, Mark:{User:undefined, Errs:0} };
2022-12-10 00:33:38 -05:00
const column = ColumnMapping[inState.Freq.Value];
2022-12-10 09:56:35 -05:00
if(column && output.Test)
2022-11-27 23:56:37 -05:00
{
2022-12-10 00:33:38 -05:00
const hz = column[0];
2022-12-10 09:56:35 -05:00
for(let i=0; i<output.Test.Plot.length; i++)
2022-11-27 23:56:37 -05:00
{
2022-12-10 09:56:35 -05:00
const plot = output.Test.Plot[i];
2022-12-10 00:33:38 -05:00
if(plot.Hz == hz)
2022-11-27 23:56:37 -05:00
{
2022-12-10 00:33:38 -05:00
output.Freq = plot;
2023-06-14 22:32:21 -04:00
output.Mark.User = inState.Chan.Value ? plot.UserR : plot.UserL;
output.Mark.Test = inState.Chan.Value ? plot.TestR : plot.TestL;
2023-05-31 18:28:49 -04:00
break;
2022-11-27 23:56:37 -05:00
}
}
2022-11-28 10:00:18 -05:00
}
2022-12-10 00:33:38 -05:00
return output;
2022-11-28 10:00:18 -05:00
};
2022-12-03 09:36:01 -05:00
2022-12-10 09:56:35 -05:00
/** Creates a new Store.DrawGroup object for the given Test and settings
* @type {(inTest:Store.Test|undefined, inChan:number, inStim:Store.Range, inIsUser:boolean)=>Store.DrawGroup} */
2022-12-10 00:33:38 -05:00
const Redraw =(inTest, inChan, inStim, inIsUser)=>
2022-12-03 09:36:01 -05:00
{
2022-12-04 16:19:22 -05:00
/** @type {Store.DrawGroup} */
2022-12-03 13:32:04 -05:00
const output = {Points:[], Paths:[]};
2022-12-10 00:33:38 -05:00
if(inTest)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
let plot;
for(let i=0; i<inTest.Plot.length; i++)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
plot = inTest.Plot[i];
2022-12-10 09:56:35 -05:00
const mark = plot[`${inIsUser ? "User" : "Test"}${inChan ? "R" : "L"}`]
2022-12-10 00:33:38 -05:00
if(mark)
2022-12-03 09:36:01 -05:00
{
2022-12-10 00:33:38 -05:00
const lookup = ColumnLookup(plot.Hz);
if(lookup)
{
/** @type {Store.DrawPoint} */
const point = {
2022-12-10 13:24:31 -05:00
X: Perc(lookup[1]),
Y: Perc((mark.Stim - inStim.Min)/(inStim.Max - inStim.Min)),
2022-12-10 00:33:38 -05:00
Mark: mark
};
output.Points.push(point);
}
2022-12-03 09:36:01 -05:00
}
}
2022-12-10 00:33:38 -05:00
for(let i=1; i<output.Points.length; i++)
2022-12-05 23:22:37 -05:00
{
2022-12-10 00:33:38 -05:00
/** @type {Store.DrawLine} */
const line = {Head:output.Points[i-1], Tail:output.Points[i]};
2022-12-10 13:24:31 -05:00
if(line.Head.Mark?.Resp && line.Tail.Mark?.Resp)
2022-12-10 00:33:38 -05:00
{
output.Paths.push(line);
}
2022-12-05 23:22:37 -05:00
}
}
2022-12-03 13:32:04 -05:00
return output;
2022-12-10 13:24:31 -05:00
};
/** Create a new cursor position from the state
* @type {(inState:Store.State)=>Store.DrawPoint} */
const Reposition =(inState)=> ({
X: Perc(ColumnMapping[inState.Freq.Value][1]),
Y: Perc((inState.Stim.Value - inState.Stim.Min)/(inState.Stim.Max - inState.Stim.Min))
});
2022-12-03 09:36:01 -05:00
2022-12-04 16:19:22 -05:00
/** @type {Store.Reducer} */
2022-11-28 10:00:18 -05:00
export function Reducer(inState, inAction)
{
const clone = {...inState};
2022-11-28 18:42:41 -05:00
const {Name, Data} = inAction;
2022-11-28 10:00:18 -05:00
2022-11-28 18:42:41 -05:00
if(Name == "Test")
{
2022-12-10 14:45:15 -05:00
const test = clone.Test[Data];
if(test)
2022-12-10 13:24:31 -05:00
{
2023-06-07 21:09:10 -04:00
clone.Pick = Data;
2022-12-10 14:45:15 -05:00
clone.Live = Reselect(clone, test);
clone.Draw =
{
Cross: Reposition(clone),
UserL: Redraw(test, 0, clone.Stim, true ),
UserR: Redraw(test, 1, clone.Stim, true ),
TestL: Redraw(test, 0, clone.Stim, false),
TestR: Redraw(test, 1, clone.Stim, false)
};
2023-06-07 19:31:10 -04:00
test.Done = Grade(test);
SaveTests(clone);
2022-12-10 14:45:15 -05:00
}
2022-11-28 18:42:41 -05:00
}
else if (Name == "Mark")
2022-11-28 10:00:18 -05:00
{
2022-12-03 19:05:26 -05:00
if(clone.Live.Test && clone.Live.Freq)
2022-11-28 18:42:41 -05:00
{
2022-12-10 00:33:38 -05:00
const key = clone.Chan.Value == 0 ? "UserL" : "UserR";
2023-06-14 22:32:21 -04:00
clone.Live.Mark.User = Data !== null ? {Stim:clone.Stim.Value, Resp:Data} : undefined;
clone.Live.Freq[key] = clone.Live.Mark.User;
2022-12-10 00:33:38 -05:00
clone.Draw[key] = Redraw(clone.Live.Test, clone.Chan.Value, clone.Stim, true);
2023-06-07 19:31:10 -04:00
clone.Live.Test.Done = Grade(clone.Live.Test);
2023-06-04 14:54:46 -04:00
SaveTests(clone);
2022-11-28 18:42:41 -05:00
}
}
else if( Name=="Stim" || Name=="Chan" || Name=="Freq")
{
2022-12-10 11:57:45 -05:00
const tone = clone[Name];
2022-12-03 21:40:23 -05:00
tone.Value += Data*tone.Step;
2022-12-10 09:56:35 -05:00
tone.Value = Math.max(tone.Value, tone.Min);
tone.Value = Math.min(tone.Value, tone.Max);
2022-12-10 13:24:31 -05:00
clone.Draw.Cross = Reposition(clone);
2022-12-10 11:57:45 -05:00
if(Name != "Stim")
{
clone.Live = Reselect(clone);
}
2022-11-27 23:56:37 -05:00
}
2023-06-07 19:30:56 -04:00
else if (Name == "Errs")
{
clone.Errs = Data;
}
else if (Name == "Kill")
{
2023-06-07 19:31:10 -04:00
if(clone.Live.Test)
2023-06-07 19:30:56 -04:00
{
2023-06-07 19:31:10 -04:00
clone.Live.Test.Plot.forEach(freq=>
{
freq.UserL = undefined;
freq.UserR = undefined;
});
clone.Draw["UserL"] = Redraw(clone.Live.Test, clone.Chan.Value, clone.Stim, true);
clone.Draw["UserR"] = Redraw(clone.Live.Test, clone.Chan.Value, clone.Stim, true);
clone.Live.Test.Done = Grade(clone.Live.Test);
SaveTests(clone);
}
2023-06-07 19:30:56 -04:00
}
2022-12-12 15:57:49 -05:00
else if (Name == "ShowCursor")
{
clone.Show.Cursor = Data;
}
else if (Name == "ShowAnswer")
{
clone.Show.Answer = Data;
}
2022-11-28 18:42:41 -05:00
2023-06-14 22:53:59 -04:00
ErrorProbability(clone);
2023-06-07 21:09:10 -04:00
SaveSettings(clone);
document.dispatchEvent(new CustomEvent("EarmarkUpdate", {detail:clone}));
2022-11-27 23:56:37 -05:00
return clone;
2022-12-05 23:22:37 -05:00
}
2023-06-04 14:54:46 -04:00
/** @type {Store.Test[]} */
const TestDefault = [
{
Name: "Patient A Asymmetric Notch",
Plot:
[
{ Hz: 500, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 1000, TestL: { Stim: 10, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 2000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 20, Resp: true } },
{ Hz: 3000, TestL: { Stim: 30, Resp: true }, TestR: { Stim: 40, Resp: true } },
{ Hz: 4000, TestL: { Stim: 40, Resp: true }, TestR: { Stim: 55, Resp: true } },
{ Hz: 6000, TestL: { Stim: 35, Resp: true }, TestR: { Stim: 40, Resp: true } },
{ Hz: 8000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 15, Resp: true } }
]
},
{
Name: "Patient B High Freq Hearing Loss",
Plot:
[
{ Hz: 500, TestL: { Stim: 10, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 1000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 2000, TestL: { Stim: 10, Resp: true }, TestR: { Stim: 15, Resp: true } },
{ Hz: 3000, TestL: { Stim: 25, Resp: true }, TestR: { Stim: 20, Resp: true } },
{ Hz: 4000, TestL: { Stim: 35, Resp: true }, TestR: { Stim: 35, Resp: true } },
{ Hz: 6000, TestL: { Stim: 50, Resp: true }, TestR: { Stim: 55, Resp: true } },
{ Hz: 8000, TestL: { Stim: 80, Resp: true }, TestR: { Stim: 75, Resp: true } }
]
},
{
Name: "Patient C Unilateral Hearing Loss",
Plot:
[
{ Hz: 500, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 40, Resp: true } },
{ Hz: 1000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 50, Resp: true } },
{ Hz: 2000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 65, Resp: true } },
{ Hz: 3000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 70, Resp: true } },
{ Hz: 4000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 65, Resp: true } },
{ Hz: 6000, TestL: { Stim: 25, Resp: true }, TestR: { Stim: 60, Resp: true } },
{ Hz: 8000, TestL: { Stim: 20, Resp: true }, TestR: { Stim: 45, Resp: true } }
]
},
{
Name: "Patient D Normal Hearing",
Plot:
[
{ Hz: 500, TestL: { Stim: 5, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 1000, TestL: { Stim: 0, Resp: true }, TestR: { Stim: 5, Resp: true } },
{ Hz: 2000, TestL: { Stim: 5, Resp: true }, TestR: { Stim: 5, Resp: true } },
{ Hz: 3000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 4000, TestL: { Stim: 15, Resp: true }, TestR: { Stim: 15, Resp: true } },
{ Hz: 6000, TestL: { Stim: 5, Resp: true }, TestR: { Stim: 10, Resp: true } },
{ Hz: 8000, TestL: { Stim: 0, Resp: true }, TestR: { Stim: 5, Resp: true } }
]
}
];
let PreviewData = false;
const PreviewText = atob(new URL(window.location.href).searchParams.get("test")||"");
if(PreviewText)
{
try
{
PreviewData = JSON.parse(PreviewText);
}
catch(e)
{
alert(`Could not read test data`);
PreviewData = false;
}
}
const AppVersion = `0.0.0`;
const savedVersion = localStorage.getItem("app-version");
if(savedVersion && (AppVersion > savedVersion))
{
console.log(`New version "${AppVersion}"; clearing saved session.`);
localStorage.clear();
}
localStorage.setItem("app-version", AppVersion);
2023-06-07 21:09:10 -04:00
/** @type {Store.Test[]} */
const TestActual = PreviewData ? PreviewData : JSON.parse(localStorage.getItem("app-tests")||"false") || TestDefault;
2023-06-04 14:54:46 -04:00
/**@type {(inState:Store.State)=>void} */
const SaveTests =(inState)=>
{
if(!PreviewData)
{
localStorage.setItem("app-tests", JSON.stringify(inState.Test));
}
}
2023-06-04 14:54:46 -04:00
2023-06-07 21:09:10 -04:00
/** @type {Store.StatePartSimple} */
const SettingsDefault =
{
Chan: { Min:0, Max:1, Value:0, Step:1 },
Freq: { Min:2, Max:8, Value:3, Step:1 },
Stim: { Min:-10, Max:120, Value:30, Step:5 },
Errs: 0,
Pick: 0,
Show: { Cursor:true, Answer:false }
};
/** @type {Store.StatePartSimple} */
const SettingsActual = PreviewData ? SettingsDefault : JSON.parse(localStorage.getItem("app-settings")||"false") || SettingsDefault;
2023-06-04 14:54:46 -04:00
/**@type {(inState:Store.State)=>void} */
2023-06-07 19:31:10 -04:00
const SaveSettings =(inState)=>
{
if(!PreviewData)
{
/** @type {Store.StatePartSimple} */
const clone = {
Chan:inState.Chan,
Freq:inState.Freq,
Stim:inState.Stim,
Errs:inState.Errs,
Pick:inState.Pick,
Show:inState.Show
};
localStorage.setItem("app-settings", JSON.stringify(clone));
}
2023-06-07 21:09:10 -04:00
};
2023-06-04 14:54:46 -04:00
2022-12-10 13:24:31 -05:00
export const Initial = Reducer(
2022-12-10 09:56:35 -05:00
{
2023-06-07 21:09:10 -04:00
...SettingsActual,
Test: TestActual,
2022-12-10 09:56:35 -05:00
Live:
{
Test: undefined,
Freq: undefined,
Mark: {User: undefined, Errs:0}
2022-12-10 09:56:35 -05:00
},
Draw:
{
UserL:{Points:[], Paths:[]},
UserR:{Points:[], Paths:[]},
TestL:{Points:[], Paths:[]},
TestR:{Points:[], Paths:[]}
2023-06-07 21:09:10 -04:00
}
}, {Name:"Test", Data:SettingsActual.Pick});
2022-12-05 23:22:37 -05:00
2023-06-03 11:38:52 -04:00
export const Context = React.createContext(/** @type {Store.Binding} */([Initial, (_a)=>{}]));
2022-12-10 09:56:35 -05:00
2022-12-05 23:22:37 -05:00
/** @type {(props:{children:preact.ComponentChildren})=>preact.VNode} */
export const Provider =(props)=>
{
/** @type {Store.Binding} */
2022-12-10 13:24:31 -05:00
const reducer = React.useReducer(Reducer, Initial);
2022-12-05 23:22:37 -05:00
return React.createElement(Context.Provider, {value:reducer, children:props.children});
2022-12-10 09:56:35 -05:00
};
2022-12-05 23:22:37 -05:00
/** @type {()=>Store.Binding} */
2023-06-07 19:31:10 -04:00
export const Consumer =()=> React.useContext(Context);