TOTPFit/setting/index.js

165 lines
3.8 KiB
JavaScript
Raw Normal View History

import { getTOTPByLink } from './utils/queryParser.js'
2024-12-21 22:51:36 +03:00
let _props = null;
AppSettingsPage({
build(props) {
2024-12-21 22:51:36 +03:00
_props = props;
2025-02-09 20:54:10 +03:00
2025-02-25 21:05:50 +03:00
console.log(props.settingsStorage.getItem("TOTPs"))
const storage = JSON.parse(props.settingsStorage.getItem("TOTPs") ?? "[]")
const totpEntrys = GetTOTPList(storage)
const createButton = TextInput({
placeholder: "otpauth://",
label: "Add new OTP Link",
onChange: (changes) => {
2025-02-09 20:54:10 +03:00
var link = getTOTPByLink(changes)
if(link == null){
console.log("link is invalid")
return;
}
storage.push(link)
updateStorage(storage)
},
labelStyle: {
backgroundColor: "#14213D",
display: "flex",
alignItems: "center",
justifyContent: "center",
margin: "10px",
flexGrow: 1,
fontSize: "20px",
color: "#FFFFFF",
borderRadius: "5px"
}
});
var body = Section(
{
style: {
backgroundColor: "black",
minHeight: "100vh",
},
},
[
View(
{
style: {
textAlign: "center",
},
},
Text(
{
align: "center",
paragraph: true,
style: {
marginBottom: "10px",
color: "#fff",
fontSize: 23,
verticalAlign: "middle",
},
},
"TOTPS:"
)
),
...totpEntrys,
createButton
]
);
return body;
},
});
function GetTOTPList(storage){
let totpEntrys = [];
let counter = 0;
storage.forEach((element) => {
2024-12-21 22:51:36 +03:00
const elementId = counter;
const textInput = TextInput({
placeholder: "otpauth://",
label: "Change OTP link",
onChange: (changes) => {
2024-12-30 02:08:50 +03:00
try{
storage[elementId] = getTOTPByLink(changes)
updateStorage(storage)
2024-12-30 02:08:50 +03:00
}
catch(err){
console.log(err)
}
},
labelStyle: {
backgroundColor: "#14213D",
textAlign: "center",
2024-12-21 22:51:36 +03:00
display: "flex",
alignItems: "center",
justifyContent: "center",
margin: "10px",
2024-12-21 22:51:36 +03:00
flexGrow: 1,
fontSize: "20px",
color: "#E5E5E5",
borderRadius: "5px"
},
});
const textBig = Text(
{
align: "center",
style: {
color: "#ffffff",
fontSize: "16px"
},
paragraph: true,
},
`${element.issuer}: ${element.client}`
);
const delButton = Button(
{
onClick: () => {
2024-12-21 22:51:36 +03:00
storage = storage.filter(x => storage.indexOf(x) != elementId)
updateStorage(storage)
},
style: {
backgroundColor: "#ba181b",
2024-12-21 22:51:36 +03:00
fontSize: "18px",
color: "#ffffff",
height: "fit-content",
margin: "10px"
},
label: "DEL"
}
);
const text = Text(
{
style: {
color: "#ffffff",
fontSize: "14px"
},
align: "center"
},
`${element.hashType} | ${element.digits} digits | ${element.fetchTime} seconds | offset ${element.timeOffset} seconds`
);
const view = View(
{
style: {
textAlign: "center",
border: "2px solid white",
borderRadius: "5px",
margin: "10px"
},
},
[textBig, text, View({style: {
2024-12-21 22:51:36 +03:00
display: "grid",
gridTemplateColumns: "1fr 100px"
}}, [textInput, delButton])]
);
totpEntrys.push({ text: text, view: view });
counter++;
});
return totpEntrys.map(x => x.view);
2024-12-21 22:51:36 +03:00
}
function updateStorage(storage){
console.log("new storage is:")
console.log(storage)
2025-02-25 21:05:50 +03:00
_props.settingsStorage.setItem('TOTPs', JSON.stringify(storage))
}