Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SY-1787 Prevent Snapshot of Schematics from Toolbar Outside of Workspace #1023

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions console/src/schematic/toolbar/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@

import { schematic } from "@synnaxlabs/client";
import { Icon } from "@synnaxlabs/media";
import { Align, Breadcrumb, Button, Status, Tabs, Text } from "@synnaxlabs/pluto";
import {
Align,
Breadcrumb,
Button,
Status,
Synnax,
Tabs,
Text,
} from "@synnaxlabs/pluto";
import { useQuery } from "@tanstack/react-query";
import { type ReactElement, useCallback } from "react";
import { useDispatch } from "react-redux";

Expand Down Expand Up @@ -91,6 +100,12 @@ export const Toolbar = ({ layoutKey }: ToolbarProps): ReactElement | null => {
},
[layoutKey, state?.editable],
);
const client = Synnax.use();
const queryResult = useQuery({
pjdotson marked this conversation as resolved.
Show resolved Hide resolved
queryKey: [layoutKey, client?.key],
queryFn: async () => await client?.workspaces.schematic.retrieve(layoutKey),
});
const existsOnServer = queryResult.isSuccess && queryResult.data?.key === layoutKey;

const handleTabSelect = useCallback(
(tabKey: string): void => {
Expand Down Expand Up @@ -123,6 +138,8 @@ export const Toolbar = ({ layoutKey }: ToolbarProps): ReactElement | null => {

const activeRange = Range.useSelect();
const hasActiveRange = activeRange != null;
const activeRangeIsPersisted = activeRange?.persisted ?? false;
const canSnapshot = existsOnServer && hasActiveRange && activeRangeIsPersisted;

if (state == null) return null;

Expand All @@ -143,9 +160,17 @@ export const Toolbar = ({ layoutKey }: ToolbarProps): ReactElement | null => {
<Align.Space direction="x" empty style={{ height: "100%", width: 93 }}>
<Button.Icon
sharp
disabled={!hasActiveRange}
disabled={!canSnapshot}
tooltip={
hasActiveRange ? `Snapshot to ${activeRange.name}` : "No active range"
canSnapshot
? `Snapshot to ${activeRange.name}`
: !hasActiveRange
? "No active range"
: !activeRangeIsPersisted
? `${activeRange.name} is not persisted`
: !existsOnServer
? `${name} does not exist on ${client?.props.name ?? "server"}`
: `Cannot snapshot ${name}`
}
onClick={handleRangeSnapshot}
size="medium"
Expand Down
Loading