mirror of
https://github.com/lumehq/lume.git
synced 2025-09-27 09:46:19 +02:00
add create block
This commit is contained in:
@@ -1,12 +1,25 @@
|
|||||||
import { Dialog, Transition } from "@headlessui/react";
|
import { Dialog, Transition } from "@headlessui/react";
|
||||||
import CancelIcon from "@icons/cancel";
|
import CancelIcon from "@icons/cancel";
|
||||||
import PlusIcon from "@icons/plus";
|
import PlusIcon from "@icons/plus";
|
||||||
|
import { useActiveAccount } from "@utils/hooks/useActiveAccount";
|
||||||
|
import { createBlock } from "@utils/storage";
|
||||||
import { Fragment, useState } from "react";
|
import { Fragment, useState } from "react";
|
||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
|
||||||
export function CreateBlockModal() {
|
export function CreateBlockModal() {
|
||||||
|
const {
|
||||||
|
register,
|
||||||
|
handleSubmit,
|
||||||
|
reset,
|
||||||
|
setValue,
|
||||||
|
formState: { isDirty, isValid },
|
||||||
|
} = useForm();
|
||||||
|
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
|
const { account } = useActiveAccount();
|
||||||
|
|
||||||
const closeModal = () => {
|
const closeModal = () => {
|
||||||
setIsOpen(false);
|
setIsOpen(false);
|
||||||
};
|
};
|
||||||
@@ -15,12 +28,25 @@ export function CreateBlockModal() {
|
|||||||
setIsOpen(true);
|
setIsOpen(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onSubmit = (data: any) => {
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
createBlock(account.id, data.kind, data.title, data.content).then(() => {
|
||||||
|
// reset form
|
||||||
|
reset();
|
||||||
|
// close modal
|
||||||
|
setIsOpen(false);
|
||||||
|
// stop loading
|
||||||
|
setLoading(false);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
onClick={() => openModal()}
|
onClick={() => openModal()}
|
||||||
className="group inline-flex h-8 items-center gap-2.5 rounded-md px-2.5 hover:bg-zinc-900"
|
className="group inline-flex flex-col items-center gap-2.5 p-4 rounded-md hover:bg-zinc-900"
|
||||||
>
|
>
|
||||||
<div className="inline-flex h-5 w-5 shrink items-center justify-center rounded bg-zinc-900 group-hover:bg-zinc-800">
|
<div className="inline-flex h-5 w-5 shrink items-center justify-center rounded bg-zinc-900 group-hover:bg-zinc-800">
|
||||||
<PlusIcon width={12} height={12} className="text-zinc-500" />
|
<PlusIcon width={12} height={12} className="text-zinc-500" />
|
||||||
@@ -77,12 +103,94 @@ export function CreateBlockModal() {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<Dialog.Description className="leading-tight text-zinc-300">
|
<Dialog.Description className="leading-tight text-zinc-300">
|
||||||
Channels are freedom square, everyone can speech freely,
|
Personalize your space by adding a new block.
|
||||||
no one can stop you or deceive what to speech
|
|
||||||
</Dialog.Description>
|
</Dialog.Description>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex h-full w-full flex-col overflow-y-auto px-5 pb-5 pt-3" />
|
<div className="flex h-full w-full flex-col overflow-y-auto px-5 pb-5 pt-3">
|
||||||
|
<form
|
||||||
|
onSubmit={handleSubmit(onSubmit)}
|
||||||
|
className="flex h-full w-full flex-col gap-4"
|
||||||
|
>
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<label className="text-base font-semibold uppercase tracking-wider text-zinc-400">
|
||||||
|
Type *
|
||||||
|
</label>
|
||||||
|
<div className="relative w-full shrink-0 overflow-hidden before:pointer-events-none before:absolute before:-inset-1 before:rounded-[11px] before:border before:border-fuchsia-500 before:opacity-0 before:ring-2 before:ring-fuchsia-500/20 before:transition after:pointer-events-none after:absolute after:inset-px after:rounded-[7px] after:shadow-highlight after:shadow-white/5 after:transition focus-within:before:opacity-100 focus-within:after:shadow-fuchsia-500/100 dark:focus-within:after:shadow-fuchsia-500/20">
|
||||||
|
<input
|
||||||
|
type={"text"}
|
||||||
|
{...register("kind", {
|
||||||
|
required: true,
|
||||||
|
})}
|
||||||
|
spellCheck={false}
|
||||||
|
className="relative h-10 w-full rounded-lg border border-black/5 px-3 py-2 shadow-input shadow-black/5 !outline-none placeholder:text-zinc-400 dark:bg-zinc-800 dark:text-white dark:shadow-black/10 dark:placeholder:text-zinc-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<label className="text-base font-semibold uppercase tracking-wider text-zinc-400">
|
||||||
|
Block Title *
|
||||||
|
</label>
|
||||||
|
<div className="relative w-full shrink-0 overflow-hidden before:pointer-events-none before:absolute before:-inset-1 before:rounded-[11px] before:border before:border-fuchsia-500 before:opacity-0 before:ring-2 before:ring-fuchsia-500/20 before:transition after:pointer-events-none after:absolute after:inset-px after:rounded-[7px] after:shadow-highlight after:shadow-white/5 after:transition focus-within:before:opacity-100 focus-within:after:shadow-fuchsia-500/100 dark:focus-within:after:shadow-fuchsia-500/20">
|
||||||
|
<input
|
||||||
|
type={"text"}
|
||||||
|
{...register("title", {
|
||||||
|
required: true,
|
||||||
|
minLength: 4,
|
||||||
|
})}
|
||||||
|
spellCheck={false}
|
||||||
|
className="relative h-10 w-full rounded-lg border border-black/5 px-3 py-2 shadow-input shadow-black/5 !outline-none placeholder:text-zinc-400 dark:bg-zinc-800 dark:text-white dark:shadow-black/10 dark:placeholder:text-zinc-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex flex-col gap-1">
|
||||||
|
<label className="text-base font-semibold uppercase tracking-wider text-zinc-400">
|
||||||
|
Content *
|
||||||
|
</label>
|
||||||
|
<div className="relative h-20 w-full shrink-0 overflow-hidden before:pointer-events-none before:absolute before:-inset-1 before:rounded-[11px] before:border before:border-fuchsia-500 before:opacity-0 before:ring-2 before:ring-fuchsia-500/20 before:transition after:pointer-events-none after:absolute after:inset-px after:rounded-[7px] after:shadow-highlight after:shadow-white/5 after:transition focus-within:before:opacity-100 focus-within:after:shadow-fuchsia-500/100 dark:focus-within:after:shadow-fuchsia-500/20">
|
||||||
|
<textarea
|
||||||
|
{...register("content", {
|
||||||
|
required: true,
|
||||||
|
})}
|
||||||
|
spellCheck={false}
|
||||||
|
className="relative h-20 w-full resize-none rounded-lg border border-black/5 px-3 py-2 shadow-input shadow-black/5 !outline-none placeholder:text-zinc-400 dark:bg-zinc-800 dark:text-white dark:shadow-black/10 dark:placeholder:text-zinc-500"
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
className="inline-flex h-11 w-full transform items-center justify-center rounded-lg bg-fuchsia-500 font-medium text-white shadow-button active:translate-y-1 disabled:cursor-not-allowed disabled:opacity-30"
|
||||||
|
>
|
||||||
|
{loading ? (
|
||||||
|
<svg
|
||||||
|
className="h-4 w-4 animate-spin text-black dark:text-white"
|
||||||
|
xmlns="http://www.w3.org/2000/svg"
|
||||||
|
fill="none"
|
||||||
|
viewBox="0 0 24 24"
|
||||||
|
>
|
||||||
|
<title id="loading">Loading</title>
|
||||||
|
<circle
|
||||||
|
className="opacity-25"
|
||||||
|
cx="12"
|
||||||
|
cy="12"
|
||||||
|
r="10"
|
||||||
|
stroke="currentColor"
|
||||||
|
strokeWidth="4"
|
||||||
|
/>
|
||||||
|
<path
|
||||||
|
className="opacity-75"
|
||||||
|
fill="currentColor"
|
||||||
|
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
|
||||||
|
/>
|
||||||
|
</svg>
|
||||||
|
) : (
|
||||||
|
"Create block"
|
||||||
|
)}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
</Dialog.Panel>
|
</Dialog.Panel>
|
||||||
</Transition.Child>
|
</Transition.Child>
|
||||||
</div>
|
</div>
|
||||||
|
@@ -12,14 +12,12 @@ export const METADATA_SERVICE = "https://rbr.bio";
|
|||||||
export const READONLY_RELAYS = [
|
export const READONLY_RELAYS = [
|
||||||
"wss://welcome.nostr.wine",
|
"wss://welcome.nostr.wine",
|
||||||
"wss://relay.nostr.band/all",
|
"wss://relay.nostr.band/all",
|
||||||
"wss://relay.damus.io",
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// write-only relay list
|
// write-only relay list
|
||||||
export const WRITEONLY_RELAYS = [
|
export const WRITEONLY_RELAYS = [
|
||||||
"wss://nostr.mutinywallet.com",
|
"wss://nostr.mutinywallet.com",
|
||||||
"wss://relay.nostr.band",
|
"wss://relay.nostr.band",
|
||||||
"wss://relay.damus.io",
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// full-relay list
|
// full-relay list
|
||||||
@@ -27,5 +25,4 @@ export const FULL_RELAYS = [
|
|||||||
"wss://welcome.nostr.wine",
|
"wss://welcome.nostr.wine",
|
||||||
"wss://relay.nostr.band/all",
|
"wss://relay.nostr.band/all",
|
||||||
"wss://nostr.mutinywallet.com",
|
"wss://nostr.mutinywallet.com",
|
||||||
"wss://relay.damus.io",
|
|
||||||
];
|
];
|
||||||
|
@@ -328,3 +328,17 @@ export async function getBlocks(account_id: number) {
|
|||||||
`SELECT * FROM blocks WHERE account_id <= "${account_id}";`,
|
`SELECT * FROM blocks WHERE account_id <= "${account_id}";`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// create block
|
||||||
|
export async function createBlock(
|
||||||
|
account_id: number,
|
||||||
|
kind: number,
|
||||||
|
title: string,
|
||||||
|
content: any,
|
||||||
|
) {
|
||||||
|
const db = await connect();
|
||||||
|
return await db.execute(
|
||||||
|
"INSERT OR IGNORE INTO blocks (account_id, kind, title, content) VALUES (?, ?, ?, ?);",
|
||||||
|
[account_id, kind, title, content],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user