mirror of
https://github.com/outline/outline.git
synced 2026-11-26 15:43:48 +03:00
type and status filtering
This commit is contained in:
1 parent
371c52979e
commit
36937d5180
10 files changed
+261
-34
No files matched your search
+3
-1
@@ -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
|
||||
|
||||
@@ -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 (
|
||||
<Scene
|
||||
title={t("Shared Links")}
|
||||
@@ -151,13 +218,11 @@ function Shares() {
|
||||
placeholder={`${t("Filter")}…`}
|
||||
onChange={handleSearch}
|
||||
/>
|
||||
{/*
|
||||
to do: finish implementation
|
||||
<Switch
|
||||
label={t("Published")}
|
||||
checked={showPublished}
|
||||
onChange={setShowPublished}
|
||||
/> */}
|
||||
<ShareTypeFilter typeFilter={typeFilter} onSelect={handleTypeFilter} />
|
||||
<ShareStatusFilter
|
||||
statusFilter={statusFilter}
|
||||
onSelect={handleStatusFilter}
|
||||
/>
|
||||
</StickyFilters>
|
||||
<ConditionalFade animate={!data}>
|
||||
<SharesTable
|
||||
|
||||
@@ -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 { ShareStatus } from "@shared/types";
|
||||
import FilterOptions from "~/components/FilterOptions";
|
||||
|
||||
type Props = {
|
||||
onSelect: (option: { statusFilter: ShareStatus[] }) => 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 (
|
||||
<FilterOptions
|
||||
options={options}
|
||||
selectedKeys={statusFilter}
|
||||
onSelect={handleSelect}
|
||||
defaultLabel={"Any status"}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default observer(ShareStatusFilter);
|
||||
@@ -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 (
|
||||
<FilterOptions
|
||||
options={options}
|
||||
selectedKeys={typeFilter}
|
||||
onSelect={handleSelect}
|
||||
defaultLabel={"Any type"}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export default observer(ShareTypeFilter);
|
||||
@@ -193,15 +193,6 @@ export default class SharesStore extends Store<Share> {
|
||||
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
|
||||
|
||||
@@ -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<typeof SharesInfoSchema>;
|
||||
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), {
|
||||
|
||||
@@ -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({
|
||||
|
||||
@@ -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<T.SharesListReq>) => {
|
||||
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<Share> = {};
|
||||
|
||||
if (query) {
|
||||
collectionWhere["$collection.name$"] = { [Op.iLike]: `%${query}%` };
|
||||
documentWhere["$document.title$"] = {
|
||||
[Op.iLike]: `%${query}%`,
|
||||
};
|
||||
|
||||
shareTitleWhere.title = {
|
||||
[Op.iLike]: `%${query}%`,
|
||||
};
|
||||
}
|
||||
|
||||
const shareWhere: WhereOptions<Share> = {
|
||||
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: [
|
||||
{
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -807,3 +807,8 @@ export enum ShareTypes {
|
||||
Web = "web",
|
||||
Private = "private",
|
||||
}
|
||||
|
||||
export enum ShareStatus {
|
||||
Active = "active",
|
||||
Inactive = "inactive",
|
||||
}
|
||||
Reference in new issue
Block a user