Skip to content

Commit

Permalink
simplified interface; new message modal; performance tweaks; better a…
Browse files Browse the repository at this point in the history
…ccount handling
  • Loading branch information
franzos committed Sep 6, 2023
1 parent 32c30d5 commit 9e901d5
Show file tree
Hide file tree
Showing 22 changed files with 511 additions and 535 deletions.
21 changes: 18 additions & 3 deletions client-web/src/components/bottom-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Button,
HStack,
Link,
Spacer,
Text,
useColorMode,
useToast,
Expand All @@ -12,6 +13,7 @@ import { useEffect, useState } from "react";
import { RELAY_MESSAGE_TYPE } from "@nostr-ts/common";
import { excerpt } from "../lib/excerpt";
import { NavLink } from "react-router-dom";
import { EventFormModal } from "./event-form-modal";

export function BottomBar() {
const { colorMode, toggleColorMode } = useColorMode();
Expand All @@ -24,6 +26,7 @@ export function BottomBar() {
const [lastCount, setLastCount] = useState(0);
const [subscriptionsCount, setSubscriptionsCount] = useState<number>(0);
const [relaysCount, setRelaysCount] = useState<number>(0);
const [queueItemsCount, setQueueItemsCount] = useState<number>(0);

const toast = useToast();

Expand All @@ -40,6 +43,10 @@ export function BottomBar() {
if (relays) {
setRelaysCount(relays.length);
}
const queue = await useNClient.getState().getQueueItems();
if (queue) {
setQueueItemsCount(queue.length);
}
};

useEffect(() => {
Expand Down Expand Up @@ -103,6 +110,11 @@ export function BottomBar() {
>
<HStack spacing={4}>
<>
<Button variant={"outline"} size="sm" onClick={toggleColorMode}>
Toggle {colorMode === "light" ? "Dark" : "Light"}
</Button>
<EventFormModal buttonSize="sm" />
<Spacer />
<HStack spacing={2}>
<Text fontSize="sm">Events:</Text>
<Text fontSize="xl" marginLeft={1}>
Expand All @@ -119,15 +131,18 @@ export function BottomBar() {
<Text fontSize="xl">{relaysCount}</Text>
</HStack>
</Link>
<Link as={NavLink} to="/queue">
<HStack spacing={2}>
<Text fontSize="sm">Queue:</Text>
<Text fontSize="xl">{queueItemsCount}</Text>
</HStack>
</Link>
<Link as={NavLink} to="/subscriptions">
<HStack spacing={2}>
<Text fontSize="sm">Subscriptions:</Text>
<Text fontSize="xl">{subscriptionsCount}</Text>
</HStack>
</Link>
<Button variant={"outline"} onClick={toggleColorMode}>
Toggle {colorMode === "light" ? "Dark" : "Light"}
</Button>
</>
</HStack>
</Box>
Expand Down
31 changes: 23 additions & 8 deletions client-web/src/components/connect-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Input,
ModalFooter,
useToast,
ButtonGroup,
} from "@chakra-ui/react";
import { useNClient } from "../state/client";
import { useEffect, useState } from "react";
Expand Down Expand Up @@ -91,6 +92,10 @@ export function ConnectModal({ isOpen, onClose }: ConnectModalProps) {
}
};

const resetRelays = () => {
setInitialRelayUrls(objectOfRelaysToArray(DEFAULT_RELAYS));
};

useEffect(() => {
const relays = localStorage.getItem("nostr-client:relays");
if (relays) {
Expand Down Expand Up @@ -189,14 +194,24 @@ export function ConnectModal({ isOpen, onClose }: ConnectModalProps) {
</Button>
</Flex>

<Button
isLoading={loadingRelaysNos2x}
onClick={() => relaysFromExtention()}
size="xs"
marginTop={2}
>
Load from nos2x
</Button>
<ButtonGroup>
<Button
isLoading={loadingRelaysNos2x}
onClick={() => relaysFromExtention()}
size="xs"
marginTop={2}
>
Load from nos2x
</Button>
<Button
isLoading={loadingRelaysNos2x}
onClick={resetRelays}
size="xs"
marginTop={2}
>
Reset
</Button>
</ButtonGroup>
</ModalBody>
<ModalFooter>
<Button
Expand Down
25 changes: 21 additions & 4 deletions client-web/src/components/create-event-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ interface CreateEventFormProps {
inResponseTo?: NEvent | undefined;
relayUrls?: string[];
kind?: NKIND;
sendCallback?: (eventId: string) => void;
}

export const CreateEventForm = (props: CreateEventFormProps) => {
const [isReady] = useNClient((state) => [
const [isReady, keypairIsLoaded] = useNClient((state) => [
state.connected && state.keystore !== "none",
state.keypairIsLoaded,
]);
const [isBusy, setBusy] = useState<boolean>(false);
const [errors, setErrors] = useState<string[]>([]);
Expand All @@ -56,6 +58,11 @@ export const CreateEventForm = (props: CreateEventFormProps) => {
state.keypair,
]);

const message =
isReady || keypairIsLoaded
? undefined
: "Login and connect to send events.";

/**
* Event data
*/
Expand Down Expand Up @@ -98,7 +105,9 @@ export const CreateEventForm = (props: CreateEventFormProps) => {

const toast = useToast();

const relayUrls = props.relayUrls ? props.relayUrls : [];
const relayUrls = props.relayUrls ? props.relayUrls : undefined;

const relayUrl = relayUrls && relayUrls.length > 0 ? relayUrls[0] : "";

/**
* Create event from inputs
Expand Down Expand Up @@ -135,7 +144,7 @@ export const CreateEventForm = (props: CreateEventFormProps) => {
case "NewRecommendRelay":
return {
event: NewRecommendRelay({
relayUrl: relayUrls[0],
relayUrl: relayUrl,
}),
};
case "NewQuoteRepost":
Expand All @@ -147,7 +156,7 @@ export const CreateEventForm = (props: CreateEventFormProps) => {
return {
event: NewQuoteRepost({
inResponseTo: props.inResponseTo,
relayUrl: relayUrls[0],
relayUrl: relayUrl,
}),
};
default:
Expand Down Expand Up @@ -230,6 +239,9 @@ export const CreateEventForm = (props: CreateEventFormProps) => {

reset();
setBusy(false);
if (props.sendCallback) {
props.sendCallback(evId);
}
}
} catch (e) {
let error = "";
Expand Down Expand Up @@ -302,6 +314,11 @@ export const CreateEventForm = (props: CreateEventFormProps) => {
{error}
</Box>
))}
{message && (
<Box color="red.500" paddingBottom={2}>
{message}
</Box>
)}
<Button
type="submit"
variant={"solid"}
Expand Down
48 changes: 48 additions & 0 deletions client-web/src/components/event-form-modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import {
useDisclosure,
Modal,
ModalOverlay,
ModalContent,
ModalHeader,
ModalCloseButton,
ModalBody,
ModalFooter,
Button,
} from "@chakra-ui/react";
import { CreateEventForm } from "./create-event-form";

interface EventFormModalProps {
buttonSize: string;
}

export function EventFormModal(props?: EventFormModalProps) {
const { isOpen, onOpen, onClose } = useDisclosure();

const sendCallback = () => {
onClose();
};

return (
<>
<Button size={props?.buttonSize || "md"} onClick={onOpen}>
Post message
</Button>
<Modal isOpen={isOpen} onClose={onClose}>
<ModalOverlay />
<ModalContent>
<ModalHeader>Broadcast to the Network</ModalHeader>
<ModalCloseButton />
<ModalBody>
<CreateEventForm sendCallback={sendCallback} />
</ModalBody>

<ModalFooter>
<Button colorScheme="blue" mr={3} onClick={onClose}>
Close
</Button>
</ModalFooter>
</ModalContent>
</Modal>
</>
);
}
31 changes: 21 additions & 10 deletions client-web/src/components/event.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ export function Event({
onOpen();
};

const sendCallback = () => {
onReplyClose();
};

const relatedRelay = async () => {
const relays = await useNClient.getState().getRelays();
const relay = relays.find((r) => eventRelayUrls.includes(r.url));
Expand Down Expand Up @@ -391,6 +395,23 @@ export function Event({
return (
<>
{EventCard}
{isReplyOpen && (
<Box
padding={4}
marginBottom={2}
marginTop={2}
background="background"
borderRadius={4}
>
<CreateEventForm
isResponse={true}
inResponseTo={event}
relayUrls={eventRelayUrls}
kind="NewShortTextNoteResponse"
sendCallback={sendCallback}
/>
</Box>
)}
<HStack padding={2} flexWrap="wrap">
{reactions && (
<>
Expand Down Expand Up @@ -479,16 +500,6 @@ export function Event({
})}
{ImageModal}
{EventModal}
{isReplyOpen && (
<Box padding={4} background="background" borderRadius={4}>
<CreateEventForm
isResponse={true}
inResponseTo={event}
relayUrls={eventRelayUrls}
kind="NewShortTextNoteResponse"
/>
</Box>
)}
</>
);
}
17 changes: 9 additions & 8 deletions client-web/src/components/events.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Box, Button, Text } from "@chakra-ui/react";
import { NFilters } from "@nostr-ts/common";
import { useNClient } from "../state/client";
import { Event } from "../components/event";
import { useEffect, useState } from "react";
import { MAX_EVENTS } from "../defaults";
import { User } from "./user";
import { NFilters } from "@nostr-ts/common";

export function Events(props: {
userComponent?: typeof User;
Expand Down Expand Up @@ -46,19 +46,19 @@ export function Events(props: {
const newEvents = async () => {
setMoreEventsCount(0);
setFilters(props.filters);
await useNClient.getState().setMaxEvents(MAX_EVENTS);
await useNClient.getState().clearEvents();
await useNClient.getState().setViewSubscription(props.view, filters);
await useNClient.getState().setViewSubscription(props.view, filters, {
reset: true,
});
setShowButtonsAnyway(false);
};

const showButtons = events.length >= maxEvents || showButtonsAnyway;

return (
<Box style={{ overflowWrap: "break-word", wordWrap: "break-word" }}>
{events.map((event) => {
{events.map((event, index) => {
return (
<Box padding={2} key={event.event.id}>
<Box padding={2} key={`${event.event.id}-${index}`}>
<Event
event={event.event}
user={event.user}
Expand All @@ -67,7 +67,6 @@ export function Events(props: {
mentions={event.mentions}
replies={event.replies}
eventRelayUrls={event.eventRelayUrls}
key={event.event.id}
userComponent={
props && props.userComponent ? (
event.user && event.user.pubkey ? (
Expand Down Expand Up @@ -96,7 +95,9 @@ export function Events(props: {
);
})}
{events.length === 0 && (
<Text>Waiting for fresh content ... hold on.</Text>
<Box marginTop={5} marginBottom={5} textAlign={"center"}>
<Text>Waiting for fresh content ... hold on.</Text>
</Box>
)}
{showButtons && (
<Box display="flex" justifyContent="space-between" padding={2}>
Expand Down
Loading

0 comments on commit 9e901d5

Please sign in to comment.