feat: add static previews to the room pane
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.
This commit is contained in:
1 parent
5be17cf4ef
commit
36de0aace7
20 files changed
+1046
-145
No files matched your search
@@ -1,25 +1,38 @@
|
||||
<script setup lang="ts">
|
||||
import { ref } from 'vue'
|
||||
import { Maximize2, Minimize2, X } from 'lucide-vue-next'
|
||||
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 }>()
|
||||
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)
|
||||
|
||||
async function load() {
|
||||
const value = Number(port.value)
|
||||
if (!Number.isInteger(value) || value < 1 || value > 65535) {
|
||||
error.value = 'Enter a port between 1 and 65535.'
|
||||
return
|
||||
}
|
||||
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?port=${value}`, {
|
||||
const response = await fetch(`/api/rooms/${encodeURIComponent(props.roomId)}/preview${query}`, {
|
||||
credentials: 'include',
|
||||
headers: { Accept: 'application/json' },
|
||||
})
|
||||
@@ -41,7 +54,8 @@ async function load() {
|
||||
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 { url: string }).url
|
||||
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.'
|
||||
@@ -50,27 +64,103 @@ async function load() {
|
||||
}
|
||||
}
|
||||
|
||||
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">
|
||||
<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"
|
||||
>
|
||||
<button type="button" class="btn btn-sm" :disabled="loading" @click="load">
|
||||
{{ loading ? 'Opening' : url ? 'Reload' : 'Go' }}
|
||||
</button>
|
||||
<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"
|
||||
@@ -97,14 +187,15 @@ function toggleFullscreen() {
|
||||
|
||||
<div v-if="url" class="preview-frame-wrap">
|
||||
<iframe
|
||||
:key="frameKey"
|
||||
class="preview-frame"
|
||||
:src="url"
|
||||
:title="`Preview on port ${port}`"
|
||||
:title="frameTitle"
|
||||
sandbox="allow-scripts allow-same-origin allow-forms allow-popups"
|
||||
/>
|
||||
</div>
|
||||
<p v-else-if="!error" class="preview-message">
|
||||
Enter the port the room serves and choose Go.
|
||||
{{ emptyText }}
|
||||
</p>
|
||||
</div>
|
||||
</template>
|
||||
Reference in new issue
Block a user