Skip to content

Commit

Permalink
highlight fileState
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Apr 26, 2024
1 parent 142e977 commit 46011cd
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 13 additions & 6 deletions packages/admin/src/client/views/Migrations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const workingFSContext = createCascadingState({} as Record<string, string>, v =>
export const Migrations = file.wrap(workingFSContext.wrap(_Migrations))

const useMigrations = () => {
const [_, setFileState] = file.useState()

const util = trpc.useUtils()
const mutationConfig = {
onSuccess: () => util.migrations.invalidate(),
Expand All @@ -71,7 +73,12 @@ const useMigrations = () => {
description: `This may delete data, which will not be restored even if you reapply the migration.`,
})
const update = trpc.migrations.update.useMutation(mutationConfig)
const downify = trpc.migrations.downify.useMutation(mutationConfig)
const downify = trpc.migrations.downify.useMutation({
onSuccess: data => {
setFileState(data.downPath)
return util.migrations.invalidate()
},
})
const definitions = trpc.migrations.definitions.useMutation(mutationConfig)

return {list, create, up, down, update, downify, definitions}
Expand Down Expand Up @@ -134,7 +141,7 @@ function _Migrations() {
</Button>
</ContextMenuTrigger>
<ContextMenuContent className="mt-5 bg-gray-800 text-gray-100">
{!numExecuted && <ContextMenuItem disabled>No executed migrations!</ContextMenuItem>}
{!numExecuted && <ContextMenuItem disabled>No migrations to revert</ContextMenuItem>}
{Boolean(numExecuted) && (
<ContextMenuItem onClick={() => down.mutate({to: 0})}>
<icons.CircleArrowDown />
Expand All @@ -151,7 +158,7 @@ function _Migrations() {
</Button>
</ContextMenuTrigger>
<ContextMenuContent className="mt-5 bg-gray-800 text-gray-100">
{!numPending && <ContextMenuItem disabled>No pending migrations!</ContextMenuItem>}
{!numPending && <ContextMenuItem disabled>No migrations to apply</ContextMenuItem>}
{Array.from({length: numPending || 0}).map((_, i) => {
const step = i + 1

Expand Down Expand Up @@ -282,7 +289,7 @@ const _sampleFilesJson: Record<string, string> = {
}

export const FileTree = (tree: File | Folder) => {
const [_, setFileState] = file.useState()
const [fileState, setFileState] = file.useState()

const {up, down, list} = useMigrations()

Expand All @@ -297,7 +304,7 @@ export const FileTree = (tree: File | Folder) => {
title={tree.path}
onClick={() => setFileState(tree.path)}
>
<span className="inline-flex">
<span className={clsx('inline-flex', tree.path === fileState && 'underline')}>
<icons.File className="mr-2 h-4 w-4" />
{basename(tree.path)}
</span>
Expand Down Expand Up @@ -343,7 +350,7 @@ export const FileTree = (tree: File | Folder) => {

return (
<>
<Collapsible defaultOpen={basename(tree.path) !== 'down'}>
<Collapsible open={fileState.startsWith(tree.path) || undefined} defaultOpen={basename(tree.path) !== 'down'}>
<CollapsibleTrigger asChild>
<div
title={tree.path}
Expand Down
4 changes: 3 additions & 1 deletion packages/admin/src/server/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export const migrationsRotuer = trpc.router({
.mutation(async ({input, ctx}) => {
const {content, info} = await ctx.migrator.generateDownMigration({name: input.name})

await fs.promises.writeFile(ctx.migrator.downPath(info.migration.path as string), content)
const downPath = ctx.migrator.downPath(info.migration.path as string)
await fs.promises.writeFile(downPath, content)
return {downPath, content, info}
}),
update: migrationsProcedure
.input(
Expand Down

0 comments on commit 46011cd

Please sign in to comment.