Split chat into public and private widgets, Places out of Collage, hide stamp scrollbars
Chat.vue (public) and PrivateChat.vue (admins only) are now separate canvas widgets over a shared ChatPanel.vue, replacing the tabbed widget. The messages store counts connect()/disconnect() so both can share one socket, and reopens it on an admin change itself so two mounted chats reconnect once. Places is its own widget (views/home/Places.vue); Collage is just the slideshow. The stamp wall hides its scrollbars. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,336 +1,12 @@
|
||||
<script setup>
|
||||
/**
|
||||
* The chat widget: message list, composer, file attach, admin delete and an
|
||||
* admins-only "private" channel. Drives stores/messages.js and is mounted in
|
||||
* the right-hand sidebar of views/home/Home.vue.
|
||||
*
|
||||
* Admins see two tabs. The socket carries both channels in one stream (the
|
||||
* server only sends private messages to admin connections), so the tab is a
|
||||
* view over `messages` and also decides whether what you send is private —
|
||||
* you cannot post to a channel you are not looking at.
|
||||
*
|
||||
* Owns the socket lifecycle — connect() on mount, disconnect() on unmount — so
|
||||
* the socket exists only while the widget is on screen.
|
||||
* The public chat widget. All of it is ChatPanel.vue; the admins-only channel
|
||||
* is the separate PrivateChat.vue widget. This file keeps the name the canvas
|
||||
* stores ("Chat" in widgetRegistry.js) pointing at the public channel.
|
||||
*/
|
||||
import { ref, computed, watch, nextTick, onMounted, onUnmounted } from "vue";
|
||||
import Button from "@/components/input/Button.vue";
|
||||
import { useMessagesStore } from "@/stores/messages";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import Header from "@/components/text/Header.vue";
|
||||
import Link from "@/components/text/Link.vue";
|
||||
import { ACCEPT, isUploadUrl, kindOf } from "@/uploads";
|
||||
|
||||
const messagesStore = useMessagesStore();
|
||||
const authStore = useAuthStore();
|
||||
const messageInput = ref("");
|
||||
const messagesContainer = ref(null);
|
||||
const messagesInner = ref(null);
|
||||
const fileInput = ref(null);
|
||||
const isAdmin = computed(() => !!authStore.user.admin);
|
||||
|
||||
const activeTab = ref("public");
|
||||
const isPrivateTab = computed(
|
||||
() => isAdmin.value && activeTab.value === "private",
|
||||
);
|
||||
|
||||
// The private tab is the only place private messages are shown, so the public
|
||||
// tab stays public even for an admin.
|
||||
const messages = computed(() =>
|
||||
messagesStore.messages.filter((m) => !!m.private === isPrivateTab.value),
|
||||
);
|
||||
|
||||
// Autoscroll only when the reader is already at the bottom: scrolling up to
|
||||
// read history must not be yanked back down by an incoming message.
|
||||
const isNearBottom = ref(true);
|
||||
const SCROLL_THRESHOLD = 100;
|
||||
let resizeObserver = null;
|
||||
|
||||
function scrollToBottom() {
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
function scrollToBottomIfNear() {
|
||||
if (isNearBottom.value) {
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
function onScroll() {
|
||||
if (!messagesContainer.value) return;
|
||||
const { scrollHeight, scrollTop, clientHeight } = messagesContainer.value;
|
||||
isNearBottom.value =
|
||||
scrollHeight - scrollTop - clientHeight < SCROLL_THRESHOLD;
|
||||
}
|
||||
|
||||
function goToBottom() {
|
||||
isNearBottom.value = true;
|
||||
scrollToBottom();
|
||||
}
|
||||
|
||||
// nextTick: scrollHeight is only correct once Vue has patched the new row in.
|
||||
watch(
|
||||
() => messages.value.length,
|
||||
() => {
|
||||
nextTick(scrollToBottomIfNear);
|
||||
},
|
||||
);
|
||||
|
||||
// A tab switch swaps the whole list, so start the new one at its newest message
|
||||
// rather than at whatever scroll offset the old one left behind.
|
||||
watch(activeTab, () => {
|
||||
isNearBottom.value = true;
|
||||
nextTick(scrollToBottom);
|
||||
});
|
||||
|
||||
function sendMessage() {
|
||||
const text = messageInput.value.trim();
|
||||
if (!text) return;
|
||||
isNearBottom.value = true;
|
||||
messagesStore.sendMessage(text, isPrivateTab.value);
|
||||
messageInput.value = "";
|
||||
}
|
||||
|
||||
function deleteMessage(id) {
|
||||
messagesStore.deleteMessage(id);
|
||||
}
|
||||
|
||||
// Admin status is fixed when the socket connects (from the auth cookies), so
|
||||
// reconnect after a login/logout to pick up or drop private messages. A
|
||||
// demoted admin must also be moved off the private tab, which no longer has
|
||||
// anything to show.
|
||||
watch(isAdmin, (admin) => {
|
||||
if (!admin) activeTab.value = "public";
|
||||
if (!messagesStore.isConnected) return;
|
||||
messagesStore.disconnect();
|
||||
messagesStore.connect();
|
||||
});
|
||||
|
||||
async function onFileSelected(e) {
|
||||
const file = e.target.files[0];
|
||||
if (!file) return;
|
||||
isNearBottom.value = true;
|
||||
// The private flag both routes the upload to /uploads/private/ and marks the
|
||||
// message itself private; the two must agree.
|
||||
await messagesStore.uploadAndSendFile(file, isPrivateTab.value);
|
||||
fileInput.value.value = "";
|
||||
}
|
||||
|
||||
// Attachments are rendered only if isUploadUrl() accepts them: the fileUrl
|
||||
// arrives over the socket and is otherwise attacker-controlled.
|
||||
|
||||
// NOTE: a module-level /g regex carries `lastIndex` between calls. It is safe
|
||||
// here only because parseMessageParts() below always runs exec() to completion
|
||||
// (exec returns null, resetting lastIndex) before returning. An early return
|
||||
// from that loop would make the next message parse from the wrong offset.
|
||||
const urlRegex = /(https?:\/\/[^\s<]+)/g;
|
||||
|
||||
/** Split message text into alternating {type:'text'|'link', value} parts so
|
||||
* URLs can be rendered as anchors without using v-html. */
|
||||
function parseMessageParts(text) {
|
||||
const parts = [];
|
||||
let lastIndex = 0;
|
||||
let match;
|
||||
while ((match = urlRegex.exec(text)) !== null) {
|
||||
if (match.index > lastIndex) {
|
||||
parts.push({
|
||||
type: "text",
|
||||
value: text.slice(lastIndex, match.index),
|
||||
});
|
||||
}
|
||||
parts.push({ type: "link", value: match[1] });
|
||||
lastIndex = urlRegex.lastIndex;
|
||||
}
|
||||
if (lastIndex < text.length) {
|
||||
parts.push({ type: "text", value: text.slice(lastIndex) });
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
messagesStore.connect();
|
||||
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.addEventListener("scroll", onScroll, {
|
||||
passive: true,
|
||||
});
|
||||
}
|
||||
|
||||
// Images and videos load after their row is inserted and change its height,
|
||||
// which the length watcher above has already missed. Observing the inner
|
||||
// wrapper catches those late reflows.
|
||||
if (messagesInner.value) {
|
||||
resizeObserver = new ResizeObserver(scrollToBottomIfNear);
|
||||
resizeObserver.observe(messagesInner.value);
|
||||
}
|
||||
|
||||
scrollToBottom();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
messagesStore.disconnect();
|
||||
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.removeEventListener("scroll", onScroll);
|
||||
}
|
||||
|
||||
if (resizeObserver) {
|
||||
resizeObserver.disconnect();
|
||||
resizeObserver = null;
|
||||
}
|
||||
});
|
||||
import ChatPanel from "./ChatPanel.vue";
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-root flex-col flex min-h-0">
|
||||
<Header>{{ isPrivateTab ? "Private Chat" : "Chat" }}</Header>
|
||||
<div v-if="isAdmin" class="flex gap-1 pt-1" role="tablist">
|
||||
<button
|
||||
type="button"
|
||||
class="chat-tab flex-1"
|
||||
role="tab"
|
||||
:aria-selected="activeTab === 'public'"
|
||||
:class="{ 'is-active': activeTab === 'public' }"
|
||||
@click="activeTab = 'public'"
|
||||
>
|
||||
Public
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class="chat-tab flex-1"
|
||||
role="tab"
|
||||
:aria-selected="activeTab === 'private'"
|
||||
:class="{ 'is-active': activeTab === 'private' }"
|
||||
@click="activeTab = 'private'"
|
||||
>
|
||||
🔒 Private
|
||||
</button>
|
||||
</div>
|
||||
<div
|
||||
ref="messagesContainer"
|
||||
class="flex flex-col flex-1 min-h-0 overflow-y-auto overflow-x-hidden p-2 min-w-0"
|
||||
>
|
||||
<div ref="messagesInner">
|
||||
<p
|
||||
v-for="message in messages"
|
||||
:key="message.id"
|
||||
class="break-words min-w-0 w-full"
|
||||
:class="{ 'text-secondary italic': message.private }"
|
||||
>
|
||||
<button
|
||||
v-if="isAdmin && message.id"
|
||||
type="button"
|
||||
class="text-tertiary hover:text-primary cursor-pointer mr-1"
|
||||
aria-label="Delete message"
|
||||
title="Delete message"
|
||||
@click="deleteMessage(message.id)"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
<span class="text-tertiary">{{ message.authorId }}:</span>
|
||||
<template
|
||||
v-for="(part, i) in parseMessageParts(message.text || '')"
|
||||
:key="i"
|
||||
>
|
||||
<Link
|
||||
v-if="part.type === 'link'"
|
||||
bare
|
||||
:href="part.value"
|
||||
target="_blank"
|
||||
class="text-primary underline break-all"
|
||||
>{{ part.value }}</Link
|
||||
>
|
||||
<span v-else>{{ part.value }}</span>
|
||||
</template>
|
||||
<template v-if="isUploadUrl(message.fileUrl)">
|
||||
<img
|
||||
v-if="kindOf(message.fileUrl) === 'image'"
|
||||
:src="message.fileUrl"
|
||||
alt="Uploaded image"
|
||||
loading="lazy"
|
||||
class="w-full max-w-full rounded block"
|
||||
/>
|
||||
<video
|
||||
v-else-if="kindOf(message.fileUrl) === 'video'"
|
||||
:src="message.fileUrl"
|
||||
controls
|
||||
preload="none"
|
||||
class="w-full max-w-full max-h-48 rounded block"
|
||||
/>
|
||||
<audio
|
||||
v-else-if="kindOf(message.fileUrl) === 'audio'"
|
||||
:src="message.fileUrl"
|
||||
controls
|
||||
preload="none"
|
||||
class="w-full max-w-full block"
|
||||
/>
|
||||
<Link
|
||||
v-else
|
||||
bare
|
||||
:href="message.fileUrl"
|
||||
target="_blank"
|
||||
class="underline break-all"
|
||||
>{{ message.fileUrl.split("/").pop() }}</Link
|
||||
>
|
||||
</template>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<input
|
||||
v-model="messageInput"
|
||||
@keyup.enter="sendMessage"
|
||||
:aria-label="isPrivateTab ? 'Private chat message' : 'Chat message'"
|
||||
:placeholder="isPrivateTab ? 'Admins only' : ''"
|
||||
/>
|
||||
<input
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
:accept="ACCEPT.any"
|
||||
class="hidden"
|
||||
@change="onFileSelected"
|
||||
/>
|
||||
<div class="flex gap-2">
|
||||
<Button class="flex-1" @click="sendMessage">Send</Button>
|
||||
<Button v-if="isAdmin" class="flex-1" @click="fileInput.click()"
|
||||
>Attach</Button
|
||||
>
|
||||
<Button v-if="!isNearBottom" class="flex-1" @click="goToBottom"
|
||||
>Bottom</Button
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<ChatPanel />
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.chat-tab {
|
||||
padding: 2px 6px;
|
||||
color: var(--color-primary);
|
||||
background-color: var(--color-link-bg);
|
||||
border: 1px solid transparent;
|
||||
font-family: var(--font-heading);
|
||||
font-size: 0.875rem;
|
||||
cursor: pointer;
|
||||
transition:
|
||||
background-color 120ms ease,
|
||||
border-color 120ms ease,
|
||||
color 120ms ease;
|
||||
}
|
||||
.chat-tab:hover {
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
.chat-tab.is-active {
|
||||
border-color: var(--color-primary);
|
||||
color: var(--color-tertiary);
|
||||
background-color: var(--color-surface-tint);
|
||||
}
|
||||
|
||||
@media (max-width: 850px) {
|
||||
.chat-root {
|
||||
max-height: none;
|
||||
height: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
<script setup>
|
||||
/**
|
||||
* One chat channel: message list, composer, file attach and admin delete.
|
||||
* Drives stores/messages.js. Not a widget itself — Chat.vue shows the public
|
||||
* channel and PrivateChat.vue the admins-only one.
|
||||
*
|
||||
* The socket carries both channels in one stream (the server only sends
|
||||
* private messages to admin connections), so `isPrivate` picks which half of
|
||||
* `messages` to show and also marks what you send — you cannot post to a
|
||||
* channel you are not looking at.
|
||||
*
|
||||
* Holds the socket while mounted — connect() on mount, disconnect() on
|
||||
* unmount; the store counts them, so two panels share one socket and it
|
||||
* exists only while a chat is on screen.
|
||||
*/
|
||||
import { ref, computed, watch, nextTick, onMounted, onUnmounted } from "vue";
|
||||
import Button from "@/components/input/Button.vue";
|
||||
import { useMessagesStore } from "@/stores/messages";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
import Header from "@/components/text/Header.vue";
|
||||
import Link from "@/components/text/Link.vue";
|
||||
import { ACCEPT, isUploadUrl, kindOf } from "@/uploads";
|
||||
|
||||
const messagesStore = useMessagesStore();
|
||||
const authStore = useAuthStore();
|
||||
const messageInput = ref("");
|
||||
const messagesContainer = ref(null);
|
||||
const messagesInner = ref(null);
|
||||
const fileInput = ref(null);
|
||||
const isAdmin = computed(() => !!authStore.user.admin);
|
||||
|
||||
const props = defineProps({
|
||||
// Only PrivateChat.vue sets this, and it mounts the panel for admins only.
|
||||
isPrivate: { type: Boolean, default: false },
|
||||
});
|
||||
|
||||
// The private panel is the only place private messages are shown, so the
|
||||
// public one stays public even for an admin.
|
||||
const messages = computed(() =>
|
||||
messagesStore.messages.filter((m) => !!m.private === props.isPrivate),
|
||||
);
|
||||
|
||||
// Autoscroll only when the reader is already at the bottom: scrolling up to
|
||||
// read history must not be yanked back down by an incoming message.
|
||||
const isNearBottom = ref(true);
|
||||
const SCROLL_THRESHOLD = 100;
|
||||
let resizeObserver = null;
|
||||
|
||||
function scrollToBottom() {
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.scrollTop = messagesContainer.value.scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
function scrollToBottomIfNear() {
|
||||
if (isNearBottom.value) {
|
||||
scrollToBottom();
|
||||
}
|
||||
}
|
||||
|
||||
function onScroll() {
|
||||
if (!messagesContainer.value) return;
|
||||
const { scrollHeight, scrollTop, clientHeight } = messagesContainer.value;
|
||||
isNearBottom.value =
|
||||
scrollHeight - scrollTop - clientHeight < SCROLL_THRESHOLD;
|
||||
}
|
||||
|
||||
function goToBottom() {
|
||||
isNearBottom.value = true;
|
||||
scrollToBottom();
|
||||
}
|
||||
|
||||
// nextTick: scrollHeight is only correct once Vue has patched the new row in.
|
||||
watch(
|
||||
() => messages.value.length,
|
||||
() => {
|
||||
nextTick(scrollToBottomIfNear);
|
||||
},
|
||||
);
|
||||
|
||||
function sendMessage() {
|
||||
const text = messageInput.value.trim();
|
||||
if (!text) return;
|
||||
isNearBottom.value = true;
|
||||
messagesStore.sendMessage(text, props.isPrivate);
|
||||
messageInput.value = "";
|
||||
}
|
||||
|
||||
function deleteMessage(id) {
|
||||
messagesStore.deleteMessage(id);
|
||||
}
|
||||
|
||||
async function onFileSelected(e) {
|
||||
const file = e.target.files[0];
|
||||
if (!file) return;
|
||||
isNearBottom.value = true;
|
||||
// The private flag both routes the upload to /uploads/private/ and marks the
|
||||
// message itself private; the two must agree.
|
||||
await messagesStore.uploadAndSendFile(file, props.isPrivate);
|
||||
fileInput.value.value = "";
|
||||
}
|
||||
|
||||
// Attachments are rendered only if isUploadUrl() accepts them: the fileUrl
|
||||
// arrives over the socket and is otherwise attacker-controlled.
|
||||
|
||||
// NOTE: a module-level /g regex carries `lastIndex` between calls. It is safe
|
||||
// here only because parseMessageParts() below always runs exec() to completion
|
||||
// (exec returns null, resetting lastIndex) before returning. An early return
|
||||
// from that loop would make the next message parse from the wrong offset.
|
||||
const urlRegex = /(https?:\/\/[^\s<]+)/g;
|
||||
|
||||
/** Split message text into alternating {type:'text'|'link', value} parts so
|
||||
* URLs can be rendered as anchors without using v-html. */
|
||||
function parseMessageParts(text) {
|
||||
const parts = [];
|
||||
let lastIndex = 0;
|
||||
let match;
|
||||
while ((match = urlRegex.exec(text)) !== null) {
|
||||
if (match.index > lastIndex) {
|
||||
parts.push({
|
||||
type: "text",
|
||||
value: text.slice(lastIndex, match.index),
|
||||
});
|
||||
}
|
||||
parts.push({ type: "link", value: match[1] });
|
||||
lastIndex = urlRegex.lastIndex;
|
||||
}
|
||||
if (lastIndex < text.length) {
|
||||
parts.push({ type: "text", value: text.slice(lastIndex) });
|
||||
}
|
||||
return parts;
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
messagesStore.connect();
|
||||
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.addEventListener("scroll", onScroll, {
|
||||
passive: true,
|
||||
});
|
||||
}
|
||||
|
||||
// Images and videos load after their row is inserted and change its height,
|
||||
// which the length watcher above has already missed. Observing the inner
|
||||
// wrapper catches those late reflows.
|
||||
if (messagesInner.value) {
|
||||
resizeObserver = new ResizeObserver(scrollToBottomIfNear);
|
||||
resizeObserver.observe(messagesInner.value);
|
||||
}
|
||||
|
||||
scrollToBottom();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
messagesStore.disconnect();
|
||||
|
||||
if (messagesContainer.value) {
|
||||
messagesContainer.value.removeEventListener("scroll", onScroll);
|
||||
}
|
||||
|
||||
if (resizeObserver) {
|
||||
resizeObserver.disconnect();
|
||||
resizeObserver = null;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="chat-root flex-col flex min-h-0">
|
||||
<Header>{{ isPrivate ? "🔒 Private Chat" : "Chat" }}</Header>
|
||||
<div
|
||||
ref="messagesContainer"
|
||||
class="flex flex-col flex-1 min-h-0 overflow-y-auto overflow-x-hidden p-2 min-w-0"
|
||||
>
|
||||
<div ref="messagesInner">
|
||||
<p
|
||||
v-for="message in messages"
|
||||
:key="message.id"
|
||||
class="break-words min-w-0 w-full"
|
||||
:class="{ 'text-secondary italic': message.private }"
|
||||
>
|
||||
<button
|
||||
v-if="isAdmin && message.id"
|
||||
type="button"
|
||||
class="text-tertiary hover:text-primary cursor-pointer mr-1"
|
||||
aria-label="Delete message"
|
||||
title="Delete message"
|
||||
@click="deleteMessage(message.id)"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
<span class="text-tertiary">{{ message.authorId }}:</span>
|
||||
<template
|
||||
v-for="(part, i) in parseMessageParts(message.text || '')"
|
||||
:key="i"
|
||||
>
|
||||
<Link
|
||||
v-if="part.type === 'link'"
|
||||
bare
|
||||
:href="part.value"
|
||||
target="_blank"
|
||||
class="text-primary underline break-all"
|
||||
>{{ part.value }}</Link
|
||||
>
|
||||
<span v-else>{{ part.value }}</span>
|
||||
</template>
|
||||
<template v-if="isUploadUrl(message.fileUrl)">
|
||||
<img
|
||||
v-if="kindOf(message.fileUrl) === 'image'"
|
||||
:src="message.fileUrl"
|
||||
alt="Uploaded image"
|
||||
loading="lazy"
|
||||
class="w-full max-w-full rounded block"
|
||||
/>
|
||||
<video
|
||||
v-else-if="kindOf(message.fileUrl) === 'video'"
|
||||
:src="message.fileUrl"
|
||||
controls
|
||||
preload="none"
|
||||
class="w-full max-w-full max-h-48 rounded block"
|
||||
/>
|
||||
<audio
|
||||
v-else-if="kindOf(message.fileUrl) === 'audio'"
|
||||
:src="message.fileUrl"
|
||||
controls
|
||||
preload="none"
|
||||
class="w-full max-w-full block"
|
||||
/>
|
||||
<Link
|
||||
v-else
|
||||
bare
|
||||
:href="message.fileUrl"
|
||||
target="_blank"
|
||||
class="underline break-all"
|
||||
>{{ message.fileUrl.split("/").pop() }}</Link
|
||||
>
|
||||
</template>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<input
|
||||
v-model="messageInput"
|
||||
@keyup.enter="sendMessage"
|
||||
:aria-label="isPrivate ? 'Private chat message' : 'Chat message'"
|
||||
:placeholder="isPrivate ? 'Admins only' : ''"
|
||||
/>
|
||||
<input
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
:accept="ACCEPT.any"
|
||||
class="hidden"
|
||||
@change="onFileSelected"
|
||||
/>
|
||||
<div class="flex gap-2">
|
||||
<Button class="flex-1" @click="sendMessage">Send</Button>
|
||||
<Button v-if="isAdmin" class="flex-1" @click="fileInput.click()"
|
||||
>Attach</Button
|
||||
>
|
||||
<Button v-if="!isNearBottom" class="flex-1" @click="goToBottom"
|
||||
>Bottom</Button
|
||||
>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
@media (max-width: 850px) {
|
||||
.chat-root {
|
||||
max-height: none;
|
||||
height: 100%;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,24 @@
|
||||
<script setup>
|
||||
/**
|
||||
* The admins-only chat widget: ChatPanel.vue on the private channel.
|
||||
*
|
||||
* The panel is mounted for admins only, so a non-admin never holds the socket
|
||||
* on its account. The box itself is not hidden: a canvas box shows to everyone
|
||||
* unless it is marked admin-only, and an empty box would look broken.
|
||||
*/
|
||||
import { computed } from "vue";
|
||||
import ChatPanel from "./ChatPanel.vue";
|
||||
import Header from "@/components/text/Header.vue";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
|
||||
const auth = useAuthStore();
|
||||
const isAdmin = computed(() => !!auth.user.admin);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ChatPanel v-if="isAdmin" is-private />
|
||||
<div v-else>
|
||||
<Header>🔒 Private Chat</Header>
|
||||
<p class="p-2 text-tertiary">Admins only.</p>
|
||||
</div>
|
||||
</template>
|
||||
@@ -1,14 +1,20 @@
|
||||
/**
|
||||
* WebSocket-backed chat, used by components/util/Chat.vue.
|
||||
* WebSocket-backed chat, used by components/util/ChatPanel.vue (behind both the
|
||||
* public Chat and the admins-only PrivateChat widgets).
|
||||
*
|
||||
* Owns one socket at a time, auto-reconnects with exponential backoff (1s
|
||||
* doubling to a 30s ceiling, reset on a successful open) and refuses to reconnect
|
||||
* after an intentional disconnect().
|
||||
* after an intentional close.
|
||||
*
|
||||
* Both chat widgets can be on screen at once and share the socket (it carries
|
||||
* both channels), so connect()/disconnect() are counted: the socket opens with
|
||||
* the first widget and closes with the last.
|
||||
*/
|
||||
|
||||
import { defineStore } from "pinia";
|
||||
import { ref, computed } from "vue";
|
||||
import { ref, computed, watch } from "vue";
|
||||
import { uploadFile } from "@/uploads";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
|
||||
/**
|
||||
* Same-origin ws:// or wss:// URL for the chat socket, derived from the page's
|
||||
@@ -32,11 +38,25 @@ export const useMessagesStore = defineStore("messages", () => {
|
||||
let intentionalClose = false;
|
||||
let reconnectDelay = INITIAL_RECONNECT_DELAY_MS;
|
||||
let reconnectTimer = null;
|
||||
// How many mounted widgets want the socket open.
|
||||
let users = 0;
|
||||
|
||||
const messagesCount = computed(() => messages.value.length);
|
||||
|
||||
/** Open the socket (no-op if one already exists) and arm auto-reconnect. */
|
||||
/** Register a widget that needs the socket, opening it if it is the first. */
|
||||
function connect() {
|
||||
users++;
|
||||
open();
|
||||
}
|
||||
|
||||
/** Release a connect(); the socket closes once no widget holds it. */
|
||||
function disconnect() {
|
||||
users = Math.max(0, users - 1);
|
||||
if (users === 0) close();
|
||||
}
|
||||
|
||||
/** Open the socket (no-op if one already exists) and arm auto-reconnect. */
|
||||
function open() {
|
||||
if (socket.value) return;
|
||||
intentionalClose = false;
|
||||
clearTimeout(reconnectTimer);
|
||||
@@ -93,7 +113,7 @@ export const useMessagesStore = defineStore("messages", () => {
|
||||
socket.value = null;
|
||||
if (!intentionalClose) {
|
||||
reconnectTimer = setTimeout(() => {
|
||||
connect();
|
||||
open();
|
||||
}, reconnectDelay);
|
||||
reconnectDelay = Math.min(reconnectDelay * 2, MAX_RECONNECT_DELAY_MS);
|
||||
}
|
||||
@@ -103,9 +123,9 @@ export const useMessagesStore = defineStore("messages", () => {
|
||||
/**
|
||||
* Close the socket and suppress auto-reconnect. Clearing `socket.value`
|
||||
* *before* calling close() is what makes the onclose guard above see a stale
|
||||
* socket, so a later connect() is not fought over by the dying one.
|
||||
* socket, so a later open() is not fought over by the dying one.
|
||||
*/
|
||||
function disconnect() {
|
||||
function close() {
|
||||
intentionalClose = true;
|
||||
clearTimeout(reconnectTimer);
|
||||
if (!socket.value) return;
|
||||
@@ -115,6 +135,19 @@ export const useMessagesStore = defineStore("messages", () => {
|
||||
ws.close();
|
||||
}
|
||||
|
||||
// Admin status is fixed when the socket connects (from the auth cookies), so
|
||||
// reopen after a login/logout to pick up or drop private messages. Here
|
||||
// rather than in the widgets so two mounted chats reconnect once, not twice.
|
||||
const auth = useAuthStore();
|
||||
watch(
|
||||
() => !!auth.user.admin,
|
||||
() => {
|
||||
if (users === 0) return;
|
||||
close();
|
||||
open();
|
||||
},
|
||||
);
|
||||
|
||||
/** Send a chat line. Silently drops it if the socket is not open. */
|
||||
function sendMessage(text, isPrivate = false) {
|
||||
if (!socket.value || !isConnected.value) return;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* Places-to-go list (the "Places" tab inside the Collage widget) — full CRUD,
|
||||
* Places-to-go list (the Places widget, views/home/Places.vue) — full CRUD,
|
||||
* built on createCrudStore().
|
||||
*
|
||||
* Not part of the homeData query because it is sign-in-only; PlacesTable.vue
|
||||
|
||||
@@ -1,125 +1,24 @@
|
||||
<script setup>
|
||||
/**
|
||||
* "鸟" widget on /stp — a two-tab cell: a <Slideshow> and, once signed in, the
|
||||
* Places list.
|
||||
*
|
||||
* The Places tab is added to `tabs` only when logged in, and the watcher forces
|
||||
* the view back to the collage on logout so a signed-out user is never left
|
||||
* staring at an empty private tab.
|
||||
* "鸟" widget on /stp — a <Slideshow>. The Places list that used to share it
|
||||
* as a second tab is its own widget now, views/home/Places.vue.
|
||||
*/
|
||||
import { ref, computed, watch } from "vue";
|
||||
import Slideshow from "@/components/util/Slideshow.vue";
|
||||
import Modal from "@/components/util/Modal.vue";
|
||||
import PlacesTable from "./PlacesTable.vue";
|
||||
import CreatePlace from "@/views/admin/CreatePlace.vue";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
|
||||
const images = [{ url: "/img/memes/pidgeon.gif", comment: "鸟" }];
|
||||
|
||||
const auth = useAuthStore();
|
||||
const loggedIn = computed(() => auth.loggedIn);
|
||||
|
||||
const tabs = computed(() => [
|
||||
{ id: "collage", label: "鸟" },
|
||||
...(loggedIn.value ? [{ id: "places", label: "Places" }] : []),
|
||||
]);
|
||||
|
||||
const activeTab = ref("collage");
|
||||
const showAdd = ref(false);
|
||||
|
||||
// On logout the Places tab disappears from `tabs`; without this the component
|
||||
// would be left pointing at a tab that no longer exists and render nothing.
|
||||
watch(loggedIn, (isIn) => {
|
||||
if (!isIn && activeTab.value === "places") activeTab.value = "collage";
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="collage-cell">
|
||||
<div class="tabs">
|
||||
<button
|
||||
v-for="tab in tabs"
|
||||
:key="tab.id"
|
||||
class="tab"
|
||||
:class="{ 'is-active': activeTab === tab.id }"
|
||||
@click="activeTab = tab.id"
|
||||
>
|
||||
{{ tab.label }}
|
||||
</button>
|
||||
<div class="tab-spacer" />
|
||||
<button
|
||||
v-if="loggedIn && activeTab === 'places'"
|
||||
class="tab add-btn"
|
||||
title="Add a place / thing to do"
|
||||
@click="showAdd = true"
|
||||
>
|
||||
+
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="tab-body">
|
||||
<Slideshow v-if="activeTab === 'collage'" :images="images" />
|
||||
<PlacesTable v-else-if="loggedIn" />
|
||||
</div>
|
||||
|
||||
<Modal v-model="showAdd">
|
||||
<CreatePlace @done="showAdd = false" @cancel="showAdd = false" />
|
||||
</Modal>
|
||||
<Slideshow :images="images" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.collage-cell {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
position: relative;
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.tabs {
|
||||
display: flex;
|
||||
align-items: stretch;
|
||||
gap: 2px;
|
||||
padding: 2px;
|
||||
border-bottom: 1px solid var(--color-quaternary);
|
||||
background-color: var(--color-surface-deep);
|
||||
}
|
||||
|
||||
.tab {
|
||||
background-color: var(--color-link-bg);
|
||||
color: var(--color-primary);
|
||||
border: 1px solid transparent;
|
||||
padding: 2px 8px;
|
||||
font-family: var(--font-heading);
|
||||
letter-spacing: 0.05em;
|
||||
cursor: pointer;
|
||||
transition:
|
||||
background-color 120ms ease,
|
||||
border-color 120ms ease,
|
||||
color 120ms ease;
|
||||
}
|
||||
.tab:hover {
|
||||
border-color: var(--color-primary);
|
||||
}
|
||||
.tab.is-active {
|
||||
border-color: var(--color-primary);
|
||||
color: var(--color-tertiary);
|
||||
background-color: var(--color-surface-tint);
|
||||
}
|
||||
|
||||
.tab-spacer {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.add-btn {
|
||||
min-width: 24px;
|
||||
padding: 0 6px;
|
||||
}
|
||||
|
||||
.tab-body {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Places-to-go widget: the <PlacesTable> under a header whose "+" opens the
|
||||
* create form. Sign-in only, like the places mutations themselves, so a
|
||||
* signed-out visitor gets a one-line prompt instead of an empty table.
|
||||
*
|
||||
* Its own "+" rather than <CreateToggle>, because that one is admin-only and
|
||||
* any signed-in user may add a place.
|
||||
*/
|
||||
import { computed, ref } from "vue";
|
||||
import Header from "@/components/text/Header.vue";
|
||||
import Modal from "@/components/util/Modal.vue";
|
||||
import PlacesTable from "./PlacesTable.vue";
|
||||
import CreatePlace from "@/views/admin/CreatePlace.vue";
|
||||
import { useAuthStore } from "@/stores/auth";
|
||||
|
||||
const auth = useAuthStore();
|
||||
const loggedIn = computed(() => auth.loggedIn);
|
||||
const showAdd = ref(false);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="places-cell">
|
||||
<Header class="text-left">
|
||||
Places
|
||||
<template v-if="loggedIn" #action>
|
||||
<button
|
||||
class="text-sm px-1"
|
||||
title="Add a place / thing to do"
|
||||
@click="showAdd = !showAdd"
|
||||
>
|
||||
{{ showAdd ? "x" : "+" }}
|
||||
</button>
|
||||
</template>
|
||||
</Header>
|
||||
<div class="places-body">
|
||||
<PlacesTable v-if="loggedIn" />
|
||||
<p v-else class="places-signin">Sign in to see places.</p>
|
||||
</div>
|
||||
|
||||
<Modal v-model="showAdd">
|
||||
<CreatePlace @done="showAdd = false" @cancel="showAdd = false" />
|
||||
</Modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.places-cell {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.places-body {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
position: relative;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.places-signin {
|
||||
padding: 12px;
|
||||
text-align: center;
|
||||
color: var(--color-muted);
|
||||
}
|
||||
</style>
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup>
|
||||
/**
|
||||
* The Places-to-go table inside the Collage widget. Sign-in only.
|
||||
* The Places-to-go table inside the Places widget (Places.vue). Sign-in only.
|
||||
*
|
||||
* Fetches lazily and at most once: onMounted covers an already-signed-in visitor,
|
||||
* the `loggedIn` watcher covers someone who signs in while the page is open, and
|
||||
|
||||
@@ -129,8 +129,8 @@ function bounce() {
|
||||
|
||||
onMounted(() => {
|
||||
fitWall();
|
||||
// The cell's size follows the home grid's breakpoints (and the scrollbars
|
||||
// appearing inside it), not just window resizes, so observe the element.
|
||||
// The cell's size follows the box it sits in, not just window resizes, so
|
||||
// observe the element.
|
||||
resizeObserver = new ResizeObserver(fitWall);
|
||||
resizeObserver.observe(touchscreen.value.$el);
|
||||
animId = requestAnimationFrame(bounce);
|
||||
@@ -143,7 +143,7 @@ onUnmounted(() => {
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Touchscreen ref="touchscreen">
|
||||
<Touchscreen ref="touchscreen" class="stamp-wall">
|
||||
<div class="wall" :style="{ '--cols': cols }">
|
||||
<template v-for="(stamp, i) in cells" :key="i">
|
||||
<Link v-if="stamp.href" bare :href="stamp.href">
|
||||
@@ -162,6 +162,15 @@ img {
|
||||
height: 59px;
|
||||
}
|
||||
|
||||
/* The wall drifts by itself and can be dragged, so scrollbars are only noise.
|
||||
Hidden rather than overflow: hidden, which would stop both. */
|
||||
.stamp-wall {
|
||||
scrollbar-width: none;
|
||||
}
|
||||
.stamp-wall::-webkit-scrollbar {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Fixed tracks rather than flex-wrap: the wall's size comes from --cols (set
|
||||
by fitWall), never from how much room the cell happens to have. */
|
||||
.wall {
|
||||
|
||||
@@ -17,6 +17,7 @@ export const WIDGETS = {
|
||||
Feed: { component: lazy(() => import("@/views/home/Feed.vue")), size: [235, 440] },
|
||||
Links: { component: lazy(() => import("@/views/home/Links.vue")), size: [155, 440] },
|
||||
Collage: { component: lazy(() => import("@/views/home/Collage.vue")), size: [395, 440] },
|
||||
Places: { component: lazy(() => import("@/views/home/Places.vue")), size: [395, 440] },
|
||||
Consumption: { component: lazy(() => import("@/views/home/Consumption.vue")), size: [315, 220] },
|
||||
Favorites: { component: lazy(() => import("@/views/home/Favorites.vue")), size: [235, 220] },
|
||||
Gym: { component: lazy(() => import("@/views/home/Gym2.vue")), size: [235, 220] },
|
||||
@@ -27,5 +28,6 @@ export const WIDGETS = {
|
||||
Timer: { component: lazy(() => import("@/components/util/Timer.vue")), size: [240, 130] },
|
||||
Radio: { component: lazy(() => import("@/components/util/Radio.vue")), size: [240, 160] },
|
||||
Chat: { component: lazy(() => import("@/components/util/Chat.vue")), size: [240, 450] },
|
||||
PrivateChat: { component: lazy(() => import("@/components/util/PrivateChat.vue")), size: [240, 450] },
|
||||
Commits: { component: lazy(() => import("@/components/util/CommitHistory.vue")), size: [240, 400] },
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user