Skip to content

Commit

Permalink
feat(community): add proposals comments (#1136)
Browse files Browse the repository at this point in the history
  • Loading branch information
OggyKUN authored Apr 10, 2024
1 parent 44fd917 commit d8f8160
Show file tree
Hide file tree
Showing 7 changed files with 292 additions and 21 deletions.
42 changes: 22 additions & 20 deletions src/containers/governance/proposalsDetail.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react/no-children-prop */
import { useEffect, useState } from 'react';
import { Pane, Text, ActionBar } from '@cybercongress/gravity';
import { Link, useParams } from 'react-router-dom';
import { Link, useParams, Routes, Route } from 'react-router-dom';
import { connect } from 'react-redux';
import ReactMarkdown from 'react-markdown';
import remarkGfm from 'remark-gfm';
Expand All @@ -21,12 +21,12 @@ import ActionBarDetail from './actionBarDatail';

import { formatNumber } from '../../utils/utils';

import ProposalsIdDetail from './proposalsIdDetail';
import ProposalsDetailProgressBar from './proposalsDetailProgressBar';
import ProposalsIdDetailTableVoters from './proposalsDetailTableVoters';
import { PROPOSAL_STATUS } from '../../utils/config';
import useSetActiveAddress from '../../hooks/useSetActiveAddress';
import { MainContainer } from '../portal/components';
import ProposalsRoutes from './proposalsRoutes';
import { useLocation, useNavigate } from 'react-router-dom';

const finalTallyResult = (item) => {
const finalVotes = {
Expand Down Expand Up @@ -184,6 +184,15 @@ function ProposalsDetail({ defaultAccount }) {
console.log(`proposals`, proposals);
console.log(`addressActive`, addressActive);

const location = useLocation();
const navigate = useNavigate();

useEffect(() => {
if (location.pathname === `/senate/${proposalId}`) {
navigate(`/senate/${proposalId}/comments`);
}
}, [location.pathname]);

return (
<>
<MainContainer width="100%">
Expand All @@ -192,7 +201,6 @@ function ProposalsDetail({ defaultAccount }) {
{proposals.title && ` #${proposalId} ${proposals.title}`}
</Text>
</Pane>

{proposals.status && (
<Pane>
<IconStatus status={proposals.status} text marginRight={8} />
Expand Down Expand Up @@ -275,29 +283,23 @@ function ProposalsDetail({ defaultAccount }) {
/>
)}
</ContainerGradientText>

<ProposalsIdDetail
<ProposalsDetailProgressBar
proposals={proposals}
totalDeposit={totalDeposit}
minDeposit={minDeposit}
tallying={tallying}
tally={tally}
totalDeposit={totalDeposit}
marginBottom={20}
/>

<ProposalsDetailProgressBar
<ProposalsRoutes
proposalId={proposalId}
proposals={proposals}
totalDeposit={totalDeposit}
minDeposit={minDeposit}
tallying={tallying}
tally={tally}
totalDeposit={totalDeposit}
updateFunc={updateFunc}
proposalStatus={PROPOSAL_STATUS}
/>

{proposals.status > PROPOSAL_STATUS.PROPOSAL_STATUS_DEPOSIT_PERIOD && (
<ProposalsIdDetailTableVoters
proposalId={proposalId}
updateFunc={updateFunc}
/>
)}
</MainContainer>
{addressActive !== null && addressActive.keys === 'keplr' ? (
<ActionBarDetail
Expand All @@ -318,9 +320,9 @@ function ProposalsDetail({ defaultAccount }) {
display: 'block',
}}
className="btn"
to="/"
to="/keys"
>
add address to your pocket from keplr
connect
</Link>
</Pane>
</ActionBar>
Expand Down
175 changes: 175 additions & 0 deletions src/containers/governance/proposalsDetailTableComments.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
import { useState, useEffect } from 'react';
import InfiniteScroll from 'react-infinite-scroll-component';
import Spark from 'src/components/search/Spark/Spark';
import useSearchData from 'src/containers/Search/hooks/useSearchData';
import { LinksTypeFilter, SortBy } from 'src/containers/Search/types';
import styles from 'src/containers/Search/SearchResults.module.scss';
import FirstItems from 'src/containers/Search/_FirstItems.refactor';
import Loader2 from 'src/components/ui/Loader2';
import Display from 'src/components/containerGradient/Display/Display';
import ActionBarContainer from 'src/containers/Search/ActionBarContainer';
import Filters from 'src/containers/Search/Filters/Filters';
import { useBackend } from 'src/contexts/backend/backend';
import { useDevice } from 'src/contexts/device';

import { IpfsContentType } from 'src/utils/ipfs/ipfs';

export const initialContentTypeFilterState = {
text: false,
image: false,
video: false,
pdf: false,
link: false,
// audio: false,
};

const sortByLSKey = 'search-sort';

function ProposalsDetailTableComments({ proposalId }) {
const { ipfsApi } = useBackend();
const query = `bostrom proposal ${proposalId}`;

const [proposalHash, setproposalHash] = useState('');
const [rankLink, setRankLink] = useState(null);

const [contentType, setContentType] = useState<{
[key: string]: IpfsContentType;
}>({});

const [contentTypeFilter, setContentTypeFilter] = useState(
initialContentTypeFilterState
);
const [sortBy, setSortBy] = useState(
localStorage.getItem(sortByLSKey) || SortBy.rank
);
const [linksTypeFilter, setLinksTypeFilter] = useState(LinksTypeFilter.all);

const { isMobile: mobile } = useDevice();

useEffect(() => {
setContentTypeFilter(initialContentTypeFilterState);
setContentType({});

(async () => {
let keywordHashTemp = '';

keywordHashTemp = await ipfsApi.addContent(query);

setproposalHash(keywordHashTemp);
})();
}, [query]);

const onClickRank = async (key) => {
if (rankLink === key) {
setRankLink(null);
} else {
setRankLink(key);
}
};

const {
data: items,
total,
error,
hasMore,
isInitialLoading,
refetch,
fetchNextPage: next,
} = useSearchData(proposalHash, {
sortBy,
linksType: linksTypeFilter,
});

const renderItems = items
.filter((item) => {
const { cid } = item;

if (!Object.values(contentTypeFilter).some((value) => value)) {
return true;
}
if (!contentType[cid]) {
return false;
}
return contentTypeFilter[contentType[cid]];
})
.map((key, i) => {

return (
<Spark
itemData={key}
cid={key.cid}
key={key.cid + i}
linkType={key.type}
query={query}
rankSelected={rankLink === key.cid}
handleRankClick={onClickRank}
handleContentType={(type) =>
setContentType((items) => {
return {
...items,
[key.cid]: type,
};
})
}
/>
);
});

return (
<>
<Filters
filters={contentTypeFilter}
setFilters={setContentTypeFilter}
filter2={sortBy}
setFilter2={setSortBy}
linksFilter={linksTypeFilter}
setLinksFilter={setLinksTypeFilter}
total={total}
total2={items.length}
contentType={contentType}
/>

<div className={styles.search}>
<FirstItems query={query} />

{isInitialLoading ? (
<Loader2 />
) : Object.keys(renderItems).length > 0 ? (
<InfiniteScroll
dataLength={items.length}
next={next}
className={styles.infiniteScroll}
hasMore={hasMore}
loader={<Loader2 />}
>
{renderItems}
</InfiniteScroll>
) : error ? (
<Display color="red">
<p>{error.message}</p>
</Display>
) : (
<Display color="white">
there are no comments to this proposal <br /> be the first and
create one
</Display>
)}
</div>
{!mobile && (
<div className={styles.actionBar}>
<ActionBarContainer
textBtn={'Comment'}
keywordHash={proposalHash}
update={() => {
refetch();
setRankLink(null);
}}
rankLink={rankLink}
/>
</div>
)}
</>
);
}
export default ProposalsDetailTableComments;

54 changes: 54 additions & 0 deletions src/containers/governance/proposalsRoutes.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Routes, Route } from 'react-router-dom';
import ProposalsDetailTableComments from './proposalsDetailTableComments';
import ProposalsIdDetail from './proposalsIdDetail';
import ProposalsIdDetailTableVoters from './proposalsDetailTableVoters';
import Layout from './Layout';

function ProposalsRoutes({
proposalId,
proposals,
tallying,
tally,
totalDeposit,
updateFunc,
proposalStatus,
}) {
return (
<Routes>
<Route path="/" element={<Layout />}>
<Route
index
element={<ProposalsDetailTableComments proposalId={proposalId} />}
/>
<Route
path="comments"
element={<ProposalsDetailTableComments proposalId={proposalId} />}
/>
<Route
path="meta"
element={
<ProposalsIdDetail
proposals={proposals}
tallying={tallying}
tally={tally}
totalDeposit={totalDeposit}
/>
}
/>
<Route
path="voters"
element={
proposals.status > proposalStatus.PROPOSAL_STATUS_DEPOSIT_PERIOD ? (
<ProposalsIdDetailTableVoters
proposalId={proposalId}
updateFunc={updateFunc}
/>
) : null
}
/>
</Route>
</Routes>
);
}

export default ProposalsRoutes;
22 changes: 22 additions & 0 deletions src/containers/governance/tabList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Tabs } from 'src/components';
import { useLocation } from 'react-router-dom';
import { TypePages } from './type';

function TabListGoverance() {
const location = useLocation();
const locationSplit = location.pathname.replace(/^\/|\/$/g, '').split('/');
let active = Object.values(TypePages).find(
(item) => item === locationSplit[2]
);
if (!active) {
return null;
}
return (
<Tabs
selected={active}
options={Object.keys(TypePages).map((key) => ({ to: key, key }))}
/>
);
}

export default TabListGoverance;
13 changes: 13 additions & 0 deletions src/containers/governance/tabsLayout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Outlet } from 'react-router-dom';
import TabListGoverance from './tabList';

function Layout() {
return (
<div>
<TabListGoverance />
<Outlet />
</div>
);
}

export default Layout;
5 changes: 5 additions & 0 deletions src/containers/governance/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum TypePages {
voters = 'voters',
comments = 'comments',
meta = 'meta',
}
2 changes: 1 addition & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const routes = {
path: '/temple',
},
senateProposal: {
path: '/senate/:proposalId',
path: '/senate/:proposalId/*',
getLink: (proposalId: number) => `/senate/${proposalId}`,
},
sphere: {
Expand Down

0 comments on commit d8f8160

Please sign in to comment.