Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip #64

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft

wip #64

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/enterprise-api/src/v1/daos/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const search = (params: ReturnType<typeof parseQueryParameters>): Promise<Entity
':a': { S: 'dao' },
':b': { S: params.query.toLowerCase() },
},
ExclusiveStartKey: params.start_after
},
DAO_IGNORE_PROPERTIES
);
Expand All @@ -44,6 +45,7 @@ const search = (params: ReturnType<typeof parseQueryParameters>): Promise<Entity
ExpressionAttributeValues: {
':a': { S: 'dao' },
},
ExclusiveStartKey: params.start_after
},
DAO_IGNORE_PROPERTIES
);
Expand Down
3 changes: 3 additions & 0 deletions apps/enterprise-api/src/v1/daos/parseQueryParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import {
} from "@apps-shared/api/utils";
import { QueryParamConfigMap, StringParam } from "serialize-query-params";
import { ParsedQs } from "qs";
import { AttributeValue } from "@aws-sdk/client-dynamodb";

interface QueryStringParameters {
query?: string;
limit: number;
direction: Direction;
start_after?: Record<string, AttributeValue>;
}

export const parseQueryParameters = (
Expand All @@ -20,6 +22,7 @@ export const parseQueryParameters = (
query: StringParam,
limit: withLimitParam(),
direction: withDirectionParam(),
start_after: StringParam
};

const validation = (params: QueryStringParameters): QueryStringParameters => {
Expand Down
2 changes: 1 addition & 1 deletion apps/enterprise/src/hooks/useApiEndpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type HealthCheckEndpoint = {

type DaosEndpoint = {
path: 'v1/daos';
params: { query?: string; limit: number; direction?: Direction };
params: { query?: string; limit: number; direction?: Direction, start_after?: string };
};

type DaoEndpoint = {
Expand Down
66 changes: 51 additions & 15 deletions apps/enterprise/src/pages/daos/Page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,21 @@ import { daoTypes } from 'dao';
import { DaoFilter } from './DaoFilter';
import { IndexersAreRequired } from 'settings/components/IndexersAreRequired';
import { useDebounceSearch } from 'hooks/useDebounce';
import { PrimaryButton } from 'lib/ui/buttons/rect/PrimaryButton';

const MAX_PREVIEW_SIZE = 100;
const MAX_PREVIEW_SIZE = 30;

export const Page = () => {
const stickyRef = useRef<HTMLDivElement>(null);
const [showDropdown, setShowDropdown] = useState(false);
const dropdownRef = useRef<HTMLDivElement>(null);
const [daoTypesToDisplay, setDaoTypesToDisplay] = useState<enterprise.DaoType[]>(daoTypes);
const [searchText, setSearchText] = useState('');
const debouncedSearchText = useDebounceSearch(searchText, 500);
const [daosQueryKey, setDaosQueryKey] = useState<string>(QUERY_KEY.DAOS);
const [pageLastIndex, setPageLastIndex] = useState<string | null>(null);
const [allItems, setAllItems] = useState<any[]>([]);
const [loadMore, setLoadMore] = useState(true);

useEffect(() => {
if (showDropdown) {
Expand All @@ -40,9 +47,7 @@ export const Page = () => {
}, [showDropdown]);


const [searchText, setSearchText] = useState('');
const debouncedSearchText = useDebounceSearch(searchText, 500);
const [daosQueryKey, setDaosQueryKey] = useState<string>(QUERY_KEY.DAOS)


useEffect(() => {
setDaosQueryKey(debouncedSearchText === '' ? QUERY_KEY.DAOS : `${QUERY_KEY.DAOS}-${debouncedSearchText}`);
Expand All @@ -51,10 +56,35 @@ export const Page = () => {
const { data, isLoading } = useDAOsQuery({
query: debouncedSearchText,
limit: MAX_PREVIEW_SIZE,
queryKey: daosQueryKey
start_after: pageLastIndex || '',
queryKey: daosQueryKey,
});

const items = data?.filter(item => daoTypesToDisplay.includes(item.type));
useEffect(() => {
if (data && data.length && loadMore) {
const items = data.filter(item => daoTypesToDisplay.includes(item.type));
setPageLastIndex(data[data.length - 1].address);
setAllItems(prevItems => [...prevItems, ...items]);
}


if (!isLoading && loadMore) {
setLoadMore(false);
}
}, [data, daoTypesToDisplay, loadMore, isLoading]);

const viewMoreButton = (
<PrimaryButton
kind="secondary"
onClick={() => {
const newQueryKey = `${daosQueryKey}-${pageLastIndex}`;
setDaosQueryKey(newQueryKey);
setLoadMore(true)
}}
>
View More
</PrimaryButton>
);

const searchInput = (
<SearchInput
Expand Down Expand Up @@ -99,8 +129,11 @@ export const Page = () => {
</Text>
<IndexersAreRequired>
{searchInput}
{data && data?.length ? (
<List items={items} isLoading={isLoading} />
{allItems && allItems.length ? (
<>
<List items={allItems} isLoading={isLoading} />
{viewMoreButton}
</>
) : (
noResults
)}
Expand All @@ -115,7 +148,7 @@ export const Page = () => {
<Header
compact={true}
isLoading={isLoading}
totalCount={items?.length ?? 0}
totalCount={allItems?.length ?? 0}
searchInput={searchInput}
filters={filters}
/>
Expand All @@ -127,18 +160,21 @@ export const Page = () => {
<Header
ref={stickyRef}
isLoading={isLoading}
totalCount={items?.length ?? 0}
totalCount={allItems?.length ?? 0}
searchInput={searchInput}
filters={filters}
/>
}
>
<IndexersAreRequired>
{data && data?.length ? (
<List items={items} isLoading={isLoading} />
) : (
noResults
)}
{allItems && allItems.length ? (
<>
<List items={allItems} isLoading={isLoading} />
{viewMoreButton}
</>
) : (
noResults
)}
</IndexersAreRequired>
</PageLayout>
</ScrollableContainer>
Expand Down
8 changes: 5 additions & 3 deletions apps/enterprise/src/queries/useDAOsQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface DAOsQueryOptions {
limit?: number;
direction?: Direction;
queryKey?: string;
start_after?: string;
}

export type DAOsQueryResponse = Array<{
Expand Down Expand Up @@ -49,22 +50,23 @@ export const fetchDAOsQuery = async (endpoint: string) => {
};

export const useDAOsQuery = (options: DAOsQueryOptions = {}): UseQueryResult<Array<DAO> | undefined> => {
const { query, limit = 100, direction = query?.length === 0 ? 'desc' : 'asc', queryKey = QUERY_KEY.DAOS } = options;
const { query, limit = 100, direction = query?.length === 0 ? 'desc' : 'asc', queryKey = QUERY_KEY.DAOS, start_after } = options;

const [areIndexersEnabled] = useAreIndexersEnabled()
const [areIndexersEnabled] = useAreIndexersEnabled();

const endpoint = useApiEndpoint({
path: 'v1/daos',
params: {
query,
limit,
direction,
start_after,
},
});

return useQuery([queryKey, endpoint], () => {
if (!areIndexersEnabled) {
throw new Error('DAOs query is disabled. Enable indexers to use this query.')
throw new Error('DAOs query is disabled. Enable indexers to use this query.');
}

return fetchDAOsQuery(endpoint);
Expand Down