Skip to content

Commit

Permalink
fix(ceremony): save progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Swepool committed Sep 13, 2024
1 parent 850e69e commit f0e833f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 45 deletions.
32 changes: 8 additions & 24 deletions ceremony/src/lib/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { get, post } from "$lib/client/http.ts"
import type { ContributeBody, ClientStatus } from "$lib/client/types.ts"
import { user } from "$lib/stores/user.svelte.ts"
import { getQueuePayloadId } from "$lib/supabase/queries.ts"
import type {ClientState} from "$lib/stores/state.svelte.ts";
import {get, post} from "$lib/client/http.ts"
import type {ClientState, ContributeBody} from "$lib/client/types.ts"
import {user} from "$lib/stores/user.svelte.ts"
import {getQueuePayloadId} from "$lib/supabase/queries.ts"

export const start = async (): Promise<ClientStatus | undefined> => {
export const start = async (): Promise<ClientState | undefined> => {
const userId = user?.session?.user.id
const email = user?.session?.user?.email

Expand Down Expand Up @@ -32,34 +31,19 @@ export const start = async (): Promise<ClientStatus | undefined> => {
supabaseProject: import.meta.env.VITE_SUPABASE_URL,
apiKey: import.meta.env.VITE_SUPABASE_ANON_KEY,
bucket: import.meta.env.VITE_BUCKET_ID,
userEmail: email
}

return post<ClientStatus>("contribute", {}, contributeBody)
return post<ClientState>("contribute", {}, contributeBody)
}


export const checkState = async (): Promise<ClientState> => {
try {
const response = await get<any>("contribute", {});
console.log(response);

if (typeof response === 'string') {
return response;
}

if (response && typeof response.status === 'string') {
return response.status;
}

if (Array.isArray(response) && typeof response[0] === 'string') {
return response[0];
}

throw new Error("Invalid response format. Status is undefined.");
return await get<ClientState>("contribute", {});
} catch (error) {
console.log('Error fetching status:', error);
return 'offline';
}
};


26 changes: 14 additions & 12 deletions ceremony/src/lib/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
export interface ClientStatus {
status: "idle" | "initializing" | "contributionStarted" | "contributionEnded" | "successful" | "offline"
downloadStarted?: string
downloading?: {
file: string
progress: number
}
downloadEnded?: string
uploadStarted?: string
uploadEnded?: string
failed?: string
}
export type ClientState =
| "idle"
| "initializing"
| "downloadStarted"
| "downloading"
| "downloadEnded"
| "contributionStarted"
| "contributionEnded"
| "uploadStarted"
| "uploadEnded"
| "failed"
| "successful"
| "offline"
| undefined

export interface ContributeBody {
supabaseProject: string
Expand Down
12 changes: 3 additions & 9 deletions ceremony/src/lib/stores/state.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {onDestroy} from "svelte";
import {checkState} from "$lib/client";
import {checkContributionState, getUserQueueInfo} from "$lib/supabase";
import type {ClientState} from "$lib/client/types.ts";

type IntervalID = NodeJS.Timeout | number;

Expand All @@ -21,13 +22,6 @@ export type ContributionState =
| 'verifying'
| 'notContributed'

export type ClientState =
"idle"
| "initializing"
| "contributionStarted"
| "contributionEnded"
| "successful"
| "offline"

interface UserContext {
position: number | null;
Expand Down Expand Up @@ -226,8 +220,8 @@ export class ContributorState {
} else if (this.contributionState === 'contribute') {
if (this.clientState === 'idle') {
this.state = 'contribute';
} else {
this.state = 'noClient'
} else if (this.clientState === 'contributionStarted') {
this.state = 'contributing'
}
} else if (this.queueState.position !== null) {
this.state = 'inQueue';
Expand Down
6 changes: 6 additions & 0 deletions ceremony/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@
}
})
window.addEventListener("beforeunload", (e: BeforeUnloadEvent) => {
e.preventDefault()
e.returnValue = ""
})
</script>

Expand Down
1 change: 1 addition & 0 deletions ceremony/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
let contributor: ContributorState = new ContributorState()
$effect(() => {
console.log(contributor.state)
const userId = user.session?.user.id
if (userId) contributor.setUserId(userId)
})
Expand Down

0 comments on commit f0e833f

Please sign in to comment.