-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathShareButton.tsx
More file actions
197 lines (189 loc) · 5.62 KB
/
ShareButton.tsx
File metadata and controls
197 lines (189 loc) · 5.62 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
import { Show, createMemo, createSignal, onCleanup } from "solid-js";
import { Button } from "~/components/ui/button";
import { encodeAppState } from "~/lib/encode";
import { analyses, experiments } from "~/lib/store";
import {
MdiClipboard,
MdiClipboardCheck,
MdiShareVariantOutline,
} from "./icons";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "./ui/dialog";
import { TextField, TextFieldInput } from "./ui/text-field";
import { showToast } from "./ui/toast";
const MAX_SHAREABLE_LINK_LENGTH = 32_000;
function ShareStateAsFile({ state }: { state: string }) {
const downloadUrl = createMemo(() => {
return URL.createObjectURL(
new Blob([decodeURI(state)], {
type: "application/json",
}),
);
});
onCleanup(() => {
URL.revokeObjectURL(downloadUrl());
});
const filename = createMemo(() => {
const names = experiments.map((e) => e.config.reference.name).join("-");
return `class-${names.slice(0, 120)}.json`;
});
return (
<>
<p>
Alternatively you can create your own shareable link by hosting the
state remotely:
</p>
<ol class="list-inside list-decimal space-y-1">
<li>
<a
class="underline"
href={downloadUrl()}
download={filename()}
type="application/json"
>
Download state
</a>{" "}
as file
</li>
<li>
Upload the state file to some static hosting service like your own web
server or an AWS S3 bucket.
</li>
<li>
Open the CLASS web application with
"https://classmodel.github.io/class-web?s=<your remote url>".
</li>
</ol>
<p>
Make sure the CLASS web application is{" "}
<a
href="https://enable-cors.org/server.html"
target="_blank"
rel="noreferrer"
class="underline"
>
allowed to download from remote location
</a>
.
</p>
</>
);
}
export function ShareButton() {
const [open, setOpen] = createSignal(false);
const [isCopied, setIsCopied] = createSignal(false);
let inputRef: HTMLInputElement | undefined;
const encodedAppState = createMemo(() => {
if (!open()) {
return "";
}
return encodeAppState(experiments, analyses);
});
const shareableLink = createMemo(() => {
const basePath = import.meta.env.DEV
? ""
: import.meta.env.BASE_URL.replace("/_build", "");
const url = `${window.location.origin}${basePath}#${encodedAppState()}`;
return url;
});
async function copyToClipboard() {
try {
await navigator.clipboard.writeText(shareableLink());
setIsCopied(true);
setTimeout(() => setIsCopied(false), 2000); // Reset copied state after 2 seconds
showToast({
title: "Share link copied to clipboard",
duration: 1000,
});
} catch (err) {
console.error("Failed to copy text: ", err);
}
}
const handleOpenChange = (open: boolean) => {
setOpen(open);
if (open) {
setTimeout(() => {
inputRef?.focus();
inputRef?.select();
}, 0);
}
};
return (
<Dialog open={open()} onOpenChange={handleOpenChange}>
<DialogTrigger class="flex items-center gap-2 border-transparent border-b-2 hover:border-sky-600">
Share <MdiShareVariantOutline />
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle class="mr-10">Share link</DialogTitle>
</DialogHeader>
<Show
when={shareableLink().length < MAX_SHAREABLE_LINK_LENGTH}
fallback={
<>
<p>
Cannot embed application state in shareable link, it is too
large.
</p>
<ShareStateAsFile state={encodedAppState()} />
</>
}
>
<Show
when={experiments.length > 0}
fallback={
<p>Nothing to share. Please add at least one experiment.</p>
}
>
<div>
Anyone with{" "}
<a
target="_blank"
rel="noreferrer"
class="font-medium underline underline-offset-4"
href={shareableLink()}
>
this link
</a>{" "}
will be able to view the current application state in their web
browser.
</div>
<div class="flex items-center space-x-2">
<TextField class="w-full" defaultValue={shareableLink()}>
<TextFieldInput
ref={inputRef}
type="text"
readonly
class="w-full"
aria-label="Shareable link for current application state"
/>
</TextField>
<Button
type="submit"
variant="outline"
size="icon"
class="px-3"
onClick={copyToClipboard}
aria-label={isCopied() ? "Link copied" : "Copy link"}
>
<span class="sr-only">Copy</span>
<Show when={isCopied()} fallback={<MdiClipboard />}>
<MdiClipboardCheck />
</Show>
</Button>
</div>
<ShareStateAsFile state={encodedAppState()} />
</Show>
</Show>
<div aria-live="polite" class="sr-only">
<Show when={isCopied()}>Link copied to clipboard</Show>
</div>
</DialogContent>
</Dialog>
);
}