Convert all remaining files to typescript.

This commit is contained in:
Jas Singh
2024-07-26 23:11:33 -07:00
parent ca5dcc629b
commit b511d76e11
84 changed files with 2075 additions and 1987 deletions

View File

@@ -1,23 +0,0 @@
const getWifiIcon = (iconName) => {
const deviceIconMap = [
["network-wireless-acquiring", "󰤩"],
["network-wireless-connected", "󰤨"],
["network-wireless-encrypted", "󰤪"],
["network-wireless-hotspot", "󰤨"],
["network-wireless-no-route", "󰤩"],
["network-wireless-offline", "󰤮"],
["network-wireless-signal-excellent", "󰤨"],
["network-wireless-signal-good", "󰤥"],
["network-wireless-signal-ok", "󰤢"],
["network-wireless-signal-weak", "󰤟"],
["network-wireless-signal-none", "󰤯"],
];
const foundMatch = deviceIconMap.find((icon) =>
RegExp(icon[0]).test(iconName.toLowerCase()),
);
return foundMatch ? foundMatch[1] : "󰤨";
};
export { getWifiIcon };

View File

@@ -0,0 +1,23 @@
const getWifiIcon = (iconName: string) => {
const deviceIconMap = [
["network-wireless-acquiring", "󰤩"],
["network-wireless-connected", "󰤨"],
["network-wireless-encrypted", "󰤪"],
["network-wireless-hotspot", "󰤨"],
["network-wireless-no-route", "󰤩"],
["network-wireless-offline", "󰤮"],
["network-wireless-signal-excellent", "󰤨"],
["network-wireless-signal-good", "󰤥"],
["network-wireless-signal-ok", "󰤢"],
["network-wireless-signal-weak", "󰤟"],
["network-wireless-signal-none", "󰤯"],
];
const foundMatch = deviceIconMap.find((icon) =>
RegExp(icon[0]).test(iconName.toLowerCase()),
);
return foundMatch ? foundMatch[1] : "󰤨";
};
export { getWifiIcon };

View File

@@ -1,97 +0,0 @@
const renderWapStaging = (self, network, staging, connecting) => {
Utils.merge([network.bind("wifi"), staging.bind("value")], () => {
if (!Object.keys(staging.value).length) {
return (self.child = Widget.Box());
}
return (self.child = Widget.Box({
class_name: "network-element-item staging",
vertical: true,
children: [
Widget.Box({
hpack: "fill",
hexpand: true,
children: [
Widget.Icon({
class_name: `network-icon wifi`,
icon: `${staging.value.iconName}`,
}),
Widget.Box({
class_name: "connection-container",
hexpand: true,
vertical: true,
children: [
Widget.Label({
class_name: "active-connection",
hpack: "start",
truncate: "end",
wrap: true,
label: staging.value.ssid,
}),
],
}),
Widget.Revealer({
hpack: "end",
reveal_child: connecting
.bind("value")
.as((c) => staging.value.bssid === c),
child: Widget.Spinner({
class_name: "spinner wap",
}),
}),
],
}),
Widget.Box({
class_name: "network-password-input-container",
hpack: "fill",
hexpand: true,
children: [
Widget.Entry({
hpack: "start",
hexpand: true,
visibility: false,
class_name: "network-password-input",
placeholder_text: "enter password",
onAccept: (selfInp) => {
connecting.value = staging.value.bssid;
Utils.execAsync(
`nmcli dev wifi connect ${staging.value.bssid} password ${selfInp.text}`,
)
.catch((err) => {
connecting.value = "";
console.error(
`Failed to connect to wifi: ${staging.value.ssid}... ${err}`,
);
Utils.notify({
summary: "Network",
body: err,
timeout: 5000,
});
})
.then(() => {
connecting.value = "";
staging.value = {};
});
selfInp.text = "";
},
}),
Widget.Button({
hpack: "end",
class_name: "close-network-password-input-button",
on_primary_click: () => {
connecting.value = "";
staging.value = {};
},
child: Widget.Icon({
class_name: "close-network-password-input-icon",
icon: "window-close-symbolic",
}),
}),
],
}),
],
}));
});
};
export { renderWapStaging };

View File

@@ -0,0 +1,100 @@
import { Network } from "types/service/network";
import { Variable } from "types/variable";
import { AccessPoint } from "lib/types/network";
const renderWapStaging = (self: any, network: Network, staging: Variable<AccessPoint>, connecting: Variable<string>) => {
Utils.merge([network.bind("wifi"), staging.bind("value")], () => {
if (!Object.keys(staging.value).length) {
return (self.child = Widget.Box());
}
return (self.child = Widget.Box({
class_name: "network-element-item staging",
vertical: true,
children: [
Widget.Box({
hpack: "fill",
hexpand: true,
children: [
Widget.Icon({
class_name: `network-icon wifi`,
icon: `${staging.value.iconName}`,
}),
Widget.Box({
class_name: "connection-container",
hexpand: true,
vertical: true,
children: [
Widget.Label({
class_name: "active-connection",
hpack: "start",
truncate: "end",
wrap: true,
label: staging.value.ssid,
}),
],
}),
Widget.Revealer({
hpack: "end",
reveal_child: connecting
.bind("value")
.as((c) => staging.value.bssid === c),
child: Widget.Spinner({
class_name: "spinner wap",
}),
}),
],
}),
Widget.Box({
class_name: "network-password-input-container",
hpack: "fill",
hexpand: true,
children: [
Widget.Entry({
hpack: "start",
hexpand: true,
visibility: false,
class_name: "network-password-input",
placeholder_text: "enter password",
onAccept: (selfInp) => {
connecting.value = staging.value.bssid || "";
Utils.execAsync(
`nmcli dev wifi connect ${staging.value.bssid} password ${selfInp.text}`,
)
.catch((err) => {
connecting.value = "";
console.error(
`Failed to connect to wifi: ${staging.value.ssid}... ${err}`,
);
Utils.notify({
summary: "Network",
body: err,
timeout: 5000,
});
})
.then(() => {
connecting.value = "";
staging.value = {} as AccessPoint;
});
selfInp.text = "";
},
}),
Widget.Button({
hpack: "end",
class_name: "close-network-password-input-button",
on_primary_click: () => {
connecting.value = "";
staging.value = {} as AccessPoint;
},
child: Widget.Icon({
class_name: "close-network-password-input-icon",
icon: "window-close-symbolic",
}),
}),
],
}),
],
}));
});
};
export { renderWapStaging };

View File

@@ -1,211 +0,0 @@
import { getWifiIcon } from "../utils.js";
const renderWAPs = (self, network, staging, connecting) => {
const getIdBySsid = (ssid, nmcliOutput) => {
const lines = nmcliOutput.trim().split("\n");
for (const line of lines) {
const columns = line.trim().split(/\s{2,}/);
if (columns[0].includes(ssid)) {
return columns[1];
}
}
return null;
};
const WifiStatusMap = {
unknown: "Status Unknown",
unmanaged: "Unmanaged",
unavailable: "Unavailable",
disconnected: "Disconnected",
prepare: "Preparing Connecting",
config: "Connecting",
need_auth: "Needs Authentication",
ip_config: "Requesting IP",
ip_check: "Checking Access",
secondaries: "Waiting on Secondaries",
activated: "Connected",
deactivating: "Disconnecting",
failed: "Connection Failed",
};
self.hook(network, () => {
Utils.merge([staging.bind("value"), connecting.bind("value")], () => {
// Sometimes the network service will yield a "this._device is undefined" when
// trying to access the "access_points" property. So we must validate that
// it's not 'undefined'
let WAPs =
network.wifi._device !== undefined ? network.wifi["access-points"] : [];
const dedupeWAPs = () => {
const dedupMap = {};
WAPs.forEach((item) => {
if (!Object.hasOwnProperty.call(dedupMap, item.ssid)) {
dedupMap[item.ssid] = item;
}
});
return Object.keys(dedupMap).map((itm) => dedupMap[itm]);
};
WAPs = dedupeWAPs();
const isInStaging = (wap) => {
if (Object.keys(staging.value).length === 0) {
return false;
}
return wap.bssid === staging.value.bssid;
};
const isDisconnecting = (wap) => {
if (wap.ssid === network.wifi.ssid) {
return network.wifi.state.toLowerCase() === "deactivating";
}
return false;
};
const filteredWAPs = WAPs.filter((ap) => {
return ap.ssid !== "Unknown" && !isInStaging(ap);
}).sort((a, b) => {
if (network.wifi.ssid === a.ssid) {
return -1;
}
if (network.wifi.ssid === b.ssid) {
return 1;
}
return b.strength - a.strength;
});
if (filteredWAPs.length <= 0 && Object.keys(staging.value).length === 0) {
return (self.child = Widget.Label({
class_name: "waps-not-found dim",
expand: true,
hpack: "center",
vpack: "center",
label: "No Wi-Fi Networks Found",
}));
}
return (self.children = filteredWAPs.map((ap) => {
return Widget.Box({
children: [
Widget.Button({
on_primary_click: () => {
if (ap.bssid === connecting.value || ap.active) {
return;
}
connecting.value = ap.bssid;
Utils.execAsync(`nmcli device wifi connect ${ap.bssid}`)
.then(() => {
connecting.value = "";
staging.value = {};
})
.catch((err) => {
if (
err
.toLowerCase()
.includes("secrets were required, but not provided")
) {
staging.value = ap;
} else {
Utils.notify({
summary: "Network",
body: err,
timeout: 5000,
});
}
connecting.value = "";
});
},
class_name: "network-element-item",
child: Widget.Box({
hexpand: true,
children: [
Widget.Box({
hpack: "start",
hexpand: true,
children: [
Widget.Label({
vpack: "start",
class_name: `network-icon wifi ${ap.ssid === network.wifi.ssid ? "active" : ""}`,
label: getWifiIcon(`${ap["iconName"]}`),
}),
Widget.Box({
class_name: "connection-container",
vpack: "center",
vertical: true,
children: [
Widget.Label({
vpack: "center",
class_name: "active-connection",
hpack: "start",
truncate: "end",
wrap: true,
label: ap.ssid,
}),
Widget.Revealer({
revealChild: ap.ssid === network.wifi.ssid,
child: Widget.Label({
hpack: "start",
class_name: "connection-status dim",
label:
WifiStatusMap[network.wifi.state.toLowerCase()],
}),
}),
],
}),
],
}),
Widget.Revealer({
hpack: "end",
vpack: "start",
reveal_child:
ap.bssid === connecting.value || isDisconnecting(ap),
child: Widget.Spinner({
vpack: "start",
class_name: "spinner wap",
}),
}),
],
}),
}),
Widget.Revealer({
vpack: "start",
reveal_child: ap.bssid !== connecting.value && ap.active,
child: Widget.Button({
tooltip_text: "Delete/Forget Network",
class_name: "menu-icon-button network disconnect",
on_primary_click: () => {
connecting.value = ap.bssid;
Utils.execAsync("nmcli connection show --active").then(() => {
Utils.execAsync("nmcli connection show --active").then(
(res) => {
const connectionId = getIdBySsid(ap.ssid, res);
Utils.execAsync(
`nmcli connection delete ${connectionId} "${ap.ssid}"`,
)
.then(() => (connecting.value = ""))
.catch((err) => {
connecting.value = "";
console.error(
`Error while forgetting "${ap.ssid}": ${err}`,
);
});
},
);
});
},
child: Widget.Label({
label: "󰚃",
}),
}),
}),
],
});
}));
});
});
};
export { renderWAPs };

View File

@@ -0,0 +1,216 @@
import { Network } from "types/service/network.js";
import { AccessPoint } from "lib/types/network.js";
import { Variable } from "types/variable.js";
import { getWifiIcon } from "../utils.js";
const renderWAPs = (self: any, network: Network, staging: Variable<AccessPoint>, connecting: Variable<string>) => {
const getIdBySsid = (ssid: string, nmcliOutput: string) => {
const lines = nmcliOutput.trim().split("\n");
for (const line of lines) {
const columns = line.trim().split(/\s{2,}/);
if (columns[0].includes(ssid)) {
return columns[1];
}
}
return null;
};
const WifiStatusMap = {
unknown: "Status Unknown",
unmanaged: "Unmanaged",
unavailable: "Unavailable",
disconnected: "Disconnected",
prepare: "Preparing Connecting",
config: "Connecting",
need_auth: "Needs Authentication",
ip_config: "Requesting IP",
ip_check: "Checking Access",
secondaries: "Waiting on Secondaries",
activated: "Connected",
deactivating: "Disconnecting",
failed: "Connection Failed",
};
self.hook(network, () => {
Utils.merge([staging.bind("value"), connecting.bind("value")], () => {
// Sometimes the network service will yield a "this._device is undefined" when
// trying to access the "access_points" property. So we must validate that
// it's not 'undefined'
//
// Also this is an AGS bug that needs to be fixed
let WAPs =
network.wifi._device !== undefined ? network.wifi["access-points"] : [];
const dedupeWAPs = () => {
const dedupMap = {};
WAPs.forEach((item: AccessPoint) => {
if (item.ssid !== null && !Object.hasOwnProperty.call(dedupMap, item.ssid)) {
dedupMap[item.ssid] = item;
}
});
return Object.keys(dedupMap).map((itm) => dedupMap[itm]);
};
WAPs = dedupeWAPs();
const isInStaging = (wap: AccessPoint) => {
if (Object.keys(staging.value).length === 0) {
return false;
}
return wap.bssid === staging.value.bssid;
};
const isDisconnecting = (wap: AccessPoint) => {
if (wap.ssid === network.wifi.ssid) {
return network.wifi.state.toLowerCase() === "deactivating";
}
return false;
};
const filteredWAPs = WAPs.filter((ap: AccessPoint) => {
return ap.ssid !== "Unknown" && !isInStaging(ap);
}).sort((a: AccessPoint, b: AccessPoint) => {
if (network.wifi.ssid === a.ssid) {
return -1;
}
if (network.wifi.ssid === b.ssid) {
return 1;
}
return b.strength - a.strength;
});
if (filteredWAPs.length <= 0 && Object.keys(staging.value).length === 0) {
return (self.child = Widget.Label({
class_name: "waps-not-found dim",
expand: true,
hpack: "center",
vpack: "center",
label: "No Wi-Fi Networks Found",
}));
}
return (self.children = filteredWAPs.map((ap: AccessPoint) => {
return Widget.Box({
children: [
Widget.Button({
on_primary_click: () => {
if (ap.bssid === connecting.value || ap.active) {
return;
}
connecting.value = ap.bssid || "";
Utils.execAsync(`nmcli device wifi connect ${ap.bssid}`)
.then(() => {
connecting.value = "";
staging.value = {} as AccessPoint;
})
.catch((err) => {
if (
err
.toLowerCase()
.includes("secrets were required, but not provided")
) {
staging.value = ap;
} else {
Utils.notify({
summary: "Network",
body: err,
timeout: 5000,
});
}
connecting.value = "";
});
},
class_name: "network-element-item",
child: Widget.Box({
hexpand: true,
children: [
Widget.Box({
hpack: "start",
hexpand: true,
children: [
Widget.Label({
vpack: "start",
class_name: `network-icon wifi ${ap.ssid === network.wifi.ssid ? "active" : ""}`,
label: getWifiIcon(`${ap["iconName"]}`),
}),
Widget.Box({
class_name: "connection-container",
vpack: "center",
vertical: true,
children: [
Widget.Label({
vpack: "center",
class_name: "active-connection",
hpack: "start",
truncate: "end",
wrap: true,
label: ap.ssid,
}),
Widget.Revealer({
revealChild: ap.ssid === network.wifi.ssid,
child: Widget.Label({
hpack: "start",
class_name: "connection-status dim",
label:
WifiStatusMap[network.wifi.state.toLowerCase()],
}),
}),
],
}),
],
}),
Widget.Revealer({
hpack: "end",
vpack: "start",
reveal_child:
ap.bssid === connecting.value || isDisconnecting(ap),
child: Widget.Spinner({
vpack: "start",
class_name: "spinner wap",
}),
}),
],
}),
}),
Widget.Revealer({
vpack: "start",
reveal_child: ap.bssid !== connecting.value && ap.active,
child: Widget.Button({
tooltip_text: "Delete/Forget Network",
class_name: "menu-icon-button network disconnect",
on_primary_click: () => {
connecting.value = ap.bssid || "";
Utils.execAsync("nmcli connection show --active").then(() => {
Utils.execAsync("nmcli connection show --active").then(
(res) => {
const connectionId = getIdBySsid(ap.ssid || "", res);
Utils.execAsync(
`nmcli connection delete ${connectionId} "${ap.ssid}"`,
)
.then(() => (connecting.value = ""))
.catch((err) => {
connecting.value = "";
console.error(
`Error while forgetting "${ap.ssid}": ${err}`,
);
});
},
);
});
},
child: Widget.Label({
label: "󰚃",
}),
}),
}),
],
});
}));
});
});
};
export { renderWAPs };

View File

@@ -1,72 +0,0 @@
const network = await Service.import("network");
import { renderWAPs } from "./WirelessAPs.js";
import { renderWapStaging } from "./APStaging.js";
const Staging = Variable({});
const Connecting = Variable("");
const searchInProgress = Variable(false);
const startRotation = () => {
searchInProgress.value = true;
setTimeout(() => {
searchInProgress.value = false;
}, 5 * 1000);
};
const Wifi = () => {
return Widget.Box({
class_name: "menu-section-container wifi",
vertical: true,
children: [
Widget.Box({
class_name: "menu-label-container",
hpack: "fill",
children: [
Widget.Label({
class_name: "menu-label",
hexpand: true,
hpack: "start",
label: "Wi-Fi",
}),
Widget.Button({
vpack: "center",
hpack: "end",
class_name: "menu-icon-button search network",
on_primary_click: () => {
startRotation();
network.wifi.scan();
},
child: Widget.Icon({
class_name: searchInProgress
.bind("value")
.as((v) => (v ? "spinning" : "")),
icon: "view-refresh-symbolic",
}),
}),
],
}),
Widget.Box({
class_name: "menu-items-section",
vertical: true,
children: [
Widget.Box({
class_name: "wap-staging",
setup: (self) => {
renderWapStaging(self, network, Staging, Connecting);
},
}),
Widget.Box({
class_name: "available-waps",
vertical: true,
setup: (self) => {
renderWAPs(self, network, Staging, Connecting);
},
}),
],
}),
],
});
};
export { Wifi };

View File

@@ -0,0 +1,73 @@
const network = await Service.import("network");
import { renderWAPs } from "./WirelessAPs.js";
import { renderWapStaging } from "./APStaging.js";
import { AccessPoint } from "lib/types/network.js";
const Staging = Variable({} as AccessPoint);
const Connecting = Variable("");
const searchInProgress = Variable(false);
const startRotation = () => {
searchInProgress.value = true;
setTimeout(() => {
searchInProgress.value = false;
}, 5 * 1000);
};
const Wifi = () => {
return Widget.Box({
class_name: "menu-section-container wifi",
vertical: true,
children: [
Widget.Box({
class_name: "menu-label-container",
hpack: "fill",
children: [
Widget.Label({
class_name: "menu-label",
hexpand: true,
hpack: "start",
label: "Wi-Fi",
}),
Widget.Button({
vpack: "center",
hpack: "end",
class_name: "menu-icon-button search network",
on_primary_click: () => {
startRotation();
network.wifi.scan();
},
child: Widget.Icon({
class_name: searchInProgress
.bind("value")
.as((v) => (v ? "spinning" : "")),
icon: "view-refresh-symbolic",
}),
}),
],
}),
Widget.Box({
class_name: "menu-items-section",
vertical: true,
children: [
Widget.Box({
class_name: "wap-staging",
setup: (self) => {
renderWapStaging(self, network, Staging, Connecting);
},
}),
Widget.Box({
class_name: "available-waps",
vertical: true,
setup: (self) => {
renderWAPs(self, network, Staging, Connecting);
},
}),
],
}),
],
});
};
export { Wifi };