use gpui::{App, Global, SharedString};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Preview {
pub title: Option<SharedString>,
pub description: Option<SharedString>,
pub image: Option<SharedString>,
pub icon: Option<SharedString>,
pub label: Option<SharedString>,
}
pub type LinkPreview = fn(url: &str, cx: &App) -> Option<Preview>;
struct Installed(LinkPreview);
impl Global for Installed {}
pub fn set_link_preview(cx: &mut App, preview: LinkPreview) {
cx.set_global(Installed(preview));
}
pub(crate) fn of(cx: &App, url: &str) -> Option<Preview> {
let preview = cx.try_global::<Installed>()?.0;
preview(url, cx)
}
pub(crate) fn host(url: &str) -> &str {
let after = url.split_once("://").map_or(url, |(_, rest)| rest);
let host = after.split(['/', '?', '#']).next().unwrap_or(after);
host.strip_prefix("www.").unwrap_or(host)
}