mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
* feat: Make model.save() optimistic with rollback on failure Apply saved params to the model immediately so MobX observers update without waiting for the server, and roll the model back to its previous state when the request fails. New models with a client-generated id are optimistically added to the store and removed again on failure. * chore: Apply formatter and test naming convention * fix: Scope optimistic saves to passed fields and confirmed values Only fields passed to save() are applied optimistically, and a failure reverts a field only while it still holds the optimistic value. Change hooks compare against the last confirmed values so they run when the server responds. The optimistic store add is removed. * refactor: Use optimistic save for preferences and field toggles Add savePreference to User, Team and Document, and pass changed fields to save() so they revert on failure. * fix: Only compare incoming fields against confirmed values in change hooks --------- Co-authored-by: Claude <noreply@anthropic.com>
88 lines
2.2 KiB
TypeScript
88 lines
2.2 KiB
TypeScript
import React from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import styled from "styled-components";
|
|
import { s } from "@shared/styles";
|
|
import { UserPreference } from "@shared/types";
|
|
import type { Option } from "~/components/InputSelect";
|
|
import { InputSelect } from "~/components/InputSelect";
|
|
import useCurrentUser from "~/hooks/useCurrentUser";
|
|
import { CommentSortType } from "~/types";
|
|
|
|
type Props = {
|
|
/** Callback when the sort type changes */
|
|
onChange?: (sortType: CommentSortType | "resolved") => void;
|
|
/** Whether resolved comments are being viewed */
|
|
viewingResolved?: boolean;
|
|
};
|
|
|
|
const CommentSortMenu = ({ viewingResolved, onChange }: Props) => {
|
|
const { t } = useTranslation();
|
|
const user = useCurrentUser();
|
|
|
|
const preferredSortType = user.getPreference(
|
|
UserPreference.SortCommentsByOrderInDocument
|
|
)
|
|
? CommentSortType.OrderInDocument
|
|
: CommentSortType.MostRecent;
|
|
|
|
const value = viewingResolved ? "resolved" : preferredSortType;
|
|
|
|
const handleChange = React.useCallback(
|
|
(val: CommentSortType | "resolved") => {
|
|
if (val !== "resolved") {
|
|
if (val !== preferredSortType) {
|
|
void user.savePreference(
|
|
UserPreference.SortCommentsByOrderInDocument,
|
|
val === CommentSortType.OrderInDocument
|
|
);
|
|
}
|
|
}
|
|
|
|
onChange?.(val);
|
|
},
|
|
[user, onChange, preferredSortType]
|
|
);
|
|
|
|
const options: Option[] = React.useMemo(
|
|
() =>
|
|
[
|
|
{
|
|
type: "item",
|
|
label: t("Most recent"),
|
|
value: CommentSortType.MostRecent,
|
|
},
|
|
{
|
|
type: "item",
|
|
label: t("Order in doc"),
|
|
value: CommentSortType.OrderInDocument,
|
|
},
|
|
{
|
|
type: "separator",
|
|
},
|
|
{
|
|
type: "item",
|
|
label: t("Resolved"),
|
|
value: "resolved",
|
|
},
|
|
] satisfies Option[],
|
|
[t]
|
|
);
|
|
|
|
return (
|
|
<Select
|
|
options={options}
|
|
value={value}
|
|
onChange={handleChange}
|
|
label={t("Sort comments")}
|
|
labelHidden
|
|
borderOnHover
|
|
/>
|
|
);
|
|
};
|
|
|
|
const Select = styled(InputSelect)`
|
|
color: ${s("textSecondary")};
|
|
`;
|
|
|
|
export default CommentSortMenu;
|