From b0242c625359669ab5a6c4729098096ab9a116f9 Mon Sep 17 00:00:00 2001 From: Austin Chen Date: Sat, 10 Sep 2022 12:47:33 -0700 Subject: [PATCH] Bring up a list of contracts with @ --- web/components/editor/mention-list.tsx | 33 ++++++++++++--------- web/components/editor/mention-suggestion.ts | 12 ++++---- web/components/editor/mention.tsx | 8 +++-- web/hooks/use-contracts.ts | 9 +++++- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/web/components/editor/mention-list.tsx b/web/components/editor/mention-list.tsx index aeab4636..bda9d2fc 100644 --- a/web/components/editor/mention-list.tsx +++ b/web/components/editor/mention-list.tsx @@ -1,24 +1,26 @@ import { SuggestionProps } from '@tiptap/suggestion' import clsx from 'clsx' -import { User } from 'common/user' +import { Contract } from 'common/contract' import { forwardRef, useEffect, useImperativeHandle, useState } from 'react' +import { contractPath } from 'web/lib/firebase/contracts' import { Avatar } from '../avatar' // copied from https://tiptap.dev/api/nodes/mention#usage -export const MentionList = forwardRef((props: SuggestionProps, ref) => { - const { items: users, command } = props +const M = forwardRef((props: SuggestionProps, ref) => { + const { items: contracts, command } = props const [selectedIndex, setSelectedIndex] = useState(0) - useEffect(() => setSelectedIndex(0), [users]) + useEffect(() => setSelectedIndex(0), [contracts]) const submitUser = (index: number) => { - const user = users[index] - if (user) command({ id: user.id, label: user.username } as any) + const contract = contracts[index] + if (contract) + command({ id: contract.id, label: contractPath(contract) } as any) } const onUp = () => - setSelectedIndex((i) => (i + users.length - 1) % users.length) - const onDown = () => setSelectedIndex((i) => (i + 1) % users.length) + setSelectedIndex((i) => (i + contracts.length - 1) % contracts.length) + const onDown = () => setSelectedIndex((i) => (i + 1) % contracts.length) const onEnter = () => submitUser(selectedIndex) useImperativeHandle(ref, () => ({ @@ -41,23 +43,26 @@ export const MentionList = forwardRef((props: SuggestionProps, ref) => { return (
- {!users.length ? ( + {!contracts.length ? ( No results... ) : ( - users.map((user, i) => ( + contracts.map((contract, i) => ( )) )}
) }) + +// Just to keep the formatting pretty +export { M as MentionList } diff --git a/web/components/editor/mention-suggestion.ts b/web/components/editor/mention-suggestion.ts index 9f016d47..e849385b 100644 --- a/web/components/editor/mention-suggestion.ts +++ b/web/components/editor/mention-suggestion.ts @@ -3,7 +3,8 @@ import { ReactRenderer } from '@tiptap/react' import { searchInAny } from 'common/util/parse' import { orderBy } from 'lodash' import tippy from 'tippy.js' -import { getCachedUsers } from 'web/hooks/use-users' +import { getCachedContracts } from 'web/hooks/use-contracts' +// import { getCachedUsers } from 'web/hooks/use-users' import { MentionList } from './mention-list' type Suggestion = MentionOptions['suggestion'] @@ -15,13 +16,10 @@ const beginsWith = (text: string, query: string) => export const mentionSuggestion: Suggestion = { items: async ({ query }) => orderBy( - (await getCachedUsers()).filter((u) => - searchInAny(query, u.username, u.name) + (await getCachedContracts()).filter((c) => + searchInAny(query, c.question) ), - [ - (u) => [u.name, u.username].some((s) => beginsWith(s, query)), - 'followerCountCached', - ], + [(c) => [c.question].some((s) => beginsWith(s, query))], ['desc', 'desc'] ).slice(0, 5), render: () => { diff --git a/web/components/editor/mention.tsx b/web/components/editor/mention.tsx index 5ccea6f5..22375620 100644 --- a/web/components/editor/mention.tsx +++ b/web/components/editor/mention.tsx @@ -5,14 +5,18 @@ import { ReactNodeViewRenderer, } from '@tiptap/react' import clsx from 'clsx' -import { Linkify } from '../linkify' +import { useContract } from 'web/hooks/use-contract' +import { ContractCard } from '../contract/contract-card' const name = 'mention-component' const MentionComponent = (props: any) => { + const contract = useContract(props.node.attrs.id) + return ( - + {/* */} + {contract && } ) } diff --git a/web/hooks/use-contracts.ts b/web/hooks/use-contracts.ts index 2f3bea7b..38bcd93d 100644 --- a/web/hooks/use-contracts.ts +++ b/web/hooks/use-contracts.ts @@ -11,8 +11,9 @@ import { listenForNewContracts, getUserBetContracts, getUserBetContractsQuery, + listAllContracts, } from 'web/lib/firebase/contracts' -import { useQueryClient } from 'react-query' +import { QueryClient, useQueryClient } from 'react-query' import { MINUTE_MS } from 'common/util/time' export const useContracts = () => { @@ -25,6 +26,12 @@ export const useContracts = () => { return contracts } +const q = new QueryClient() +export const getCachedContracts = async () => + q.fetchQuery(['contracts'], () => listAllContracts(1000), { + staleTime: Infinity, + }) + export const useActiveContracts = () => { const [activeContracts, setActiveContracts] = useState< Contract[] | undefined