Skip to content

Commit

Permalink
feat: global tag filter uses properties instead of Storage, active gl…
Browse files Browse the repository at this point in the history
…obal tag is (un)selected (#780)
  • Loading branch information
gabehamilton authored Apr 23, 2024
1 parent d505fc4 commit bb83a39
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
6 changes: 4 additions & 2 deletions src/DIG/Tabs.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let { items, variant, size, ...forwardedProps } = props;
let { items, variant, size, contentProps, ...forwardedProps } = props;

// this prop is just for preview
const previewItems = [
Expand Down Expand Up @@ -221,7 +221,9 @@ return (
</TabList>

{items.map((item) => (
<TabContent value={item.value}>{item.content}</TabContent>
<TabContent value={item.value}>
{typeof item.content === "function" ? item.content(contentProps) : item.content}
</TabContent>
))}
</TabGroup>
);
2 changes: 1 addition & 1 deletion src/DIG/Tabs.metadata.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "This tabs component is built with the Radix primitive: https://www.radix-ui.com/docs/primitives/components/tabs\n\n### Example\n\n```jsx\n<Widget\n src=\"near/widget/DIG.Tabs\"\n props={{\n variant: \"line\",\n size: \"default\",\n items: [\n {\n name: \"Label 1\",\n value: \"1\",\n content: \"Hello 1\",\n icon: \"ph ph-browser\",\n },\n {\n name: \"Label 2\",\n value: \"2\",\n content: \"Hello 2\",\n count: \"12\",\n icon: \"ph ph-browser\",\n },\n {\n name: \"Label 3\",\n value: \"3\",\n content: \"Hello 3\",\n disabled: true,\n icon: \"ph ph-browser\",\n },\n ],\n }}\n/>\n```\n\n### Props\n\n`variant`\n- one of `\"pill\"`, `\"line\"`, `toggle`\n- Default to `\"line\"`\n\n`size`\n- `\"small\"`, `\"default\"`, `\"large\"`\n- Default to `\"default\"`\n\n`items`\n- array of objects defining each tab with it's own content.\n- Each object requires a `value` (unique string), `name` (string) and `content` (string or JSX).\n- Also there is optional parameters:\n - `icon` - type: string (CSS Class). Example: `\"ph-bold ph-anchor-simple\"`. Adds an icon to the left of the `name`. Accepts classNames from https://phosphoricons.com and https://icons.getbootstrap.com.\n - `count` - type: string. Adds an counter label to the right of `name`.\n - `disabled` - type: `boolean`. When `true`, prevents the user from interacting with the tab. \n\nThis component also accepts all `Tabs.Root` props as outlined in the Radix documentation.",
"description": "This tabs component is built with the Radix primitive: https://www.radix-ui.com/docs/primitives/components/tabs\n\n### Example\n\n```jsx\n<Widget\n src=\"near/widget/DIG.Tabs\"\n props={{\n variant: \"line\",\n size: \"default\",\n items: [\n {\n name: \"Label 1\",\n value: \"1\",\n content: \"Hello 1\",\n icon: \"ph ph-browser\",\n },\n {\n name: \"Label 2\",\n value: \"2\",\n content: \"Hello 2\",\n count: \"12\",\n icon: \"ph ph-browser\",\n },\n {\n name: \"Label 3\",\n value: \"3\",\n content: \"Hello 3\",\n disabled: true,\n icon: \"ph ph-browser\",\n },\n ],\n }}\n/>\n```\n\n### Props\n\n`variant`\n- one of `\"pill\"`, `\"line\"`, `toggle`\n- Default to `\"line\"`\n\n`size`\n- `\"small\"`, `\"default\"`, `\"large\"`\n- Default to `\"default\"`\n\n`items`\n- array of objects defining each tab with it's own content.\n- Each object requires a `value` (unique string), `name` (string) and `content` (string or JSX).\n- Also there is optional parameters:\n - `icon` - type: string (CSS Class). Example: `\"ph-bold ph-anchor-simple\"`. Adds an icon to the left of the `name`. Accepts classNames from https://phosphoricons.com and https://icons.getbootstrap.com.\n - `count` - type: string. Adds an counter label to the right of `name`.\n - `disabled` - type: `boolean`. When `true`, prevents the user from interacting with the tab.\n\n`contentProps`\n- type: `object`.\n- Properties to be passed to content if an item's `content` is a function. \n\nThis component also accepts all `Tabs.Root` props as outlined in the Radix documentation.",
"name": "DIG.Tabs",
"tags": {
"dig": ""
Expand Down
29 changes: 22 additions & 7 deletions src/Entities/Template/EntityList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,18 @@ if (!loadItemsQueryApi) {
const loadItems = props.loadItems ?? loadItemsQueryApi;

const accountId = props.accountId || context.accountId;
const { schema, description, buildQueries, queryName, collection, renderItem, createWidget, createWidgetProps } = props;
const {
schema,
description,
buildQueries,
queryName,
collection,
renderItem,
createWidget,
createWidgetProps,
globalTagFilter,
setGlobalTagFilter,
} = props;

const finalCreateWidget = createWidget ?? `${REPL_ACCOUNT}/widget/Entities.Template.EntityCreate`;

Expand All @@ -20,13 +31,11 @@ const sortTypes = props.sortTypes ?? [
{ text: "Oldest Updates", value: "{ updated_at: asc }" },
];

const initialTagFilter = Storage.get("global-tag-filter");
if (initialTagFilter) {
Storage.set("global-tag-filter", null);
}
const [searchKey, setSearchKey] = useState("");
const [sort, setSort] = useState(sortTypes[0].value);
const [tagsFilter, setTagsFilter] = useState(initialTagFilter);
const [tagsFilter, setTagsFilter] = useState(
Array.isArray(globalTagFilter) ? globalTagFilter.map((it) => it.tag) : null,
);
const [items, setItems] = useState({ list: [], total: 0 });
const [showCreateModal, setShowCreateModal] = useState(false);
const [activeItem, setActiveItem] = useState(null);
Expand Down Expand Up @@ -55,6 +64,12 @@ const loadItemsUseState = (isResetOrPageNumber) => {
const offset = isResetOrPageNumber === true ? 0 : items.list.length;
return loadItems(buildQueries(searchKey, sort, { tags: tagsFilter }), queryName, offset, collection, loader);
};

const onTagFilterChanged = (tags) => {
setTagsFilter(tags);
setGlobalTagFilter(tags.map((tag) => ({ entity_type: schema.entityType, tag })));
};

useEffect(() => {
if (debounceTimer) clearTimeout(debounceTimer);
const search = (searchKey ?? "").toLowerCase();
Expand Down Expand Up @@ -249,7 +264,7 @@ return (
props={{
placeholder: "Filter by Tag",
value: tagsFilter,
setValue: setTagsFilter,
setValue: onTagFilterChanged,
namespace: schema.namespace,
entityType: schema.entityType,
allowNew: false,
Expand Down
10 changes: 7 additions & 3 deletions src/Entities/Template/Forms/TagCloud.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ if (!loadItems) {
return <p>Loading modules...</p>;
}

const { namespace, entityType, onSelect, limit } = props;
const { namespace, entityType, onSelect, limit, initialTags } = props;
const tagLimit = limit ?? 100;

const [items, setItems] = useState(null);
const [tags, setTags] = useState(value || []);
const [tags, setTags] = useState(initialTags);

const user = "dataplatform_near";
const entityIndexer = "entities";
Expand Down Expand Up @@ -118,6 +118,7 @@ const TooltipTag = styled.span`
`;

const selectTag = (tag) => {
setTags([tag]);
if (onSelect) {
onSelect(tag);
}
Expand All @@ -127,6 +128,9 @@ const humanize = (str) => {
return str.replace(/([A-Z])/g, " $1").replace(/^./, (s) => s.toUpperCase());
};

const isSelected = (tag, tagList) =>
tagList && tagList.find((t) => t.tag === tag.tag && t.entity_type === tag.entity_type);

return (
<div>
<p>Filter by Tag</p>
Expand All @@ -143,7 +147,7 @@ return (
</span>
),
trigger: (
<Tag key={i} onClick={() => selectTag(tag)}>
<Tag key={i} onClick={() => selectTag(tag)} primary={isSelected(tag, props.initialTags)}>
{tag.tag} <Count>{tag.count}</Count>
</Tag>
),
Expand Down
14 changes: 12 additions & 2 deletions src/Entities/Template/GenericEntityConfig.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ if (!href) {
}

const { namespace, entityType, schemaFile } = props; // data props
const { title, homeLink } = props; // display props
const { title, homeLink, globalTagFilter, setGlobalTagFilter } = props; // display props

const schemaLocation = schemaFile ? schemaFile : `${REPL_ACCOUNT}/widget/Entities.Template.GenericSchema`;
const { genSchema } = VM.require(schemaLocation);
Expand Down Expand Up @@ -302,7 +302,17 @@ return (
<div>
<Widget
src="${REPL_ACCOUNT}/widget/Entities.Template.EntityList"
props={{ table, buildQueries, queryName, collection, renderItem, createWidget, schema }}
props={{
table,
buildQueries,
queryName,
collection,
renderItem,
createWidget,
schema,
globalTagFilter,
setGlobalTagFilter,
}}
/>
</div>
);

0 comments on commit bb83a39

Please sign in to comment.