Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5aced64669 | ||
|
|
d68fc33bcb |
@@ -59,6 +59,27 @@ We created `app/Services/ActivityService.php` to centralize the creation of syst
|
||||
* **Polymorphic Subject Linking**: System messages are linked to relevant entities (e.g., a `User` who joined a dynamic, a `Ledger` that was created) via a polymorphic `subject` relationship on the `messages` table. This allows system messages on the dashboard to link directly to the relevant entity.
|
||||
* **Seeder Refactoring**: The `DatabaseSeeder` was refactored to use the `ActivityService` to generate all system messages, ensuring consistency.
|
||||
|
||||
### 8. Event-Driven Automated System Logging
|
||||
We relocated the dynamic system activity message generation out of individual controller endpoints and into the **Eloquent `Mutation` Model's `booted` -> `created` event hook**.
|
||||
* **Unified generation:** Any mutation creation (whether occurring via a controller submission, an automated Pest test factory, or seeders during `php artisan db:seed`) now automatically and reliably generates correct system log messages.
|
||||
* **Database Seeder fixed:** Reverted the database seeder (`DatabaseSeeder.php`) back to using standard clean Eloquent creations. Since model events automatically trigger, `php artisan db:seed` executes cleanly and builds a rich, fully populated database history with system messages out-of-the-box.
|
||||
|
||||
### 9. Standardized Policy-Driven UI Capabilities (`can` prop)
|
||||
To maintain strict data security and clean up front-end markup, we eliminated unstandardized, hardcoded client-side role checks (like `isOwner` properties or manual `pivot.role === 'owner'` checks) and replaced them completely with dynamic **policy-driven capabilities** returned directly from Laravel policies as `can` objects.
|
||||
* **Centralized checks:** Both the dynamic and ledger show routes return a standard `can` prop to the frontend (e.g., `can: { update: boolean, close: boolean }`).
|
||||
* **Polymorphic Resource Capabilities:** Each mutation model is wrapped in `MutationResource` which appends its own localized policy checks (`update` for approving suggestions, `void` for voiding) at the individual record level.
|
||||
* **State-based policies:** The `MutationPolicy` methods enforce both ownership authorization and state-based business constraints simultaneously (e.g., a mutation can be approved/updated *only* if its status is currently `'pending'`; and can be voided *only* if its status is not `'voided'`). This keeps the Vue templates purely declarative (e.g., `v-if="mutation.can.update"`) and automatically protects the backend controllers against illegal state transitions.
|
||||
|
||||
### 10. Ledger-Scoped Predefined Mutation Templates ("Rewards")
|
||||
Predefined mutations act as point-based templates ("purchases" or reusable chores) and belong strictly to specific **Ledgers** instead of broad Dynamics.
|
||||
* **Domain Alignment:** Moving the resource nesting under ledgers (`dynamics.ledgers.predefined-mutations`) aligns perfectly with the mental model of spending points on a ledger.
|
||||
* **Type-Less Rewards:** To simplify both the database schema and UI/UX, we eliminated the explicit `type` column ('reward' vs. 'penalty') from predefined mutations. They act as generic point-carrying "Rewards" whose amount can naturellement be positive (earning points) or negative (making a purchase / deducting points) without requiring restrictive explicit categorization.
|
||||
|
||||
### 11. Silent XHR Chat Pagination & Smooth Scrolling UX
|
||||
To optimize chat-feed performance and improve overall user experience:
|
||||
* **Silent pagination (No URL pollution):** Rather than using Inertia `router.get` visits which push `?page=x` into the browser URL and break history during page reloads, we implemented a **silent background fetch** (using native browser `fetch()`) that queries our dedicated messages JSON API endpoints and prepends older messages silently to the feed.
|
||||
* **Scroll Preservation:** Added `preserveScroll: true` to the Inertia `form.post` call in `Chat.vue` to prevent the active page scroll position from jumping or shifting when a new message is successfully submitted.
|
||||
|
||||
## Initial Database Schema
|
||||
|
||||
I will start with a basic schema and evolve it as I build features.
|
||||
|
||||
@@ -14,4 +14,19 @@ Welcome to the Ledgerrz codebase! This file defines the persistent guidelines, a
|
||||
* **PHP/Laravel:** PHP 8.4 & Laravel 13. Adhere to typed parameters and return values. Ensure controllers extend properly and use required authorization traits (e.g., `AuthorizesRequests`).
|
||||
* **Frontend Styling (BEM):** Replaced direct Tailwind inline utility-class markup with **BEM (Block, Element, Modifier)**. All custom component styles must live inside `<style scoped>` blocks with a relative `@reference "../../css/app.css"` directive to pull variables without duplications.
|
||||
* **Real-time Broadcasting:** Powered by `@laravel/echo-vue` with fallback configurations and Vite deduplication rules configured in `vite.config.ts`.
|
||||
* **Testing:** Powered by Pest PHP (v4). Every backend controller, event, or model change must be validated by running `vendor/bin/pest`.
|
||||
* **Testing & Isolation:** Powered by Pest PHP (v4). Every backend controller, event, or model change must be validated by running tests. To prevent local `.env` variables from polluting the CLI test execution (causing CSRF/session 419 errors), **always** run tests in an isolated environment using:
|
||||
```bash
|
||||
env -i PATH="$PATH" php artisan test
|
||||
```
|
||||
* **Standardized Authorization (`can` prop):** Never write manual role checks (such as `pivot.role === 'owner'`) or hardcoded boolean flags (such as `isOwner`) inside Vue pages or components. Instead, always leverage Laravel policies on the backend and pass permissions reactively to the frontend as structured `can` objects (e.g., `can: { update: boolean, close: boolean }`).
|
||||
* **Vue-Defined Breadcrumbs Layout:** All page-specific breadcrumbs should be declared locally inside the page's `.vue` file rather than returned from controllers. For dynamic, prop-dependent paths, always use the Inertia v3 layout callback function inside `defineOptions`:
|
||||
```typescript
|
||||
defineOptions({
|
||||
layout: (props: any) => ({
|
||||
breadcrumbs: [
|
||||
{ title: 'Dynamics', href: route('dynamics.index') },
|
||||
{ title: props.dynamic.name, href: route('dynamics.show', props.dynamic.id) }
|
||||
]
|
||||
})
|
||||
});
|
||||
```
|
||||
|
||||
@@ -41,3 +41,27 @@ During this session, we successfully built out and verified several core archite
|
||||
* Configured real-time notifications utilizing Laravel Reverb.
|
||||
* Documented CLI environment test pollution learnings inside `AGENTS.md` to prevent future CSRF `419` errors.
|
||||
* Ensured full production assets compilation (`npm run build`) and achieved **45/45 passing Pest PHP tests with 206 assertions**.
|
||||
|
||||
6. **Ledger-Scoped Predefined Mutation Templates ("Rewards")**:
|
||||
* Associated reusable point-based predefined mutation templates under specific Ledgers rather than broad Dynamics, mapping perfectly to the mental model of spending points on a ledger.
|
||||
* Designed them purely as "Rewards" with positive or negative point amounts (handling both demerit-purchases and chores), removing the obsolete `type` categorization for a simpler, type-less, and sleeker UI/UX.
|
||||
|
||||
7. **User Activity Profiling & Detail Pages**:
|
||||
* Created a dynamic user detail page (`dynamics.users.show`) scoped to each dynamic. It displays a participant's role, custom display name, fallback real name, and a clean chronological listing of their 10 most recent mutations (activities) in that dynamic.
|
||||
|
||||
8. **Polymorphic System Message placeholders & Dynamic Client-Side Linking**:
|
||||
* Refactored system log activity messages to use native `<user:userId>` placeholders and associated them with polymorphic `subject_id` and `subject_type` objects.
|
||||
* On the client-side, the chat component parses these placeholders into rich, clickable links to User Profiles, and dynamically matches and wraps referenced ledger names into links pointing directly to the ledger show page.
|
||||
* Added backend-side placeholder resolution inside `ActivityService` for the dashboard, ensuring unread system logs translate cleanly to real names across multiple dynamics.
|
||||
|
||||
9. **Vite/Inertia v3 Layout Callback Breadcrumbs**:
|
||||
* Utilized Inertia v3's powerful new layout callback API inside Vue page `defineOptions` to reactively resolve page-specific dynamic breadcrumbs at runtime using parsed page props, making the pages self-contained and keeping PHP controllers beautifully slim.
|
||||
|
||||
10. **Silent background Chat Pagination & Smooth Scrolling UX**:
|
||||
* Implemented silent background XHR queries (using native browser `fetch()`) on our dedicated message JSON API routes to load older chat pages, completely bypassing browser history/URL pollution and preserving page state on refreshes.
|
||||
* Integrated `preserveScroll: true` inside chat form submissions to completely prevent scroll jumps when sending messages.
|
||||
|
||||
11. **Standardized Policy-Driven UI Capabilities**:
|
||||
* Eliminated unstandardized client-side role checks and boolean flags, replacing them with structured `can` capability objects returned directly from Laravel policies.
|
||||
* Combined permission validation with state-based business constraints in `MutationPolicy` (e.g., suggestions can be approved/rejected only if `'pending'`; and voided only if not `'voided'`), securing both the frontend action buttons and backend controllers simultaneously.
|
||||
* Achieved **65/65 passing Pest PHP tests with 333 assertions**.
|
||||
+199
-236
@@ -1,9 +1,11 @@
|
||||
<script setup lang="ts">
|
||||
import { useForm, usePage, router } from '@inertiajs/vue3';
|
||||
import { usePage } from '@inertiajs/vue3';
|
||||
import { useEcho, echoIsConfigured, configureEcho } from '@laravel/echo-vue';
|
||||
import { Paperclip, Info } from '@lucide/vue';
|
||||
import { ref, computed, watch } from 'vue';
|
||||
import { route } from 'ziggy-js';
|
||||
import ChatInput from './chat/ChatInput.vue';
|
||||
import ChatSystemMessage from './chat/ChatSystemMessage.vue';
|
||||
import ChatUserMessage from './chat/ChatUserMessage.vue';
|
||||
|
||||
const props = withDefaults(
|
||||
defineProps<{
|
||||
@@ -131,30 +133,10 @@ if (!echoIsConfigured()) {
|
||||
});
|
||||
}
|
||||
|
||||
const fileInput = ref<HTMLInputElement | null>(null);
|
||||
|
||||
const form = useForm({
|
||||
content: '',
|
||||
media: [] as File[],
|
||||
});
|
||||
|
||||
useEcho(`chats.${props.chat.id}`, 'MessageSent', (e: any) => {
|
||||
messages.value.push(e.message);
|
||||
});
|
||||
|
||||
function formatTimestamp(isoString: string): { full: string; time: string } {
|
||||
const date = new Date(isoString);
|
||||
|
||||
return {
|
||||
full: date.toLocaleString(),
|
||||
time: date.toLocaleTimeString([], {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
const participantsById = computed(() => {
|
||||
const list = props.participants || [];
|
||||
|
||||
@@ -175,83 +157,6 @@ const participantsById = computed(() => {
|
||||
);
|
||||
});
|
||||
|
||||
function parseMessageContent(message: {
|
||||
content: string;
|
||||
subject_id?: number | null;
|
||||
subject_type?: string | null;
|
||||
subject?: any;
|
||||
}) {
|
||||
let content = message.content;
|
||||
|
||||
// 1. Replace <user:id> placeholders with links to their dynamic profile
|
||||
const userRegex = /<user:(\d+)>/g;
|
||||
content = content.replace(userRegex, (match, userId) => {
|
||||
const user = participantsById.value[Number(userId)];
|
||||
|
||||
if (user) {
|
||||
const url = route('dynamics.users.show', [props.dynamicId, Number(userId)]);
|
||||
|
||||
return `<a href="${url}" class="c-chat__user-link font-semibold text-blue-500 hover:underline">${
|
||||
user.pivot?.display_name ?? user.name
|
||||
}</a>`;
|
||||
}
|
||||
|
||||
return `User #${userId}`;
|
||||
});
|
||||
|
||||
// 2. Link subjects if found in the text
|
||||
if (message.subject_id && message.subject_type) {
|
||||
if (
|
||||
message.subject_type === 'App\\Models\\Mutation' ||
|
||||
message.subject_type === 'App\\Models\\Ledger'
|
||||
) {
|
||||
const ledgerId =
|
||||
message.subject_type === 'App\\Models\\Mutation'
|
||||
? message.subject?.ledger_id
|
||||
: message.subject?.id;
|
||||
|
||||
const ledgerName =
|
||||
message.subject_type === 'App\\Models\\Mutation'
|
||||
? message.subject?.ledger?.name
|
||||
: message.subject?.name;
|
||||
|
||||
if (ledgerId && ledgerName) {
|
||||
const ledgerUrl = route('dynamics.ledgers.show', [
|
||||
props.dynamicId,
|
||||
ledgerId,
|
||||
]);
|
||||
|
||||
const escapedName = ledgerName.replace(
|
||||
/[-\/\\^$*+?.()|[\]{}]/g,
|
||||
'\\$&',
|
||||
);
|
||||
|
||||
const nameRegex = new RegExp(`"${escapedName}"`, 'g');
|
||||
content = content.replace(
|
||||
nameRegex,
|
||||
`"<a href="${ledgerUrl}" class="c-chat__subject-link font-semibold text-blue-500 hover:underline">${ledgerName}</a>"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
function handleFileChange(event: Event) {
|
||||
const files = (event.target as HTMLInputElement).files;
|
||||
|
||||
if (files) {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
form.media.push(files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeFile(index: number) {
|
||||
form.media.splice(index, 1);
|
||||
}
|
||||
|
||||
const currentUser = computed(() => usePage().props.auth?.user);
|
||||
|
||||
function isOwnMessage(messageUserId: number | null): boolean {
|
||||
@@ -262,19 +167,6 @@ function isOwnMessage(messageUserId: number | null): boolean {
|
||||
return currentUser.value && currentUser.value.id === messageUserId;
|
||||
}
|
||||
|
||||
function submit() {
|
||||
form.post(route('chats.messages.store', props.chat.id), {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.reset();
|
||||
|
||||
if (fileInput.value) {
|
||||
fileInput.value.value = '';
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Lightbox Modal state
|
||||
const activeLightboxUrl = ref<string | null>(null);
|
||||
const activeLightboxType = ref<'image' | 'video' | null>(null);
|
||||
@@ -314,138 +206,29 @@ function closeLightbox() {
|
||||
]"
|
||||
>
|
||||
<!-- Standard User Chat Message -->
|
||||
<template v-if="message.user">
|
||||
<div class="c-chat__message-header">
|
||||
<span class="c-chat__message-author">{{
|
||||
message.user.name
|
||||
}}</span>
|
||||
<span
|
||||
class="c-chat__message-time"
|
||||
:title="formatTimestamp(message.created_at).full"
|
||||
>
|
||||
{{ formatTimestamp(message.created_at).time }}
|
||||
</span>
|
||||
</div>
|
||||
<p class="c-chat__message-text" v-html="parseMessageContent(message)"></p>
|
||||
|
||||
<!-- Attached Media Display -->
|
||||
<div
|
||||
v-if="message.media && message.media.length > 0"
|
||||
class="c-chat__message-media"
|
||||
>
|
||||
<div
|
||||
v-for="item in message.media"
|
||||
:key="item.id"
|
||||
class="c-chat__media-item"
|
||||
>
|
||||
<img
|
||||
v-if="item.mime_type.startsWith('image/')"
|
||||
:src="item.url"
|
||||
:alt="item.file_name"
|
||||
class="c-chat__image cursor-pointer transition-opacity hover:opacity-90"
|
||||
@click="openLightbox(item.url, item.mime_type)"
|
||||
/>
|
||||
<div
|
||||
v-else-if="item.mime_type.startsWith('video/')"
|
||||
class="relative cursor-pointer transition-opacity hover:opacity-90"
|
||||
@click="openLightbox(item.url, item.mime_type)"
|
||||
>
|
||||
<video
|
||||
:src="item.url"
|
||||
class="c-chat__video"
|
||||
></video>
|
||||
<div class="c-chat__play-overlay">▶</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<ChatUserMessage
|
||||
v-if="message.user"
|
||||
:message="message"
|
||||
:participants-by-id="participantsById"
|
||||
:dynamic-id="dynamicId"
|
||||
@open-lightbox="openLightbox"
|
||||
/>
|
||||
|
||||
<!-- Subtle Activity Log System Message -->
|
||||
<template v-else>
|
||||
<div class="c-chat__system-inner">
|
||||
<Info class="c-chat__system-icon" />
|
||||
<span
|
||||
class="c-chat__system-text"
|
||||
v-html="parseMessageContent(message)"
|
||||
></span>
|
||||
<span
|
||||
class="c-chat__system-time"
|
||||
:title="formatTimestamp(message.created_at).full"
|
||||
>
|
||||
{{ formatTimestamp(message.created_at).time }}
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
<ChatSystemMessage
|
||||
v-else
|
||||
:message="message"
|
||||
:participants-by-id="participantsById"
|
||||
:dynamic-id="dynamicId"
|
||||
/>
|
||||
</div>
|
||||
<div v-if="messages.length === 0" class="c-chat__empty">
|
||||
No messages yet.
|
||||
</div>
|
||||
</div>
|
||||
<form @submit.prevent="submit" class="c-chat__form">
|
||||
<div class="c-chat__form-group">
|
||||
<label for="content" class="c-chat__label">Message</label>
|
||||
<textarea
|
||||
v-model="form.content"
|
||||
id="content"
|
||||
rows="3"
|
||||
class="c-chat__textarea"
|
||||
placeholder="Type a message... (Press Enter to send, Shift+Enter for newline)"
|
||||
@keydown.enter.exact.prevent="submit"
|
||||
></textarea>
|
||||
<div v-if="form.errors.content" class="c-chat__error">
|
||||
{{ form.errors.content }}
|
||||
</div>
|
||||
|
||||
<!-- Attachment Button & Hidden Input -->
|
||||
<div class="c-chat__attachment-container">
|
||||
<button
|
||||
type="button"
|
||||
@click="fileInput?.click()"
|
||||
class="c-chat__attach-btn"
|
||||
>
|
||||
<Paperclip class="c-chat__attach-icon" />
|
||||
Attach Photos/Videos
|
||||
</button>
|
||||
<input
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
multiple
|
||||
accept="image/*,video/*"
|
||||
class="hidden"
|
||||
@change="handleFileChange"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Previews List -->
|
||||
<div v-if="form.media.length > 0" class="c-chat__preview-list">
|
||||
<div
|
||||
v-for="(file, index) in form.media"
|
||||
:key="index"
|
||||
class="c-chat__preview-item"
|
||||
>
|
||||
<span class="c-chat__preview-name">{{
|
||||
file.name
|
||||
}}</span>
|
||||
<button
|
||||
type="button"
|
||||
@click="removeFile(index)"
|
||||
class="c-chat__preview-remove"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="c-chat__submit-box">
|
||||
<button
|
||||
type="submit"
|
||||
:disabled="form.processing"
|
||||
class="c-chat__button"
|
||||
>
|
||||
Send
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
<!-- Cohesive Chat input Form -->
|
||||
<ChatInput :chat-id="chat.id" />
|
||||
|
||||
<!-- Gorgeous Dark Lightbox Modal -->
|
||||
<div v-if="activeLightboxUrl" class="c-lightbox" @click="closeLightbox">
|
||||
@@ -467,3 +250,183 @@ function closeLightbox() {
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
@reference "../../css/app.css";
|
||||
|
||||
.c-chat {
|
||||
@apply flex flex-col h-[500px];
|
||||
background-color: var(--card);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius);
|
||||
}
|
||||
|
||||
.c-chat__title {
|
||||
@apply p-4 font-bold border-b text-sm;
|
||||
border-color: var(--border);
|
||||
color: var(--foreground);
|
||||
}
|
||||
|
||||
.c-chat__list {
|
||||
@apply flex-1 overflow-y-auto p-4 space-y-4;
|
||||
}
|
||||
|
||||
.c-chat__load-more {
|
||||
@apply flex justify-center py-2;
|
||||
}
|
||||
|
||||
.c-chat__load-more-btn {
|
||||
@apply text-xs font-semibold text-blue-500 hover:underline;
|
||||
}
|
||||
|
||||
.c-chat__message {
|
||||
@apply max-w-[75%] p-3 rounded-lg flex flex-col gap-1;
|
||||
}
|
||||
|
||||
.c-chat__message--own {
|
||||
@apply self-end;
|
||||
background-color: var(--primary);
|
||||
color: var(--primary-foreground);
|
||||
border-bottom-right-radius: 0;
|
||||
|
||||
.c-chat__message-author {
|
||||
@apply hidden;
|
||||
}
|
||||
|
||||
.c-chat__message-time {
|
||||
@apply text-blue-100;
|
||||
}
|
||||
|
||||
.c-chat__message-text {
|
||||
color: var(--primary-foreground);
|
||||
}
|
||||
}
|
||||
|
||||
.c-chat__message--other {
|
||||
@apply self-start;
|
||||
background-color: var(--muted);
|
||||
color: var(--muted-foreground);
|
||||
border-bottom-left-radius: 0;
|
||||
}
|
||||
|
||||
.c-chat__message--system {
|
||||
@apply self-center max-w-full w-full bg-transparent border-0 p-0 text-center gap-0;
|
||||
}
|
||||
|
||||
.c-chat__message-header {
|
||||
@apply flex items-baseline gap-2 mb-1;
|
||||
}
|
||||
|
||||
.c-chat__message-author {
|
||||
@apply font-semibold text-xs;
|
||||
}
|
||||
|
||||
.c-chat__message-time {
|
||||
@apply text-[10px];
|
||||
color: var(--muted-foreground);
|
||||
}
|
||||
|
||||
.c-chat__message-text {
|
||||
@apply text-sm break-words whitespace-pre-wrap leading-relaxed;
|
||||
}
|
||||
|
||||
.c-chat__message-media {
|
||||
@apply mt-2 flex flex-wrap gap-2;
|
||||
}
|
||||
|
||||
.c-chat__media-item {
|
||||
@apply max-w-[120px] overflow-hidden rounded border border-black dark:border-gray-600 bg-black;
|
||||
}
|
||||
|
||||
.c-chat__image {
|
||||
@apply h-auto max-h-[80px] w-full object-cover;
|
||||
}
|
||||
|
||||
.c-chat__video {
|
||||
@apply h-auto max-h-[80px] w-full;
|
||||
}
|
||||
|
||||
.c-chat__play-overlay {
|
||||
@apply absolute inset-0 flex items-center justify-center bg-black/40 text-lg font-bold text-white;
|
||||
}
|
||||
|
||||
.c-chat__system-inner {
|
||||
@apply inline-flex items-center gap-2 bg-neutral-100 dark:bg-neutral-900/30 px-3 py-1 rounded-full text-xs text-neutral-500 dark:text-neutral-400;
|
||||
}
|
||||
|
||||
.c-chat__system-icon {
|
||||
@apply size-3.5;
|
||||
}
|
||||
|
||||
.c-chat__system-text {
|
||||
@apply font-medium leading-relaxed;
|
||||
}
|
||||
|
||||
.c-chat__system-time {
|
||||
@apply text-[10px] opacity-75 ml-1;
|
||||
}
|
||||
|
||||
.c-chat__empty {
|
||||
@apply text-center text-xs py-8;
|
||||
color: var(--muted-foreground);
|
||||
}
|
||||
|
||||
.c-chat__form {
|
||||
@apply p-4 border-t flex flex-col gap-2 bg-neutral-50 dark:bg-neutral-900/10;
|
||||
border-color: var(--border);
|
||||
}
|
||||
|
||||
.c-chat__form-group {
|
||||
@apply relative flex flex-col gap-1;
|
||||
}
|
||||
|
||||
.c-chat__label {
|
||||
@apply sr-only;
|
||||
}
|
||||
|
||||
.c-chat__textarea {
|
||||
@apply w-full rounded border p-2 text-sm resize-none focus:outline-none focus:ring-1 focus:ring-blue-500 dark:bg-neutral-900 dark:border-neutral-800;
|
||||
color: var(--foreground);
|
||||
border-color: var(--border);
|
||||
}
|
||||
|
||||
.c-chat__error {
|
||||
@apply text-xs text-red-500 mt-1;
|
||||
}
|
||||
|
||||
.c-chat__attachment-container {
|
||||
@apply flex items-center mt-1;
|
||||
}
|
||||
|
||||
.c-chat__attach-btn {
|
||||
@apply inline-flex items-center gap-1.5 text-xs text-neutral-500 hover:text-neutral-900 dark:hover:text-neutral-100 transition-colors;
|
||||
}
|
||||
|
||||
.c-chat__attach-icon {
|
||||
@apply size-3.5;
|
||||
}
|
||||
|
||||
.c-chat__preview-list {
|
||||
@apply mt-2 flex flex-wrap gap-2;
|
||||
}
|
||||
|
||||
.c-chat__preview-item {
|
||||
@apply inline-flex items-center gap-2 bg-neutral-100 dark:bg-neutral-900/50 px-2 py-1 rounded text-xs;
|
||||
}
|
||||
|
||||
.c-chat__preview-name {
|
||||
@apply max-w-[150px] truncate text-neutral-600 dark:text-neutral-400;
|
||||
}
|
||||
|
||||
.c-chat__preview-remove {
|
||||
@apply text-neutral-400 hover:text-red-500 transition-colors;
|
||||
}
|
||||
|
||||
.c-chat__submit-box {
|
||||
@apply flex justify-end mt-1;
|
||||
}
|
||||
|
||||
.c-chat__button {
|
||||
@apply inline-flex items-center justify-center rounded-md border border-transparent bg-gray-800 px-4 py-2 text-xs font-semibold tracking-widest text-white uppercase transition duration-150 ease-in-out hover:bg-gray-700 focus:bg-gray-700 focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 focus:outline-none active:bg-gray-900 dark:bg-gray-200 dark:text-gray-800 dark:hover:bg-white dark:focus:bg-white dark:focus:ring-offset-gray-800 dark:active:bg-gray-300;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
<script setup lang="ts">
|
||||
import { useForm } from '@inertiajs/vue3';
|
||||
import { Paperclip } from '@lucide/vue';
|
||||
import { ref } from 'vue';
|
||||
import { route } from 'ziggy-js';
|
||||
|
||||
const props = defineProps<{
|
||||
chatId: number;
|
||||
}>();
|
||||
|
||||
const fileInput = ref<HTMLInputElement | null>(null);
|
||||
|
||||
const form = useForm({
|
||||
content: '',
|
||||
media: [] as File[],
|
||||
});
|
||||
|
||||
function handleFileChange(event: Event) {
|
||||
const files = (event.target as HTMLInputElement).files;
|
||||
|
||||
if (files) {
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
form.media.push(files[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeFile(index: number) {
|
||||
form.media.splice(index, 1);
|
||||
}
|
||||
|
||||
function submit() {
|
||||
form.post(route('chats.messages.store', props.chatId), {
|
||||
preserveScroll: true,
|
||||
onSuccess: () => {
|
||||
form.reset();
|
||||
|
||||
if (fileInput.value) {
|
||||
fileInput.value.value = '';
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<form @submit.prevent="submit" class="c-chat__form">
|
||||
<div class="c-chat__form-group">
|
||||
<label for="content" class="c-chat__label">Message</label>
|
||||
<textarea
|
||||
v-model="form.content"
|
||||
id="content"
|
||||
rows="3"
|
||||
class="c-chat__textarea"
|
||||
placeholder="Type a message... (Press Enter to send, Shift+Enter for newline)"
|
||||
@keydown.enter.exact.prevent="submit"
|
||||
></textarea>
|
||||
<div v-if="form.errors.content" class="c-chat__error">
|
||||
{{ form.errors.content }}
|
||||
</div>
|
||||
|
||||
<!-- Attachment Button & Hidden Input -->
|
||||
<div class="c-chat__attachment-container">
|
||||
<button
|
||||
type="button"
|
||||
@click="fileInput?.click()"
|
||||
class="c-chat__attach-btn"
|
||||
>
|
||||
<Paperclip class="c-chat__attach-icon" />
|
||||
Attach Photos/Videos
|
||||
</button>
|
||||
<input
|
||||
ref="fileInput"
|
||||
type="file"
|
||||
multiple
|
||||
accept="image/*,video/*"
|
||||
class="hidden"
|
||||
@change="handleFileChange"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<!-- Previews List -->
|
||||
<div v-if="form.media.length > 0" class="c-chat__preview-list">
|
||||
<div
|
||||
v-for="(file, index) in form.media"
|
||||
:key="index"
|
||||
class="c-chat__preview-item"
|
||||
>
|
||||
<span class="c-chat__preview-name">{{
|
||||
file.name
|
||||
}}</span>
|
||||
<button
|
||||
type="button"
|
||||
@click="removeFile(index)"
|
||||
class="c-chat__preview-remove"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="c-chat__submit-box">
|
||||
<button
|
||||
type="submit"
|
||||
:disabled="form.processing"
|
||||
class="c-chat__button"
|
||||
>
|
||||
Send
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</template>
|
||||
@@ -0,0 +1,108 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue';
|
||||
import { route } from 'ziggy-js';
|
||||
import { Info } from '@lucide/vue';
|
||||
|
||||
const props = defineProps<{
|
||||
message: {
|
||||
id: number;
|
||||
content: string;
|
||||
created_at: string;
|
||||
subject_id?: number | null;
|
||||
subject_type?: string | null;
|
||||
subject?: any;
|
||||
};
|
||||
participantsById: Record<
|
||||
number,
|
||||
{
|
||||
id: number;
|
||||
name: string;
|
||||
pivot?: { display_name: string | null } | null;
|
||||
}
|
||||
>;
|
||||
dynamicId: string;
|
||||
}>();
|
||||
|
||||
function formatTimestamp(isoString: string): { full: string; time: string } {
|
||||
const date = new Date(isoString);
|
||||
return {
|
||||
full: date.toLocaleString(),
|
||||
time: date.toLocaleTimeString([], {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
const parsedContent = computed(() => {
|
||||
let content = props.message.content;
|
||||
|
||||
// 1. Replace <user:id> placeholders with links to their dynamic profile
|
||||
const userRegex = /<user:(\d+)>/g;
|
||||
content = content.replace(userRegex, (match, userId) => {
|
||||
const user = props.participantsById[Number(userId)];
|
||||
if (user) {
|
||||
const url = route('dynamics.users.show', [props.dynamicId, Number(userId)]);
|
||||
return `<a href="${url}" class="c-chat__user-link font-semibold text-blue-500 hover:underline">${
|
||||
user.pivot?.display_name ?? user.name
|
||||
}</a>`;
|
||||
}
|
||||
return `User #${userId}`;
|
||||
});
|
||||
|
||||
// 2. Link subjects if found in the text
|
||||
if (props.message.subject_id && props.message.subject_type) {
|
||||
if (
|
||||
props.message.subject_type === 'mutation' ||
|
||||
props.message.subject_type === 'ledger'
|
||||
) {
|
||||
const ledgerId =
|
||||
props.message.subject_type === 'mutation'
|
||||
? props.message.subject?.ledger_id
|
||||
: props.message.subject?.id;
|
||||
|
||||
const ledgerName =
|
||||
props.message.subject_type === 'mutation'
|
||||
? props.message.subject?.ledger?.name
|
||||
: props.message.subject?.name;
|
||||
|
||||
if (ledgerId && ledgerName) {
|
||||
const ledgerUrl = route('dynamics.ledgers.show', [
|
||||
props.dynamicId,
|
||||
ledgerId,
|
||||
]);
|
||||
|
||||
const escapedName = ledgerName.replace(
|
||||
/[-\/\\^$*+?.()|[\]{}]/g,
|
||||
'\\$&',
|
||||
);
|
||||
|
||||
const nameRegex = new RegExp(`"${escapedName}"`, 'g');
|
||||
content = content.replace(
|
||||
nameRegex,
|
||||
`"<a href="${ledgerUrl}" class="c-chat__subject-link font-semibold text-blue-500 hover:underline">${ledgerName}</a>"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return content;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="c-chat__system-inner">
|
||||
<Info class="c-chat__system-icon" />
|
||||
<span
|
||||
class="c-chat__system-text"
|
||||
v-html="parsedContent"
|
||||
></span>
|
||||
<span
|
||||
class="c-chat__system-time"
|
||||
:title="formatTimestamp(message.created_at).full"
|
||||
>
|
||||
{{ formatTimestamp(message.created_at).time }}
|
||||
</span>
|
||||
</div>
|
||||
</template>
|
||||
@@ -0,0 +1,150 @@
|
||||
<script setup lang="ts">
|
||||
import { computed } from 'vue';
|
||||
import { route } from 'ziggy-js';
|
||||
|
||||
const props = defineProps<{
|
||||
message: {
|
||||
id: number;
|
||||
user: { id: number; name: string } | null;
|
||||
content: string;
|
||||
created_at: string;
|
||||
subject_id?: number | null;
|
||||
subject_type?: string | null;
|
||||
subject?: any;
|
||||
media?: Array<{
|
||||
id: number;
|
||||
url: string;
|
||||
file_name: string;
|
||||
mime_type: string;
|
||||
}>;
|
||||
};
|
||||
participantsById: Record<
|
||||
number,
|
||||
{
|
||||
id: number;
|
||||
name: string;
|
||||
pivot?: { display_name: string | null } | null;
|
||||
}
|
||||
>;
|
||||
dynamicId: string;
|
||||
}>();
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'open-lightbox', url: string, mimeType: string): void;
|
||||
}>();
|
||||
|
||||
function formatTimestamp(isoString: string): { full: string; time: string } {
|
||||
const date = new Date(isoString);
|
||||
return {
|
||||
full: date.toLocaleString(),
|
||||
time: date.toLocaleTimeString([], {
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
hour12: false,
|
||||
}),
|
||||
};
|
||||
}
|
||||
|
||||
const parsedContent = computed(() => {
|
||||
let content = props.message.content;
|
||||
|
||||
// 1. Replace <user:id> placeholders with links to their dynamic profile
|
||||
const userRegex = /<user:(\d+)>/g;
|
||||
content = content.replace(userRegex, (match, userId) => {
|
||||
const user = props.participantsById[Number(userId)];
|
||||
if (user) {
|
||||
const url = route('dynamics.users.show', [props.dynamicId, Number(userId)]);
|
||||
return `<a href="${url}" class="c-chat__user-link font-semibold text-blue-500 hover:underline">${
|
||||
user.pivot?.display_name ?? user.name
|
||||
}</a>`;
|
||||
}
|
||||
return `User #${userId}`;
|
||||
});
|
||||
|
||||
// 2. Link subjects if found in the text
|
||||
if (props.message.subject_id && props.message.subject_type) {
|
||||
if (
|
||||
props.message.subject_type === 'mutation' ||
|
||||
props.message.subject_type === 'ledger'
|
||||
) {
|
||||
const ledgerId =
|
||||
props.message.subject_type === 'mutation'
|
||||
? props.message.subject?.ledger_id
|
||||
: props.message.subject?.id;
|
||||
|
||||
const ledgerName =
|
||||
props.message.subject_type === 'mutation'
|
||||
? props.message.subject?.ledger?.name
|
||||
: props.message.subject?.name;
|
||||
|
||||
if (ledgerId && ledgerName) {
|
||||
const ledgerUrl = route('dynamics.ledgers.show', [
|
||||
props.dynamicId,
|
||||
ledgerId,
|
||||
]);
|
||||
|
||||
const escapedName = ledgerName.replace(
|
||||
/[-\/\\^$*+?.()|[\]{}]/g,
|
||||
'\\$&',
|
||||
);
|
||||
|
||||
const nameRegex = new RegExp(`"${escapedName}"`, 'g');
|
||||
content = content.replace(
|
||||
nameRegex,
|
||||
`"<a href="${ledgerUrl}" class="c-chat__subject-link font-semibold text-blue-500 hover:underline">${ledgerName}</a>"`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return content;
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
<div class="c-chat__message-header">
|
||||
<span class="c-chat__message-author">{{
|
||||
message.user?.name
|
||||
}}</span>
|
||||
<span
|
||||
class="c-chat__message-time"
|
||||
:title="formatTimestamp(message.created_at).full"
|
||||
>
|
||||
{{ formatTimestamp(message.created_at).time }}
|
||||
</span>
|
||||
</div>
|
||||
<p class="c-chat__message-text" v-html="parsedContent"></p>
|
||||
|
||||
<!-- Attached Media Display -->
|
||||
<div
|
||||
v-if="message.media && message.media.length > 0"
|
||||
class="c-chat__message-media"
|
||||
>
|
||||
<div
|
||||
v-for="item in message.media"
|
||||
:key="item.id"
|
||||
class="c-chat__media-item"
|
||||
>
|
||||
<img
|
||||
v-if="item.mime_type.startsWith('image/')"
|
||||
:src="item.url"
|
||||
:alt="item.file_name"
|
||||
class="c-chat__image cursor-pointer transition-opacity hover:opacity-90"
|
||||
@click="emit('open-lightbox', item.url, item.mime_type)"
|
||||
/>
|
||||
<div
|
||||
v-else-if="item.mime_type.startsWith('video/')"
|
||||
class="relative cursor-pointer transition-opacity hover:opacity-90"
|
||||
@click="emit('open-lightbox', item.url, item.mime_type)"
|
||||
>
|
||||
<video
|
||||
:src="item.url"
|
||||
class="c-chat__video"
|
||||
></video>
|
||||
<div class="c-chat__play-overlay">▶</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
Reference in New Issue
Block a user