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

TSify utils/Dialogs and some related code #3983

Merged
merged 6 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions catalog/app/containers/Admin/Buckets/Buckets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1296,7 +1296,6 @@ function CRUD({ bucketName }: CRUDProps) {
title: 'Add bucket',
icon: <M.Icon>add</M.Icon>,
fn: React.useCallback(() => {
// @ts-expect-error
openDialog(({ close }) => <Add {...{ close }} />)
}, [openDialog]),
},
Expand All @@ -1311,7 +1310,6 @@ function CRUD({ bucketName }: CRUDProps) {
title: 'Delete',
icon: <M.Icon>delete</M.Icon>,
fn: () => {
// @ts-expect-error
openDialog(({ close }) => <Delete {...{ bucket, close }} />)
},
},
Expand Down
11 changes: 3 additions & 8 deletions catalog/app/containers/Admin/RolesAndPolicies/Roles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@

interface SettingsMenuProps {
role: Role
openDialog: (render: (props: DialogsOpenProps) => JSX.Element, props?: $TSFixMe) => void
openDialog: Dialogs.Dialogs['open']
}

function SettingsMenu({ role, openDialog }: SettingsMenuProps) {
Expand Down Expand Up @@ -684,11 +684,6 @@
)
}

// XXX: move to dialogs module
interface DialogsOpenProps {
close: (reason?: string) => void
}

export default function Roles() {
const data = GQL.useQueryS(ROLES_QUERY)
const rows = data.roles
Expand All @@ -709,7 +704,7 @@
title: 'Create',
icon: <M.Icon>add</M.Icon>,
fn: React.useCallback(() => {
dialogs.open(({ close }: DialogsOpenProps) => <Create {...{ close }} />)
dialogs.open(({ close }) => <Create {...{ close }} />)

Check warning on line 707 in catalog/app/containers/Admin/RolesAndPolicies/Roles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Roles.tsx#L707

Added line #L707 was not covered by tests
}, [dialogs.open]), // eslint-disable-line react-hooks/exhaustive-deps
},
]
Expand All @@ -726,7 +721,7 @@
title: 'Edit',
icon: <M.Icon>edit</M.Icon>,
fn: () => {
dialogs.open(({ close }: DialogsOpenProps) => (
dialogs.open(({ close }) => (

Check warning on line 724 in catalog/app/containers/Admin/RolesAndPolicies/Roles.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Admin/RolesAndPolicies/Roles.tsx#L724

Added line #L724 was not covered by tests
<Edit
{...{
role,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,11 @@
return FI.computeHash(f) as FI.LocalFile
}

// XXX: move to dialogs module
interface DialogsOpenProps {
close: (reason?: string) => void
interface ConfirmReadmeProps {
close: Dialogs.Close<'cancel' | 'empty' | 'readme'>
}

function ConfirmReadme({ close }: DialogsOpenProps) {
function ConfirmReadme({ close }: ConfirmReadmeProps) {

Check warning on line 100 in catalog/app/containers/Bucket/PackageDialog/PackageCreationForm.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageDialog/PackageCreationForm.tsx#L100

Added line #L100 was not covered by tests
return (
<>
<M.DialogTitle>Add a README file?</M.DialogTitle>
Expand Down Expand Up @@ -311,7 +310,7 @@
const entries = filesStateToEntries(files)

if (!entries.length) {
const reason = await dialogs.open((props: DialogsOpenProps) => (
const reason = await dialogs.open<'cancel' | 'empty' | 'readme'>((props) => (

Check warning on line 313 in catalog/app/containers/Bucket/PackageDialog/PackageCreationForm.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/containers/Bucket/PackageDialog/PackageCreationForm.tsx#L313

Added line #L313 was not covered by tests
<ConfirmReadme {...props} />
))
if (reason === 'cancel') return mkFormError(CANCEL)
Expand Down
52 changes: 0 additions & 52 deletions catalog/app/utils/Dialogs.js

This file was deleted.

67 changes: 67 additions & 0 deletions catalog/app/utils/Dialogs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import * as React from 'react'
import * as M from '@material-ui/core'

Check warning on line 2 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L1-L2

Added lines #L1 - L2 were not covered by tests

import defer from 'utils/defer'

Check warning on line 4 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L4

Added line #L4 was not covered by tests
import type { Resolver } from 'utils/defer'

type DialogState = 'open' | 'closing' | 'closed'

type ExtraDialogProps = Omit<M.DialogProps, 'open' | 'onClose' | 'onExited'>

export type Close<R> = [R] extends [never] ? () => void : Resolver<R>['resolve']
fiskus marked this conversation as resolved.
Show resolved Hide resolved

type Render<R> = (props: { close: Close<R> }) => JSX.Element

interface Dialog {
render: Render<any>
props?: ExtraDialogProps
resolver: Resolver<any>
}

export const useDialogs = () => {
const [state, setState] = React.useState<DialogState>('closed')
const [dialog, setDialog] = React.useState<Dialog | null>(null)

Check warning on line 23 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L21-L23

Added lines #L21 - L23 were not covered by tests

const open = React.useCallback(
<R = never,>(render: Render<R>, props?: ExtraDialogProps) => {
const { resolver, promise } = defer<R>()
setDialog({ render, props, resolver })
setState('open')
return promise

Check warning on line 30 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L25-L30

Added lines #L25 - L30 were not covered by tests
},
[setDialog, setState],
)

const close = React.useCallback(
(reason: any) => {

Check warning on line 36 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L35-L36

Added lines #L35 - L36 were not covered by tests
fiskus marked this conversation as resolved.
Show resolved Hide resolved
if (dialog) dialog.resolver.resolve(reason)
setState('closing')

Check warning on line 38 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L38

Added line #L38 was not covered by tests
},
[setState, dialog],
)

const cleanup = React.useCallback(() => {

Check warning on line 43 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L43

Added line #L43 was not covered by tests
if (state === 'closing') {
setState('closed')
setDialog(null)

Check warning on line 46 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L45-L46

Added lines #L45 - L46 were not covered by tests
}
}, [state, setState, setDialog])

const render = (props?: ExtraDialogProps) => (

Check warning on line 50 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L50

Added line #L50 was not covered by tests
<M.Dialog
open={state === 'open'}
onClose={close}
onExited={cleanup}
{...props}
{...dialog?.props}
>
{dialog ? dialog.render({ close }) : ''}
</M.Dialog>
)

return { open, render }

Check warning on line 62 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L62

Added line #L62 was not covered by tests
}

export const use = useDialogs

Check warning on line 65 in catalog/app/utils/Dialogs.tsx

View check run for this annotation

Codecov / codecov/patch/informational

catalog/app/utils/Dialogs.tsx#L65

Added line #L65 was not covered by tests

export type Dialogs = ReturnType<typeof use>
4 changes: 2 additions & 2 deletions catalog/app/utils/defer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
interface Resolver<T> {
export interface Resolver<T> {
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
}

interface Deferred<T> {
export interface Deferred<T> {
resolver: Resolver<T>
promise: Promise<T>
}
Expand Down