package com.example.aiapp import androidx.compose.foundation.background import androidx.compose.foundation.layout.padding import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.text.font.FontFamily import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp /** * Whether [ref] names an image the server stored as one -- `.`, with an extension * from the list it writes -- rather than a file kept under its own name. Mirrors the server's * `media` table, which is the one other place the list lives. */ fun isImageRef(ref: String): Boolean = ref.substringAfterLast('.', "") in IMAGE_EXTENSIONS private val IMAGE_EXTENSIONS = setOf("png", "jpg", "gif", "webp") /** * The name a file was attached under: the ref less the hex the server put before it. The hex has no * dash in it, so the first one is the boundary however many the name has. */ fun attachmentName(ref: String): String = ref.substringAfter('-', ref) /** * One attachment on a sent message, drawn as what it is: an image inline, a file as its name. A * file is not fetched -- there is nothing on this phone to open a trace with -- so the name is all * of it. */ @Composable fun Attachment( settings: ServerSettings, sessionId: String, ref: String, onOpenImage: (String) -> Unit, ) { if (isImageRef(ref)) SessionImage(settings, sessionId, ref, onOpenImage) else FileName( attachmentName(ref), Modifier.clip(MaterialTheme.shapes.extraSmall) .background(rawSurface) .padding(horizontal = 8.dp, vertical = 4.dp), ) } /** * A file's name, one line, in the face names are read in. Overlong names lose their middle: a name * is identified by both ends -- what it is at the front, what kind at the back. */ @Composable fun FileName(name: String, modifier: Modifier = Modifier) { Text( name, modifier = modifier, style = MaterialTheme.typography.bodySmall, fontFamily = FontFamily.Monospace, maxLines = 1, overflow = TextOverflow.MiddleEllipsis, ) }