Skip to content

Commit c5fb466

Browse files
committed
[frontend] add puter sign-in
1 parent 5c98a8f commit c5fb466

File tree

2 files changed

+95
-7
lines changed

2 files changed

+95
-7
lines changed

frontend/src/main.tsx

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ let app = document.getElementById("app")!;
99
import { Shell } from "./components/Shell";
1010
import { App } from "./App";
1111
import { startCDP } from "./CDP";
12+
import { css, type Component } from "dreamland/core";
1213

1314
const { ScramjetController } = $scramjetLoadController();
14-
export const scramjet = new ScramjetController({
15+
16+
const scramjetcfg = {
1517
wisp: "ws://localhost:1337/",
1618
files: {
1719
wasm: "/scram/scramjet.wasm.wasm",
@@ -21,21 +23,85 @@ export const scramjet = new ScramjetController({
2123
flags: {
2224
rewriterLogs: false,
2325
captureErrors: false,
24-
naiiveRewriter: false,
2526
},
2627
siteFlags: {
27-
"https://www.google.com/(search|sorry).*": {
28-
naiiveRewriter: true,
29-
},
3028
"https://worker-playground.glitch.me/.*": {
3129
serviceworkers: true,
3230
},
3331
},
34-
});
32+
};
33+
34+
export const scramjet = new ScramjetController(scramjetcfg);
35+
36+
export function setWispUrl(wispurl: string) {
37+
scramjetcfg.wisp = wispurl;
38+
scramjet.modifyConfig(scramjetcfg);
39+
}
40+
let signedinr: any;
41+
let signedin = new Promise((resolve) => (signedinr = resolve));
42+
export const SignIn: Component<{}, { status: string }> = function (cx) {
43+
this.status = "";
44+
45+
function handleModalClose(modal: any) {
46+
modal.style.opacity = 0;
47+
setTimeout(() => {
48+
modal.close();
49+
modal.style.opacity = 1;
50+
}, 250);
51+
}
52+
53+
const signin = async () => {
54+
this.status = "Signing in...";
55+
try {
56+
await puter.auth.signIn();
57+
this.status = "Signed in successfully!";
58+
signedinr();
59+
handleModalClose(cx.root);
60+
} catch (e: any) {
61+
console.log(e);
62+
this.status = "Error signing in: " + e.message;
63+
return;
64+
}
65+
};
66+
67+
return (
68+
<dialog class="signin">
69+
<h1>Sign In</h1>
70+
<br></br>
71+
<p>Sign in with Puter</p>
72+
73+
<button on:click={signin}>Sign In</button>
74+
<p>{use(this.status)}</p>
75+
</dialog>
76+
);
77+
};
78+
SignIn.style = css`
79+
:scope {
80+
transition: opacity 0.4s ease;
81+
width: 50%;
82+
height: 20%;
83+
border: none;
84+
border-radius: 1em;
85+
text-align: center;
86+
}
87+
h1 {
88+
text-align: center;
89+
font-weight: bold;
90+
font-size: 2em;
91+
}
92+
:modal[open] {
93+
animation: fade 0.4s ease normal;
94+
}
95+
96+
:modal::backdrop {
97+
backdrop-filter: blur(3px);
98+
}
99+
`;
35100

36101
export let browser: Browser;
102+
declare var puter: any;
37103

38-
function mount() {
104+
async function mount() {
39105
try {
40106
let shell = <Shell></Shell>;
41107
browser = new Browser();
@@ -54,6 +120,20 @@ function mount() {
54120
e.preventDefault();
55121
});
56122

123+
if (!import.meta.env.LOCAL) {
124+
if (!puter.auth.isSignedIn()) {
125+
const signin: any = <SignIn></SignIn>;
126+
document.body.append(signin);
127+
signin.showModal();
128+
await signedin;
129+
return;
130+
}
131+
132+
let wisp = await puter.net.generateWispV1URL();
133+
setWispUrl(wisp);
134+
console.log(wisp);
135+
}
136+
57137
// let playwrightWindow = window.open(
58138
// "http://localhost:5013",
59139
// "playwright",

frontend/src/vite-env.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
/// <reference types="vite/client" />
22
/// <reference types="@mercuryworkshop/scramjet" />
3+
4+
interface ImportMetaEnv {
5+
readonly LOCAL: boolean;
6+
}
7+
8+
interface ImportMeta {
9+
readonly env: ImportMetaEnv;
10+
}

0 commit comments

Comments
 (0)