Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: threads interface PoC #43

Merged
merged 8 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@tailwindcss/vite": "^4.0.0",
"bits-ui": "^0.22.0",
"svelte-boring-avatars": "^1.2.6",
"svelte-french-toast": "^1.2.0",
"ulidx": "^2.4.1"
},
"devDependencies": {
Expand Down
73 changes: 73 additions & 0 deletions src/lib/components/ThreadRow.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<script lang="ts">
import Icon from "@iconify/svelte";
import type { Ulid, Thread } from "$lib/schemas/types";
import { decodeTime } from "ulidx";
import { formatDistanceToNowStrict } from "date-fns";
import { Button, Dialog, Separator } from "bits-ui";
import { fade } from "svelte/transition";

type Props = {
id: Ulid;
thread: Thread;
onclick: () => void;
onclickDelete: () => void;
}

let { id, thread, onclick, onclickDelete }: Props = $props();
let latest = $derived.by(() => {
let allTime: number[] = [];
for (const id of thread.timeline) {
allTime.push(decodeTime(id));
}
allTime.sort();
return new Date(allTime[allTime.length-1]);
});
</script>

<div {id} class="border border-white rounded text-white flex w-full justify-between p-4 hover:bg-white/5 transition-all duration-75">
<Button.Root {onclick} class="flex gap-4 items-center grow cursor-pointer">
<h1 class="text-xl font-semibold">{thread.title}</h1>
<time class="text-sm text-gray-300">Latest: {formatDistanceToNowStrict(latest)} ago</time>
</Button.Root>
<div class="flex gap-4 items-center">
<p class="flex gap-2 items-center">
{thread.timeline.length}
<Icon icon="tabler:message" class="text-2xl" />
</p>
<Dialog.Root>
<Dialog.Trigger class="hover:scale-105 active:scale-95 transition-all duration-150 cursor-pointer">
<Icon icon="tabler:trash" color="red" class="text-2xl" />
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay
transition={fade}
transitionConfig={{ duration: 150 }}
class="fixed inset-0 z-50 bg-black/80"
/>
<Dialog.Content
class="fixed p-5 flex flex-col text-white gap-4 w-dvw max-w-(--breakpoint-sm) left-[50%] top-[50%] z-50 translate-x-[-50%] translate-y-[-50%] rounded-lg border bg-purple-950"
>
<Dialog.Title
class="text-bold font-bold text-xl flex items-center justify-center gap-4"
>
<Icon icon="ri:alarm-warning-fill" color="red" class="text-2xl" />
<span> Delete Thread </span>
<Icon icon="ri:alarm-warning-fill" color="red" class="text-2xl" />
</Dialog.Title>
<Separator.Root class="border border-white" />
<div class="flex flex-col items-center gap-4">
<p>
The thread will be unrecoverable once deleted.
</p>
<Button.Root
onclick={onclickDelete}
class="flex items-center gap-3 px-4 py-2 max-w-[20em] bg-red-600 text-white rounded-lg hover:scale-[102%] active:scale-95 transition-all duration-150"
>
Confirm Delete
</Button.Root>
</div>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
</div>
</div>
4 changes: 3 additions & 1 deletion src/routes/(app)/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { goto } from "$app/navigation";
import { RoomyPdsStorageAdapter } from "$lib/autodoc-storage";
import { page } from "$app/state";
import { Toaster } from "svelte-french-toast";

let { children } = $props();

Expand Down Expand Up @@ -58,6 +59,7 @@

<!-- Container -->
<div class="flex gap-4 p-4 bg-violet-900 w-screen h-screen">
<Toaster />
<!-- Server Bar -->
<aside
class="flex flex-col justify-between w-20 h-full bg-violet-950 rounded-lg px-4 py-8 items-center"
Expand Down Expand Up @@ -256,4 +258,4 @@
</aside>

{@render children()}
</div>
</div>
169 changes: 130 additions & 39 deletions src/routes/(app)/dm/[did]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,36 @@
import type { Autodoc } from "$lib/autodoc.svelte";
import ChatArea from "$lib/components/ChatArea.svelte";
import { g } from "$lib/global.svelte";
import type { Channel, Ulid } from "$lib/schemas/types";
import type { Channel, Thread, Ulid } from "$lib/schemas/types";
import { page } from "$app/state";
import { user } from "$lib/user.svelte";
import { setContext, untrack } from "svelte";
import { Avatar, Button, Popover, Tabs, Toggle } from "bits-ui";
import { Avatar, Button, Dialog, Popover, Separator, Tabs, Toggle } from "bits-ui";
import { AvatarBeam } from "svelte-boring-avatars";
import Icon from "@iconify/svelte";
import { fly } from "svelte/transition";
import { fade, fly } from "svelte/transition";
import { ulid } from "ulidx";
import ThreadRow from "$lib/components/ThreadRow.svelte";
import { goto } from "$app/navigation";
import ChatMessage from "$lib/components/ChatMessage.svelte";
import toast from "svelte-french-toast";

let tab = $state("chat");
let channel: Autodoc<Channel> | undefined = $derived(g.dms[page.params.did]);
let messageInput = $state("");
let info = $derived(g.catalog?.view.dms[page.params.did]);
let currentThread = $derived.by(() => {
if (page.url.searchParams.has("thread")) {
return channel.view.threads[page.url.searchParams.get("thread")!] as Thread;
}
else {
return null;
}
});

$effect(() => {
if (currentThread) { tab = "threads" }
});

// thread maker
let isThreading = $state({ value: false });
Expand All @@ -30,8 +45,6 @@
selectedMessages = selectedMessages.filter((m) => m != message);
});

$inspect({ selectedMessages });

$effect(() => {
if (!isThreading.value && selectedMessages.length > 0) {
selectedMessages = [];
Expand Down Expand Up @@ -69,6 +82,7 @@

threadTitleInput = "";
isThreading.value = false;
toast.success("Thread created", { position: "bottom-end" })
}

function sendMessage(e: SubmitEvent) {
Expand All @@ -89,6 +103,17 @@

messageInput = "";
}

function deleteThread(id: Ulid) {
if (!channel) return;

channel.change((doc) => {
delete doc.threads[id]
});

toast.success("Thread deleted", { position: "bottom-end" });
goto(page.url.pathname);
}
</script>

<header class="flex flex-none items-center justify-between border-b-1 pb-4">
Expand All @@ -99,30 +124,27 @@
<AvatarBeam name={info?.name} />
</Avatar.Fallback>
</Avatar.Root>
<h4 class="text-white text-lg font-bold">
{info?.name}
</h4>
<Button.Root
title="Copy invite link"
class="p-2 hover:scale-105 active:scale-95 transition-all duration-150"
onclick={() => {
navigator.clipboard.writeText(
`${page.url.protocol}//${page.url.host}/invite/dm/${user.agent?.assertDid}`,
);
}}
>
<Icon
icon="icon-park-outline:copy-link"
color="white"
font-size="1.75em"
/>
</Button.Root>

<span class="flex gap-2 items-center">
<h4 class="text-white text-lg font-bold">
{info?.name}
</h4>
{#if currentThread}
<Icon icon="mingcute:right-line" color="white" />
<Icon icon="lucide-lab:reel-thread" color="white" />
<h5 class="text-white text-lg font-bold">
{currentThread.title}
</h5>
{/if}
</span>

</div>

<Tabs.Root bind:value={tab}>
<Tabs.List class="grid grid-cols-2 gap-4 border text-white p-1 rounded">
<Tabs.Trigger
value="chat"
onclick={() => goto(page.url.pathname)}
class="flex gap-2 w-full justify-center transition-all duration-150 items-center px-4 py-1 data-[state=active]:bg-violet-800 rounded"
>
<Icon icon="tabler:message" color="white" class="text-2xl" />
Expand All @@ -142,12 +164,12 @@
</Tabs.List>
</Tabs.Root>

<menu class="flex items-center">
<menu class="flex items-center gap-2">
{#if isThreading.value}
<div in:fly>
<Popover.Root>
<Popover.Trigger
class="mx-2 px-4 py-2 rounded bg-violet-800 text-white"
class="cursor-pointer mx-2 px-4 py-2 rounded bg-violet-800 text-white"
>
Create Thread
</Popover.Trigger>
Expand Down Expand Up @@ -185,12 +207,28 @@
{/if}
<Toggle.Root
bind:pressed={isThreading.value}
class={`p-2 ${isThreading.value && "bg-white/10"} hover:scale-105 active:scale-95 transition-all duration-150 rounded`}
disabled={tab !== "chat"}
class={`p-2 ${isThreading.value && "bg-white/10"} cursor-pointer hover:scale-105 active:scale-95 transition-all duration-150 rounded`}
>
<Icon icon="tabler:needle-thread" color="white" class="text-2xl" />
<Icon icon="tabler:needle-thread" color={tab !== "chat" ? "gray" : "white"} class="text-2xl" />
</Toggle.Root>
<Button.Root
class="p-2 hover:scale-105 active:scale-95 transition-all duration-150"
title="Copy invite link"
class="cursor-pointer hover:scale-105 active:scale-95 transition-all duration-150"
onclick={() => {
navigator.clipboard.writeText(
`${page.url.protocol}//${page.url.host}/invite/dm/${user.agent?.assertDid}`,
);
}}
>
<Icon
icon="icon-park-outline:copy-link"
color="white"
class="text-2xl"
/>
</Button.Root>
<Button.Root
class="p-2 cursor-pointer hover:scale-105 active:scale-95 transition-all duration-150"
>
<Icon icon="basil:settings-alt-solid" color="white" class="text-2xl" />
</Button.Root>
Expand All @@ -212,15 +250,68 @@

<!-- TODO: Render Threads -->
{#if tab === "threads"}
<div class="overflow-y-auto px-2 gap-3 flex flex-col">
{#each Object.entries(channel.view.threads) as [id, thread] (id)}
<div class="p-3 border-white border-solid border-2 rounded-md">
<h2 class="text-white text-2xl mb-1">{thread.title}</h2>
{#each thread.timeline as id}
<ChatMessage {id} message={channel.view.messages[id]} />
{/each}
</div>
{/each}
</div>
{#if currentThread}
<section class="flex flex-col gap-4 items-start">
<menu class="px-4 py-2 flex w-full justify-between">
<Button.Root
onclick={() => goto(page.url.pathname)}
class="flex gap-2 items-center text-white cursor-pointer hover:scale-105 transitiona-all duration-150"
>
<Icon icon="uil:left" />
Back
</Button.Root>
<Dialog.Root>
<Dialog.Trigger class="hover:scale-105 active:scale-95 transition-all duration-150 cursor-pointer">
<Icon icon="tabler:trash" color="red" class="text-2xl" />
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay
transition={fade}
transitionConfig={{ duration: 150 }}
class="fixed inset-0 z-50 bg-black/80"
/>
<Dialog.Content
class="fixed p-5 flex flex-col text-white gap-4 w-dvw max-w-(--breakpoint-sm) left-[50%] top-[50%] z-50 translate-x-[-50%] translate-y-[-50%] rounded-lg border bg-purple-950"
>
<Dialog.Title
class="text-bold font-bold text-xl flex items-center justify-center gap-4"
>
<Icon icon="ri:alarm-warning-fill" color="red" class="text-2xl" />
<span> Delete Thread </span>
<Icon icon="ri:alarm-warning-fill" color="red" class="text-2xl" />
</Dialog.Title>
<Separator.Root class="border border-white" />
<div class="flex flex-col items-center gap-4">
<p>
The thread will be unrecoverable once deleted.
</p>
<Button.Root
onclick={() => deleteThread(page.url.searchParams.get("thread")!)}
class="flex items-center gap-3 px-4 py-2 max-w-[20em] bg-red-600 text-white rounded-lg hover:scale-[102%] active:scale-95 transition-all duration-150"
>
Confirm Delete
</Button.Root>
</div>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
</menu>
{#each currentThread.timeline as id}
<ChatMessage {id} message={channel.view.messages[id]} />
{/each}
</section>
{:else}
<ul class="overflow-y-auto px-2 gap-3 flex flex-col">
{#each Object.entries(channel.view.threads) as [id, thread] (id)}
<ThreadRow
{id}
{thread}
onclick={() => goto(`?thread=${id}`)}
onclickDelete={() => deleteThread(id)}
/>
{/each}
</ul>
{/if}
{/if}
{/if}

{/if}