Skip to content

Commit

Permalink
Merge pull request #111 from indexnetwork/sources
Browse files Browse the repository at this point in the history
new item models & default questions
  • Loading branch information
confxsd authored May 17, 2024
2 parents 66a6742 + 6f9b402 commit 806790b
Show file tree
Hide file tree
Showing 20 changed files with 429 additions and 84 deletions.
1 change: 1 addition & 0 deletions web-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@ceramicnetwork/common": "^3.0.4",
"@ceramicnetwork/http-client": "^3.0.4",
"@ceramicnetwork/stream-tile": "^3.0.4",
"@ceramicnetwork/streamid": "^4.1.0",
"@composedb/client": "^0.5.2",
"@composedb/types": "^0.5.1",
"@didtools/cacao": "^2.0.0",
Expand Down
5 changes: 5 additions & 0 deletions web-app/public/images/ic_default_index_item.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions web-app/public/images/ic_index_item.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions web-app/public/images/ic_team_index_item.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useApi } from "@/context/APIContext";
import { useApp } from "@/context/AppContext";
import { useRole } from "@/hooks/useRole";
import { IndexItem } from "@/types/entity";
import { filterValidUrls, removeDuplicates } from "@/utils/helper";
import { filterValidUrls, isStreamID, removeDuplicates } from "@/utils/helper";
import { useCallback, useEffect, useState } from "react";
import toast from "react-hot-toast";
import { useIndexConversation } from "../IndexConversationContext";
Expand All @@ -23,7 +23,6 @@ export default function IndexItemsTabSection() {
setLoading,
fetchIndexItems,
fetchMoreIndexItems,
// loadMoreItems,
} = useIndexConversation();
const { isCreator } = useRole();
const { api, ready: apiReady } = useApi();
Expand Down Expand Up @@ -70,26 +69,23 @@ export default function IndexItemsTabSection() {
const executeNextBatch = async () => {
if (currentIndex >= urls.length) return;

// Determine the next batch of URLs to process
const batch = urls.slice(currentIndex, currentIndex + CONCURRENCY_LIMIT);
currentIndex += CONCURRENCY_LIMIT;

// Process the current batch
await Promise.allSettled(batch.map(processUrl));

// Execute the next batch
await executeNextBatch();
};

await executeNextBatch();
};

const handleAddItem = useCallback(
async (inputUrls: string[]) => {
async (inputItems: string[]) => {
if (!apiReady || !viewedIndex) return;

// add only unique and valid URLs
const filteredUrls = filterValidUrls(inputUrls);
const filteredUrls = filterValidUrls(inputItems);
const uniqueUrls = removeDuplicates(filteredUrls);
const urls = removeDuplicates(
uniqueUrls,
Expand All @@ -99,23 +95,39 @@ export default function IndexItemsTabSection() {
.map((i) => i.node.url),
);

// add only unique and valid indexes
const inputIndexIds = inputItems.filter((id) => isStreamID(id));
const uniqueIndexIds = removeDuplicates(inputIndexIds);
const indexIds = removeDuplicates(
uniqueIndexIds,
itemsState.items
.filter((i) => i.type === "Index")
// @ts-ignore
.map((i) => i.node.id),
);

const items = [...urls, ...indexIds];

console.log("items", indexIds);

setLoading(true);
setProgress({ current: 0, total: urls.length });
setProgress({ current: 0, total: items.length });

await processUrlsInBatches(urls, async (url: string) => {
await processUrlsInBatches(items, async (item: string) => {
try {
const createdLink = await api!.crawlLink(url);
if (!createdLink) return;
let itemId = item;

if (!isStreamID(item)) {
const createdLink = await api!.crawlLink(item);
itemId = createdLink.id;
}

const createdItem = await api!.createItem(
viewedIndex.id,
createdLink.id,
);
const createdItem = await api!.createItem(viewedIndex.id, itemId);

setAddedItem(createdItem);
} catch (error) {
console.error("Error adding item", error);
toast.error(`Error adding item: ${url}`);
toast.error(`Error adding item: ${item}`);
}
});

Expand Down
104 changes: 104 additions & 0 deletions web-app/src/components/site/index-details/DefaultIndexItem/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Button from "components/base/Button";
import IconContextMenu from "components/base/Icon/IconContextMenu";
import Text from "components/base/Text";
import Col from "components/layout/base/Grid/Col";
import FlexRow from "components/layout/base/Grid/FlexRow";
import IndexDetailItemPopup from "components/site/popup/IndexDetailItemPopup";
import { useBreakpoint } from "hooks/useBreakpoint";
import { useRole } from "hooks/useRole";
import moment from "moment";
import React from "react";
import sanitize from "sanitize-html";
import { DefaultIndexNodeItem } from "types/entity";
import { BREAKPOINTS } from "utils/constants";
import cm from "./style.module.scss";

// TODO: data prop will be Index object
export interface DefaultIndexItemProps {
item: DefaultIndexNodeItem;
onChange?(val: DefaultIndexNodeItem[]): void;
search?: boolean;
handleRemove?(): void;
}

const DefaultIndexItem: React.FC<DefaultIndexItemProps> = ({
item,
search = false,
handleRemove,
}) => {
const breakpoint = useBreakpoint(BREAKPOINTS, true);
const { node } = item;

const { isCreator } = useRole();

return (
<div className="index-detail-list-item-wrapper">
<FlexRow className="index-detail-list-item py-3">
<Col xs={12}>
<FlexRow wrap={false}>
<Col className="idxflex-grow-1">
<img
className="mr-3"
src={"/images/ic_default_index_item.svg"}
alt="favicon"
width={16}
height={16}
onError={(e: React.SyntheticEvent<HTMLImageElement, Event>) => {
const target = e.target as HTMLImageElement;
target.onerror = null; // Prevents infinite loop in case fallback image also fails
target.src = "/images/globe.svg";
}}
style={{
verticalAlign: "middle",
}}
/>
<Text
className={cm.title}
fontWeight={700}
dangerouslySetInnerHTML={{
__html: sanitize(node?.id as string),
}}
></Text>
</Col>
{!search && isCreator ? (
<Col className="idxflex-shrink-0 index-detail-list-item-buttons ml-3">
<FlexRow>
<Col></Col>
<Col>
<IndexDetailItemPopup onDelete={handleRemove}>
<Button size="xs" iconButton theme="clear" borderless>
<IconContextMenu />
</Button>
</IndexDetailItemPopup>
</Col>
</FlexRow>
</Col>
) : (
<Col className="idxflex-shrink-0 index-detail-list-item-buttons ml-3">
<FlexRow>
<Col></Col>
<Col>
<IndexDetailItemPopup onDelete={handleRemove} />
</Col>
</FlexRow>
</Col>
)}
</FlexRow>
</Col>
<Col xs={12} className="mt-2">
<Text size="md" theme="gray5">
{item.type}
{" • "}
</Text>

<Text size="md" theme="gray5">
{node?.updatedAt
? `Updated ${moment(new Date(node.updatedAt)).fromNow()}`
: ""}
</Text>
</Col>
</FlexRow>
</div>
);
};
export default DefaultIndexItem;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.title {
font-family: "Freizeit" !important;
font-size: 16px;
}
141 changes: 141 additions & 0 deletions web-app/src/components/site/index-details/IndexIndexItem/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import Button from "components/base/Button";
import IconContextMenu from "components/base/Icon/IconContextMenu";
import Text from "components/base/Text";
import Col from "components/layout/base/Grid/Col";
import FlexRow from "components/layout/base/Grid/FlexRow";
import IndexDetailItemPopup from "components/site/popup/IndexDetailItemPopup";
import { useBreakpoint } from "hooks/useBreakpoint";
import { useRole } from "hooks/useRole";
import moment from "moment";
import React from "react";
import sanitize from "sanitize-html";
import { IndexIndexNodeItem } from "types/entity";
import { BREAKPOINTS } from "utils/constants";
import cm from "./style.module.scss";

// TODO: data prop will be Index object
export interface IndexIndexItemProps {
item: IndexIndexNodeItem;
onChange?(val: IndexIndexNodeItem[]): void;
search?: boolean;
handleRemove?(): void;
}

const IndexIndexItem: React.FC<IndexIndexItemProps> = ({
item,
search = false,
handleRemove,
}) => {
const breakpoint = useBreakpoint(BREAKPOINTS, true);
const { node } = item;

const { isCreator } = useRole();

return (
<div className="index-detail-list-item-wrapper">
<FlexRow className="index-detail-list-item py-3">
<Col xs={12}>
<FlexRow wrap={false}>
<Col className="idxflex-grow-1">
<img
className="mr-3"
src={node?.logo || "/images/ic_index_item.svg"}
alt="favicon"
width={16}
height={16}
onError={(e: React.SyntheticEvent<HTMLImageElement, Event>) => {
const target = e.target as HTMLImageElement;
target.onerror = null; // Prevents infinite loop in case fallback image also fails
target.src = "/images/globe.svg";
}}
style={{
verticalAlign: "middle",
}}
/>
<a
target="_blank"
rel="noreferrer"
href={`${window.location.origin}/${node?.id}`}
>
<Text
className={cm.title}
fontWeight={700}
dangerouslySetInnerHTML={{
__html: sanitize(node?.title as string),
}}
></Text>
{/* dangerouslySetInnerHTML={{ __html: sanitize((node.highlight && item.highlight["link.title"]) ? item.highlight["link.title"] : node?.title as string) }}></Text> */}
</a>
</Col>
{!search && isCreator ? (
<Col className="idxflex-shrink-0 index-detail-list-item-buttons ml-3">
<FlexRow>
<Col></Col>
<Col>
<IndexDetailItemPopup onDelete={handleRemove}>
<Button size="xs" iconButton theme="clear" borderless>
<IconContextMenu />
</Button>
</IndexDetailItemPopup>
</Col>
</FlexRow>
</Col>
) : (
<Col className="idxflex-shrink-0 index-detail-list-item-buttons ml-3">
<FlexRow>
<Col></Col>
<Col>
<IndexDetailItemPopup onDelete={handleRemove}>
{/* <Button size="xs" iconButton theme="clear" borderless>
<IconContextMenu />
</Button> */}
</IndexDetailItemPopup>
</Col>
</FlexRow>
</Col>
)}
</FlexRow>
</Col>
<Col xs={12} className="mt-2">
<Text size="md" theme="gray5">
{node?.updatedAt
? `Updated ${moment(new Date(node.updatedAt)).fromNow()}`
: ""}
</Text>
</Col>
{/* {
search && node.highlight && node.highlight["link.content"] && (
<Col className="mt-5">
<Text className="listItem" theme="secondary" dangerouslySetInnerHTML={{ __html: sanitize(item.highlight["link.content"]) }}></Text>
</Col>
)
} */}
{
<Col xs={12} className="idxflex idxflex-gap-3 idxflex-wrap mt-3">
{/* {
node?.tags?.map((t, ind) => (
<TagIndexDetailItem
key={ind}
text={t}
removable
onRemove={handleRemoveTag}
/>))
} */}
{/* {
<TagIndexDetailItem
theme="clear"
text=""
placeholder="New Tag"
editable={true}
inputActive
// onEdit={handleNewTagEdit}
// onBlur={handleToggleNewTag}
/>
} */}
</Col>
}
</FlexRow>
</div>
);
};
export default IndexIndexItem;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.title {
font-family: "Freizeit" !important;
font-size: 16px;
}
Loading

0 comments on commit 806790b

Please sign in to comment.