Skip to content

Commit

Permalink
feat(ceremony): rejoin queue
Browse files Browse the repository at this point in the history
  • Loading branch information
Swepool committed Oct 3, 2024
1 parent 05f8973 commit 8c19dc8
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
95 changes: 90 additions & 5 deletions ceremony/src/lib/components/Terminal/Missed.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,101 @@ import { onDestroy, onMount } from "svelte"
import { getState } from "$lib/state/index.svelte.ts"
import { axiom } from "$lib/utils/axiom.ts"
import { user } from "$lib/state/session.svelte.ts"
import { sleep } from "$lib/utils/utils.ts"
import { queryContributionWindow } from "$lib/supabase/queries.ts"
import Buttons from "$lib/components/Terminal/Install/Buttons.svelte"
import { rejoin } from "$lib/supabase"
const { terminal } = getState()
const { terminal, contributor } = getState()
let showButtons = $state(false)
onMount(() => {
onMount(async () => {
terminal.setStep(0)
terminal.updateHistory({ text: "Too bad, you missed your slot.", replace: true })
axiom.ingest("monitor", [{ user: user.session?.user.id, type: "missed" }])
contributor.stopPolling()
axiom.ingest("monitor", [{ user: user.session?.user.id, type: "mount_missed" }])
const userId = user.session?.user.id
if (userId) {
const window = await queryContributionWindow(userId)
if (window?.data?.started && window?.data?.expire) {
const formatDate = (date: string | number | Date): string => {
const d = new Date(date)
const pad = (num: number): string => num.toString().padStart(2, "0")
return `${d.getFullYear()}/${pad(d.getMonth() + 1)}/${pad(d.getDate())} - ${pad(d.getHours())}:${pad(d.getMinutes())}`
}
const startFormatted = formatDate(window.data.started)
const expireFormatted = formatDate(window.data.expire)
terminal.updateHistory({ text: "Looking for active slot..", replace: true })
await sleep(1000)
terminal.updateHistory({ text: "Expired slot found...", replace: true })
await sleep(1000)
terminal.updateHistory({
text: `Your slot started at ${startFormatted} and expired at ${expireFormatted}.`,
replace: true
})
await sleep(1000)
showButtons = true
} else {
terminal.updateHistory({ text: "No active slot found.", replace: true })
showButtons = true
}
}
})
onDestroy(() => {
terminal.clearHistory()
})
</script>
async function trigger(value: "retry" | "help") {
if (value === "retry") {
terminal.updateHistory({ text: "Retrying..." })
await sleep(1000)
terminal.updateHistory({ text: "Clearing old user data..." })
localStorage.removeItem("ceremony:show-boot-sequence")
localStorage.removeItem("downloaded-secret")
const bootSequenceCleared = !localStorage.getItem("ceremony:show-boot-sequence")
const secretCleared = !localStorage.getItem("downloaded-secret")
if (bootSequenceCleared && secretCleared) {
await sleep(1000)
terminal.updateHistory({ text: "Successfully cleared user data." })
await sleep(1000)
terminal.updateHistory({ text: "Attempting to add user to queue..." })
const rejoined = await rejoin()
await sleep(1000)
if (rejoined) {
terminal.updateHistory({ text: "Successfully added user to queue" })
await sleep(1000)
terminal.updateHistory({ text: "Redirecting..." })
await sleep(1000)
window.location.href = "/"
} else {
terminal.updateHistory({ text: "Failed to add user to queue." })
await sleep(1000)
terminal.updateHistory({ text: "Please contact support." })
window.location.href = "/"
}
} else {
terminal.updateHistory({ text: "Failed to clear user data." })
await sleep(1000)
terminal.updateHistory({ text: "Please contact support." })
}
} else if (value === "help") {
terminal.updateHistory({ text: "Redirecting to discord..." })
await sleep(1000)
window.open("https://discord.union.build/", "_blank")
}
}
</script>

{#if showButtons}
<Buttons data={[{text: "Retry", action: "retry"}, {text: "I need help", action: "help"}]} trigger={(action) => trigger(action)}/>
{/if}
20 changes: 20 additions & 0 deletions ceremony/src/lib/supabase/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,23 @@ export const getAverageTimes = async (): Promise<TimeResult> => {
totalMs
}
}

export const rejoin = async () => {
if (!user.session) {
throw new Error("User is not logged in")
}

try {
const { error } = await supabase.rpc("rejoin_queue")

if (error) {
console.log("Error joining queue:", error)
return false
}

return true
} catch (err) {
console.log("Unexpected error:", err)
return false
}
}

0 comments on commit 8c19dc8

Please sign in to comment.