Spaces:
Runtime error
Runtime error
File size: 6,231 Bytes
9e93b10 | 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 | <script lang="ts">
import { getContext, onMount } from "svelte";
import { models, config, user } from "$lib/stores";
import { toast } from "svelte-sonner";
import {
deleteSharedChatById,
getChatById,
shareChatById,
getChatAccessGrants,
updateChatAccessGrants,
} from "$lib/apis/chats";
import { copyToClipboard } from "$lib/utils";
import Modal from "../common/Modal.svelte";
import Link from "../icons/Link.svelte";
import XMark from "$lib/components/icons/XMark.svelte";
import AccessControl from "$lib/components/workspace/common/AccessControl.svelte";
export let chatId;
let chat = null;
let shareUrl = null;
let accessGrants: any[] = [];
const i18n = getContext("i18n");
const shareLocalChat = async () => {
const _chat = chat;
const sharedChat = await shareChatById(localStorage.token, chatId);
shareUrl = `${window.location.origin}/s/${sharedChat.share_id}`;
console.log(shareUrl);
chat = await getChatById(localStorage.token, chatId);
return shareUrl;
};
const shareChat = async () => {
const _chat = chat.chat;
console.log("share", _chat);
toast.success($i18n.t("Redirecting you to rexpro-ai Community"));
const url = "https://rexpro-ai.rexpro.com";
// const url = 'http://localhost:5173';
const tab = await window.open(`${url}/chats/upload`, "_blank");
window.addEventListener(
"message",
(event) => {
if (event.origin !== url) return;
if (event.data === "loaded") {
tab.postMessage(
JSON.stringify({
chat: _chat,
models: $models.filter((m) =>
_chat.models.includes(m.id),
),
}),
"*",
);
}
},
false,
);
};
const loadAccessGrants = async () => {
if (!chatId) return;
try {
accessGrants =
(await getChatAccessGrants(localStorage.token, chatId)) ?? [];
} catch (e) {
console.error("Failed to load access grants", e);
accessGrants = [];
}
};
const saveAccessGrants = async () => {
try {
await updateChatAccessGrants(
localStorage.token,
chatId,
accessGrants,
);
toast.success($i18n.t("Access updated"));
} catch (e) {
toast.error(`${e}`);
}
};
export let show = false;
const isDifferentChat = (_chat) => {
if (!chat) {
return true;
}
if (!_chat) {
return false;
}
return chat.id !== _chat.id || chat.share_id !== _chat.share_id;
};
$: if (show) {
(async () => {
if (chatId) {
const _chat = await getChatById(localStorage.token, chatId);
if (isDifferentChat(_chat)) {
chat = _chat;
}
await loadAccessGrants();
} else {
chat = null;
accessGrants = [];
console.log(chat);
}
})();
}
</script>
<Modal bind:show size="md">
<div>
<div class=" flex justify-between dark:text-gray-300 px-5 pt-4 pb-0.5">
<div class=" text-lg font-medium self-center">
{$i18n.t("Share Chat")}
</div>
<button
class="self-center"
aria-label={$i18n.t("Close")}
on:click={() => {
show = false;
}}
>
<XMark className={"size-5"} />
</button>
</div>
{#if chat}
<div class="px-5 pt-4 pb-5 w-full flex flex-col">
<div class="text-sm dark:text-gray-300">
{#if chat.share_id}
<a href="/s/{chat.share_id}" target="_blank"
>{$i18n.t("You have shared this chat")}
<span class=" underline">{$i18n.t("before")}</span
>.</a
>
{$i18n.t("Click here to")}
<button
class="underline"
on:click={async () => {
const res = await deleteSharedChatById(
localStorage.token,
chatId,
);
if (res) {
chat = await getChatById(
localStorage.token,
chatId,
);
}
}}
>{$i18n.t("delete this link")}
</button>
{$i18n.t("and create a new shared link.")}
{:else}
{$i18n.t(
"Messages you send after creating your link won't be shared. Users with the URL will be able to view the shared chat.",
)}
{/if}
</div>
{#if chat.share_id}
<div class="mt-3">
<AccessControl
bind:accessGrants
accessRoles={["read"]}
sharePublic={$user?.permissions?.sharing
?.public_chats || $user?.role === "admin"}
onChange={saveAccessGrants}
/>
</div>
{/if}
<div class="flex justify-end gap-1 mt-3">
{#if $config?.features.enable_community_sharing}
<button
class="flex items-center gap-1 px-3.5 py-2 text-sm font-medium bg-gray-100 hover:bg-gray-200 text-gray-800 dark:bg-gray-850 dark:text-white dark:hover:bg-gray-800 transition rounded-full"
type="button"
on:click={() => {
shareChat();
}}
>
{$i18n.t("Share to rexpro-ai Community")}
</button>
{/if}
<button
class="flex items-center gap-1 px-3.5 py-2 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
type="button"
id="copy-and-share-chat-button"
on:click={async () => {
const isSafari =
/^((?!chrome|android).)*safari/i.test(
navigator.userAgent,
);
if (isSafari) {
console.log("isSafari");
const getUrlPromise = async () => {
const url = await shareLocalChat();
return new Blob([url], {
type: "text/plain",
});
};
navigator.clipboard
.write([
new ClipboardItem({
"text/plain": getUrlPromise(),
}),
])
.then(() => {
console.log(
"Async: Copying to clipboard was successful!",
);
return true;
})
.catch((error) => {
console.error(
"Async: Could not copy text: ",
error,
);
return false;
});
} else {
copyToClipboard(await shareLocalChat());
}
toast.success(
$i18n.t("Copied shared chat URL to clipboard!"),
);
}}
>
<Link />
{#if chat.share_id}
{$i18n.t("Update and Copy Link")}
{:else}
{$i18n.t("Copy Link")}
{/if}
</button>
</div>
</div>
{/if}
</div>
</Modal>
|