agenda: text now only updates when the fetch is successful

This commit is contained in:
Tigor Hutasuhut 2024-11-24 09:44:47 +07:00
parent 0f91ff34ab
commit 847555fe05
3 changed files with 24 additions and 19 deletions

View file

@ -36,14 +36,13 @@
killall gjs || true killall gjs || true
zellij delete-session --force ags || true zellij delete-session --force ags || true
''; '';
agsPkg = ags.packages.${system};
in in
{ {
packages.${system} = { packages.${system} = {
default = ags.lib.bundle { default = ags.lib.bundle {
inherit pkgs; inherit pkgs;
src = ./.; src = ./.;
name = "ags-calendar"; name = "ags-agenda";
entry = "app.ts"; entry = "app.ts";
# additional libraries and executables to add to gjs' runtime # additional libraries and executables to add to gjs' runtime

View file

@ -12,10 +12,10 @@ window.Agenda {
> box { > box {
/* From https://css.glass */ /* From https://css.glass */
font-size: 1.8em; font-size: 1.8em;
text-shadow: 0px 0px 4px #000000; text-shadow: 0px 0px 3em #000000;
background: rgba(0, 0, 0, 0.2); background: rgba(0, 0, 0, 0.2);
border-radius: 16px; border-radius: 16px;
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1); box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
border: 1px solid rgba(0, 0, 0, 0.3); border: 1px solid rgba(0, 0, 0, 0.3);
padding: 24px; padding: 24px;
} }

View file

@ -55,21 +55,27 @@ function createAgenda(agendaItems: AgendaItem[]): string {
return result.trim(); return result.trim();
} }
async function fetchAgenda() {
const output = await execAsync(["gcalcli", "agenda", "--tsv"]);
const items = output.trim().split("\n").map(transform);
return createAgenda(items);
}
export default function Agenda(gdkmonitor: Gdk.Monitor) { export default function Agenda(gdkmonitor: Gdk.Monitor) {
const agenda = Variable([] as AgendaItem[]); const text = Variable("");
const watch = () => {
agenda.set([]); const fetchAgendaData = () => {
agenda.stopWatch(); fetchAgenda()
agenda.watch(["gcalcli", "agenda", "--tsv"], (stdout, prev) => [ .then((agenda) => text.set(agenda))
...prev, .catch((err) => {
transform(stdout), text.set(`failed to fetch agenda: ${err.message}`);
]); timeout(5000, fetchAgendaData);
});
}; };
agenda.onError((err) => { const intervalHandler = interval(
console.error(`failed to fetch agenda: ${err}`); 1000 * 60 * 10 /* 10 minutes */,
timeout(5000, watch); fetchAgendaData,
}); );
const intervalHandler = interval(1000 * 60 * 10 /* 10 minutes */, watch);
return ( return (
<window <window
className="Agenda" className="Agenda"
@ -88,10 +94,10 @@ export default function Agenda(gdkmonitor: Gdk.Monitor) {
<box <box
onDestroy={() => { onDestroy={() => {
intervalHandler.cancel(); intervalHandler.cancel();
agenda.drop(); text.drop();
}} }}
> >
{bind(agenda).as(createAgenda)} {bind(text)}
</box> </box>
</button> </button>
</window> </window>