2022-08-17 10:43:57 +02:00
|
|
|
import { dev } from '$app/env';
|
2022-08-12 16:09:52 +02:00
|
|
|
import cuid from 'cuid';
|
2022-10-28 15:50:57 +02:00
|
|
|
import Cookies from 'js-cookie';
|
2022-08-11 08:21:30 +00:00
|
|
|
import { writable, readable, type Writable } from 'svelte/store';
|
2022-07-08 13:38:19 +02:00
|
|
|
|
2022-07-06 11:02:36 +02:00
|
|
|
interface AppSession {
|
2022-09-07 15:59:37 +02:00
|
|
|
isRegistrationEnabled: boolean;
|
2022-07-22 20:48:04 +00:00
|
|
|
ipv4: string | null,
|
|
|
|
ipv6: string | null,
|
2022-07-08 13:38:19 +02:00
|
|
|
version: string | null,
|
2022-07-06 11:02:36 +02:00
|
|
|
userId: string | null,
|
|
|
|
teamId: string | null,
|
|
|
|
permission: string,
|
|
|
|
isAdmin: boolean,
|
|
|
|
whiteLabeled: boolean,
|
|
|
|
whiteLabeledDetails: {
|
|
|
|
icon: string | null,
|
|
|
|
},
|
|
|
|
tokens: {
|
|
|
|
github: string | null,
|
|
|
|
gitlab: string | null,
|
2022-08-29 15:29:00 +02:00
|
|
|
},
|
2022-09-29 15:46:52 +02:00
|
|
|
pendingInvitations: Array<any>
|
2022-07-06 11:02:36 +02:00
|
|
|
}
|
2022-08-09 15:28:26 +00:00
|
|
|
interface AddToast {
|
|
|
|
type?: "info" | "success" | "error",
|
|
|
|
message: string,
|
|
|
|
timeout?: number | undefined
|
2022-08-12 16:09:52 +02:00
|
|
|
}
|
2022-09-19 14:05:25 +02:00
|
|
|
export const updateLoading: Writable<boolean> = writable(false);
|
|
|
|
export const isUpdateAvailable: Writable<boolean> = writable(false);
|
2022-09-08 10:45:12 +02:00
|
|
|
export const search: any = writable('')
|
2022-07-06 11:02:36 +02:00
|
|
|
export const loginEmail: Writable<string | undefined> = writable()
|
|
|
|
export const appSession: Writable<AppSession> = writable({
|
2022-09-07 15:59:37 +02:00
|
|
|
isRegistrationEnabled: false,
|
2022-07-22 20:48:04 +00:00
|
|
|
ipv4: null,
|
|
|
|
ipv6: null,
|
2022-07-08 13:38:19 +02:00
|
|
|
version: null,
|
2022-07-06 11:02:36 +02:00
|
|
|
userId: null,
|
|
|
|
teamId: null,
|
|
|
|
permission: 'read',
|
|
|
|
isAdmin: false,
|
|
|
|
whiteLabeled: false,
|
|
|
|
whiteLabeledDetails: {
|
|
|
|
icon: null
|
|
|
|
},
|
|
|
|
tokens: {
|
|
|
|
github: null,
|
|
|
|
gitlab: null
|
2022-08-29 15:29:00 +02:00
|
|
|
},
|
2022-09-29 15:46:52 +02:00
|
|
|
pendingInvitations: []
|
2022-07-06 11:02:36 +02:00
|
|
|
});
|
|
|
|
export const disabledButton: Writable<boolean> = writable(false);
|
2022-09-05 09:09:32 +02:00
|
|
|
export const isDeploymentEnabled: Writable<boolean> = writable(false);
|
|
|
|
export function checkIfDeploymentEnabledApplications(isAdmin: boolean, application: any) {
|
|
|
|
return (
|
|
|
|
isAdmin &&
|
2022-10-06 10:25:41 +02:00
|
|
|
(application.buildPack === 'compose') ||
|
2022-09-05 09:19:25 +02:00
|
|
|
(application.fqdn || application.settings.isBot) &&
|
2022-09-05 09:09:32 +02:00
|
|
|
application.gitSource &&
|
|
|
|
application.repository &&
|
|
|
|
application.destinationDocker &&
|
|
|
|
application.buildPack
|
|
|
|
);
|
|
|
|
}
|
2022-09-05 09:15:32 +02:00
|
|
|
export function checkIfDeploymentEnabledServices(isAdmin: boolean, service: any) {
|
|
|
|
return (
|
|
|
|
isAdmin &&
|
|
|
|
service.fqdn &&
|
|
|
|
service.destinationDocker &&
|
|
|
|
service.version &&
|
|
|
|
service.type
|
|
|
|
);
|
|
|
|
}
|
2022-07-06 11:02:36 +02:00
|
|
|
export const status: Writable<any> = writable({
|
|
|
|
application: {
|
2022-10-06 10:25:41 +02:00
|
|
|
statuses: [],
|
2022-10-06 14:24:28 +02:00
|
|
|
overallStatus: 'stopped',
|
2022-07-06 11:02:36 +02:00
|
|
|
loading: false,
|
|
|
|
initialLoading: true
|
|
|
|
},
|
|
|
|
service: {
|
2022-10-14 15:48:37 +02:00
|
|
|
statuses: [],
|
|
|
|
overallStatus: 'stopped',
|
2022-07-06 11:02:36 +02:00
|
|
|
loading: false,
|
2022-10-28 15:50:57 +02:00
|
|
|
startup: {},
|
2022-07-21 12:43:53 +00:00
|
|
|
initialLoading: true
|
2022-07-18 07:45:29 +00:00
|
|
|
},
|
2022-07-06 11:02:36 +02:00
|
|
|
database: {
|
2022-07-21 12:43:53 +00:00
|
|
|
isRunning: false,
|
|
|
|
isExited: false,
|
2022-07-06 11:02:36 +02:00
|
|
|
loading: false,
|
2022-09-08 10:54:24 +02:00
|
|
|
initialLoading: true,
|
|
|
|
isPublic: false
|
2022-07-06 11:02:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
export const features = readable({
|
|
|
|
beta: window.localStorage.getItem('beta') === 'true',
|
|
|
|
latestVersion: window.localStorage.getItem('latestVersion')
|
|
|
|
});
|
2022-07-18 07:45:29 +00:00
|
|
|
|
|
|
|
export const location: Writable<null | string> = writable(null)
|
2022-08-16 16:03:09 +02:00
|
|
|
export const setLocation = (resource: any, settings?: any) => {
|
2022-08-17 10:18:38 +02:00
|
|
|
if (resource.settings.isBot && resource.exposePort) {
|
|
|
|
disabledButton.set(false);
|
2022-08-17 10:43:57 +02:00
|
|
|
return location.set(`http://${dev ? 'localhost' : settings.ipv4}:${resource.exposePort}`)
|
2022-08-16 16:03:09 +02:00
|
|
|
}
|
2022-07-18 07:45:29 +00:00
|
|
|
if (GITPOD_WORKSPACE_URL && resource.exposePort) {
|
|
|
|
const { href } = new URL(GITPOD_WORKSPACE_URL);
|
|
|
|
const newURL = href
|
|
|
|
.replace('https://', `https://${resource.exposePort}-`)
|
|
|
|
.replace(/\/$/, '');
|
2022-08-11 08:18:17 +00:00
|
|
|
return location.set(newURL)
|
2022-08-12 16:09:52 +02:00
|
|
|
} else if (CODESANDBOX_HOST) {
|
|
|
|
const newURL = `https://${CODESANDBOX_HOST.replace(/\$PORT/, resource.exposePort)}`
|
|
|
|
return location.set(newURL)
|
2022-07-18 07:45:29 +00:00
|
|
|
}
|
2022-08-17 10:18:38 +02:00
|
|
|
if (resource.fqdn) {
|
|
|
|
return location.set(resource.fqdn)
|
|
|
|
} else {
|
|
|
|
location.set(null);
|
2022-08-18 19:27:38 +00:00
|
|
|
disabledButton.set(false);
|
2022-08-17 10:18:38 +02:00
|
|
|
}
|
2022-08-09 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const toasts: any = writable([])
|
|
|
|
|
2022-08-12 16:09:52 +02:00
|
|
|
export const dismissToast = (id: string) => {
|
|
|
|
toasts.update((all: any) => all.filter((t: any) => t.id !== id))
|
|
|
|
}
|
|
|
|
export const pauseToast = (id: string) => {
|
|
|
|
toasts.update((all: any) => {
|
|
|
|
const index = all.findIndex((t: any) => t.id === id);
|
|
|
|
if (index > -1) clearTimeout(all[index].timeoutInterval);
|
|
|
|
return all;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
export const resumeToast = (id: string) => {
|
|
|
|
toasts.update((all: any) => {
|
|
|
|
const index = all.findIndex((t: any) => t.id === id);
|
|
|
|
if (index > -1) {
|
|
|
|
all[index].timeoutInterval = setTimeout(() => {
|
|
|
|
dismissToast(id)
|
|
|
|
}, all[index].timeout)
|
|
|
|
}
|
|
|
|
return all;
|
|
|
|
})
|
2022-08-09 15:28:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const addToast = (toast: AddToast) => {
|
2022-08-12 16:09:52 +02:00
|
|
|
const id = cuid();
|
|
|
|
const defaults = {
|
|
|
|
id,
|
|
|
|
type: 'info',
|
|
|
|
timeout: 2000,
|
|
|
|
}
|
|
|
|
let t: any = { ...defaults, ...toast }
|
|
|
|
if (t.timeout) t.timeoutInterval = setTimeout(() => dismissToast(id), t.timeout)
|
|
|
|
toasts.update((all: any) => [t, ...all])
|
2022-09-15 13:57:20 +02:00
|
|
|
}
|
|
|
|
|
2022-10-28 15:50:57 +02:00
|
|
|
export const selectedBuildId: any = writable(null)
|
|
|
|
|
|
|
|
type State = {
|
|
|
|
requests: Array<Request>;
|
|
|
|
};
|
|
|
|
export const state = writable<State>({
|
|
|
|
requests: [],
|
|
|
|
});
|
|
|
|
export const connect = () => {
|
|
|
|
const token = Cookies.get('token')
|
|
|
|
if (token) {
|
2022-10-28 14:29:21 +00:00
|
|
|
let url = `ws://${window.location.hostname}/realtime`
|
2022-10-28 15:50:57 +02:00
|
|
|
if (dev) {
|
|
|
|
url = "ws://localhost:3001/realtime"
|
|
|
|
}
|
|
|
|
const ws = new WebSocket(url);
|
|
|
|
ws.addEventListener("message", (message: any) => {
|
|
|
|
appSession.subscribe((session: any) => {
|
|
|
|
const data: Request = { ...JSON.parse(message.data), timestamp: message.timeStamp };
|
|
|
|
if (data.teamId === session.teamId) {
|
|
|
|
if (data.type === 'service') {
|
|
|
|
const ending = data.message === "ending"
|
|
|
|
status.update((status: any) => ({
|
|
|
|
...status,
|
|
|
|
service: {
|
|
|
|
...status.service,
|
|
|
|
startup: {
|
|
|
|
...status.service.startup,
|
|
|
|
...(ending ? { [data.id]: undefined } : { [data.id]: data.message })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
});
|
|
|
|
ws.addEventListener('open', (event) => {
|
|
|
|
ws.send(JSON.stringify({ type: 'subscribe', message: 'ping' }))
|
|
|
|
});
|
|
|
|
ws.addEventListener('error', (event) => {
|
|
|
|
console.log('error with ws');
|
|
|
|
console.log(event)
|
|
|
|
});
|
|
|
|
ws.addEventListener('close', (event) => {
|
|
|
|
if (!ws || ws.readyState == 3) {
|
|
|
|
setTimeout(() => {
|
|
|
|
connect()
|
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|