import { useMemo } from "react";
import { useUIState } from "src/useUIState";
import { useEntity, useReplicache } from "src/replicache";
import { BlockProps, BlockLayout } from "./Block";
import {
usePublicationData,
useNormalizedPublicationRecord,
} from "app/(app)/lish/[did]/[publication]/dashboard/PublicationSWRProvider";
import { PublicationPostsList } from "app/(app)/lish/[did]/[publication]/PublicationPostsList";
import { Popover } from "components/Popover";
import { Toggle } from "components/Toggle";
import { ToggleGroup } from "components/ToggleGroup";
import { SettingsTriggerButton } from "./SettingsTriggerButton";
type PostsListView = "compact" | "full";
export const PostsListBlock = (props: BlockProps & { preview?: boolean }) => {
let isSelected = useUIState((s) =>
!!s.selectedBlocks.find((b) => b.value === props.value),
);
if (props.preview) {
return (
);
}
return (
}
>
);
};
function PostsListBlockContent({ entityID }: { entityID: string }) {
let { data } = usePublicationData();
let publicationRecord = useNormalizedPublicationRecord();
let viewFact = useEntity(entityID, "posts-list/view");
let view: PostsListView = viewFact?.data.value ?? "full";
let highlightFirstFact = useEntity(
entityID,
"posts-list/highlight-first-post",
);
let highlightFirst = highlightFirstFact?.data.value ?? false;
let filterTagFact = useEntity(entityID, "posts-list/filter-tag");
let filterTag = filterTagFact?.data.value;
let filteredPosts = useMemo(() => {
if (!data?.documents) return data?.documents;
if (!filterTag) return data.documents;
return data.documents.filter((d) => d.record.tags?.includes(filterTag));
}, [data?.documents, filterTag]);
if (data === undefined) return ;
if (!data?.publication) return ;
return (
);
}
function PostsListPlaceholder() {
return (
{[0, 1, 2].map((i) => (
))}
);
}
function PostsListSettingsButton(props: { entityID: string }) {
let { rep } = useReplicache();
let { data } = usePublicationData();
let viewFact = useEntity(props.entityID, "posts-list/view");
let view: PostsListView = viewFact?.data.value ?? "full";
let highlightFirstFact = useEntity(
props.entityID,
"posts-list/highlight-first-post",
);
let highlightFirst = highlightFirstFact?.data.value ?? false;
let filterTagFact = useEntity(props.entityID, "posts-list/filter-tag");
let filterTag = filterTagFact?.data.value;
let allTags = useMemo(() => {
let tagSet = new Set();
for (let doc of data?.documents ?? []) {
for (let tag of doc.record.tags ?? []) tagSet.add(tag);
}
return Array.from(tagSet).sort((a, b) =>
a.localeCompare(b, undefined, { sensitivity: "base" }),
);
}, [data?.documents]);
return (
}
>
Post View
fullWidth
value={view}
onChange={(value) => {
if (!rep) return;
rep.mutate.assertFact({
entity: props.entityID,
attribute: "posts-list/view",
data: { type: "posts-list-view-union", value },
});
}}
options={[
{ value: "compact", label: "Compact" },
{ value: "full", label: "Full" },
]}
/>
{
if (!rep) return;
rep.mutate.assertFact({
entity: props.entityID,
attribute: "posts-list/highlight-first-post",
data: { type: "boolean", value: !highlightFirst },
});
}}
>
Highlight First Post
Filter by Tag
{allTags.length === 0 ? (
no tags yet
) : (
{allTags.map((tag) => {
let isSelected = filterTag === tag;
return (
);
})}
)}
);
}