mirror of
https://github.com/docmost/docmost.git
synced 2025-11-21 10:41:07 +10:00
fixes
* fix comments * fix page history * fix aside width on smaller screens
This commit is contained in:
@ -1,50 +1,76 @@
|
||||
import { usePageHistoryListQuery, usePageHistoryQuery } from '@/features/page-history/queries/page-history-query';
|
||||
import { useParams } from 'react-router-dom';
|
||||
import HistoryItem from '@/features/page-history/components/history-item';
|
||||
import { activeHistoryIdAtom, historyAtoms } from '@/features/page-history/atoms/history-atoms';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useCallback, useEffect } from 'react';
|
||||
import { Button, ScrollArea, Group, Divider, Text } from '@mantine/core';
|
||||
import { pageEditorAtom, titleEditorAtom } from '@/features/editor/atoms/editor-atoms';
|
||||
import { modals } from '@mantine/modals';
|
||||
import { notifications } from '@mantine/notifications';
|
||||
import {
|
||||
usePageHistoryListQuery,
|
||||
usePageHistoryQuery,
|
||||
} from "@/features/page-history/queries/page-history-query";
|
||||
import { useParams } from "react-router-dom";
|
||||
import HistoryItem from "@/features/page-history/components/history-item";
|
||||
import {
|
||||
activeHistoryIdAtom,
|
||||
historyAtoms,
|
||||
} from "@/features/page-history/atoms/history-atoms";
|
||||
import { useAtom } from "jotai";
|
||||
import { useCallback, useEffect } from "react";
|
||||
import { Button, ScrollArea, Group, Divider, Text } from "@mantine/core";
|
||||
import {
|
||||
pageEditorAtom,
|
||||
titleEditorAtom,
|
||||
} from "@/features/editor/atoms/editor-atoms";
|
||||
import { modals } from "@mantine/modals";
|
||||
import { notifications } from "@mantine/notifications";
|
||||
|
||||
function HistoryList() {
|
||||
const [activeHistoryId, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
|
||||
const { pageId } = useParams();
|
||||
const { data, isLoading, isError } = usePageHistoryListQuery(pageId);
|
||||
const {
|
||||
data: pageHistoryList,
|
||||
isLoading,
|
||||
isError,
|
||||
} = usePageHistoryListQuery(pageId);
|
||||
const { data: activeHistoryData } = usePageHistoryQuery(activeHistoryId);
|
||||
|
||||
const [mainEditor] = useAtom(pageEditorAtom);
|
||||
const [mainEditorTitle] = useAtom(titleEditorAtom);
|
||||
const [, setHistoryModalOpen] = useAtom(historyAtoms);
|
||||
|
||||
const confirmModal = () => modals.openConfirmModal({
|
||||
title: 'Please confirm your action',
|
||||
children: (
|
||||
<Text size="sm">
|
||||
Are you sure you want to restore this version? Any changes not versioned will be lost.
|
||||
</Text>
|
||||
),
|
||||
labels: { confirm: 'Confirm', cancel: 'Cancel' },
|
||||
onConfirm: handleRestore,
|
||||
});
|
||||
const confirmModal = () =>
|
||||
modals.openConfirmModal({
|
||||
title: "Please confirm your action",
|
||||
children: (
|
||||
<Text size="sm">
|
||||
Are you sure you want to restore this version? Any changes not
|
||||
versioned will be lost.
|
||||
</Text>
|
||||
),
|
||||
labels: { confirm: "Confirm", cancel: "Cancel" },
|
||||
onConfirm: handleRestore,
|
||||
});
|
||||
|
||||
const handleRestore = useCallback(() => {
|
||||
if (activeHistoryData) {
|
||||
mainEditorTitle.chain().clearContent().setContent(activeHistoryData.title, true).run();
|
||||
mainEditor.chain().clearContent().setContent(activeHistoryData.content).run();
|
||||
mainEditorTitle
|
||||
.chain()
|
||||
.clearContent()
|
||||
.setContent(activeHistoryData.title, true)
|
||||
.run();
|
||||
mainEditor
|
||||
.chain()
|
||||
.clearContent()
|
||||
.setContent(activeHistoryData.content)
|
||||
.run();
|
||||
setHistoryModalOpen(false);
|
||||
notifications.show({ message: 'Successfully restored' });
|
||||
|
||||
notifications.show({ message: "Successfully restored" });
|
||||
}
|
||||
}, [activeHistoryData]);
|
||||
|
||||
useEffect(() => {
|
||||
if (data && data.length > 0 && !activeHistoryId) {
|
||||
setActiveHistoryId(data[0].id);
|
||||
if (
|
||||
pageHistoryList &&
|
||||
pageHistoryList.items.length > 0 &&
|
||||
!activeHistoryId
|
||||
) {
|
||||
setActiveHistoryId(pageHistoryList.items[0].id);
|
||||
}
|
||||
}, [data]);
|
||||
}, [pageHistoryList]);
|
||||
|
||||
if (isLoading) {
|
||||
return <></>;
|
||||
@ -54,31 +80,39 @@ function HistoryList() {
|
||||
return <div>Error loading page history.</div>;
|
||||
}
|
||||
|
||||
if (!data || data.length === 0) {
|
||||
if (!pageHistoryList || pageHistoryList.items.length === 0) {
|
||||
return <>No page history saved yet.</>;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<ScrollArea h={620} w="100%" type="scroll" scrollbarSize={5}>
|
||||
{data && data.map((historyItem, index) => (
|
||||
<HistoryItem
|
||||
key={index}
|
||||
historyItem={historyItem}
|
||||
onSelect={setActiveHistoryId}
|
||||
isActive={historyItem.id === activeHistoryId}
|
||||
/>
|
||||
))}
|
||||
{pageHistoryList &&
|
||||
pageHistoryList.items.map((historyItem, index) => (
|
||||
<HistoryItem
|
||||
key={index}
|
||||
historyItem={historyItem}
|
||||
onSelect={setActiveHistoryId}
|
||||
isActive={historyItem.id === activeHistoryId}
|
||||
/>
|
||||
))}
|
||||
</ScrollArea>
|
||||
|
||||
<Divider />
|
||||
|
||||
<Group p="xs" wrap="nowrap">
|
||||
<Button size="compact-md" onClick={confirmModal}>Restore</Button>
|
||||
<Button variant="default" size="compact-md" onClick={() => setHistoryModalOpen(false)}>Cancel</Button>
|
||||
<Button size="compact-md" onClick={confirmModal}>
|
||||
Restore
|
||||
</Button>
|
||||
<Button
|
||||
variant="default"
|
||||
size="compact-md"
|
||||
onClick={() => setHistoryModalOpen(false)}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
</Group>
|
||||
</div>
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -1,18 +1,26 @@
|
||||
import { useQuery, UseQueryResult } from '@tanstack/react-query';
|
||||
import { getPageHistoryById, getPageHistoryList } from '@/features/page-history/services/page-history-service';
|
||||
import { IPageHistory } from '@/features/page-history/types/page.types';
|
||||
import { useQuery, UseQueryResult } from "@tanstack/react-query";
|
||||
import {
|
||||
getPageHistoryById,
|
||||
getPageHistoryList,
|
||||
} from "@/features/page-history/services/page-history-service";
|
||||
import { IPageHistory } from "@/features/page-history/types/page.types";
|
||||
import { IPagination } from "@/lib/types.ts";
|
||||
|
||||
export function usePageHistoryListQuery(pageId: string): UseQueryResult<IPageHistory[], Error> {
|
||||
export function usePageHistoryListQuery(
|
||||
pageId: string,
|
||||
): UseQueryResult<IPagination<IPageHistory>, Error> {
|
||||
return useQuery({
|
||||
queryKey: ['page-history-list', pageId],
|
||||
queryKey: ["page-history-list", pageId],
|
||||
queryFn: () => getPageHistoryList(pageId),
|
||||
enabled: !!pageId,
|
||||
});
|
||||
}
|
||||
|
||||
export function usePageHistoryQuery(historyId: string): UseQueryResult<IPageHistory, Error> {
|
||||
export function usePageHistoryQuery(
|
||||
historyId: string,
|
||||
): UseQueryResult<IPageHistory, Error> {
|
||||
return useQuery({
|
||||
queryKey: ['page-history', historyId],
|
||||
queryKey: ["page-history", historyId],
|
||||
queryFn: () => getPageHistoryById(historyId),
|
||||
enabled: !!historyId,
|
||||
staleTime: 10 * 60 * 1000,
|
||||
|
||||
@ -1,12 +1,20 @@
|
||||
import api from '@/lib/api-client';
|
||||
import { IPageHistory } from '@/features/page-history/types/page.types';
|
||||
import api from "@/lib/api-client";
|
||||
import { IPageHistory } from "@/features/page-history/types/page.types";
|
||||
|
||||
export async function getPageHistoryList(pageId: string): Promise<IPageHistory[]> {
|
||||
const req = await api.post<IPageHistory[]>('/pages/history', { pageId });
|
||||
return req.data as IPageHistory[];
|
||||
export async function getPageHistoryList(
|
||||
pageId: string,
|
||||
): Promise<IPageHistory[]> {
|
||||
const req = await api.post("/pages/history", {
|
||||
pageId,
|
||||
});
|
||||
return req.data;
|
||||
}
|
||||
|
||||
export async function getPageHistoryById(id: string): Promise<IPageHistory> {
|
||||
const req = await api.post<IPageHistory>('/pages/history/details', { id });
|
||||
return req.data as IPageHistory;
|
||||
export async function getPageHistoryById(
|
||||
historyId: string,
|
||||
): Promise<IPageHistory> {
|
||||
const req = await api.post<IPageHistory>("/pages/history/info", {
|
||||
historyId,
|
||||
});
|
||||
return req.data;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user