import { fetchTweet, lurkTweet } from "@/logic/twatter/calls";
import { pokeDister, scryDister, scryGangs } from "@/logic/requests/tlon";
import { useEffect, useState } from "react";
import Tweet from "@/sections/twatter/Tweet";
import { toFlat } from "@/sections/feed/thread/helpers";
import PostData from "@/sections/feed/PostData";
import Post from "@/sections/feed/post/Post";
import { FullNode, SortugRef } from "@/types/trill";
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { subscribe, unsub } from "@/logic/requests/generic";
import { AppData, GroupMetadata } from "@/types/tlon";
import comet from "@/assets/icons/comet.svg";
import Sigil from "@/ui/Sigil";
import { PollLoader } from "@/sections/feed/poll/Show";
import { parseThread, parseTweet } from "@/logic/twatter/parser";
import { Tweet as TweetType } from "@/types/twatter";
import { scryRadio } from "@/logic/requests/nostril";
import useLocalState from "@/state/state";
import { RadioTower, ScheduledRadio, radioLink } from "@/logic/requests/radio";
import { Ship } from "@/types/urbit";
import { RADIO } from "@/logic/constants";
import { SigilOnly } from "../Avatar";
import { date_diff } from "@/logic/utils";
import ShipsModal from "../modals/ShipsModal";
export function TrillSnippet({ r }: { r: SortugRef }) {
const { ship, path } = r;
return PostData({ host: ship, id: path.slice(1) })(TrillSnippetMarkup);
}
function TrillSnippetMarkup({
data,
refetch,
}: {
data: FullNode;
refetch: Function;
}) {
return (
);
}
// {
// if (pop) pop(link);
// }}
// className="chat-snippet trill-snippet"
// >
// Post not found
//
// );
export function TweetSnippet({
link,
giveBack,
}: {
link: string;
giveBack?: Function;
}) {
const id = link.split("/")[5];
const { isLoading, isError, data } = useQuery({
queryKey: ["twatter-thread", id],
queryFn: () => lurkTweet(id),
});
const [tw, setTw] = useState();
useEffect(() => {
if (data && "thread-lurk" in data) {
const js = JSON.parse(data["thread-lurk"]).data.tweetResult;
if (JSON.stringify(js) === "{}") return;
if (giveBack) giveBack(JSON.stringify(parseTweet(js.result)));
}
}, [data]);
if (isLoading || isError)
return (
Fetching Tweet from your Urbit...
);
else {
if ("no-coki" in data)
return (
);
if ("fail" in data)
return (
Bad request. Please send some feedback (here) of what you were trying
to fetch.
);
if ("thread-lurk" in data) {
const js = JSON.parse(data["thread-lurk"]).data.tweetResult;
if (JSON.stringify(js) === "{}")
return null; // TODO wtf
else
return (
);
}
// else {
// const head = parseThread(JSON.parse(data.thread));
// const tweet = head.thread.tweets[0]
// giveBack(JSON.stringify(tweet))
// return (
//
//
//
// );
// }
}
}
export function AppSnippet({ r }: { r: SortugRef }) {
async function sub() {
if (!subn) {
const s = await subscribe(
"treaty",
"/treaties",
(data: { add: AppData }) => {
if ("ini" in data) {
const app = Object.values(data.ini).find((d) => d.desk === name);
setApp(app);
}
if ("add" in data && data.add.desk === name) setApp(data.add);
if (appData) unsub(subn);
},
);
setSub(s);
const res = await pokeDister(ship);
}
}
const { ship, path } = r;
const name = path.slice(1);
const [appData, setApp] = useState();
const [subn, setSub] = useState();
const { isLoading, data, isError } = useQuery({
queryKey: ["dister", ship],
queryFn: () => scryDister(ship),
});
if (isLoading || isError) return ...
;
else {
const app = Object.values(data.ini).find((d) => d.desk === name);
if (!app && !appData) sub();
const a = app
? app
: appData
? appData
: { title: name, image: comet, info: "", ship };
return (
);
}
}
function AppDiv({ app }: { app: Partial }) {
return (
<>
{app.title}
{app.info}
App from {app.ship}
>
);
}
export function TlonSnippet({ r }: { r: SortugRef }) {
if (r.type === "app") return ;
if (r.type === "groups") return ;
}
export function GroupSnippet({ r }: { r: SortugRef }) {
const queryClient = useQueryClient();
async function sub() {
if (!subn) {
const path = `/gangs/index/${ship}`;
const s = await subscribe("groups", path, (data: any) => {
const key = `${ship}/${name}`;
const val = data[key];
queryClient.setQueryData(["gangs"], (old: any) => {
return { ...old, [key]: { preview: val } };
});
});
setSub(s);
}
}
const { ship, path } = r;
const name = path.slice(1);
const [groupData, setGroup] = useState();
const [subn, setSub] = useState();
const { isLoading, data, isError } = useQuery({
queryKey: ["gangs"],
queryFn: scryGangs,
});
if (isLoading || isError) return ...
;
else {
const group = data[`${ship}/${name}`];
if (!group && !groupData) sub();
const a =
group && group.preview
? group.preview.meta
: groupData
? groupData
: { title: name, image: comet, cover: "", description: "" };
return (
{a.image.startsWith("#") ? (
) : (

)}
{a.title}
{a.description.length > 25
? a.description.substring(0, 25) + "..."
: a.description}
Group by {ship}
{/*
*/}
);
}
}
export function PollSnippet({ r }: { r: SortugRef }) {
return (
);
}
export function SnippetHandler(props: { r: SortugRef }) {
if (props.r.type === "trill") return ;
if (props.r.type === "trill-polls") return ;
if (props.r.type === "app") return ;
if (props.r.type === "groups") return ;
}
export function RadioSnippet({ ship }: { ship: Ship }) {
const { our } = useLocalState();
return ship === our ? : ;
}
function DudesRadio({ ship }: { ship }) {
function onc() {
radioLink(ship);
}
const { radioTowers } = useLocalState();
const tower = radioTowers.find((t) => t.location === ship);
if (!tower)
return (
{RADIO}
Radio data not published. Click and check.
;
);
else
return (
{RADIO}
Radio Session. Playing: {tower.description}
Started {new Date(tower.time).toLocaleString()}
{tower.viewers}
👀
);
}
function OwnRadio() {
const { currentRadio, our, setModal, radioTowers } = useLocalState();
const [scheduled, setS] = useState(null);
function onc() {
radioLink(our);
}
useEffect(() => {
scryRadio().then((r) => {
if (r) setS(r.radio);
});
}, []);
function showViewers() {
const modal = (
);
setModal(modal);
}
if (scheduled && scheduled.time > Date.now())
return (
{RADIO}
Radio Session. Playing:
{scheduled.desc}
Starting at {new Date(scheduled.time).toLocaleString()}
);
else if (!currentRadio)
return (
);
else
return (
{RADIO}
{currentRadio?.viewers?.length || ""}
👀
);
// return (
// {scheduled > Date.now()
// ? (<>
//
// Radio Session. Playing:
//
// {currentRadio.description}
//
//
// Starting at {new Date(scheduled).toLocaleString()}
// >
// ): scheduled !== 0()
// }
//
// Radio Session. Playing:
//
// {currentRadio.description}
//
//
// {scheduled && scheduled > Date.now() ? (
// Starting at {new Date(scheduled).toLocaleString()}
// ) : scheduled !== 0 ? (
// Started {date_diff(new Date(scheduled), "long")}. Click to join.
// ) : (
// Unscheduled session. Click to join.
// )}
// );
}