-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
629 lines (568 loc) · 23.3 KB
/
index.html
File metadata and controls
629 lines (568 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./output.css" />
<script type="module" src="./app.js"></script>
</head>
<body class="bg-white m-0 p-0 font-serif overflow-hidden">
<div
id="titlebar"
class="h-8 fixed top-0 left-0 w-full bg-secondary text-accent-foreground z-50"
>
<pear-ctrl></pear-ctrl>
<div
class="flex items-center justify-center h-8 absolute w-full top-0 left-0"
>
<h1>Pear Browser</h1>
</div>
</div>
<main
class="flex flex-col h-screen justify-start pt-8 bg-white text-foreground"
x-data="searchApp()"
>
<!-- Browser Header -->
<div class="bg-background border-b border-border px-4 relative">
<!-- Header with flex wrap layout -->
<div class="flex flex-wrap items-center gap-4 py-2">
<!-- Navigation Buttons (Back/Forward) -->
<div class="flex items-center z-10 gap-2">
<button
@click="goBack()"
class="p-2 rounded-full bg-secondary hover:bg-secondary/70 text-accent-foreground transition-colors cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
title="Go Back"
:disabled="!canGoBack"
>
<svg
class="w-5 h-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M15 19l-7-7 7-7"
></path>
</svg>
</button>
<button
@click="goForward()"
class="p-2 rounded-full bg-secondary hover:bg-secondary/70 text-accent-foreground transition-colors cursor-pointer disabled:opacity-50 disabled:cursor-not-allowed"
title="Go Forward"
:disabled="!canGoForward"
>
<svg
class="w-5 h-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M9 5l7 7-7 7"
></path>
</svg>
</button>
</div>
<!-- Search Bar: Natural flex item that wraps on small screens -->
<div
class="order-1 flex-[1_0_100%] md:flex-1 md:order-none md:absolute md:inset-0 md:flex md:items-center md:justify-center md:pointer-events-none"
>
<div class="w-full max-w-xl px-4 md:pointer-events-auto">
<form
@submit.prevent="handleSearch()"
class="relative flex items-center gap-2"
>
<img
src="./assets/logo.png"
alt="Pear Logo"
class="w-4 h-4 m-2"
/>
<input
x-model="searchQuery"
name="search"
type="text"
:placeholder="openApp ? openApp.url : 'Quick search...'"
class="flex-1 h-8 rounded-full px-3 py-1 text-sm border border-input focus:outline-none focus:ring-1 focus:ring-ring focus:border-transparent transition bg-card text-card-foreground"
/>
<button
@click="resetSearch()"
type="button"
class="p-2 rounded-full bg-secondary hover:bg-secondary/70 text-accent-foreground transition-colors cursor-pointer"
title="Home"
>
<svg
class="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6"
></path>
</svg>
</button>
</form>
</div>
</div>
<!-- Settings Icon (Top Right) -->
<div class="flex items-center gap-2 ml-auto">
<!-- App Info (when app is open) -->
<div
x-cloak
x-show="openApp !== null"
class="flex items-center gap-2"
>
<img
:src="openApp?.icon || './assets/logo.png'"
:alt="openApp?.name + ' icon'"
class="w-6 h-6 rounded object-cover"
@error="$el.src='./assets/logo.png'"
/>
<div class="min-w-0 flex-1">
<div
class="text-sm font-medium text-foreground truncate max-w-32"
x-text="openApp?.name || 'Unknown App'"
></div>
</div>
</div>
<button
@click="showHistoryModal = true; listHistory()"
class="p-2 rounded-full bg-secondary hover:bg-secondary/70 text-accent-foreground transition-colors cursor-pointer"
title="History"
>
<svg
class="w-5 h-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"
></path>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"
></path>
</svg>
</button>
</div>
</div>
</div>
<!-- History Modal -->
<div
x-show="showHistoryModal"
x-cloak
class="fixed inset-0 bg-black/20 backdrop-blur-sm z-50 flex items-center justify-center p-4"
@click.self="showHistoryModal = false"
>
<div class="bg-background border border-border/50 rounded-2xl shadow-standard max-w-3xl w-full max-h-[75vh] flex flex-col">
<!-- Modal Header -->
<div class="flex items-center justify-between p-6 border-b border-border/30">
<h2 class="text-xl font-medium text-foreground">History</h2>
<button
@click="showHistoryModal = false"
class="p-2 rounded-full bg-secondary hover:bg-primary/70 text-primary-foreground hover:text-foreground transition-colors cursor-pointer"
>
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path>
</svg>
</button>
</div>
<!-- Modal Content -->
<div class="flex-1 overflow-y-auto p-6">
<div x-show="!historyList || historyList.length === 0" class="text-center py-12">
<svg class="w-12 h-12 text-muted-foreground mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<p class="text-muted-foreground text-sm">No history found</p>
</div>
<div x-show="historyList && historyList.length > 0" class="space-y-3">
<template x-for="(entry, index) in historyList" :key="index">
<div
@click="entry.openApp ? loadApp(entry.openApp) : (searchQuery = entry.searchQuery, handleSearch()); showHistoryModal = false"
class="group border border-border/30 rounded-xl p-4 hover:bg-muted/50 hover:border-primary/30 transition-all duration-200 cursor-pointer"
>
<div class="flex items-start">
<div class="flex-1 min-w-0">
<!-- App Entry Content -->
<div x-show="entry.openApp" class="flex items-center gap-3">
<img
:src="entry.openApp?.icon || './assets/logo.png'"
:alt="(entry.openApp?.name || 'Unknown App') + ' icon'"
class="w-5 h-5 rounded object-cover flex-shrink-0"
@error="$el.src='./assets/logo.png'"
/>
<div class="flex-1 min-w-0">
<div class="flex items-center gap-2 mb-1">
<span class="font-medium text-foreground truncate" x-text="entry.openApp?.name || 'Unknown App'"></span>
<span class="text-xs bg-primary/10 text-primary px-2 py-0.5 rounded-full flex-shrink-0" x-text="entry.openApp?.type || 'app'"></span>
</div>
<div class="text-xs text-muted-foreground truncate" x-text="entry.openApp?.url || entry.openApp?.description || 'No description'"></div>
</div>
</div>
<!-- Search Entry Content -->
<div x-show="entry.searchQuery && !entry.openApp" class="flex items-center gap-2">
<svg class="w-4 h-4 text-primary flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path>
</svg>
<span class="font-medium text-foreground truncate" x-text="entry.searchQuery"></span>
</div>
</div>
</div>
</div>
</template>
</div>
</div>
<!-- Modal Footer -->
<div class="flex items-center justify-between p-6 border-t border-border/30">
<div class="text-xs text-muted-foreground">
<span x-text="historyList ? historyList.length : 0"></span> entries
</div>
</div>
</div>
</div>
<!-- Main Content Area -->
<div class="flex-1">
<!-- Search Results (when no app is open) -->
<div x-cloak x-show="openApp === null && isReady" class="h-full">
<!-- Welcome Screen -->
<div
x-show="!isSearching"
class="flex flex-col items-center justify-center h-full"
>
<img
src="./assets/logo.png"
alt="Pear Logo"
class="w-64 h-64 mb-8"
/>
<h2 class="text-2xl font-semibold text-gray-700 mb-4">
Search your peers for P2P apps
</h2>
<p class="text-gray-500 text-center max-w-md mb-8">
Find and discover P2P applications in your network.
</p>
<!-- Prominent Search Bar -->
<div class="w-full max-w-2xl px-4 mb-32">
<form @submit.prevent="handleSearch()" class="relative">
<input
x-model="searchQuery"
name="search"
type="text"
placeholder="Search for P2P apps..."
class="w-full h-14 rounded-full px-6 py-4 text-lg border-2 border-input focus:outline-none focus:ring-4 focus:ring-ring/20 focus:border-ring transition-all bg-card text-card-foreground"
/>
<button
type="submit"
class="absolute right-4 top-1/2 transform -translate-y-1/2 p-2 text-muted-foreground hover:text-foreground transition-colors bg-card rounded-full hover:bg-muted cursor-pointer"
>
<svg
class="w-6 h-6"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"
></path>
</svg>
</button>
</form>
</div>
</div>
<!-- Search Results -->
<div x-cloak x-show="isSearching" class="h-full p-6 overflow-y-auto">
<div class="max-w-4xl mx-auto">
<!-- No results message -->
<p
x-show="searchResults.length === 0"
class="text-gray-500 text-center py-8"
>
No apps found matching your search.
</p>
<!-- Results list -->
<template x-for="app in searchResults" :key="app.name">
<div class="max-w-2xl mb-6">
<div class="flex items-start space-x-3">
<div class="flex-shrink-0 mt-1">
<img
:src="app.icon || './assets/logo.png'"
:alt="app.name + ' icon'"
class="w-6 h-6 rounded object-cover"
@error="$el.src='./assets/logo.png'"
/>
</div>
<div class="flex-1 min-w-0">
<div class="flex items-center space-x-2 mb-1">
<a
@click="loadApp(app)"
class="text-blue-600 hover:underline text-lg font-medium leading-6 cursor-pointer"
x-text="app.name || 'Unnamed App'"
></a>
<span class="text-gray-500 text-sm">•</span>
<span
class="text-green-700 text-sm font-medium"
x-text="app.type"
></span>
</div>
<div
class="text-sm text-green-600 mb-1"
x-text="app.url || '#'"
></div>
<p
class="text-sm text-gray-600 leading-5"
x-text="app.description || 'No description available'"
></p>
</div>
</div>
</div>
</template>
</div>
</div>
</div>
<div
x-cloak
x-show="!isReady"
class="text-center flex items-center justify-center h-full flex-col"
>
<img src="./assets/logo.png" alt="Pear" class="h-64 w-64 mx-auto" />
<span class="block mt-4 text-lg">Loading...</span>
</div>
</div>
<div
x-cloak
x-show="appTypeOpened === 'standalone'"
class="w-full h-full flex justify-center items-center flex-col gap-4"
>
<h1>Standalone site opened in new window</h1>
<img src="./assets/logo.png" alt="Pear Logo" class="w-64 h-64 mb-8" />
</div>
<!-- App Container (when app is open) -->
<div
id="app-container"
x-cloak
x-show="openApp !== null"
class="w-full h-full p-2 overflow-y-auto"
hx-get="/"
hx-swap="outerHTML"
hx-trigger="loadApp"
hx-indicator="#app-loading"
>
<template shadowrootmode="open">
<main
hx-boost="true"
hx-push-url="false"
style="height: 100%"
></main>
</template>
<div
id="app-loading"
class="htmx-indicator text-center flex items-center justify-center h-full"
>
<img src="./assets/logo.png" alt="Pear" class="h-64 w-64 mx-auto" />
<span class="block mt-4 text-lg">Loading...</span>
</div>
</div>
</div>
</main>
<script>
// Ensure we're always on the base URL to prevent refresh issues
if (
window.location.pathname !== "/" &&
window.location.pathname !== "/index.html"
) {
window.history.replaceState(null, "", "/");
}
// Trigger htmx to load the app content
const appContainer = document.getElementById("app-container");
// document.addEventListener("click", (evt) => {
// console.log("clicked anywhere", evt.target);
// let anchor = evt.target.closest && evt.target.closest("a");
// if (!anchor) return;
// console.log("anchor", anchor);
// evt.preventDefault();
// });
// Catch all <a> clicks and handle "pear://" links
appContainer.shadowRoot.addEventListener("click", async (evt) => {
// Find the closest anchor element in the event path
console.log("clicked", evt.target);
let anchor = evt.target.closest && evt.target.closest("a[href]");
if (!anchor) return;
evt.preventDefault();
const href = anchor.getAttribute("href");
if (href && href.startsWith("pear://")) {
console.log("todo: handle pear:// links", href);
return;
}
const state = hyperHistory.state;
await hyperHistory.pushState({
...state,
url: href,
});
});
document.body.addEventListener("htmx:beforeSwap", function (evt) {
const headers = evt.detail.xhr.getAllResponseHeaders();
const target = headers["HX-Retarget"] || "main";
const swap = headers["HX-Reswap"] || "innerHTML";
const appTarget = appContainer.shadowRoot.querySelector(target);
evt.detail.target = appTarget;
evt.detail.swapOverride = swap;
});
// Prevent htmx from updating browser history
document.body.addEventListener("htmx:beforeRequest", function (evt) {
// Ensure htmx doesn't update the URL for any requests
evt.detail.parameters = evt.detail.parameters || {};
evt.detail.parameters._pushUrl = false;
});
const searchApp = () => {
return {
isSearching: false,
searchQuery: "",
searchResults: [],
openApp: null,
openPipe: null,
canGoBack: false,
canGoForward: false,
appTypeOpened: null,
isReady: false,
showHistoryModal: false,
historyList: [],
init() {
if (globalThis.pearBrowserReady) {
this.isReady = true;
} else {
document.addEventListener("pear-browser-ready", () => {
this.isReady = true;
}, { once: true });
}
setTimeout(async () => {
this.canGoBack = await globalThis.hyperHistory.peek(-1);
this.canGoForward = await globalThis.hyperHistory.peek(1);
console.log("canGoBack", this.canGoBack);
console.log("canGoForward", this.canGoForward);
globalThis.hyperHistory.on("popState", this.updateNavigation.bind(this));
}, 1000);
},
updateNavigation() {
this.canGoBack = globalThis.hyperHistory.peek(-1);
this.canGoForward = globalThis.hyperHistory.peek(1);
console.log("canGoBack", this.canGoBack);
console.log("canGoForward", this.canGoForward);
},
async loadApp(app, skipState) {
this.openApp = app;
this.searchQuery = app.url;
console.log("opening app", app.name, app.url, app.type);
this.openPipe = Pear.worker.run(app.url);
Pear.teardown(() => {
this.openPipe.destroy();
});
globalThis.XMLHttpRequest = PearRequest.create(this.openPipe);
if (!skipState) {
await hyperHistory.pushState({
openApp: this.openApp,
searchQuery: this.searchQuery,
});
if (this.showHistoryModal) {
await this.refreshHistory();
}
}
switch (app.type) {
case "hypersite":
htmx.trigger(appContainer, "loadApp");
break;
case "standalone":
break;
default:
break;
}
this.appTypeOpened = app.type;
},
async handleSearch(skipState) {
if (this.searchQuery.trim()) {
this.isSearching = true;
if (!skipState) {
await hyperHistory.pushState({
searchQuery: this.searchQuery,
});
if (this.showHistoryModal) {
await this.refreshHistory();
}
}
const apps = await sheets.list(schemas.apps, {
query: `[?name = '${this.searchQuery}']`,
});
this.searchResults = apps.map((app) => app.json);
}
},
resetSearch() {
console.log("resetting search");
this.isSearching = false;
this.searchQuery = "";
this.searchResults = [];
this.openApp = null;
this.appTypeOpened = null;
if (this.openPipe) {
this.openPipe.destroy();
}
this.openPipe = null;
globalThis.hyperHistory.off("popState", this.updateNavigation);
},
async reloadState(state) {
this.resetSearch();
if (!state) {
return;
}
if (state.searchQuery) {
this.searchQuery = state.searchQuery;
this.handleSearch(true);
}
if (state.openApp) {
this.loadApp(state.openApp, true);
}
if (state.url) {
// TODO: trigger app to load the url
}
},
async goBack() {
if (this.canGoBack) {
const state = await hyperHistory.back();
this.reloadState(state);
}
},
async goForward() {
if (this.canGoForward) {
const state = await hyperHistory.forward();
this.reloadState(state);
}
},
async listHistory() {
this.historyList = await hyperHistory.list();
},
async clearHistory() {
await hyperHistory.clear();
this.historyList = [];
this.showHistoryModal = false;
},
async refreshHistory() {
await this.listHistory();
},
};
};
</script>
</body>
</html>