Skip to content

Commit

Permalink
Merge pull request #8 from maxpetretta/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
maxpetretta authored Sep 20, 2022
2 parents b093102 + ba40b7b commit b64b789
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 69 deletions.
5 changes: 3 additions & 2 deletions react/components/Address.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import Link from "next/link"
import { useEffect, useState } from "react"
import { useEnsName } from "wagmi"
import { AddressProps } from "../lib/types"

export default function Address(props) {
export default function Address(props: AddressProps) {
const match = props.address.match(/^(0x.{4}).+(.{4})$/)
const truncated = match[1] + "..." + match[2]
const truncated = match![1] + "..." + match![2]

const [ens, setEns] = useState(truncated)

Expand Down
20 changes: 13 additions & 7 deletions react/components/AppProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
import { ethers } from "ethers"
import { createContext, useContext, useState } from "react"
import React, { createContext, useContext, useState } from "react"
import Confetti from "react-confetti"
import toast from "react-hot-toast"
import { useAccount, useContractEvent, useContractRead } from "wagmi"
import { contractABI, contractAddress } from "../lib/contract.js"
import { contractABI, contractAddress } from "../lib/contract"
import { Tweet as TweetType } from "../lib/types"

const AppContext = createContext(null)
const AppContext = createContext<{
tweets: Map<number, TweetType> | undefined
setTweets: React.Dispatch<
React.SetStateAction<Map<number, TweetType> | undefined>
>
}>(undefined!)

export const AppProvider = ({ children }) => {
export const AppProvider = ({ children }: { children: React.ReactNode }) => {
const [address, setAddress] = useState("")
const [tweets, setTweets] = useState(null)
const [tweets, setTweets] = useState<Map<number, TweetType> | undefined>()
const [confetti, setConfetti] = useState(false)
useAccount({
onSuccess(data) {
if (data && !address) {
if (data.address && !address) {
setAddress(data.address)
}
},
Expand All @@ -31,7 +37,7 @@ export const AppProvider = ({ children }) => {
{
onSuccess(data) {
if (data) {
setTweets((prevState) => {
setTweets((prevState: Map<number, TweetType> | undefined) => {
let newState = new Map(prevState)
data.forEach((tweet, id) => {
newState.set(id + 1, {
Expand Down
7 changes: 4 additions & 3 deletions react/components/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useState } from "react"
import { useEnsAvatar } from "wagmi"
import { AvatarProps } from "../lib/types"

export default function Avatar(props) {
export default function Avatar(props: AvatarProps) {
const [avatar, setAvatar] = useState("")
const seedrandom = require("seedrandom")
const colors = [
Expand Down Expand Up @@ -73,9 +74,9 @@ export default function Avatar(props) {
avatar
? "hidden"
: props.styles
? `${props.styles} ${getColorFromAddress(props.address)}`
? `${props.styles} ${getColorFromAddress(Number(props.address))}`
: `mx-3 inline h-12 w-12 self-start rounded-full ${getColorFromAddress(
props.address
Number(props.address)
)}`
}
>
Expand Down
5 changes: 4 additions & 1 deletion react/components/Controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export default function Controls() {
toast.success("Cleared tweets")
console.debug("Cleared --", data.hash)
totalTweetsRefetch().then((value) => {
console.debug("Retrieved total tweet count --", value.data.toNumber())
console.debug(
"Retrieved total tweet count --",
value.data!.toNumber()
)
})
},
onError(error) {
Expand Down
10 changes: 7 additions & 3 deletions react/components/EditModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
useContractWrite,
UserRejectedRequestError,
} from "wagmi"
import { contractABI, contractAddress } from "../lib/contract.js"
import { contractABI, contractAddress } from "../lib/contract"
import { EditProps } from "../lib/types.js"
import Avatar from "./Avatar"

export default function ReplyTweet(props) {
export default function EditTweet(props: EditProps) {
/**
* Contract hooks
*/
Expand All @@ -30,7 +31,10 @@ export default function ReplyTweet(props) {
totalTweetsRefetch().then((value) => {
toast.success("Edited tweet!")
console.debug("Edited --", data.hash)
console.debug("Retrieved total tweet count --", value.data.toNumber())
console.debug(
"Retrieved total tweet count --",
value.data!.toNumber()
)
})
},
onError(error) {
Expand Down
9 changes: 6 additions & 3 deletions react/components/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
useContractWrite,
UserRejectedRequestError,
} from "wagmi"
import { contractABI, contractAddress } from "../lib/contract.js"
import { contractABI, contractAddress } from "../lib/contract"
import Avatar from "./Avatar"

export default function Editor() {
Expand All @@ -18,7 +18,7 @@ export default function Editor() {
const [price, setPrice] = useState("")
useAccount({
onSuccess(data) {
if (data && !address) {
if (data.address && !address) {
setAddress(data.address)
}
},
Expand Down Expand Up @@ -59,7 +59,10 @@ export default function Editor() {
totalTweetsRefetch().then((value) => {
toast.success("Sent tweet!")
console.debug("Tweeted --", data.hash)
console.debug("Retrieved total tweet count --", value.data.toNumber())
console.debug(
"Retrieved total tweet count --",
value.data!.toNumber()
)
})
},
onError(error) {
Expand Down
9 changes: 5 additions & 4 deletions react/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { useRouter } from "next/router"
import { useState } from "react"
import { toast, Toaster } from "react-hot-toast"
import { useAccount, useContractRead } from "wagmi"
import { contractABI, contractAddress } from "../lib/contract.js"
import { contractABI, contractAddress } from "../lib/contract"
import { LayoutProps } from "../lib/types.js"
import Nav from "./Nav"
import Sidebar from "./Sidebar"
import TweetModal from "./TweetModal"

export default function Layout(props) {
export default function Layout(props: LayoutProps) {
const { children, ...pageMeta } = props
const router = useRouter()
const meta = {
Expand All @@ -25,7 +26,7 @@ export default function Layout(props) {
const [isOwner, setIsOwner] = useState(false)
useAccount({
onSuccess(data) {
if (data && !address) {
if (data.address && !address) {
setAddress(data.address)
console.debug("Found authorized account: ", data.address)

Expand All @@ -48,7 +49,7 @@ export default function Layout(props) {
// Check if this is the owner's wallet
if (
ownerData &&
ownerData.toUpperCase() === data.address.toUpperCase()
ownerData.toUpperCase() === data.address!.toUpperCase()
) {
setIsOwner(true)
} else {
Expand Down
2 changes: 1 addition & 1 deletion react/components/Nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default function Nav() {
const [address, setAddress] = useState("")
useAccount({
onSuccess(data) {
if (data && !address) {
if (data.address && !address) {
setAddress(data.address)
}
},
Expand Down
10 changes: 7 additions & 3 deletions react/components/ReplyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
useContractWrite,
UserRejectedRequestError,
} from "wagmi"
import { contractABI, contractAddress } from "../lib/contract.js"
import { contractABI, contractAddress } from "../lib/contract"
import { ReplyProps } from "../lib/types.js"
import Address from "./Address"
import Avatar from "./Avatar"

Expand All @@ -15,7 +16,7 @@ import dayjs from "dayjs"
import relativeTime from "dayjs/plugin/relativeTime"
dayjs.extend(relativeTime)

export default function ReplyTweet(props) {
export default function ReplyTweet(props: ReplyProps) {
const [price, setPrice] = useState("")
const [message, setMessage] = useState("")

Expand Down Expand Up @@ -54,7 +55,10 @@ export default function ReplyTweet(props) {
totalTweetsRefetch().then((value) => {
toast.success("Sent tweet!")
console.debug("Tweeted --", data.hash)
console.debug("Retrieved total tweet count --", value.data.toNumber())
console.debug(
"Retrieved total tweet count --",
value.data!.toNumber()
)
})
},
onError(error) {
Expand Down
2 changes: 1 addition & 1 deletion react/components/Sidebar.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ConnectButton } from "@rainbow-me/rainbowkit"
import Controls from "./Controls"

export default function Sidebar(props) {
export default function Sidebar(props: { isOwner: boolean }) {
return (
<aside className="hidden min-h-screen w-1/4 flex-col lg:flex">
<div className="mt-3 self-center">
Expand Down
37 changes: 23 additions & 14 deletions react/components/Tweet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import {
useContractWrite,
UserRejectedRequestError,
} from "wagmi"
import { contractABI, contractAddress } from "../lib/contract.js"
import { Tweet as TweetType } from "../lib/types"
import { contractABI, contractAddress } from "../lib/contract"
import { Tweet as TweetType, TweetProps } from "../lib/types"
import Address from "./Address"
import { useTweets } from "./AppProvider"
import Avatar from "./Avatar"
Expand All @@ -20,7 +20,7 @@ import dayjs from "dayjs"
import relativeTime from "dayjs/plugin/relativeTime"
dayjs.extend(relativeTime)

export default function Tweet(props) {
export default function Tweet(props: TweetProps) {
const [address, setAddress] = useState("")
const [price, setPrice] = useState("")
const [tweet, setTweet]: [any, any] = useState()
Expand All @@ -32,7 +32,7 @@ export default function Tweet(props) {
const { tweets } = useTweets()
useAccount({
onSuccess(data) {
if (data && !address) {
if (data.address && !address) {
setAddress(data.address)
}
},
Expand Down Expand Up @@ -73,7 +73,10 @@ export default function Tweet(props) {
totalTweetsRefetch().then((value) => {
toast.success("Sent tweet!")
console.debug("Tweeted --", data.hash)
console.debug("Retrieved total tweet count --", value.data.toNumber())
console.debug(
"Retrieved total tweet count --",
value.data!.toNumber()
)
})
},
onError(error) {
Expand All @@ -99,7 +102,10 @@ export default function Tweet(props) {
totalTweetsRefetch().then((value) => {
toast.success("Deleted tweet!")
console.debug("Deleted --", data.hash)
console.debug("Retrieved total tweet count --", value.data.toNumber())
console.debug(
"Retrieved total tweet count --",
value.data!.toNumber()
)
})
},
onError(error) {
Expand Down Expand Up @@ -153,21 +159,24 @@ export default function Tweet(props) {
* @param {number} id
* @returns {Array}
*/
const getReplies = (id: number): TweetType[] => {
let replies = [...tweets.entries()].filter(
(tweet) => tweet[1].replyID.eq(id) && !tweet[1].deleted
)
return replies
const getReplies = (id: number): [number, TweetType][] => {
if (tweets) {
let replies = [...tweets.entries()].filter(
(tweet) => tweet[1].replyID.eq(id) && !tweet[1].deleted
)
return replies
}
return []
}

/*
* On page load, get the relevant tweet or retweet
*/
useEffect(() => {
if (props.id && tweets.get(props.id).retweetID.isZero()) {
if (tweets && props.id && tweets.get(props.id)!.retweetID.isZero()) {
setTweet(tweets.get(props.id))
} else if (props.id) {
const retweetID = tweets.get(props.id).retweetID
} else if (tweets && props.id) {
const retweetID = tweets.get(props.id)!.retweetID
setTweet(tweets.get(retweetID.toNumber()))
setRetweet(tweets.get(props.id))
}
Expand Down
30 changes: 18 additions & 12 deletions react/components/TweetList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@ import { Tweet as TweetType } from "../lib/types"
import { useTweets } from "./AppProvider"
import Tweet from "./Tweet"

export default function TweetList(props) {
export default function TweetList(props: { author?: string }) {
const { tweets } = useTweets()

/**
* Filter for all tweets from the specified address
* @returns {Array}
*/
const getAuthorTweets = (): TweetType[] => {
let filtered = [...tweets.values()].filter(
(tweet) => tweet.replyID.eq(0) && !tweet.deleted
)
if (props.author) {
filtered = filtered.filter((tweet) => tweet.from == props.author)
if (tweets) {
let filtered = [...tweets.values()].filter(
(tweet) => tweet.replyID.eq(0) && !tweet.deleted
)
if (props.author) {
filtered = filtered.filter((tweet) => tweet.from == props.author)
}
return filtered
}
return filtered
return []
}

/**
* Filter for all replies to a tweet
* @param {number} id
* @returns {Array}
*/
const getReplies = (id: number): TweetType[] => {
let replies = [...tweets.entries()].filter(
(tweet) => tweet[1].replyID.eq(id) && !tweet[1].deleted
)
return replies
const getReplies = (id: number): [number, TweetType][] => {
if (tweets) {
let replies = [...tweets.entries()].filter(
(tweet) => tweet[1].replyID.eq(id) && !tweet[1].deleted
)
return replies
}
return []
}

return (
Expand Down
10 changes: 7 additions & 3 deletions react/components/TweetModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import {
useContractWrite,
UserRejectedRequestError,
} from "wagmi"
import { contractABI, contractAddress } from "../lib/contract.js"
import { contractABI, contractAddress } from "../lib/contract"
import { ModalProps } from "../lib/types.js"
import Avatar from "./Avatar"

export default function TweetModal(props) {
export default function TweetModal(props: ModalProps) {
const [price, setPrice] = useState("")
const [message, setMessage] = useState("")

Expand Down Expand Up @@ -48,7 +49,10 @@ export default function TweetModal(props) {
totalTweetsRefetch().then((value) => {
toast.success("Sent tweet!")
console.debug("Tweeted --", data.hash)
console.debug("Retrieved total tweet count --", value.data.toNumber())
console.debug(
"Retrieved total tweet count --",
value.data!.toNumber()
)
})
},
onError(error) {
Expand Down
File renamed without changes.
Loading

1 comment on commit b64b789

@vercel
Copy link

@vercel vercel bot commented on b64b789 Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

twitt3r – ./

twitt3r.xyz
twitt3r-git-master-maxpetretta.vercel.app
twitt3r-maxpetretta.vercel.app

Please sign in to comment.