Skip to content

Commit

Permalink
Add abort changes button
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur-fontaine committed Aug 28, 2023
1 parent 41d6672 commit 789d151
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/octent/src/hooks/use-content-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ContentExplorer } from '@/lib/utils'

// eslint-disable-next-line functional/no-mixed-types
interface ContentStatusState {
status: 'modified' | 'unmodified' | 'pushing'
status: 'modified' | 'unmodified' | 'pushing' | 'resetting'
updateStatus: (data: ContentExplorer | ContentStatusState['status']) => void
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { createListDirectories } from './functions/create-list-directory'
import { createPull } from './functions/create-pull'
import { createPush } from './functions/create-push'
import { createRenameDirectory } from './functions/create-rename-directory'
import { createReset } from './functions/create-reset'
import { createUpdateCollection } from './functions/create-update-collection'
import { createUpdateContent } from './functions/create-update-content'

Expand Down Expand Up @@ -71,6 +72,7 @@ export interface ContentExplorer {

push: () => Promise<void>
pull: () => Promise<void>
reset: () => Promise<void>

formatContent: (content: Readonly<Content<Collection>>) => string
getContent: (contentPath: string) => Promise<Content<Collection>>
Expand Down Expand Up @@ -173,6 +175,7 @@ export async function createContentExplorer(

push: createPush({ gitRepoOptions: GIT_REPO_OPTIONS }),
pull: createPull({ gitRepoOptions: GIT_REPO_OPTIONS }),
reset: createReset({ fs, gitRepoOptions: GIT_REPO_OPTIONS }),

formatContent: createFormatContent({ dataType: options.dataType }),
getContent: createGetContent({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type LightningFS from '@isomorphic-git/lightning-fs'
import * as git from 'isomorphic-git'

import type { GitRepoOptions } from '../content-explorer'

/**
* @param parameters The parameters to pass to the function
* @param parameters.fs The file system to use
* @param parameters.gitRepoOptions The options to pass to git
* @returns A function that pulls the repository
*/
export function createReset(parameters: {
fs: LightningFS.PromisifiedFS,
gitRepoOptions: Readonly<GitRepoOptions>
}) {
// eslint-disable-next-line functional/functional-parameters
return async function reset() {
const branch = await git.currentBranch(parameters.gitRepoOptions)

if (branch === undefined) {
return
}

await parameters.fs.unlink(
`${parameters.gitRepoOptions.dir}/.git/refs/heads/${branch}`,
)

await git.checkout({
...parameters.gitRepoOptions,
ref: branch,
force: true,
})
}
}
19 changes: 17 additions & 2 deletions packages/octent/src/pages/dashboard/dashboard-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ export function DashboardPage() {
? null
: (
<div className='h-full mx-32 my-16 flex flex-col'>
<header className='w-full flex flex-row-reverse mb-8'>
<header className='w-full flex flex-row-reverse mb-8 gap-4'>
<Button
variant='secondary'
variant='default'
onClick={function () {
void updateContentStatus('pushing')
void content_explorer.push().then(function () {
Expand All @@ -101,6 +101,21 @@ export function DashboardPage() {
>
{content_status === 'pushing' ? 'Saving...' : 'Save'}
</Button>
{(content_status === 'modified' || content_status === 'resetting')
&& (
<Button
variant='destructive'
onClick={function () {
void updateContentStatus('resetting')
void content_explorer.reset().then(function () {
return updateContentStatus(content_explorer)
})
}}
disabled={content_status === 'resetting'}
>
{content_status === 'resetting' ? 'Aborting...' : 'Abort'}
</Button>
)}
</header>
<div className='flex-1 flex flex-row justify-center'>
<main className='flex-1 max-w-4xl'>
Expand Down

0 comments on commit 789d151

Please sign in to comment.