Spaces:
Runtime error
Runtime error
Delete src/routes/+layout.svelte
Browse files- src/routes/+layout.svelte +0 -250
src/routes/+layout.svelte
DELETED
|
@@ -1,250 +0,0 @@
|
|
| 1 |
-
<script>
|
| 2 |
-
import { io } from 'socket.io-client';
|
| 3 |
-
import { spring } from 'svelte/motion';
|
| 4 |
-
|
| 5 |
-
let loadingProgress = spring(0, {
|
| 6 |
-
stiffness: 0.05
|
| 7 |
-
});
|
| 8 |
-
|
| 9 |
-
import { onMount, tick, setContext } from 'svelte';
|
| 10 |
-
import {
|
| 11 |
-
config,
|
| 12 |
-
user,
|
| 13 |
-
theme,
|
| 14 |
-
WEBUI_NAME,
|
| 15 |
-
mobile,
|
| 16 |
-
socket,
|
| 17 |
-
activeUserCount,
|
| 18 |
-
USAGE_POOL
|
| 19 |
-
} from '$lib/stores';
|
| 20 |
-
import { goto } from '$app/navigation';
|
| 21 |
-
import { page } from '$app/stores';
|
| 22 |
-
import { Toaster, toast } from 'svelte-sonner';
|
| 23 |
-
|
| 24 |
-
import { getBackendConfig } from '$lib/apis';
|
| 25 |
-
import { getSessionUser } from '$lib/apis/auths';
|
| 26 |
-
|
| 27 |
-
import '../tailwind.css';
|
| 28 |
-
import '../app.css';
|
| 29 |
-
|
| 30 |
-
import 'tippy.js/dist/tippy.css';
|
| 31 |
-
|
| 32 |
-
import { WEBUI_BASE_URL, WEBUI_HOSTNAME } from '$lib/constants';
|
| 33 |
-
import i18n, { initI18n, getLanguages } from '$lib/i18n';
|
| 34 |
-
import { bestMatchingLanguage } from '$lib/utils';
|
| 35 |
-
|
| 36 |
-
setContext('i18n', i18n);
|
| 37 |
-
|
| 38 |
-
let loaded = false;
|
| 39 |
-
const BREAKPOINT = 768;
|
| 40 |
-
|
| 41 |
-
// 获取当前中国时间的小时数
|
| 42 |
-
function getChinaHour() {
|
| 43 |
-
const chinaTimezoneOffset = 8 * 60; // 中国时区 UTC+8
|
| 44 |
-
const currentTime = new Date();
|
| 45 |
-
const localTimezoneOffset = currentTime.getTimezoneOffset();
|
| 46 |
-
const chinaTime = new Date(currentTime.getTime() + (chinaTimezoneOffset + localTimezoneOffset) * 60000);
|
| 47 |
-
return chinaTime.getHours();
|
| 48 |
-
}
|
| 49 |
-
|
| 50 |
-
// 根据中国时间段设置 activeUserCount 的随机数
|
| 51 |
-
function setActiveUserCountByTime() {
|
| 52 |
-
const hour = getChinaHour();
|
| 53 |
-
let minActiveUsers, maxActiveUsers;
|
| 54 |
-
|
| 55 |
-
if (hour >= 22 || hour < 10) {
|
| 56 |
-
// 22:00-次日10:00,设为10-30
|
| 57 |
-
minActiveUsers = 10;
|
| 58 |
-
maxActiveUsers = 30;
|
| 59 |
-
} else if (hour >= 10 && hour < 15) {
|
| 60 |
-
// 10:00-15:00,设为80-100
|
| 61 |
-
minActiveUsers = 80;
|
| 62 |
-
maxActiveUsers = 100;
|
| 63 |
-
} else if (hour >= 15 && hour < 22) {
|
| 64 |
-
// 15:00-20:00,设为150-180
|
| 65 |
-
minActiveUsers = 150;
|
| 66 |
-
maxActiveUsers = 180;
|
| 67 |
-
} else {
|
| 68 |
-
// 其他时间段,设为150-180
|
| 69 |
-
minActiveUsers = 150;
|
| 70 |
-
maxActiveUsers = 180;
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
const randomActiveUsers = Math.floor(Math.random() * (maxActiveUsers - minActiveUsers + 1)) + minActiveUsers;
|
| 74 |
-
activeUserCount.set(randomActiveUsers);
|
| 75 |
-
}
|
| 76 |
-
|
| 77 |
-
let wakeLock = null;
|
| 78 |
-
|
| 79 |
-
onMount(async () => {
|
| 80 |
-
theme.set(localStorage.theme);
|
| 81 |
-
|
| 82 |
-
mobile.set(window.innerWidth < BREAKPOINT);
|
| 83 |
-
const onResize = () => {
|
| 84 |
-
if (window.innerWidth < BREAKPOINT) {
|
| 85 |
-
mobile.set(true);
|
| 86 |
-
} else {
|
| 87 |
-
mobile.set(false);
|
| 88 |
-
}
|
| 89 |
-
};
|
| 90 |
-
|
| 91 |
-
window.addEventListener('resize', onResize);
|
| 92 |
-
|
| 93 |
-
const setWakeLock = async () => {
|
| 94 |
-
try {
|
| 95 |
-
wakeLock = await navigator.wakeLock.request('screen');
|
| 96 |
-
} catch (err) {
|
| 97 |
-
// The Wake Lock request has failed - usually system related, such as battery.
|
| 98 |
-
console.log(err);
|
| 99 |
-
}
|
| 100 |
-
|
| 101 |
-
if (wakeLock) {
|
| 102 |
-
// Add a listener to release the wake lock when the page is unloaded
|
| 103 |
-
wakeLock.addEventListener('release', () => {
|
| 104 |
-
// the wake lock has been released
|
| 105 |
-
console.log('Wake Lock released');
|
| 106 |
-
});
|
| 107 |
-
}
|
| 108 |
-
};
|
| 109 |
-
|
| 110 |
-
if ('wakeLock' in navigator) {
|
| 111 |
-
await setWakeLock();
|
| 112 |
-
|
| 113 |
-
document.addEventListener('visibilitychange', async () => {
|
| 114 |
-
// Re-request the wake lock if the document becomes visible
|
| 115 |
-
if (wakeLock !== null && document.visibilityState === 'visible') {
|
| 116 |
-
await setWakeLock();
|
| 117 |
-
}
|
| 118 |
-
});
|
| 119 |
-
}
|
| 120 |
-
|
| 121 |
-
let backendConfig = null;
|
| 122 |
-
try {
|
| 123 |
-
backendConfig = await getBackendConfig();
|
| 124 |
-
console.log('Backend config:', backendConfig);
|
| 125 |
-
} catch (error) {
|
| 126 |
-
console.error('Error loading backend config:', error);
|
| 127 |
-
}
|
| 128 |
-
// Initialize i18n even if we didn't get a backend config,
|
| 129 |
-
// so `/error` can show something that's not `undefined`.
|
| 130 |
-
|
| 131 |
-
initI18n();
|
| 132 |
-
if (!localStorage.locale) {
|
| 133 |
-
const languages = await getLanguages();
|
| 134 |
-
const browserLanguages = navigator.languages
|
| 135 |
-
? navigator.languages
|
| 136 |
-
: [navigator.language || navigator.userLanguage];
|
| 137 |
-
const lang = backendConfig.default_locale
|
| 138 |
-
? backendConfig.default_locale
|
| 139 |
-
: bestMatchingLanguage(languages, browserLanguages, 'en-US');
|
| 140 |
-
$i18n.changeLanguage(lang);
|
| 141 |
-
}
|
| 142 |
-
|
| 143 |
-
if (backendConfig) {
|
| 144 |
-
// Save Backend Status to Store
|
| 145 |
-
await config.set(backendConfig);
|
| 146 |
-
await WEBUI_NAME.set(backendConfig.name);
|
| 147 |
-
|
| 148 |
-
if ($config) {
|
| 149 |
-
const _socket = io(`${WEBUI_BASE_URL}`, {
|
| 150 |
-
path: '/ws/socket.io',
|
| 151 |
-
auth: { token: localStorage.token }
|
| 152 |
-
});
|
| 153 |
-
|
| 154 |
-
_socket.on('connect', () => {
|
| 155 |
-
console.log('connected');
|
| 156 |
-
});
|
| 157 |
-
|
| 158 |
-
await socket.set(_socket);
|
| 159 |
-
|
| 160 |
-
_socket.on('user-count', (data) => {
|
| 161 |
-
console.log('user-count', data);
|
| 162 |
-
activeUserCount.set(data.count);
|
| 163 |
-
});
|
| 164 |
-
|
| 165 |
-
_socket.on('usage', (data) => {
|
| 166 |
-
console.log('usage', data);
|
| 167 |
-
USAGE_POOL.set(data['models']);
|
| 168 |
-
});
|
| 169 |
-
|
| 170 |
-
if (localStorage.token) {
|
| 171 |
-
// Get Session User Info
|
| 172 |
-
const sessionUser = await getSessionUser(localStorage.token).catch((error) => {
|
| 173 |
-
toast.error(error);
|
| 174 |
-
return null;
|
| 175 |
-
});
|
| 176 |
-
|
| 177 |
-
if (sessionUser) {
|
| 178 |
-
// Save Session User to Store
|
| 179 |
-
await user.set(sessionUser);
|
| 180 |
-
} else {
|
| 181 |
-
// Redirect Invalid Session User to /auth Page
|
| 182 |
-
localStorage.removeItem('token');
|
| 183 |
-
await goto('/auth');
|
| 184 |
-
}
|
| 185 |
-
} else {
|
| 186 |
-
// Don't redirect if we're already on the auth page
|
| 187 |
-
// Needed because we pass in tokens from OAuth logins via URL fragments
|
| 188 |
-
if ($page.url.pathname !== '/auth') {
|
| 189 |
-
await goto('/auth');
|
| 190 |
-
}
|
| 191 |
-
}
|
| 192 |
-
}
|
| 193 |
-
} else {
|
| 194 |
-
// Redirect to /error when Backend Not Detected
|
| 195 |
-
await goto(`/error`);
|
| 196 |
-
}
|
| 197 |
-
|
| 198 |
-
await tick();
|
| 199 |
-
|
| 200 |
-
if (
|
| 201 |
-
document.documentElement.classList.contains('her') &&
|
| 202 |
-
document.getElementById('progress-bar')
|
| 203 |
-
) {
|
| 204 |
-
loadingProgress.subscribe((value) => {
|
| 205 |
-
const progressBar = document.getElementById('progress-bar');
|
| 206 |
-
|
| 207 |
-
if (progressBar) {
|
| 208 |
-
progressBar.style.width = `${value}%`;
|
| 209 |
-
}
|
| 210 |
-
});
|
| 211 |
-
|
| 212 |
-
await loadingProgress.set(100);
|
| 213 |
-
|
| 214 |
-
document.getElementById('splash-screen')?.remove();
|
| 215 |
-
|
| 216 |
-
const audio = new Audio(`/audio/greeting.mp3`);
|
| 217 |
-
const playAudio = () => {
|
| 218 |
-
audio.play();
|
| 219 |
-
document.removeEventListener('click', playAudio);
|
| 220 |
-
};
|
| 221 |
-
|
| 222 |
-
document.addEventListener('click', playAudio);
|
| 223 |
-
|
| 224 |
-
loaded = true;
|
| 225 |
-
} else {
|
| 226 |
-
document.getElementById('splash-screen')?.remove();
|
| 227 |
-
loaded = true;
|
| 228 |
-
}
|
| 229 |
-
|
| 230 |
-
return () => {
|
| 231 |
-
window.removeEventListener('resize', onResize);
|
| 232 |
-
};
|
| 233 |
-
});
|
| 234 |
-
</script>
|
| 235 |
-
|
| 236 |
-
<svelte:head>
|
| 237 |
-
<title>{$WEBUI_NAME}</title>
|
| 238 |
-
<link crossorigin="anonymous" rel="icon" href="{WEBUI_BASE_URL}/static/favicon.png" />
|
| 239 |
-
|
| 240 |
-
<!-- rosepine themes have been disabled as it's not up to date with our latest version. -->
|
| 241 |
-
<!-- feel free to make a PR to fix if anyone wants to see it return -->
|
| 242 |
-
<!-- <link rel="stylesheet" type="text/css" href="/themes/rosepine.css" />
|
| 243 |
-
<link rel="stylesheet" type="text/css" href="/themes/rosepine-dawn.css" /> -->
|
| 244 |
-
</svelte:head>
|
|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