Skip to content

Commit

Permalink
basic relay
Browse files Browse the repository at this point in the history
  • Loading branch information
mehotkhan committed Jun 26, 2024
1 parent 47ca287 commit 6d00117
Show file tree
Hide file tree
Showing 31 changed files with 441 additions and 816 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"localforage",
"nomicfoundation",
"nomiclabs",
"nostr",
"Nuxt",
"nuxtjs",
"pinia",
Expand Down
19 changes: 15 additions & 4 deletions components/comments/CreateForm.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<script setup lang="ts">
import { z } from "zod";
import type { FormSubmitEvent } from "#ui/types";
const { sendComment } = useComments();
import { ref, reactive } from "vue";
const { sendComment, sending } = useComments();
const state = reactive({
message: undefined,
message: "",
});
const schema = z.object({
Expand All @@ -18,6 +20,13 @@ const onSubmit = async (event: FormSubmitEvent<Schema>) => {
await sendComment(event.data.message);
state.message = "";
};
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === "Enter" && !event.shiftKey) {
event.preventDefault();
form.value?.submit();
}
};
</script>

<template>
Expand All @@ -31,11 +40,13 @@ const onSubmit = async (event: FormSubmitEvent<Schema>) => {
<UCard class="mb-10">
<template #header> ارسال دیدگاه </template>
<UFormGroup name="textarea" label="دیدگاه شما">
<UTextarea v-model="state.message" />
<UTextarea v-model="state.message" @keydown="handleKeyDown" />
</UFormGroup>
<template #footer>
<div class="flex justify-end gap-3">
<UButton variant="outline" size="xl" type="submit"> ارسال </UButton>
<UButton variant="outline" size="xl" type="submit" :loading="sending">
ارسال
</UButton>
</div>
</template>
</UCard>
Expand Down
8 changes: 2 additions & 6 deletions components/comments/Lists.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
<script setup lang="ts">
const { allComments } = useComments();
watch(allComments, () => {
console.log(allComments.value);
});
</script>

<template>
<div class="flex w-full">
<div class="flex w-full flex-col">
<UCard
v-for="comment in allComments"
:key="comment.id"
Expand Down Expand Up @@ -74,7 +70,7 @@ watch(allComments, () => {
</div>
</template>
<p class="text-gray-500 dark:text-gray-400">
{{ comment.text }}
{{ comment.message }}
</p>
<template #footer>
<div class="flex justify-end gap-3">
Expand Down
21 changes: 0 additions & 21 deletions components/global/Breadcrumb.vue

This file was deleted.

19 changes: 0 additions & 19 deletions components/global/DarkMode.vue

This file was deleted.

41 changes: 0 additions & 41 deletions components/global/IntroTabs.vue

This file was deleted.

50 changes: 0 additions & 50 deletions components/global/Sidebar.vue

This file was deleted.

103 changes: 79 additions & 24 deletions composables/comments/useComments.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,84 @@
import { Event as NostrEvent, Relay, finalizeEvent } from "nostr-tools";
import { useWebSocket } from "@vueuse/core";

export default async () => {
const { profile } = useUser()
const { $dexie } = useNuxtApp()
const relayURL = "ws://localhost:3000/nostr-relay";

let relay: any = null;
export default () => {
const { profile } = useUser();
const { $dexie } = useNuxtApp();
const sending = ref(false);
const { status, data, send, open, close } = useWebSocket(relayURL, {
autoReconnect: {
retries: 3,
delay: 1000,
onFailed() {
console.log("Failed to connect WebSocket after 3 retries");
},
},
});

watch(data, () => {
console.log('incming websocket', data.value);
});
watch(status, () => {
console.log(status.value);
});

onMounted(async () => {
open();
relay = new Relay(relayURL);
await relay.connect();
console.log(`Connected to ${relay.url}`);
startStream();
});

// Correctly typed function for adding a comment
const sendComment = async (message: string) => {
await $dexie.comments.add({
owner: profile.value.pub,
message: message,
create_at: 'sdss',
hash: 'sdss'
})
return
}

// const allComments = useLiveQuery(
// async () =>
// await $dexie.comments
// .orderBy("created_at")
// .toArray(),
// [],
// );
const allComments: any[] = []
sending.value = true;
if (!profile.value?.pub) {
sending.value = false;
throw new Error("User profile is not loaded");
}
const newComment = {
owner: String(profile.value.pub),
message,
created_at: Date.now(), // Now saving as timestamp
hash: "some_hash_generation_logic_here", // You should replace this with actual hash generation logic
status: "draft",
};

sending.value = false;
$dexie.comments.add(newComment);
};

// Use `useLiveQuery` to reactively fetch all comments ordered by 'created_at'
const allComments = useLiveQuery(async () => {
return await $dexie.comments.orderBy("created_at").reverse().toArray();
}, []);

const startStream = async () => {
if (relay !== null && relay.connected) {
console.log("listen");
relay.subscribe(
[
{
// authors: mockRelay.authors,
kinds: [1],
},
],
{
onevent(event: any) {
console.log("incoming", event);
},
}
);
}
};

return {
sending,
sendComment,
allComments
}

}
allComments,
};
};
10 changes: 5 additions & 5 deletions layouts/default.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<script lang="ts" setup>
const { welcomeMessage } = useSupport();
// const { welcomeMessage } = useSupport();
onMounted(() => {
welcomeMessage();
});
// onMounted(() => {
// welcomeMessage();
// });
</script>
<template>
<div class="flex-col flex">
Expand All @@ -22,5 +22,5 @@ onMounted(() => {
<div id="app-after"></div>
</main>
</div>
<SupportStart />
<!-- <SupportStart /> -->
</template>
Binary file modified main.db
Binary file not shown.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@
"moment-jalaali": "^0.10.1",
"nostr-relaypool": "^0.6.30",
"nostr-tools": "^2.7.0",
"vue-audio-tapir": "^1.5.0",
"vue-audio-tapir": "^1.6.1",
"vue3-perfect-scrollbar": "^2.0.0",
"web-uuid": "0.1.0-pre.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240614.0",
"@iconify/json": "^2.2.220",
"@nuxt/content": "^2.12.1",
"@cloudflare/workers-types": "^4.20240620.0",
"@iconify/json": "^2.2.222",
"@nuxt/content": "^2.13.0",
"@nuxt/devtools": "latest",
"@nuxt/image": "^1.7.0",
"@nuxt/ui": "^2.17.0",
"@nuxtjs/eslint-config-typescript": "^12.1.0",
"@nuxtjs/i18n": "^8.3.1",
"@types/node": "^20.14.6",
"@types/node": "^20.14.9",
"crypto": "^1.0.1",
"dotenv": "^16.4.5",
"drizzle-kit": "^0.22.7",
Expand All @@ -63,13 +63,13 @@
"sass": "^1.77.6",
"sharp": "^0.33.4",
"typed-array-utils": "^0.2.4",
"typescript": "^5.4.5",
"typescript": "^5.5.2",
"unplugin-icons": "0.19.0",
"unplugin-vue-components": "^0.27.0",
"unplugin-vue-components": "^0.27.1",
"vite-plugin-compression": "^0.5.1",
"vue": "^3.4.29",
"vue-router": "^4.3.3",
"wrangler": "^3.61.0",
"vue": "^3.4.30",
"vue-router": "^4.4.0",
"wrangler": "^3.62.0",
"yup": "^1.4.0",
"zod": "^3.23.8"
}
Expand Down
2 changes: 1 addition & 1 deletion pages/notes/[...slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ onUnmounted(() => {
:class="doc?.dir === 'ltr' ? 'ltr' : 'rtl'"
>
<div
class="flex flex-col-reverse md:flex-row justify-between items-center h-screen-sm border-b md:border-0"
class="flex flex-col-reverse md:flex-row justify-between items-center h-screen-sm border-b md:border-0 hidden"
>
<div
class="basis-2/2 md:basis-1/2 flex-col justify-start items-center"
Expand Down
Loading

0 comments on commit 6d00117

Please sign in to comment.