diff --git a/app/models/Share.ts b/app/models/Share.ts index 2933d4f75b..8d9e10a96e 100644 --- a/app/models/Share.ts +++ b/app/models/Share.ts @@ -114,7 +114,9 @@ class Share extends Model implements Searchable { @computed get searchContent(): string[] { - return [this.sourceTitle ?? this.documentTitle]; + return [this.title, this.sourceTitle ?? this.documentTitle].filter( + (value): value is string => !!value + ); } @computed diff --git a/app/scenes/Settings/Shares.tsx b/app/scenes/Settings/Shares.tsx index 93a882ea42..6eb52654c4 100644 --- a/app/scenes/Settings/Shares.tsx +++ b/app/scenes/Settings/Shares.tsx @@ -5,6 +5,7 @@ import { useState, useMemo, useCallback, useEffect } from "react"; import { useTranslation, Trans } from "react-i18next"; import { Link, useHistory, useLocation } from "react-router-dom"; import { toast } from "sonner"; +import { ShareStatus, ShareTypes } from "@shared/types"; import { ConditionalFade } from "~/components/Fade"; import Heading from "~/components/Heading"; import InputSearch from "~/components/InputSearch"; @@ -22,7 +23,8 @@ import Button from "~/components/Button"; import { Action } from "~/components/Actions"; import useActionContext from "~/hooks/useActionContext"; import { createShareLink } from "~/actions/definitions/createShareLink"; -// import Switch from "~/components/Switch"; +import ShareTypeFilter from "./components/ShareTypeFilter"; +import ShareStatusFilter from "./components/ShareStatusFilter"; function Shares() { const team = useCurrentTeam(); @@ -35,16 +37,48 @@ function Shares() { const params = useQuery(); const [query, setQuery] = useState(""); const context = useActionContext(); + const typeFilter = useMemo( + () => + params.getAll("type")?.length + ? (params.getAll("type") as ShareTypes[]) + : [ShareTypes.Web, ShareTypes.Private], + [params] + ); + const statusFilter = useMemo( + () => + params.getAll("status")?.length + ? (params.getAll("status") as ShareStatus[]) + : [ShareStatus.Active, ShareStatus.Inactive], + [params] + ); + + const published = useMemo(() => { + const hasActive = statusFilter.includes(ShareStatus.Active); + const hasInactive = statusFilter.includes(ShareStatus.Inactive); + + if (hasActive && !hasInactive) { + return true; + } + + if (!hasActive && hasInactive) { + return false; + } + + return undefined; + }, [statusFilter]); const reqParams = useMemo( () => ({ query: params.get("query") || undefined, sort: params.get("sort") || "createdAt", + type: typeFilter.length ? typeFilter : undefined, + status: statusFilter.length ? statusFilter : undefined, + published, direction: (params.get("direction") || "desc").toUpperCase() as | "ASC" | "DESC", }), - [params] + [params, published, statusFilter, typeFilter] ); const sort: ColumnSort = useMemo( @@ -55,19 +89,40 @@ function Shares() { [reqParams.sort, reqParams.direction] ); + const filteredData = shares + .findByQuery(reqParams.query ?? "") + .filter((share) => { + const matchesType = + typeFilter.length === 0 || typeFilter.includes(share.type); + + const matchesStatus = + statusFilter.length === 0 || + (statusFilter.includes(ShareStatus.Active) && share.published) || + (statusFilter.includes(ShareStatus.Inactive) && !share.published); + + return matchesType && matchesStatus; + }); + const { data, error, loading, next } = useTableRequest({ - data: shares.findByQuery(reqParams.query ?? ""), + data: filteredData, sort, reqFn: shares.fetchPage, reqParams, }); const updateParams = useCallback( - (name: string, value: string) => { - if (value) { - params.set(name, value); + (name: string, value: string | string[]) => { + if (typeof value === "string") { + if (value) { + params.set(name, value); + } else { + params.delete(name); + } } else { params.delete(name); + for (const v of value) { + params.append(name, v); + } } history.replace({ @@ -94,6 +149,18 @@ function Shares() { return () => clearTimeout(timeout); }, [query, updateParams]); + const handleStatusFilter = useCallback( + ({ statusFilter }) => { + updateParams("status", statusFilter); + }, + [updateParams] + ); + + const handleTypeFilter = useCallback( + ({ typeFilter }) => updateParams("type", typeFilter), + [updateParams] + ); + return ( - {/* - to do: finish implementation - */} + + void; + statusFilter: ShareStatus[]; +}; + +const ShareStatusFilter = ({ onSelect, statusFilter, ...rest }: Props) => { + const { t } = useTranslation(); + + const options = useMemo( + () => + compact([ + { + key: ShareStatus.Active, + label: t("Active"), + }, + { + key: ShareStatus.Inactive, + label: t("Inactive"), + }, + ]), + [t] + ); + + const handleSelect = (key: ShareStatus) => { + let modifiedStatusFilter; + if (statusFilter.includes(key)) { + modifiedStatusFilter = statusFilter.filter((status) => status !== key); + } else { + modifiedStatusFilter = [...statusFilter, key]; + } + + onSelect({ statusFilter: modifiedStatusFilter }); + }; + + return ( + + ); +}; + +export default observer(ShareStatusFilter); diff --git a/app/scenes/Settings/components/ShareTypeFilter.tsx b/app/scenes/Settings/components/ShareTypeFilter.tsx new file mode 100644 index 0000000000..2d8ad6233f --- /dev/null +++ b/app/scenes/Settings/components/ShareTypeFilter.tsx @@ -0,0 +1,53 @@ +import { compact } from "es-toolkit/compat"; +import { observer } from "mobx-react"; +import { useMemo } from "react"; +import { useTranslation } from "react-i18next"; +import { ShareTypes } from "@shared/types"; +import FilterOptions from "~/components/FilterOptions"; + +type Props = { + onSelect: (option: { typeFilter: ShareTypes[] }) => void; + typeFilter: ShareTypes[]; +}; + +const ShareTypeFilter = ({ onSelect, typeFilter, ...rest }: Props) => { + const { t } = useTranslation(); + + const options = useMemo( + () => + compact([ + { + key: ShareTypes.Web, + label: t("Web"), + }, + { + key: ShareTypes.Private, + label: t("Private"), + }, + ]), + [t] + ); + + const handleSelect = (key: ShareTypes) => { + let modifiedTypeFilter; + if (typeFilter.includes(key)) { + modifiedTypeFilter = typeFilter.filter((type) => type !== key); + } else { + modifiedTypeFilter = [...typeFilter, key]; + } + + onSelect({ typeFilter: modifiedTypeFilter }); + }; + + return ( + + ); +}; + +export default observer(ShareTypeFilter); diff --git a/app/stores/SharesStore.ts b/app/stores/SharesStore.ts index 81a93dc137..13afadbbfe 100644 --- a/app/stores/SharesStore.ts +++ b/app/stores/SharesStore.ts @@ -193,15 +193,6 @@ export default class SharesStore extends Store { getPublishedByDocumentId = (documentId: string): Share[] => filter(this.getAllByDocumentId(documentId), (share) => share.published); - getUnpublishedByCollectionId = (collectionId: string): Share[] => - filter( - this.getAllByCollectionId(collectionId), - (share) => !share.published - ); - - getUnpublishedByDocumentId = (documentId: string): Share[] => - filter(this.getAllByDocumentId(documentId), (share) => !share.published); - getPrivateByCollectionId = ( collectionId: string, published = true diff --git a/server/routes/api/shares/schema.ts b/server/routes/api/shares/schema.ts index 3ea5c1cbe7..5349f62db6 100644 --- a/server/routes/api/shares/schema.ts +++ b/server/routes/api/shares/schema.ts @@ -7,7 +7,7 @@ import { Share } from "@server/models"; import { ValidateURL } from "@server/validation"; import { zodIdType } from "@server/utils/zod"; import { BaseSchema } from "../schema"; -import { ShareTypes } from "@shared/types"; +import { ShareStatus, ShareTypes } from "@shared/types"; const ExpiresAtSchema = z.coerce .date() @@ -40,7 +40,8 @@ export type SharesInfoReq = z.infer; export const SharesListSchema = BaseSchema.extend({ body: z.object({ query: z.string().optional(), - published: z.boolean().nullish(), + type: z.array(z.enum(ShareTypes)).optional(), + status: z.array(z.enum(ShareStatus)).optional(), sort: z .string() .refine((val) => Object.keys(Share.getAttributes()).includes(val), { diff --git a/server/routes/api/shares/shares.test.ts b/server/routes/api/shares/shares.test.ts index 4b9f6621d3..4adc7eaaa9 100644 --- a/server/routes/api/shares/shares.test.ts +++ b/server/routes/api/shares/shares.test.ts @@ -106,6 +106,32 @@ describe("#shares.list", () => { expect(body.data[0].documentTitle).toBe("test"); }); + it("should allow filtering by share title and return matching shares", async () => { + const user = await buildUser(); + const document = await buildDocument({ + userId: user.id, + teamId: user.teamId, + title: "hardcoded", + }); + const share = await buildShare({ + documentId: document.id, + teamId: user.teamId, + userId: user.id, + title: "team handbook", + }); + + const res = await server.post("/api/shares.list", user, { + body: { + query: "handbook", + }, + }); + const body = await res.json(); + expect(res.status).toEqual(200); + expect(body.data.length).toEqual(1); + expect(body.data[0].id).toEqual(share.id); + expect(body.data[0].title).toBe("team handbook"); + }); + it("should not return revoked shares", async () => { const user = await buildUser(); const document = await buildDocument({ diff --git a/server/routes/api/shares/shares.ts b/server/routes/api/shares/shares.ts index 29c97bcff0..1d01d1245f 100644 --- a/server/routes/api/shares/shares.ts +++ b/server/routes/api/shares/shares.ts @@ -4,7 +4,12 @@ import type { FindOptions, WhereAttributeHash, WhereOptions } from "sequelize"; import { Op } from "sequelize"; import { subMinutes } from "date-fns"; import { randomString } from "@shared/random"; -import { QueryNotices, ShareTypes, TeamPreference } from "@shared/types"; +import { + QueryNotices, + ShareStatus, + ShareTypes, + TeamPreference, +} from "@shared/types"; import { AuthenticationError, InvalidRequestError, @@ -154,7 +159,7 @@ router.post( pagination(), validate(T.SharesListSchema), async (ctx: APIContext) => { - const { sort, direction, query, published } = ctx.input.body; + const { sort, direction, query, status, type } = ctx.input.body; const { user } = ctx.state.auth; authorize(user, "listShares", user.team); const collectionIds = await user.collectionIds(); @@ -169,22 +174,42 @@ router.post( "$document.collectionId$": collectionIds, }; + const shareTitleWhere: WhereOptions = {}; + if (query) { collectionWhere["$collection.name$"] = { [Op.iLike]: `%${query}%` }; documentWhere["$document.title$"] = { [Op.iLike]: `%${query}%`, }; + + shareTitleWhere.title = { + [Op.iLike]: `%${query}%`, + }; } const shareWhere: WhereOptions = { teamId: user.teamId, userId: user.id, - published: published === false ? false : true, revokedAt: { [Op.is]: null, }, }; + const hasActive = status?.includes(ShareStatus.Active); + const hasInactive = status?.includes(ShareStatus.Inactive); + + if (hasActive && !hasInactive) { + shareWhere.published = true; + } else if (!hasActive && hasInactive) { + shareWhere.published = false; + } + + if (type?.length) { + shareWhere.type = { + [Op.in]: type, + }; + } + if (user.isAdmin) { delete shareWhere.userId; } @@ -192,7 +217,11 @@ router.post( const options: FindOptions = { where: { ...shareWhere, - [Op.or]: [collectionWhere, documentWhere], + [Op.or]: [ + { ...collectionWhere }, + { ...documentWhere }, + { ...shareTitleWhere }, + ], }, include: [ { diff --git a/shared/i18n/locales/en_US/translation.json b/shared/i18n/locales/en_US/translation.json index d006b57e30..c19c830a1c 100644 --- a/shared/i18n/locales/en_US/translation.json +++ b/shared/i18n/locales/en_US/translation.json @@ -1296,13 +1296,16 @@ "Last accessed": "Last accessed", "Domain": "Domain", "Views": "Views", - "Admins": "Admins", - "Editors": "Editors", - "All roles": "All roles", + "Active": "Active", + "Inactive": "Inactive", + "Web": "Web", + "Private": "Private", "Visibility": "Visibility", "Updated by": "Updated by", + "All roles": "All roles", + "Admins": "Admins", + "Editors": "Editors", "All status": "All status", - "Active": "Active", "Could not load emojis": "Could not load emojis", "Custom emojis can be used throughout your workspace in documents, comments, and reactions.": "Custom emojis can be used throughout your workspace in documents, comments, and reactions.", "Left": "Left", @@ -1542,7 +1545,6 @@ "IP": "IP", "a group": "a group", "All users": "All users", - "Private": "Private", "View and edit": "View and edit", "Sharing enabled": "Sharing enabled", "Date archived": "Date archived", diff --git a/shared/types.ts b/shared/types.ts index c388d1cc49..d5791a1cc4 100644 --- a/shared/types.ts +++ b/shared/types.ts @@ -807,3 +807,8 @@ export enum ShareTypes { Web = "web", Private = "private", } + +export enum ShareStatus { + Active = "active", + Inactive = "inactive", +}