💬 real messages page with API, compose, folders, and i18n
Browse files at this commit8382995231195c559da6a1114d362aecb8cd510fParent: 3c90c9d- feat
- 💬 real messages page with API, compose, folders, and i18n
8 changed files +478 -149
- src/api/deleteMessageById.ts +5 -0
- src/api/getMessages.ts +18 -0
- src/api/patchMessageById.ts +11 -0
- src/api/postMessage.ts +13 -0
- src/api/types.ts +16 -0
- src/components/users/user-search-select.tsx +1 -1
- src/i18n/messages.ts +55 -1
- src/pages/messages-page.tsx +359 -147
src/api/deleteMessageById.tsadded+5 -0
| @@ -0,0 +1,5 @@ | ||
| 1 | +import { client } from "./client"; | |
| 2 | + | |
| 3 | +export function deleteMessageById(id: string): Promise<void> { | |
| 4 | + return client<void>(`/messages/${id}`, { method: "DELETE" }); | |
| 5 | +} |
src/api/getMessages.tsadded+18 -0
| @@ -0,0 +1,18 @@ | ||
| 1 | +import { client } from "./client"; | |
| 2 | +import type { Page, PageParams } from "./page"; | |
| 3 | +import type { Message, MessageFolder } from "./types"; | |
| 4 | + | |
| 5 | +export function getMessages( | |
| 6 | + folder: MessageFolder = "inbox", | |
| 7 | + options?: PageParams & { read?: boolean } | |
| 8 | +): Promise<Page<Message>> { | |
| 9 | + const params = new URLSearchParams(); | |
| 10 | + params.set("folder", folder); | |
| 11 | + if (options?.read !== undefined) params.set("read", String(options.read)); | |
| 12 | + if (options?.limit !== undefined) params.set("limit", String(options.limit)); | |
| 13 | + if (options?.offset !== undefined) params.set("offset", String(options.offset)); | |
| 14 | + | |
| 15 | + const q = params.toString(); | |
| 16 | + const url = q ? `/messages?${q}` : `/messages`; | |
| 17 | + return client<Page<Message>>(url); | |
| 18 | +} |
src/api/patchMessageById.tsadded+11 -0
| @@ -0,0 +1,11 @@ | ||
| 1 | +import { client } from "./client"; | |
| 2 | +import type { Message, MessageFolder } from "./types"; | |
| 3 | + | |
| 4 | +export type PatchMessageBody = { | |
| 5 | + folder?: MessageFolder | null; | |
| 6 | + read?: boolean | null; | |
| 7 | +}; | |
| 8 | + | |
| 9 | +export function patchMessageById(id: string, body: PatchMessageBody): Promise<Message> { | |
| 10 | + return client<Message>(`/messages/${id}`, { method: "PATCH", body }); | |
| 11 | +} |
src/api/postMessage.tsadded+13 -0
| @@ -0,0 +1,13 @@ | ||
| 1 | +import { client } from "./client"; | |
| 2 | +import type { Message } from "./types"; | |
| 3 | + | |
| 4 | +export type PostMessageBody = { | |
| 5 | + recipient_id: string; | |
| 6 | + subject: string; | |
| 7 | + body?: string | null; | |
| 8 | + label?: string | null; | |
| 9 | +}; | |
| 10 | + | |
| 11 | +export function postMessage(body: PostMessageBody): Promise<Message> { | |
| 12 | + return client<Message>("/messages", { method: "POST", body }); | |
| 13 | +} |
src/api/types.tsmodified+16 -0
| @@ -371,3 +371,19 @@ export const EXAM_KINDS: KnownExamKind[] = [ | ||
| 371 | 371 | export const EXAM_MODES: ExamMode[] = ["sync", "async", "open"]; |
| 372 | 372 | |
| 373 | 373 | export const QUESTION_KINDS: QuestionKind[] = ["choice", "text"]; |
| 374 | + | |
| 375 | +export type MessageFolder = "inbox" | "sent" | "archive" | "trash"; | |
| 376 | + | |
| 377 | +export type Message = { | |
| 378 | + id: string; | |
| 379 | + sender: PersonRef; | |
| 380 | + sender_role: Role | null; | |
| 381 | + recipient: PersonRef; | |
| 382 | + recipient_role: Role | null; | |
| 383 | + subject: string; | |
| 384 | + body: string; | |
| 385 | + sent_at: number; | |
| 386 | + read: boolean; | |
| 387 | + folder: MessageFolder; | |
| 388 | + label: string | null; | |
| 389 | +}; |
src/components/users/user-search-select.tsxmodified+1 -1
| @@ -40,7 +40,7 @@ export function UserSearchSelect(props: { | ||
| 40 | 40 | const q = query().trim(); |
| 41 | 41 | if (q.length === 0) return ""; |
| 42 | 42 | if (loading()) return t("common.loading"); |
| 43 | - if (props.value.trim() && !options().some((u) => u.id === props.value.trim())) return props.value; | |
| 43 | + if (selectedUser() && selectedUser()!.id === props.value) return ""; | |
| 44 | 44 | if (options().length === 0) return props.emptyMessage ?? t("form.noStudents"); |
| 45 | 45 | return ""; |
| 46 | 46 | }); |
src/i18n/messages.tsmodified+55 -1
| @@ -606,7 +606,25 @@ export type MessageKey = | ||
| 606 | 606 | | "work.teacherIdentity" |
| 607 | 607 | | "work.noTeachers" |
| 608 | 608 | | "lookup.searchHint" |
| 609 | - | "work.userNotFound"; | |
| 609 | + | "work.userNotFound" | |
| 610 | + | "messages.inbox" | |
| 611 | + | "messages.sent" | |
| 612 | + | "messages.archive" | |
| 613 | + | "messages.trash" | |
| 614 | + | "messages.newMessage" | |
| 615 | + | "messages.recipient" | |
| 616 | + | "messages.recipientPlaceholder" | |
| 617 | + | "messages.search" | |
| 618 | + | "messages.noMessages" | |
| 619 | + | "messages.noSelection" | |
| 620 | + | "messages.reply" | |
| 621 | + | "messages.send" | |
| 622 | + | "messages.moveToArchive" | |
| 623 | + | "messages.moveToTrash" | |
| 624 | + | "messages.deleteForever" | |
| 625 | + | "messages.to" | |
| 626 | + | "messages.from" | |
| 627 | + | "messages.selectRecipient"; | |
| 610 | 628 | |
| 611 | 629 | type Dict = Record<MessageKey, string>; |
| 612 | 630 | |
| @@ -1230,6 +1248,24 @@ const en: Dict = { | ||
| 1230 | 1248 | "work.cannotEditOpen": "Open stints cannot be corrected. Check out or delete them first.", |
| 1231 | 1249 | "work.timesRequired": "Enter check-in and check-out date and time.", |
| 1232 | 1250 | "work.deleteSummary": "Delete work entry from {time}?", |
| 1251 | + "messages.inbox": "Inbox", | |
| 1252 | + "messages.sent": "Sent", | |
| 1253 | + "messages.archive": "Archive", | |
| 1254 | + "messages.trash": "Trash", | |
| 1255 | + "messages.newMessage": "New Message", | |
| 1256 | + "messages.recipient": "Recipient", | |
| 1257 | + "messages.recipientPlaceholder": "Search for name or username...", | |
| 1258 | + "messages.search": "Search messages", | |
| 1259 | + "messages.noMessages": "No messages found.", | |
| 1260 | + "messages.noSelection": "No message selected.", | |
| 1261 | + "messages.reply": "Reply", | |
| 1262 | + "messages.send": "Send", | |
| 1263 | + "messages.moveToArchive": "Archive", | |
| 1264 | + "messages.moveToTrash": "Move to Trash", | |
| 1265 | + "messages.deleteForever": "Delete Permanently", | |
| 1266 | + "messages.to": "To: ", | |
| 1267 | + "messages.from": "From: ", | |
| 1268 | + "messages.selectRecipient": "Please select a recipient.", | |
| 1233 | 1269 | }; |
| 1234 | 1270 | |
| 1235 | 1271 | const tr: Dict = { |
| @@ -1852,6 +1888,24 @@ const tr: Dict = { | ||
| 1852 | 1888 | "work.cannotEditOpen": "Açık mesai düzeltilemez. Önce çıkış yapın veya silin.", |
| 1853 | 1889 | "work.timesRequired": "Giriş ve çıkış tarih/saatini girin.", |
| 1854 | 1890 | "work.deleteSummary": "{time} mesai kaydı silinsin mi?", |
| 1891 | + "messages.inbox": "Gelenler", | |
| 1892 | + "messages.sent": "Gönderilenler", | |
| 1893 | + "messages.archive": "Arşiv", | |
| 1894 | + "messages.trash": "Çöp Kutusu", | |
| 1895 | + "messages.newMessage": "Yeni Mesaj", | |
| 1896 | + "messages.recipient": "Alıcı", | |
| 1897 | + "messages.recipientPlaceholder": "İsim veya kullanıcı adı ara...", | |
| 1898 | + "messages.search": "Mesajlarda ara...", | |
| 1899 | + "messages.noMessages": "Mesaj bulunamadı.", | |
| 1900 | + "messages.noSelection": "Mesaj seçilmedi.", | |
| 1901 | + "messages.reply": "Yanıtla", | |
| 1902 | + "messages.send": "Gönder", | |
| 1903 | + "messages.moveToArchive": "Arşivle", | |
| 1904 | + "messages.moveToTrash": "Çöp Kutusuna Taşı", | |
| 1905 | + "messages.deleteForever": "Kalıcı Olarak Sil", | |
| 1906 | + "messages.to": "Kime: ", | |
| 1907 | + "messages.from": "Kimden: ", | |
| 1908 | + "messages.selectRecipient": "Lütfen bir alıcı seçin.", | |
| 1855 | 1909 | }; |
| 1856 | 1910 | |
| 1857 | 1911 | export const messages: Record<Locale, Dict> = { en, tr }; |
src/pages/messages-page.tsxmodified+359 -147
| @@ -1,67 +1,90 @@ | ||
| 1 | -import { For, Show, createMemo, createSignal } from "solid-js"; | |
| 1 | +import { For, Show, createMemo, createResource, createSignal, Suspense, createEffect, useTransition } from "solid-js"; | |
| 2 | 2 | import { RouteGuard } from "@/components/layout/route-guard"; |
| 3 | +import { PaginationControls } from "@/components/ui/pagination-controls"; | |
| 3 | 4 | import { Badge } from "@/components/ui/badge"; |
| 4 | 5 | import { Button } from "@/components/ui/button"; |
| 5 | -import { IconArchive, IconMessage, IconSearch, IconSend, IconTrash, IconUsers } from "@/components/ui/icons"; | |
| 6 | +import { IconArchive, IconMessage, IconSearch, IconSend, IconTrash, IconPlus } from "@/components/ui/icons"; | |
| 6 | 7 | import { Input } from "@/components/ui/input"; |
| 8 | +import { SidePanel } from "@/components/ui/side-panel"; | |
| 9 | +import { Label } from "@/components/ui/label"; | |
| 10 | +import { Textarea } from "@/components/ui/textarea"; | |
| 11 | +import { UserSearchSelect } from "@/components/users/user-search-select"; | |
| 7 | 12 | import { cn } from "@/lib/cn"; |
| 8 | - | |
| 9 | -type TestMessage = { | |
| 10 | - id: string; | |
| 11 | - from: string; | |
| 12 | - role: string; | |
| 13 | - subject: string; | |
| 14 | - preview: string; | |
| 15 | - body: string; | |
| 16 | - time: string; | |
| 17 | - unread?: boolean; | |
| 18 | - label: string; | |
| 19 | -}; | |
| 20 | - | |
| 21 | -const messages: TestMessage[] = [ | |
| 22 | - { | |
| 23 | - id: "m1", | |
| 24 | - from: "Ayse Yilmaz", | |
| 25 | - role: "Ogrenci", | |
| 26 | - subject: "Bugunku etut hakkinda", | |
| 27 | - preview: "Hocam 16:00 etudune 10 dakika gec kalabilirim, bilginize.", | |
| 28 | - body: "Hocam merhaba,\n\nBugunku 16:00 etudune servis gecikmesi nedeniyle 10 dakika gec kalabilirim. Konu anlatimina yetisebilirsem notlari tamamlayacagim.\n\nTesekkurler.", | |
| 29 | - time: "09:24", | |
| 30 | - unread: true, | |
| 31 | - label: "Etut", | |
| 32 | - }, | |
| 33 | - { | |
| 34 | - id: "m2", | |
| 35 | - from: "Mehmet Demir", | |
| 36 | - role: "Veli", | |
| 37 | - subject: "Sinav sonucu gorusmesi", | |
| 38 | - preview: "Hafta ici kisa bir gorusme planlayabilir miyiz?", | |
| 39 | - body: "Merhaba,\n\nMehmet'in son deneme sonucunu birlikte degerlendirmek istiyoruz. Size uygun olan bir gun ve saat varsa gorusme planlayabiliriz.\n\nIyi calismalar.", | |
| 40 | - time: "Dun", | |
| 41 | - label: "Sinav", | |
| 42 | - }, | |
| 43 | - { | |
| 44 | - id: "m3", | |
| 45 | - from: "Fen Kulubu", | |
| 46 | - role: "Kulup", | |
| 47 | - subject: "Proje teslim listesi", | |
| 48 | - preview: "Cuma gunune kadar teslim edecek ekiplerin listesi hazir.", | |
| 49 | - body: "Kulup proje teslimleri icin ekip listesi hazirlandi. Eksik malzeme bildiren iki ekip var. Cuma gunu kontrol listesini tamamlayalim.", | |
| 50 | - time: "Sal", | |
| 51 | - unread: true, | |
| 52 | - label: "Kulup", | |
| 53 | - }, | |
| 54 | -]; | |
| 13 | +import { getMessages } from "@/api/getMessages"; | |
| 14 | +import { patchMessageById } from "@/api/patchMessageById"; | |
| 15 | +import { postMessage } from "@/api/postMessage"; | |
| 16 | +import { deleteMessageById } from "@/api/deleteMessageById"; | |
| 17 | +import type { Message, MessageFolder } from "@/api/types"; | |
| 18 | +import { formatApiError } from "@/api/client"; | |
| 19 | +import { useAuth } from "@/stores/auth-context"; | |
| 20 | +import { useT } from "@/stores/preferences-context"; | |
| 21 | +import { personLabel } from "@/lib/person"; | |
| 55 | 22 | |
| 56 | 23 | export default function MessagesPage() { |
| 57 | - const [selectedId, setSelectedId] = createSignal(messages[0]?.id ?? ""); | |
| 24 | + const t = useT(); | |
| 25 | + const [folder, setFolder] = createSignal<MessageFolder>("inbox"); | |
| 26 | + const [page, setPage] = createSignal(1); | |
| 27 | + const limit = 30; | |
| 28 | + const [selectedId, setSelectedId] = createSignal<string>(""); | |
| 58 | 29 | const [query, setQuery] = createSignal(""); |
| 30 | + const [composeOpen, setComposeOpen] = createSignal(false); | |
| 31 | + const [replyData, setReplyData] = createSignal<{ recipient: string; subject: string; body: string } | null>(null); | |
| 32 | + const [isPending, startTransition] = useTransition(); | |
| 33 | + | |
| 34 | + const [messagePage, { refetch }] = createResource( | |
| 35 | + () => ({ f: folder(), p: page() }), | |
| 36 | + async (args) => await getMessages(args.f, { limit, offset: (args.p - 1) * limit }), | |
| 37 | + ); | |
| 38 | + | |
| 39 | + const [unreadCount, { refetch: refetchUnread }] = createResource( | |
| 40 | + async () => (await getMessages("inbox", { read: false, limit: 1 })).total | |
| 41 | + ); | |
| 42 | + | |
| 43 | + const messages = createMemo(() => messagePage()?.items ?? []); | |
| 44 | + | |
| 59 | 45 | const filtered = createMemo(() => { |
| 60 | 46 | const q = query().trim().toLocaleLowerCase(); |
| 61 | - if (!q) return messages; | |
| 62 | - return messages.filter((message) => [message.from, message.role, message.subject, message.preview, message.label].join(" ").toLocaleLowerCase().includes(q)); | |
| 47 | + if (!q) return messages(); | |
| 48 | + return messages().filter((m) => { | |
| 49 | + const other = folder() === "sent" ? personLabel(m.recipient) : personLabel(m.sender); | |
| 50 | + return [other, m.subject, m.body, m.label].join(" ").toLocaleLowerCase().includes(q); | |
| 51 | + }); | |
| 63 | 52 | }); |
| 64 | - const selected = createMemo(() => messages.find((message) => message.id === selectedId()) ?? filtered()[0] ?? null); | |
| 53 | + | |
| 54 | + const selected = createMemo(() => messages().find((m) => m.id === selectedId()) ?? null); | |
| 55 | + | |
| 56 | + // Mark as read when selected | |
| 57 | + createEffect(() => { | |
| 58 | + const msg = selected(); | |
| 59 | + if (msg && !msg.read && msg.folder === "inbox") { | |
| 60 | + patchMessageById(msg.id, { read: true }) | |
| 61 | + .then(() => { | |
| 62 | + refetch(); | |
| 63 | + refetchUnread(); | |
| 64 | + }) | |
| 65 | + .catch(console.error); | |
| 66 | + } | |
| 67 | + }); | |
| 68 | + | |
| 69 | + const handleAction = async (msg: Message, action: { folder?: MessageFolder, delete?: boolean }) => { | |
| 70 | + try { | |
| 71 | + if (action.delete) { | |
| 72 | + await deleteMessageById(msg.id); | |
| 73 | + } else if (action.folder) { | |
| 74 | + await patchMessageById(msg.id, { folder: action.folder }); | |
| 75 | + } | |
| 76 | + if (selectedId() === msg.id) setSelectedId(""); | |
| 77 | + await refetch(); | |
| 78 | + if (action.folder === "inbox" || msg.folder === "inbox") await refetchUnread(); | |
| 79 | + } catch (err) { | |
| 80 | + console.error(err); | |
| 81 | + } | |
| 82 | + }; | |
| 83 | + | |
| 84 | + const timeLabel = (ts: number) => { | |
| 85 | + const d = new Date(ts); | |
| 86 | + return `${d.getHours().toString().padStart(2, "0")}:${d.getMinutes().toString().padStart(2, "0")}`; | |
| 87 | + }; | |
| 65 | 88 | |
| 66 | 89 | return ( |
| 67 | 90 | <RouteGuard> |
| @@ -69,123 +92,312 @@ export default function MessagesPage() { | ||
| 69 | 92 | <section class="data-shell overflow-hidden p-0"> |
| 70 | 93 | <div class="grid min-h-[calc(100vh-7rem)] lg:grid-cols-[13rem_minmax(26rem,34rem)_minmax(0,1fr)]"> |
| 71 | 94 | <aside class="border-b bg-muted/20 p-3 lg:border-b-0 lg:border-r"> |
| 72 | - <div class="space-y-1"> | |
| 73 | - <Button variant="secondary" class="h-9 w-full justify-start rounded-md"> | |
| 74 | - <IconMessage class="h-4 w-4" /> | |
| 75 | - Gelenler | |
| 76 | - <span class="ml-auto mono text-xs">{messages.length}</span> | |
| 95 | + <div class="space-y-3"> | |
| 96 | + <Button class="w-full rounded-md shadow-sm" onClick={() => setComposeOpen(true)}> | |
| 97 | + <IconPlus class="h-4 w-4" /> | |
| 98 | + {t("messages.newMessage")} | |
| 77 | 99 | </Button> |
| 78 | - <Button variant="ghost" class="h-9 w-full justify-start rounded-md text-muted-foreground"> | |
| 79 | - <IconSend class="h-4 w-4" /> | |
| 80 | - Gonderilen | |
| 81 | - </Button> | |
| 82 | - <Button variant="ghost" class="h-9 w-full justify-start rounded-md text-muted-foreground"> | |
| 83 | - <IconArchive class="h-4 w-4" /> | |
| 84 | - Arsiv | |
| 85 | - </Button> | |
| 86 | - <Button variant="ghost" class="h-9 w-full justify-start rounded-md text-muted-foreground"> | |
| 87 | - <IconTrash class="h-4 w-4" /> | |
| 88 | - Cop kutusu | |
| 89 | - </Button> | |
| 90 | - </div> | |
| 91 | - <div class="mt-5 border-t pt-4"> | |
| 92 | - <p class="px-2 text-[11px] font-semibold uppercase tracking-[0.12em] text-muted-foreground">Kanallar</p> | |
| 93 | - <div class="mt-2 space-y-1"> | |
| 94 | - <Button variant="ghost" class="h-8 w-full justify-start rounded-md text-muted-foreground"> | |
| 95 | - <IconUsers class="h-4 w-4" /> | |
| 96 | - Ogrenci | |
| 100 | + <div class="space-y-1"> | |
| 101 | + <Button | |
| 102 | + variant={folder() === "inbox" ? "secondary" : "ghost"} | |
| 103 | + class={cn("h-9 w-full justify-start rounded-md", folder() !== "inbox" && "text-muted-foreground", folder() === "inbox" && "bg-primary/10 text-primary")} | |
| 104 | + onClick={() => startTransition(() => { setFolder("inbox"); setPage(1); setSelectedId(""); })} | |
| 105 | + > | |
| 106 | + <IconMessage class="h-4 w-4" /> | |
| 107 | + {t("messages.inbox")} | |
| 108 | + <Show when={unreadCount() ? unreadCount()! > 0 : false}> | |
| 109 | + <Badge variant="default" class="ml-auto h-5 min-w-5 justify-center rounded-full px-1 text-[10px]">{unreadCount()}</Badge> | |
| 110 | + </Show> | |
| 111 | + </Button> | |
| 112 | + <Button | |
| 113 | + variant={folder() === "sent" ? "secondary" : "ghost"} | |
| 114 | + class={cn("h-9 w-full justify-start rounded-md", folder() !== "sent" && "text-muted-foreground")} | |
| 115 | + onClick={() => startTransition(() => { setFolder("sent"); setPage(1); setSelectedId(""); })} | |
| 116 | + > | |
| 117 | + <IconSend class="h-4 w-4" /> | |
| 118 | + {t("messages.sent")} | |
| 97 | 119 | </Button> |
| 98 | - <Button variant="ghost" class="h-8 w-full justify-start rounded-md text-muted-foreground"> | |
| 99 | - <IconUsers class="h-4 w-4" /> | |
| 100 | - Veli | |
| 120 | + <Button | |
| 121 | + variant={folder() === "archive" ? "secondary" : "ghost"} | |
| 122 | + class={cn("h-9 w-full justify-start rounded-md", folder() !== "archive" && "text-muted-foreground")} | |
| 123 | + onClick={() => startTransition(() => { setFolder("archive"); setPage(1); setSelectedId(""); })} | |
| 124 | + > | |
| 125 | + <IconArchive class="h-4 w-4" /> | |
| 126 | + {t("messages.archive")} | |
| 127 | + </Button> | |
| 128 | + <Button | |
| 129 | + variant={folder() === "trash" ? "secondary" : "ghost"} | |
| 130 | + class={cn("h-9 w-full justify-start rounded-md", folder() !== "trash" && "text-muted-foreground")} | |
| 131 | + onClick={() => startTransition(() => { setFolder("trash"); setPage(1); setSelectedId(""); })} | |
| 132 | + > | |
| 133 | + <IconTrash class="h-4 w-4" /> | |
| 134 | + {t("messages.trash")} | |
| 101 | 135 | </Button> |
| 102 | 136 | </div> |
| 103 | 137 | </div> |
| 104 | 138 | </aside> |
| 105 | 139 | |
| 106 | - <div class="border-b lg:border-b-0 lg:border-r"> | |
| 140 | + <div class="flex flex-col border-b lg:border-b-0 lg:border-r"> | |
| 107 | 141 | <div class="border-b p-3"> |
| 108 | 142 | <div class="relative"> |
| 109 | 143 | <IconSearch class="pointer-events-none absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" /> |
| 110 | - <Input class="h-9 rounded-md pl-8" placeholder="Mesajlarda ara" value={query()} onInput={(event) => setQuery(event.currentTarget.value)} /> | |
| 144 | + <Input class="h-9 rounded-md pl-8" placeholder={t("messages.search")} value={query()} onInput={(event) => setQuery(event.currentTarget.value)} /> | |
| 111 | 145 | </div> |
| 112 | 146 | </div> |
| 113 | - <div class="max-h-[calc(100vh-10.5rem)] space-y-2 overflow-auto p-3"> | |
| 114 | - <For each={filtered()}> | |
| 115 | - {(message) => ( | |
| 116 | - <button | |
| 117 | - type="button" | |
| 118 | - class={cn("w-full rounded-lg border p-3 text-left text-sm transition-colors hover:bg-muted/60", selected()?.id === message.id && "border-primary/30 bg-muted")} | |
| 119 | - onClick={() => setSelectedId(message.id)} | |
| 120 | - > | |
| 121 | - <div class="flex items-start gap-2"> | |
| 122 | - <div class="min-w-0 flex-1"> | |
| 123 | - <div class="flex items-center gap-2"> | |
| 124 | - <span class="truncate font-semibold">{message.from}</span> | |
| 125 | - <Show when={message.unread}> | |
| 126 | - <span class="h-2 w-2 rounded-full bg-primary" /> | |
| 147 | + <div class={cn("flex-1 space-y-2 overflow-auto p-3 transition-opacity", isPending() && "opacity-50 pointer-events-none")}> | |
| 148 | + <Suspense fallback={<div class="p-4 text-center text-sm text-muted-foreground">{t("common.loading")}</div>}> | |
| 149 | + <Show when={filtered().length > 0} fallback={<div class="p-4 text-center text-sm text-muted-foreground">{t("messages.noMessages")}</div>}> | |
| 150 | + <For each={filtered()}> | |
| 151 | + {(message) => { | |
| 152 | + const isSent = folder() === "sent"; | |
| 153 | + const peerName = isSent ? personLabel(message.recipient) : personLabel(message.sender); | |
| 154 | + const role = isSent ? message.recipient_role : message.sender_role; | |
| 155 | + const unread = !isSent && !message.read; | |
| 156 | + | |
| 157 | + return ( | |
| 158 | + <button | |
| 159 | + type="button" | |
| 160 | + class={cn( | |
| 161 | + "w-full border-b p-4 text-left transition-colors last:border-b-0", | |
| 162 | + selected()?.id === message.id ? "bg-primary/5" : "hover:bg-muted/40", | |
| 163 | + unread && selected()?.id !== message.id && "bg-muted/10" | |
| 164 | + )} | |
| 165 | + onClick={() => setSelectedId(message.id)} | |
| 166 | + > | |
| 167 | + <div class="flex items-start justify-between gap-2"> | |
| 168 | + <span class={cn("truncate text-xs font-semibold uppercase tracking-wider text-muted-foreground")}>{peerName}</span> | |
| 169 | + <span class={cn("mono shrink-0 text-[10px]", unread ? "font-medium text-primary" : "text-muted-foreground")}>{timeLabel(message.sent_at)}</span> | |
| 170 | + </div> | |
| 171 | + <div class="mt-1 flex items-center gap-2"> | |
| 172 | + <Show when={unread}> | |
| 173 | + <span class="h-2 w-2 shrink-0 rounded-full bg-primary" /> | |
| 174 | + </Show> | |
| 175 | + <span class={cn("truncate text-sm", unread ? "font-bold text-foreground" : "font-medium text-foreground/90")}>{message.subject}</span> | |
| 176 | + </div> | |
| 177 | + <p class={cn("mt-1.5 line-clamp-2 text-xs", unread ? "text-foreground/80" : "text-muted-foreground")}>{message.body}</p> | |
| 178 | + <Show when={message.label || role}> | |
| 179 | + <div class="mt-3 flex items-center gap-2"> | |
| 180 | + <Show when={message.label}> | |
| 181 | + <Badge variant={unread ? "default" : "secondary"} class="text-[10px]">{message.label}</Badge> | |
| 182 | + </Show> | |
| 183 | + <Show when={role}> | |
| 184 | + <span class="text-[10px] uppercase tracking-widest text-muted-foreground">{t(`role.${role}` as any)}</span> | |
| 185 | + </Show> | |
| 186 | + </div> | |
| 127 | 187 | </Show> |
| 128 | - </div> | |
| 129 | - <p class="mt-1 truncate text-xs font-medium">{message.subject}</p> | |
| 130 | - </div> | |
| 131 | - <span class="mono text-xs text-muted-foreground">{message.time}</span> | |
| 132 | - </div> | |
| 133 | - <p class="mt-2 line-clamp-2 text-xs text-muted-foreground">{message.preview}</p> | |
| 134 | - <div class="mt-3 flex items-center gap-2"> | |
| 135 | - <Badge variant={message.unread ? "default" : "secondary"}>{message.label}</Badge> | |
| 136 | - <span class="text-xs text-muted-foreground">{message.role}</span> | |
| 137 | - </div> | |
| 138 | - </button> | |
| 139 | - )} | |
| 140 | - </For> | |
| 188 | + </button> | |
| 189 | + ); | |
| 190 | + }} | |
| 191 | + </For> | |
| 192 | + </Show> | |
| 193 | + </Suspense> | |
| 141 | 194 | </div> |
| 195 | + <Show when={messagePage() && messagePage()!.total > limit}> | |
| 196 | + <div class="border-t p-3"> | |
| 197 | + <PaginationControls | |
| 198 | + page={page()} | |
| 199 | + onPageChange={setPage} | |
| 200 | + totalPages={Math.ceil((messagePage()?.total ?? 0) / limit)} | |
| 201 | + /> | |
| 202 | + </div> | |
| 203 | + </Show> | |
| 142 | 204 | </div> |
| 143 | 205 | |
| 144 | - <Show when={selected()} fallback={<div class="p-8 text-sm text-muted-foreground">Mesaj secilmedi.</div>}> | |
| 145 | - {(message) => ( | |
| 146 | - <article class="flex min-h-[30rem] flex-col"> | |
| 147 | - <div class="flex items-center gap-2 border-b p-3"> | |
| 148 | - <Button variant="ghost" size="icon" class="h-8 w-8 rounded-md"> | |
| 149 | - <IconArchive class="h-4 w-4" /> | |
| 150 | - </Button> | |
| 151 | - <Button variant="ghost" size="icon" class="h-8 w-8 rounded-md text-destructive"> | |
| 152 | - <IconTrash class="h-4 w-4" /> | |
| 153 | - </Button> | |
| 154 | - <Button class="ml-auto h-8 rounded-md" size="sm"> | |
| 155 | - <IconSend class="h-4 w-4" /> | |
| 156 | - Yanitla | |
| 157 | - </Button> | |
| 158 | - </div> | |
| 159 | - <div class="border-b p-4"> | |
| 160 | - <div class="flex items-start gap-3"> | |
| 161 | - <div class="flex h-10 w-10 items-center justify-center rounded-full bg-muted text-sm font-semibold"> | |
| 162 | - {message().from.split(" ").map((part) => part[0]).join("")} | |
| 163 | - </div> | |
| 164 | - <div class="min-w-0 flex-1"> | |
| 165 | - <h2 class="font-display text-lg font-semibold">{message().subject}</h2> | |
| 166 | - <p class="mt-1 text-sm text-muted-foreground"> | |
| 167 | - {message().from} · {message().role} | |
| 168 | - </p> | |
| 206 | + <Show when={selected()} fallback={<div class="flex items-center justify-center p-8 text-sm text-muted-foreground">{t("messages.noSelection")}</div>}> | |
| 207 | + {(message) => { | |
| 208 | + const isSent = folder() === "sent"; | |
| 209 | + const peerName = isSent ? personLabel(message().recipient) : personLabel(message().sender); | |
| 210 | + const role = isSent ? message().recipient_role : message().sender_role; | |
| 211 | + | |
| 212 | + return ( | |
| 213 | + <article class="flex h-full flex-col"> | |
| 214 | + <div class="flex items-center gap-2 border-b p-3"> | |
| 215 | + <Show when={!isSent}> | |
| 216 | + <Button | |
| 217 | + variant="ghost" | |
| 218 | + size="sm" | |
| 219 | + class="rounded-md" | |
| 220 | + onClick={() => { | |
| 221 | + setReplyData({ | |
| 222 | + recipient: message().sender.id, | |
| 223 | + subject: message().subject.startsWith("Re:") ? message().subject : `Re: ${message().subject}`, | |
| 224 | + body: `\n\n--- Önceki Mesaj ---\n${message().body}`, | |
| 225 | + }); | |
| 226 | + setComposeOpen(true); | |
| 227 | + }} | |
| 228 | + > | |
| 229 | + <IconMessage class="mr-2 h-4 w-4" /> | |
| 230 | + {t("messages.reply")} | |
| 231 | + </Button> | |
| 232 | + <div class="mx-1 h-4 w-[1px] bg-border" /> | |
| 233 | + </Show> | |
| 234 | + <Show when={folder() !== "archive"}> | |
| 235 | + <Button variant="ghost" size="icon" class="h-8 w-8 rounded-md" onClick={() => handleAction(message(), { folder: "archive" })} title={t("messages.moveToArchive")}> | |
| 236 | + <IconArchive class="h-4 w-4" /> | |
| 237 | + </Button> | |
| 238 | + </Show> | |
| 239 | + <Show when={folder() !== "trash"}> | |
| 240 | + <Button variant="ghost" size="icon" class="h-8 w-8 rounded-md text-destructive" onClick={() => handleAction(message(), { folder: "trash" })} title={t("messages.moveToTrash")}> | |
| 241 | + <IconTrash class="h-4 w-4" /> | |
| 242 | + </Button> | |
| 243 | + </Show> | |
| 244 | + <Show when={folder() === "trash"}> | |
| 245 | + <Button variant="ghost" size="icon" class="h-8 w-8 rounded-md text-destructive" onClick={() => handleAction(message(), { delete: true })} title={t("messages.deleteForever")}> | |
| 246 | + <IconTrash class="h-4 w-4" /> | |
| 247 | + </Button> | |
| 248 | + </Show> | |
| 249 | + </div> | |
| 250 | + <div class="border-b p-5"> | |
| 251 | + <div class="flex items-start gap-4"> | |
| 252 | + <div class="flex h-12 w-12 shrink-0 items-center justify-center rounded-full bg-primary/10 text-lg font-bold uppercase text-primary"> | |
| 253 | + {peerName.charAt(0)} | |
| 254 | + </div> | |
| 255 | + <div class="min-w-0 flex-1"> | |
| 256 | + <h2 class="font-display text-xl font-semibold tracking-tight">{message().subject}</h2> | |
| 257 | + <div class="mt-1.5 flex items-center gap-2 text-sm text-muted-foreground"> | |
| 258 | + <span>{isSent ? t("messages.to") : t("messages.from")}</span> | |
| 259 | + <span class="font-medium text-foreground">{peerName}</span> | |
| 260 | + <Show when={role}> | |
| 261 | + <Badge variant="outline" class="ml-1 px-1.5 py-0 text-[10px] uppercase tracking-wider">{t(`role.${role}` as any)}</Badge> | |
| 262 | + </Show> | |
| 263 | + </div> | |
| 264 | + </div> | |
| 265 | + <span class="mono shrink-0 rounded-md bg-muted/50 px-2 py-1 text-xs text-muted-foreground">{timeLabel(message().sent_at)}</span> | |
| 169 | 266 | </div> |
| 170 | - <span class="mono text-xs text-muted-foreground">{message().time}</span> | |
| 171 | 267 | </div> |
| 172 | - </div> | |
| 173 | - <div class="flex-1 whitespace-pre-wrap p-4 text-sm leading-6">{message().body}</div> | |
| 174 | - <div class="border-t p-4"> | |
| 175 | - <textarea class="min-h-24 w-full resize-none rounded-md border bg-background p-3 text-sm outline-none transition-colors placeholder:text-muted-foreground focus-visible:ring-2 focus-visible:ring-ring" placeholder={`${message().from} kisilerine yanit yaz...`} /> | |
| 176 | - <div class="mt-3 flex justify-end"> | |
| 177 | - <Button size="sm" class="rounded-md"> | |
| 178 | - <IconSend class="h-4 w-4" /> | |
| 179 | - Gonder | |
| 180 | - </Button> | |
| 268 | + <div class="flex-1 overflow-auto whitespace-pre-wrap p-5 text-sm leading-relaxed text-foreground/90"> | |
| 269 | + {message().body} | |
| 181 | 270 | </div> |
| 182 | - </div> | |
| 183 | - </article> | |
| 184 | - )} | |
| 271 | + </article> | |
| 272 | + ); | |
| 273 | + }} | |
| 185 | 274 | </Show> |
| 186 | 275 | </div> |
| 187 | 276 | </section> |
| 277 | + | |
| 278 | + <ComposePanel | |
| 279 | + open={composeOpen()} | |
| 280 | + onOpenChange={(open) => { | |
| 281 | + if (!open) setReplyData(null); | |
| 282 | + setComposeOpen(open); | |
| 283 | + }} | |
| 284 | + onSuccess={() => { | |
| 285 | + if (folder() === "sent") refetch(); | |
| 286 | + }} | |
| 287 | + initialRecipient={replyData()?.recipient} | |
| 288 | + initialSubject={replyData()?.subject} | |
| 289 | + initialBody={replyData()?.body} | |
| 290 | + /> | |
| 188 | 291 | </div> |
| 189 | 292 | </RouteGuard> |
| 190 | 293 | ); |
| 191 | 294 | } |
| 295 | + | |
| 296 | +function ComposePanel(props: { | |
| 297 | + open: boolean; | |
| 298 | + onOpenChange: (open: boolean) => void; | |
| 299 | + onSuccess: () => void; | |
| 300 | + initialRecipient?: string; | |
| 301 | + initialSubject?: string; | |
| 302 | + initialBody?: string; | |
| 303 | +}) { | |
| 304 | + const t = useT(); | |
| 305 | + const auth = useAuth(); | |
| 306 | + const [recipient, setRecipient] = createSignal(""); | |
| 307 | + const [subject, setSubject] = createSignal(""); | |
| 308 | + const [body, setBody] = createSignal(""); | |
| 309 | + const [pending, setPending] = createSignal(false); | |
| 310 | + const [error, setError] = createSignal(""); | |
| 311 | + | |
| 312 | + createEffect(() => { | |
| 313 | + if (props.open) { | |
| 314 | + setRecipient(props.initialRecipient ?? ""); | |
| 315 | + setSubject(props.initialSubject ?? ""); | |
| 316 | + setBody(props.initialBody ?? ""); | |
| 317 | + } | |
| 318 | + }); | |
| 319 | + | |
| 320 | + const reset = () => { | |
| 321 | + setRecipient(""); | |
| 322 | + setSubject(""); | |
| 323 | + setBody(""); | |
| 324 | + setError(""); | |
| 325 | + }; | |
| 326 | + | |
| 327 | + const handleSend = async (e: SubmitEvent) => { | |
| 328 | + e.preventDefault(); | |
| 329 | + if (!recipient()) { | |
| 330 | + setError(t("messages.selectRecipient")); | |
| 331 | + return; | |
| 332 | + } | |
| 333 | + setError(""); | |
| 334 | + setPending(true); | |
| 335 | + try { | |
| 336 | + await postMessage({ | |
| 337 | + recipient_id: recipient(), | |
| 338 | + subject: subject().trim(), | |
| 339 | + body: body().trim() || undefined, | |
| 340 | + }); | |
| 341 | + props.onSuccess(); | |
| 342 | + props.onOpenChange(false); | |
| 343 | + reset(); | |
| 344 | + } catch (err) { | |
| 345 | + setError(formatApiError(err)); | |
| 346 | + } finally { | |
| 347 | + setPending(false); | |
| 348 | + } | |
| 349 | + }; | |
| 350 | + | |
| 351 | + return ( | |
| 352 | + <SidePanel | |
| 353 | + open={props.open} | |
| 354 | + onOpenChange={(open) => { | |
| 355 | + if (!open) reset(); | |
| 356 | + props.onOpenChange(open); | |
| 357 | + }} | |
| 358 | + title={t("messages.newMessage")} | |
| 359 | + > | |
| 360 | + <form class="space-y-4" onSubmit={handleSend}> | |
| 361 | + <div class="space-y-1.5"> | |
| 362 | + <Label for="msg-recipient">{t("messages.recipient")}</Label> | |
| 363 | + <UserSearchSelect | |
| 364 | + id="msg-recipient" | |
| 365 | + value={recipient()} | |
| 366 | + onChange={setRecipient} | |
| 367 | + placeholder={t("messages.recipientPlaceholder")} | |
| 368 | + excludeIds={auth.user()?.id ? [auth.user()!.id] : []} | |
| 369 | + /> | |
| 370 | + </div> | |
| 371 | + <div class="space-y-1.5"> | |
| 372 | + <Label for="msg-subject">{t("form.title")}</Label> | |
| 373 | + <Input | |
| 374 | + id="msg-subject" | |
| 375 | + required | |
| 376 | + maxlength={200} | |
| 377 | + value={subject()} | |
| 378 | + onInput={(e) => setSubject(e.currentTarget.value)} | |
| 379 | + /> | |
| 380 | + </div> | |
| 381 | + <div class="space-y-1.5"> | |
| 382 | + <Label for="msg-body">{t("form.description")}</Label> | |
| 383 | + <Textarea | |
| 384 | + id="msg-body" | |
| 385 | + rows={6} | |
| 386 | + value={body()} | |
| 387 | + onInput={(e) => setBody(e.currentTarget.value)} | |
| 388 | + /> | |
| 389 | + </div> | |
| 390 | + {error() && <p class="text-sm text-destructive">{error()}</p>} | |
| 391 | + <div class="flex flex-wrap gap-2"> | |
| 392 | + <Button type="submit" class="rounded-sm" disabled={pending()}> | |
| 393 | + <IconSend class="mr-2 h-4 w-4" /> | |
| 394 | + {t("messages.send")} | |
| 395 | + </Button> | |
| 396 | + <Button type="button" variant="outline" class="rounded-sm" onClick={() => props.onOpenChange(false)}> | |
| 397 | + {t("common.cancel")} | |
| 398 | + </Button> | |
| 399 | + </div> | |
| 400 | + </form> | |
| 401 | + </SidePanel> | |
| 402 | + ); | |
| 403 | +} |