Created
March 9, 2022 16:02
-
-
Save JoepKockelkorn/cc1dccf0cdc03bbca494d6afe1012124 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Menu: Watch slack status | |
| // Description: Open a refreshing widget which shows any slack incidents | |
| /** @type {import("@johnlindquist/kit")} */ | |
| const intervalInMinutes = 2; | |
| const intervalInMs = 2 * 60 * 1000; | |
| const { incidentTitle, updatedFormatted, nextUpdateFormatted } = await getData( | |
| intervalInMs | |
| ); | |
| const w = await widget( | |
| ` | |
| <div class="p-4"> | |
| <h1>Slack status</h1> | |
| <h2>{{ incidentTitle }}</h2> | |
| <p>Updated at: {{ updatedFormatted }}</p> | |
| <p>Next update: {{ nextUpdateFormatted }}</p> | |
| </div> | |
| `, | |
| { | |
| alwaysOnTop: true, | |
| state: { incidentTitle, updatedFormatted, nextUpdateFormatted }, | |
| } | |
| ); | |
| const interval = setInterval(async () => { | |
| const state = await getData(intervalInMs); | |
| w.setState(state); | |
| }, intervalInMs); | |
| w.onClose(() => clearInterval(interval)); | |
| async function getData(intervalInMs) { | |
| const { active_incidents: incidents } = ( | |
| await get("https://status.slack.com/api/v2.0.0/current") | |
| ).data; | |
| const updated = new Date(); | |
| const updatedFormatted = updated.toLocaleTimeString("nl-NL"); | |
| const nextUpdate = new Date(updated); | |
| nextUpdate.setMilliseconds(updated.getMilliseconds() + intervalInMs); | |
| const nextUpdateFormatted = nextUpdate.toLocaleTimeString("nl-NL"); | |
| const incidentTitle = incidents[0]?.title | |
| ? `⚠️ ${incidents[0].title}` | |
| : "All good in the hood 🤘"; | |
| return { incidentTitle, updatedFormatted, nextUpdateFormatted }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment