diff --git a/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt b/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt index 089d10fd5..ea27897a3 100644 --- a/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt +++ b/app/src/main/java/org/mozilla/fenix/tabstray/ext/TabSessionState.kt @@ -4,7 +4,31 @@ package org.mozilla.fenix.tabstray.ext +import androidx.compose.runtime.Composable import mozilla.components.browser.state.state.TabSessionState +import mozilla.components.browser.toolbar.MAX_URI_LENGTH +import org.mozilla.fenix.components.components +import org.mozilla.fenix.compose.inComposePreview +import org.mozilla.fenix.ext.toShortUrl + +/** + * Shortens URLs to be more user friendly, by applying [String.toShortUrl] + * and making sure it's equal or below the [MAX_URI_LENGTH]. + */ +@Composable +fun TabSessionState.toShortUrl(): String { + // Truncate to MAX_URI_LENGTH to prevent the UI from locking up for + // extremely large URLs such as data URIs or bookmarklets. The same + // is done in the toolbar and awesomebar: + // https://github.com/mozilla-mobile/fenix/issues/1824 + // https://github.com/mozilla-mobile/android-components/issues/6985 + return if (inComposePreview) { + this.content.url.take(MAX_URI_LENGTH) + } else { + this.content.url.toShortUrl(components.publicSuffixList) + .take(MAX_URI_LENGTH) + } +} fun TabSessionState.isActive(maxActiveTime: Long): Boolean { val lastActiveTime = maxOf(lastAccess, createdAt)