The preview tool could only name a port, so a static build needed a server the terminal had to keep alive. `preview` now accepts a path: the app reads the file or folder over the room's ssh sandbox and serves it from a signed host, with `..` and dotfiles refused and a 16 MiB cap. The target is stored with the room and replayed as a durable event. The pane shows a static path with a reload control and keeps the port field as a local fallback.
202 lines
5.8 KiB
Vue
202 lines
5.8 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref, watch } from 'vue'
|
|
import { Maximize2, Minimize2, RefreshCw, X } from 'lucide-vue-next'
|
|
import type { PreviewResponse, PreviewTarget } from '~/types'
|
|
|
|
const props = defineProps<{ roomId: string; target: PreviewTarget | null }>()
|
|
|
|
const port = ref<number | string>(3000)
|
|
const url = ref('')
|
|
const frameKey = ref(0)
|
|
const loading = ref(false)
|
|
const error = ref('')
|
|
const fullscreen = ref(false)
|
|
const manual = ref(false)
|
|
|
|
const staticTarget = computed(() =>
|
|
!manual.value && props.target?.kind === 'static' ? props.target : null,
|
|
)
|
|
const targetPath = computed(() => staticTarget.value?.path ?? '')
|
|
const actionLabel = computed(() => (loading.value ? 'Opening' : url.value ? 'Reload' : 'Go'))
|
|
const frameTitle = computed(() => {
|
|
if (props.target?.kind === 'static' && !manual.value) return `Preview of ${props.target.path}`
|
|
return `Preview on port ${port.value}`
|
|
})
|
|
const emptyText = computed(() =>
|
|
props.target
|
|
? 'The preview has not loaded.'
|
|
: 'No preview yet. Enter a port, or ask @rhema to open one.',
|
|
)
|
|
|
|
async function request(query: string) {
|
|
loading.value = true
|
|
error.value = ''
|
|
try {
|
|
const response = await fetch(`/api/rooms/${encodeURIComponent(props.roomId)}/preview${query}`, {
|
|
credentials: 'include',
|
|
headers: { Accept: 'application/json' },
|
|
})
|
|
if (response.status === 401) {
|
|
await navigateTo('/login')
|
|
return
|
|
}
|
|
const type = response.headers.get('content-type') ?? ''
|
|
if (!type.includes('application/json')) {
|
|
throw new Error('The server did not return a preview address.')
|
|
}
|
|
const body: unknown = await response.json()
|
|
if (!response.ok) {
|
|
const message = body && typeof body === 'object' && 'error' in body
|
|
? String((body as { error: unknown }).error)
|
|
: 'The preview could not be opened.'
|
|
throw new Error(message)
|
|
}
|
|
if (!body || typeof body !== 'object' || typeof (body as { url?: unknown }).url !== 'string') {
|
|
throw new Error('The server returned no preview address.')
|
|
}
|
|
url.value = (body as PreviewResponse).url
|
|
frameKey.value += 1
|
|
} catch (err) {
|
|
url.value = ''
|
|
error.value = err instanceof Error ? err.message : 'The preview could not be opened.'
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
function openTarget() {
|
|
if (!props.target) return openPort()
|
|
manual.value = false
|
|
return request('')
|
|
}
|
|
|
|
function openPort() {
|
|
const value = Number(port.value)
|
|
if (!Number.isInteger(value) || value < 1 || value > 65535) {
|
|
error.value = 'Enter a port between 1 and 65535.'
|
|
return
|
|
}
|
|
manual.value = true
|
|
return request(`?port=${value}`)
|
|
}
|
|
|
|
function open() {
|
|
return manual.value || !props.target ? openPort() : openTarget()
|
|
}
|
|
|
|
function startManual() {
|
|
manual.value = true
|
|
url.value = ''
|
|
error.value = ''
|
|
if (props.target?.kind === 'port') port.value = props.target.port
|
|
}
|
|
|
|
function useTarget() {
|
|
manual.value = false
|
|
if (props.target?.kind === 'port') port.value = props.target.port
|
|
void openTarget()
|
|
}
|
|
|
|
function toggleFullscreen() {
|
|
fullscreen.value = !fullscreen.value
|
|
}
|
|
|
|
watch(
|
|
() => props.target,
|
|
(target) => {
|
|
manual.value = false
|
|
if (!target) {
|
|
url.value = ''
|
|
error.value = ''
|
|
return
|
|
}
|
|
if (target.kind === 'port') port.value = target.port
|
|
void openTarget()
|
|
},
|
|
{ immediate: true },
|
|
)
|
|
</script>
|
|
|
|
<template>
|
|
<div class="preview-wrap" :class="{ 'preview-full': fullscreen }">
|
|
<div class="preview-bar">
|
|
<template v-if="staticTarget">
|
|
<span class="preview-target mono" :title="targetPath">{{ targetPath }}</span>
|
|
<button type="button" class="btn btn-sm btn-quiet" @click="startManual">
|
|
Port
|
|
</button>
|
|
<button
|
|
type="button"
|
|
class="icon-btn"
|
|
aria-label="Reload preview"
|
|
title="Reload preview"
|
|
:disabled="loading"
|
|
@click="openTarget"
|
|
>
|
|
<RefreshCw :size="16" aria-hidden="true" />
|
|
</button>
|
|
</template>
|
|
<template v-else>
|
|
<label class="sr-only" for="preview-port">Port</label>
|
|
<input
|
|
id="preview-port"
|
|
v-model.number="port"
|
|
class="input port-field"
|
|
type="number"
|
|
min="1"
|
|
max="65535"
|
|
aria-label="Preview port"
|
|
@input="manual = true"
|
|
@keyup.enter="open"
|
|
>
|
|
<button type="button" class="btn btn-sm" :disabled="loading" @click="open">
|
|
{{ actionLabel }}
|
|
</button>
|
|
<button
|
|
v-if="target && manual"
|
|
type="button"
|
|
class="btn btn-sm btn-quiet"
|
|
@click="useTarget"
|
|
>
|
|
Room preview
|
|
</button>
|
|
</template>
|
|
<div class="room-topbar-spacer" />
|
|
<button
|
|
type="button"
|
|
class="icon-btn"
|
|
:aria-label="fullscreen ? 'Exit full screen preview' : 'Full screen preview'"
|
|
:title="fullscreen ? 'Exit full screen' : 'Full screen'"
|
|
@click="toggleFullscreen"
|
|
>
|
|
<component :is="fullscreen ? Minimize2 : Maximize2" :size="16" aria-hidden="true" />
|
|
</button>
|
|
<button
|
|
v-if="fullscreen"
|
|
type="button"
|
|
class="icon-btn"
|
|
aria-label="Close full screen preview"
|
|
title="Close full screen preview"
|
|
@click="fullscreen = false"
|
|
>
|
|
<X :size="16" aria-hidden="true" />
|
|
</button>
|
|
</div>
|
|
|
|
<p v-if="error" class="preview-message form-error" role="alert">{{ error }}</p>
|
|
|
|
<div v-if="url" class="preview-frame-wrap">
|
|
<iframe
|
|
:key="frameKey"
|
|
class="preview-frame"
|
|
:src="url"
|
|
:title="frameTitle"
|
|
sandbox="allow-scripts allow-same-origin allow-forms allow-popups"
|
|
/>
|
|
</div>
|
|
<p v-else-if="!error" class="preview-message">
|
|
{{ emptyText }}
|
|
</p>
|
|
</div>
|
|
</template>
|