Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(site): Add source code tab on template page, group buttons and add edit file option #6681

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 9 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
refactor template fetching
  • Loading branch information
BrunoQuaresma committed Mar 20, 2023
commit e26c57ff2ca9c89ee0166c4fafaba343d285006d
101 changes: 61 additions & 40 deletions site/src/components/TemplateLayout/TemplateLayout.tsx
Original file line number Diff line number Diff line change
@@ -1,36 +1,65 @@
import { makeStyles } from "@material-ui/core/styles"
import { useMachine } from "@xstate/react"
import { useOrganizationId } from "hooks/useOrganizationId"
import { createContext, FC, Suspense, useContext } from "react"
import { NavLink, Outlet, useNavigate, useParams } from "react-router-dom"
import { combineClasses } from "util/combineClasses"
import {
TemplateContext,
templateMachine,
} from "xServices/template/templateXService"
import { Margins } from "components/Margins/Margins"
import { Stack } from "components/Stack/Stack"
import { Permissions } from "xServices/auth/authXService"
import { Loader } from "components/Loader/Loader"
import { usePermissions } from "hooks/usePermissions"
import { TemplatePageHeader } from "./TemplatePageHeader"
import { AlertBanner } from "components/AlertBanner/AlertBanner"
import {
checkAuthorization,
getTemplateByName,
getTemplateDAUs,
getTemplateVersion,
getTemplateVersionResources,
getTemplateVersions,
} from "api/api"
import { useQuery } from "@tanstack/react-query"

const templatePermissions = (templateId: string) => ({
canUpdateTemplate: {
object: {
resource_type: "template",
resource_id: templateId,
},
action: "update",
},
})

const useTemplateName = () => {
const { template } = useParams()
const fetchTemplate = async (orgId: string, templateName: string) => {
const template = await getTemplateByName(orgId, templateName)
const [activeVersion, resources, versions, daus, permissions] =
await Promise.all([
getTemplateVersion(template.active_version_id),
getTemplateVersionResources(template.active_version_id),
getTemplateVersions(template.id),
getTemplateDAUs(template.id),
checkAuthorization({
checks: templatePermissions(template.id),
}),
])

if (!template) {
throw new Error("No template found in the URL")
return {
template,
activeVersion,
resources,
versions,
daus,
permissions,
}

return template
}

type TemplateLayoutContextValue = {
context: TemplateContext
permissions?: Permissions
const useTemplateData = (orgId: string, templateName: string) => {
return useQuery({
queryKey: ["template", templateName],
queryFn: () => fetchTemplate(orgId, templateName),
})
}

type TemplateLayoutContextValue = Awaited<ReturnType<typeof fetchTemplate>>

const TemplateLayoutContext = createContext<
TemplateLayoutContextValue | undefined
>(undefined)
Expand All @@ -50,38 +79,32 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
}) => {
const navigate = useNavigate()
const styles = useStyles()
const organizationId = useOrganizationId()
const templateName = useTemplateName()
const [templateState, _] = useMachine(templateMachine, {
context: {
templateName,
organizationId,
},
})
const {
template,
permissions: templatePermissions,
getTemplateError,
} = templateState.context
const permissions = usePermissions()
const orgId = useOrganizationId()
const { template } = useParams() as { template: string }
const templateData = useTemplateData(orgId, template)

if (getTemplateError) {
if (templateData.error) {
return (
<div className={styles.error}>
<AlertBanner severity="error" error={getTemplateError} />
<AlertBanner severity="error" error={templateData.error} />
</div>
)
}

if (!template || !templatePermissions) {
if (templateData.isLoading) {
return <Loader />
}

// Make typescript happy
if (!templateData.data) {
return <></>
}

return (
<>
<TemplatePageHeader
template={template}
permissions={templatePermissions}
template={templateData.data.template}
permissions={templateData.data.permissions}
onDeleteTemplate={() => {
navigate("/templates")
}}
Expand All @@ -92,7 +115,7 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
<Stack direction="row" spacing={0.25}>
<NavLink
end
to={`/templates/${template.name}`}
to={`/templates/${template}`}
className={({ isActive }) =>
combineClasses([
styles.tabItem,
Expand All @@ -103,7 +126,7 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
Summary
</NavLink>
<NavLink
to={`/templates/${template.name}/permissions`}
to={`/templates/${template}/permissions`}
className={({ isActive }) =>
combineClasses([
styles.tabItem,
Expand All @@ -118,9 +141,7 @@ export const TemplateLayout: FC<{ children?: JSX.Element }> = ({
</div>

<Margins>
<TemplateLayoutContext.Provider
value={{ permissions, context: templateState.context }}
>
<TemplateLayoutContext.Provider value={templateData.data}>
<Suspense fallback={<Loader />}>{children}</Suspense>
</TemplateLayoutContext.Provider>
</Margins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ export const TemplatePermissionsPage: FC<
React.PropsWithChildren<unknown>
> = () => {
const organizationId = useOrganizationId()
const { context } = useTemplateLayoutContext()
const { template, permissions } = context
const { template, permissions } = useTemplateLayoutContext()
const { template_rbac: isTemplateRBACEnabled } = useFeatureVisibility()
const [state, send] = useMachine(templateACLMachine, {
context: { templateId: template?.id },
context: { templateId: template.id },
})
const { templateACL, userToBeUpdated, groupToBeUpdated } = state.context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,10 @@ import { FC } from "react"
import { Helmet } from "react-helmet-async"
import { pageTitle } from "util/page"
import { TemplateSummaryPageView } from "./TemplateSummaryPageView"
import { Loader } from "components/Loader/Loader"

export const TemplateSummaryPage: FC = () => {
const { context } = useTemplateLayoutContext()
const {
template,
activeTemplateVersion,
templateResources,
templateVersions,
templateDAUs,
} = context

if (!template || !activeTemplateVersion || !templateResources) {
return <Loader />
}
const { template, activeVersion, resources, versions, daus } =
useTemplateLayoutContext()

return (
<>
Expand All @@ -34,10 +23,10 @@ export const TemplateSummaryPage: FC = () => {
</Helmet>
<TemplateSummaryPageView
template={template}
activeTemplateVersion={activeTemplateVersion}
templateResources={templateResources}
templateVersions={templateVersions}
templateDAUs={templateDAUs}
activeTemplateVersion={activeVersion}
templateResources={resources}
templateVersions={versions}
templateDAUs={daus}
/>
</>
)
Expand Down