Skip to content

Commit

Permalink
Merge pull request #3 from TheMhv/development
Browse files Browse the repository at this point in the history
Fix and update the Widget
  • Loading branch information
TheMhv authored Dec 25, 2024
2 parents 6085349 + 1601638 commit cf6ee6b
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 86 deletions.
1 change: 1 addition & 0 deletions src/app/api/create_invoice/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ async function* check_payment(invoice: Invoice) {
}

yield `data: ${JSON.stringify({ status: "settled" })}\n\n`;
return;
}
6 changes: 5 additions & 1 deletion src/app/goal/[eventId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ export default async function GoalPage({ params }: PageProps) {
<Form
npub={event.author.toBech32()}
eventId={event.id.toBech32()}
config={config}
options={{
MODELS: config.MODELS,
MAX_TEXT_LENGTH: config.MAX_TEXT_LENGTH || 200,
MIN_SATOSHI_QNT: config.MIN_SATOSHI_QNT || 21,
}}
/>
</CardContent>
</Card>
Expand Down
11 changes: 9 additions & 2 deletions src/app/profile/[npub]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface PageProps {
params: { npub: string };
}

export default async function GoalPage({ params }: PageProps) {
export default async function ProfilePage({ params }: PageProps) {
const profile = await getUser(params.npub);

const banner = profile.getBanner();
Expand Down Expand Up @@ -51,7 +51,14 @@ export default async function GoalPage({ params }: PageProps) {
</CardHeader>

<CardContent>
<Form npub={params.npub} config={config} />
<Form
npub={params.npub}
options={{
MODELS: config.MODELS,
MAX_TEXT_LENGTH: config.MAX_TEXT_LENGTH || 200,
MIN_SATOSHI_QNT: config.MIN_SATOSHI_QNT || 21,
}}
/>
</CardContent>
</Card>
</>
Expand Down
6 changes: 2 additions & 4 deletions src/app/profile/[npub]/widget/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TTSWidget } from "@/components/Widget";

import "./widget.css";
import { RemoveLogo } from "@/components/utils/RemoveLogo";

import { TTSWidget } from "@/components/Widget";

interface PageProps {
params: { npub: string };
Expand All @@ -10,7 +9,6 @@ interface PageProps {
export default function widgetPage({ params }: PageProps) {
return (
<>
<RemoveLogo />
<TTSWidget pubkey={params.npub} />
</>
);
Expand Down
36 changes: 25 additions & 11 deletions src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import { ChangeEvent, FormEvent, useRef, useState } from "react";
import Image from "next/image";
import QRCode from "qrcode";

import { Settings } from "@/lib/config";

import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
Expand All @@ -31,11 +29,19 @@ interface FormData {

interface FormProps {
npub: string;
config: Settings;
options: {
MODELS: string[];
MAX_TEXT_LENGTH: number;
MIN_SATOSHI_QNT: number;
};
eventId?: string;
}

export default function Form({ npub, config, eventId = undefined }: FormProps) {
export default function Form({
npub,
options,
eventId = undefined,
}: FormProps) {
const [qrCode, setQRCode] = useState<string>("");
const [paymentStatus, setPaymentStatus] = useState<boolean>(false);
const [isSubmitting, setIsSubmitting] = useState<boolean>(false);
Expand Down Expand Up @@ -98,7 +104,8 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {
const reader = response.body!.getReader();
const decoder = new TextDecoder();

while (true) {
let loop = true;
while (loop) {
const { value, done } = await reader.read();
if (done) break;

Expand All @@ -120,6 +127,8 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {
// @ts-expect-error
await window.webln.sendPayment(parsedData.invoice.pr);
setPaymentStatus(true);
loop = false;
break;
} catch (error) {
console.error(error);
setQRCode(await QRCode.toDataURL(parsedData.invoice.pr));
Expand All @@ -129,6 +138,7 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {

if (parsedData.status === "settled") {
setPaymentStatus(true);
loop = false;
break;
}
}
Expand Down Expand Up @@ -207,7 +217,11 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {
Pagamento bem-sucedido!
</h3>

<p className="text-gray-600">Obrigado por ajudar na nossa meta!.</p>
{eventId ? (
<p className="text-gray-600">Obrigado por ajudar na nossa meta!.</p>
) : (
<p className="text-gray-600">Obrigado por usar o LiveSatoshi.</p>
)}

<div className="mt-6">
<Button
Expand Down Expand Up @@ -240,7 +254,7 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {
/>
</div>

{config.MODELS?.length > 0 && (
{options.MODELS.length > 0 && (
<>
<div className="space-y-2">
<Label className="flex items-center gap-2">
Expand All @@ -252,7 +266,7 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {
onValueChange={handleModelChange}
className="grid grid-cols-2 gap-4"
>
{config.MODELS.map((model, index) => (
{options.MODELS.map((model, index) => (
<div key={index} className="flex items-center space-x-2">
<RadioGroupItem
value={model}
Expand Down Expand Up @@ -285,7 +299,7 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {
id="text"
name="text"
value={formData.text}
maxLength={config.MAX_TEXT_LENGTH || 200}
maxLength={options.MAX_TEXT_LENGTH || 200}
onChange={handleInputChange}
required
/>
Expand All @@ -300,15 +314,15 @@ export default function Form({ npub, config, eventId = undefined }: FormProps) {
id="amount"
name="amount"
type="number"
min={config.MIN_SATOSHI_QNT || 100}
min={options.MIN_SATOSHI_QNT}
value={formData.amount}
onChange={handleInputChange}
required
/>

<p className="text-xs text-right text-card-foreground/75">
Quantidade mínima:{" "}
<span className="font-bold">{config.MIN_SATOSHI_QNT} sats</span>
<span className="font-bold">{options.MIN_SATOSHI_QNT} sats</span>
</p>
</div>

Expand Down
37 changes: 21 additions & 16 deletions src/components/NostrProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,57 +1,62 @@
"use client";

import { createContext, useState, useEffect } from "react";
import { createContext, useEffect, useState } from "react";
import {
Client,
loadWasmSync,
Nip07Signer,
NostrSigner,
} from "@rust-nostr/nostr-sdk";
import { Settings, loadConfig } from "@/lib/config";

interface NostrProviderProps {
withSigner?: boolean;
relays: string[];
children: React.ReactNode;
}

interface NostrContextProps {
client: Client | null;
signer: Nip07Signer | null;
signer: NostrSigner | null;
}

const NostrContext = createContext({
client: null,
signer: null,
} as NostrContextProps);

const config: Settings = loadConfig();

const NostrProvider: React.FC<NostrProviderProps> = ({ children }) => {
const NostrProvider: React.FC<NostrProviderProps> = ({
withSigner = false,
relays,
children,
}) => {
const [client, setClient] = useState<Client | null>(null);
const [signer, setSigner] = useState<Nip07Signer | null>(null);
const [signer, setSigner] = useState<NostrSigner | null>(null);

loadWasmSync();

useEffect(() => {
const nip07Signer = new Nip07Signer();
setSigner(nip07Signer);
if (withSigner) {
const nip07Signer = new Nip07Signer();

const newSigner = NostrSigner.nip07(nip07Signer);
setSigner(newSigner);
}

const newSigner = NostrSigner.nip07(nip07Signer);
const newClient = new Client(newSigner);
const newClient = new Client(signer || undefined);

const relays = config.RELAYS;
relays.map(async (relay) => {
await newClient.addRelay(relay);
});

newClient.connect().then(() => {
setClient(newClient);
});
}, []);
}, [withSigner, relays]);

const values: NostrContextProps = { client, signer };

return (
<NostrContext.Provider value={{ client, signer } as NostrContextProps}>
{children}
</NostrContext.Provider>
<NostrContext.Provider value={values}>{children}</NostrContext.Provider>
);
};

Expand Down
Loading

0 comments on commit cf6ee6b

Please sign in to comment.