ConceptPortal-public/rsconcept/frontend/src/pages/RSFormPage/RSTabs.tsx

232 lines
7.4 KiB
TypeScript
Raw Normal View History

'use client';
import clsx from 'clsx';
2023-09-11 20:31:54 +03:00
import { useCallback, useLayoutEffect, useMemo, useState } from 'react';
2023-07-25 20:27:29 +03:00
import { TabList, TabPanel, Tabs } from 'react-tabs';
2023-07-29 15:37:49 +03:00
import { toast } from 'react-toastify';
2023-07-25 20:27:29 +03:00
import { urls } from '@/app/urls';
import TabLabel from '@/components/ui/TabLabel';
2024-03-20 15:27:32 +03:00
import AnimateFade from '@/components/wrap/AnimateFade';
import { useLibrary } from '@/context/LibraryContext';
2023-12-26 14:23:51 +03:00
import { useBlockNavigation, useConceptNavigation } from '@/context/NavigationContext';
2024-04-01 19:07:20 +03:00
import { useConceptOptions } from '@/context/OptionsContext';
import { useRSForm } from '@/context/RSFormContext';
import useQueryStrings from '@/hooks/useQueryStrings';
2024-03-17 19:24:12 +03:00
import { ConstituentaID, IConstituenta, IConstituentaMeta } from '@/models/rsform';
2024-04-06 14:39:49 +03:00
import { PARAMETER, prefixes } from '@/utils/constants';
import { labelVersion } from '@/utils/labels';
2023-07-28 00:03:37 +03:00
import EditorConstituenta from './EditorConstituenta';
import EditorRSForm from './EditorRSForm';
import EditorRSList from './EditorRSList';
2023-07-29 21:23:18 +03:00
import EditorTermGraph from './EditorTermGraph';
import { RSEditState } from './RSEditContext';
import RSTabsMenu from './RSTabsMenu';
2023-07-15 17:46:19 +03:00
2023-08-27 15:39:49 +03:00
export enum RSTabID {
2023-07-15 17:46:19 +03:00
CARD = 0,
CST_LIST = 1,
2023-07-29 21:23:18 +03:00
CST_EDIT = 2,
TERM_GRAPH = 3
2023-07-15 17:46:19 +03:00
}
2023-07-27 22:04:25 +03:00
function RSTabs() {
const router = useConceptNavigation();
const query = useQueryStrings();
2023-12-16 19:20:26 +03:00
const activeTab = (Number(query.get('tab')) ?? RSTabID.CARD) as RSTabID;
const version = query.get('v') ? Number(query.get('v')) : undefined;
const cstQuery = query.get('active');
2024-04-01 19:07:20 +03:00
const { setNoFooter, calculateHeight } = useConceptOptions();
const { schema, loading } = useRSForm();
2023-11-01 13:41:32 +03:00
const { destroyItem } = useLibrary();
2023-07-31 23:47:18 +03:00
const [isModified, setIsModified] = useState(false);
2023-12-13 15:03:58 +03:00
useBlockNavigation(isModified);
2023-08-31 17:25:42 +03:00
2024-03-17 19:24:12 +03:00
const [selected, setSelected] = useState<ConstituentaID[]>([]);
const activeCst: IConstituenta | undefined = useMemo(() => {
if (!schema || selected.length === 0) {
return undefined;
} else {
return schema.cstByID.get(selected.at(-1)!);
}
}, [schema, selected]);
2023-12-28 14:04:44 +03:00
2023-07-31 23:47:18 +03:00
useLayoutEffect(() => {
if (schema) {
2023-12-13 15:24:10 +03:00
const oldTitle = document.title;
document.title = schema.title;
2023-07-31 23:47:18 +03:00
return () => {
2023-12-13 15:24:10 +03:00
document.title = oldTitle;
2023-12-28 14:04:44 +03:00
};
2023-07-31 23:47:18 +03:00
}
2023-12-13 15:24:10 +03:00
}, [schema, schema?.title]);
useLayoutEffect(() => {
2023-12-16 19:20:26 +03:00
setNoFooter(activeTab === RSTabID.CST_EDIT || activeTab === RSTabID.CST_LIST);
2023-12-01 02:57:19 +03:00
setIsModified(false);
if (activeTab === RSTabID.CST_EDIT) {
const cstID = Number(cstQuery);
if (cstID && schema && schema.cstByID.has(cstID)) {
setSelected([cstID]);
} else if (schema && schema?.items.length > 0) {
setSelected([schema.items[0].id]);
} else {
setSelected([]);
}
}
2023-08-27 15:39:49 +03:00
return () => setNoFooter(false);
}, [activeTab, cstQuery, setSelected, schema, setNoFooter, setIsModified]);
2023-12-16 19:20:26 +03:00
2023-09-05 00:23:53 +03:00
const navigateTab = useCallback(
2024-03-17 19:24:12 +03:00
(tab: RSTabID, activeID?: ConstituentaID) => {
2023-12-28 14:04:44 +03:00
if (!schema) {
return;
}
const url = urls.schema_props({
id: schema.id,
tab: tab,
active: activeID,
version: version
});
2023-12-28 14:04:44 +03:00
if (activeID) {
if (tab === activeTab && tab !== RSTabID.CST_EDIT) {
router.replace(url);
2023-12-28 14:04:44 +03:00
} else {
router.push(url);
2023-12-28 14:04:44 +03:00
}
} else if (tab !== activeTab && tab === RSTabID.CST_EDIT && schema.items.length > 0) {
activeID = schema.items[0].id;
router.replace(url);
} else {
router.push(url);
}
2023-12-28 14:04:44 +03:00
},
[router, schema, activeTab, version]
2023-12-28 14:04:44 +03:00
);
function onSelectTab(index: number) {
navigateTab(index, selected.length > 0 ? selected.at(-1) : undefined);
}
const onCreateCst = useCallback(
(newCst: IConstituentaMeta) => {
navigateTab(activeTab, newCst.id);
if (activeTab === RSTabID.CST_EDIT || activeTab === RSTabID.CST_LIST) {
setTimeout(() => {
const element = document.getElementById(`${prefixes.cst_list}${newCst.alias}`);
if (element) {
element.scrollIntoView({
behavior: 'smooth',
block: 'nearest',
inline: 'nearest'
});
}
2024-04-06 14:39:49 +03:00
}, PARAMETER.refreshTimeout);
2023-07-29 15:37:49 +03:00
}
2023-12-28 14:04:44 +03:00
},
[activeTab, navigateTab]
2023-12-28 14:04:44 +03:00
);
const onDeleteCst = useCallback(
2024-03-17 19:24:12 +03:00
(newActive?: ConstituentaID) => {
if (!newActive) {
navigateTab(RSTabID.CST_LIST);
} else if (activeTab === RSTabID.CST_EDIT) {
navigateTab(activeTab, newActive);
2023-12-28 14:04:44 +03:00
} else {
navigateTab(activeTab);
2023-12-28 14:04:44 +03:00
}
},
[activeTab, navigateTab]
2023-12-28 14:04:44 +03:00
);
const onOpenCst = useCallback(
2024-03-17 19:24:12 +03:00
(cstID: ConstituentaID) => {
2024-04-08 11:47:47 +03:00
if (cstID !== activeCst?.id) {
navigateTab(RSTabID.CST_EDIT, cstID);
}
2023-12-28 14:04:44 +03:00
},
2024-04-08 11:47:47 +03:00
[navigateTab, activeCst]
2023-12-28 14:04:44 +03:00
);
2023-12-28 14:04:44 +03:00
const onDestroySchema = useCallback(() => {
if (!schema || !window.confirm('Вы уверены, что хотите удалить данную схему?')) {
return;
}
2023-11-01 13:41:32 +03:00
destroyItem(schema.id, () => {
toast.success('Схема удалена');
router.push(urls.library);
});
}, [schema, destroyItem, router]);
2024-03-12 19:54:10 +03:00
const panelHeight = useMemo(() => calculateHeight('1.75rem + 4px'), [calculateHeight]);
2023-12-28 14:04:44 +03:00
return (
<RSEditState
selected={selected}
setSelected={setSelected}
activeCst={activeCst}
isModified={isModified}
onCreateCst={onCreateCst}
onDeleteCst={onDeleteCst}
>
2023-12-28 14:04:44 +03:00
{schema && !loading ? (
<Tabs
selectedIndex={activeTab}
onSelect={onSelectTab}
defaultFocus
selectedTabClassName='clr-selected'
2024-02-17 12:50:25 +03:00
className='flex flex-col min-w-fit'
2023-12-28 14:04:44 +03:00
>
2024-03-06 21:33:59 +03:00
<TabList className={clsx('mx-auto w-fit', 'flex items-stretch', 'border-b-2 border-x-2 divide-x-2')}>
<RSTabsMenu onDestroy={onDestroySchema} />
<TabLabel
label='Карточка'
titleHtml={`Название: <b>${schema.title ?? ''}</b><br />Версия: ${labelVersion(schema)}`}
/>
<TabLabel
2023-12-28 14:04:44 +03:00
label='Содержание'
titleHtml={`Конституент: ${schema.stats?.count_all ?? 0}<br />Ошибок: ${schema.stats?.count_errors ?? 0}`}
2023-12-28 14:04:44 +03:00
/>
<TabLabel label='Редактор' />
<TabLabel label='Граф термов' />
2023-12-28 14:04:44 +03:00
</TabList>
2024-03-12 19:54:10 +03:00
<AnimateFade className='overflow-y-auto' style={{ maxHeight: panelHeight }}>
<TabPanel forceRender style={{ display: activeTab === RSTabID.CARD ? '' : 'none' }}>
<EditorRSForm
isModified={isModified} // prettier: split lines
setIsModified={setIsModified}
onDestroy={onDestroySchema}
/>
</TabPanel>
<TabPanel forceRender style={{ display: activeTab === RSTabID.CST_LIST ? '' : 'none' }}>
2024-04-03 15:51:57 +03:00
<EditorRSList onOpenEdit={onOpenCst} />
</TabPanel>
<TabPanel forceRender style={{ display: activeTab === RSTabID.CST_EDIT ? '' : 'none' }}>
<EditorConstituenta
isModified={isModified}
setIsModified={setIsModified}
activeCst={activeCst}
onOpenEdit={onOpenCst}
/>
</TabPanel>
<TabPanel style={{ display: activeTab === RSTabID.TERM_GRAPH ? '' : 'none' }}>
2024-04-03 15:51:57 +03:00
<EditorTermGraph onOpenEdit={onOpenCst} />
</TabPanel>
2024-01-07 03:29:16 +03:00
</AnimateFade>
2023-12-28 14:04:44 +03:00
</Tabs>
) : null}
</RSEditState>
2023-12-28 14:04:44 +03:00
);
2023-07-15 17:46:19 +03:00
}
2023-07-27 22:04:25 +03:00
export default RSTabs;