LinksSettings.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import React, { useState } from 'react';
  2. import { DashboardModel } from '../../state/DashboardModel';
  3. import { LinkSettingsEdit, LinkSettingsList } from '../LinksSettings';
  4. import { newLink } from '../LinksSettings/LinkSettingsEdit';
  5. import { DashboardSettingsHeader } from './DashboardSettingsHeader';
  6. interface Props {
  7. dashboard: DashboardModel;
  8. }
  9. export type LinkSettingsMode = 'list' | 'new' | 'edit';
  10. export const LinksSettings: React.FC<Props> = ({ dashboard }) => {
  11. const [editIdx, setEditIdx] = useState<number | null>(null);
  12. const onGoBack = () => {
  13. setEditIdx(null);
  14. };
  15. const onNew = () => {
  16. dashboard.links = [...dashboard.links, { ...newLink }];
  17. setEditIdx(dashboard.links.length - 1);
  18. };
  19. const onEdit = (idx: number) => {
  20. setEditIdx(idx);
  21. };
  22. const isEditing = editIdx !== null;
  23. return (
  24. <>
  25. <DashboardSettingsHeader onGoBack={onGoBack} title="Dashboard links" isEditing={isEditing} />
  26. {!isEditing && <LinkSettingsList dashboard={dashboard} onNew={onNew} onEdit={onEdit} />}
  27. {isEditing && <LinkSettingsEdit dashboard={dashboard} editLinkIdx={editIdx!} onGoBack={onGoBack} />}
  28. </>
  29. );
  30. };