-
Notifications
You must be signed in to change notification settings - Fork 881
chore: simplify workspaces data fetching #17703
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,10 @@ import InfoIcon from "@mui/icons-material/InfoOutlined"; | |
import RefreshIcon from "@mui/icons-material/Refresh"; | ||
import Link from "@mui/material/Link"; | ||
import Skeleton from "@mui/material/Skeleton"; | ||
import { getErrorDetail, getErrorMessage } from "api/errors"; | ||
import { templateVersion } from "api/queries/templates"; | ||
import type { Workspace } from "api/typesGenerated"; | ||
import { displayError } from "components/GlobalSnackbar/utils"; | ||
import { | ||
HelpTooltip, | ||
HelpTooltipAction, | ||
|
@@ -17,102 +20,99 @@ import { usePopover } from "components/deprecated/Popover/Popover"; | |
import { linkToTemplate, useLinks } from "modules/navigation"; | ||
import type { FC } from "react"; | ||
import { useQuery } from "react-query"; | ||
|
||
const Language = { | ||
outdatedLabel: "Outdated", | ||
versionTooltipText: | ||
"This workspace version is outdated and a newer version is available.", | ||
updateVersionLabel: "Update", | ||
}; | ||
import { | ||
WorkspaceUpdateDialogs, | ||
useWorkspaceUpdate, | ||
} from "../WorkspaceUpdateDialogs"; | ||
|
||
interface TooltipProps { | ||
organizationName: string; | ||
templateName: string; | ||
latestVersionId: string; | ||
onUpdateVersion: () => void; | ||
ariaLabel?: string; | ||
workspace: Workspace; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Big fan 😎 |
||
} | ||
|
||
export const WorkspaceOutdatedTooltip: FC<TooltipProps> = (props) => { | ||
return ( | ||
<HelpTooltip> | ||
<HelpTooltipTrigger | ||
size="small" | ||
aria-label="More info" | ||
hoverEffect={false} | ||
> | ||
<HelpTooltipTrigger size="small" hoverEffect={false}> | ||
<InfoIcon css={styles.icon} /> | ||
<span className="sr-only">Outdated info</span> | ||
</HelpTooltipTrigger> | ||
|
||
<WorkspaceOutdatedTooltipContent {...props} /> | ||
</HelpTooltip> | ||
); | ||
}; | ||
|
||
const WorkspaceOutdatedTooltipContent: FC<TooltipProps> = ({ | ||
organizationName, | ||
templateName, | ||
latestVersionId, | ||
onUpdateVersion, | ||
ariaLabel, | ||
}) => { | ||
const WorkspaceOutdatedTooltipContent: FC<TooltipProps> = ({ workspace }) => { | ||
const getLink = useLinks(); | ||
const theme = useTheme(); | ||
const popover = usePopover(); | ||
const { data: activeVersion } = useQuery({ | ||
...templateVersion(latestVersionId), | ||
...templateVersion(workspace.template_active_version_id), | ||
enabled: popover.open, | ||
}); | ||
const updateWorkspace = useWorkspaceUpdate({ | ||
workspace, | ||
latestVersion: activeVersion, | ||
onError: (error) => { | ||
displayError( | ||
getErrorMessage(error, "Error updating workspace"), | ||
getErrorDetail(error), | ||
); | ||
}, | ||
}); | ||
|
||
const versionLink = `${getLink( | ||
linkToTemplate(organizationName, templateName), | ||
linkToTemplate(workspace.organization_name, workspace.template_name), | ||
)}`; | ||
|
||
return ( | ||
<HelpTooltipContent> | ||
<HelpTooltipTitle>{Language.outdatedLabel}</HelpTooltipTitle> | ||
<HelpTooltipText>{Language.versionTooltipText}</HelpTooltipText> | ||
<> | ||
<HelpTooltipContent disablePortal={false}> | ||
<HelpTooltipTitle>Outdated</HelpTooltipTitle> | ||
<HelpTooltipText> | ||
This workspace version is outdated and a newer version is available. | ||
</HelpTooltipText> | ||
|
||
<div css={styles.container}> | ||
<div css={{ lineHeight: "1.6" }}> | ||
<div css={styles.bold}>New version</div> | ||
<div> | ||
{activeVersion ? ( | ||
<Link | ||
href={`${versionLink}/versions/${activeVersion.name}`} | ||
target="_blank" | ||
css={{ color: theme.palette.primary.light }} | ||
> | ||
{activeVersion.name} | ||
</Link> | ||
) : ( | ||
<Skeleton variant="text" height={20} width={100} /> | ||
)} | ||
<div css={styles.container}> | ||
<div css={{ lineHeight: "1.6" }}> | ||
<div css={styles.bold}>New version</div> | ||
<div> | ||
{activeVersion ? ( | ||
<Link | ||
href={`${versionLink}/versions/${activeVersion.name}`} | ||
target="_blank" | ||
css={{ color: theme.palette.primary.light }} | ||
> | ||
{activeVersion.name} | ||
</Link> | ||
) : ( | ||
<Skeleton variant="text" height={20} width={100} /> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div css={{ lineHeight: "1.6" }}> | ||
<div css={styles.bold}>Message</div> | ||
<div> | ||
{activeVersion ? ( | ||
activeVersion.message || "No message" | ||
) : ( | ||
<Skeleton variant="text" height={20} width={150} /> | ||
)} | ||
<div css={{ lineHeight: "1.6" }}> | ||
<div css={styles.bold}>Message</div> | ||
<div> | ||
{activeVersion ? ( | ||
activeVersion.message || "No message" | ||
) : ( | ||
<Skeleton variant="text" height={20} width={150} /> | ||
)} | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<HelpTooltipLinksGroup> | ||
<HelpTooltipAction | ||
icon={RefreshIcon} | ||
onClick={onUpdateVersion} | ||
ariaLabel={ariaLabel} | ||
> | ||
{Language.updateVersionLabel} | ||
</HelpTooltipAction> | ||
</HelpTooltipLinksGroup> | ||
</HelpTooltipContent> | ||
<HelpTooltipLinksGroup> | ||
<HelpTooltipAction | ||
icon={RefreshIcon} | ||
onClick={updateWorkspace.update} | ||
> | ||
Update | ||
</HelpTooltipAction> | ||
</HelpTooltipLinksGroup> | ||
</HelpTooltipContent> | ||
<WorkspaceUpdateDialogs {...updateWorkspace.dialogs} /> | ||
</> | ||
); | ||
}; | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import { workspaces } from "api/queries/workspaces"; | ||
import type { Template, Workspace } from "api/typesGenerated"; | ||
import { compareAsc } from "date-fns"; | ||
import { calcOffset } from "hooks/usePagination"; | ||
import { useWorkspacesData } from "pages/WorkspacesPage/data"; | ||
import { useQuery } from "react-query"; | ||
import type { TemplateScheduleFormValues } from "./formHelpers"; | ||
|
||
export const useWorkspacesToGoDormant = ( | ||
template: Template, | ||
formValues: TemplateScheduleFormValues, | ||
fromDate: Date, | ||
) => { | ||
const { data } = useWorkspacesData({ | ||
offset: calcOffset(0, 0), | ||
limit: 0, | ||
q: `template:${template.name}`, | ||
}); | ||
const { data } = useQuery( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Big fan 😎 |
||
workspaces({ | ||
q: `template:${template.name}`, | ||
}), | ||
); | ||
|
||
return data?.workspaces?.filter((workspace: Workspace) => { | ||
if (!formValues.time_til_dormant_ms) { | ||
|
@@ -40,11 +40,12 @@ export const useWorkspacesToBeDeleted = ( | |
formValues: TemplateScheduleFormValues, | ||
fromDate: Date, | ||
) => { | ||
const { data } = useWorkspacesData({ | ||
offset: calcOffset(0, 0), | ||
limit: 0, | ||
q: `template:${template.name} dormant:true`, | ||
}); | ||
const { data } = useQuery( | ||
workspaces({ | ||
q: `template:${template.name} dormant:true`, | ||
}), | ||
); | ||
|
||
return data?.workspaces?.filter((workspace: Workspace) => { | ||
if (!workspace.dormant_at || !formValues.time_til_dormant_autodelete_ms) { | ||
return false; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kinda like the pattern of putting all the language in a constant but it truly doesn't matter haha.