-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#745 Speedup resource history creation and provide feedback to user
- Loading branch information
Showing
7 changed files
with
245 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import styled from 'styled-components'; | ||
|
||
interface ProgressBarProps { | ||
value: number; | ||
} | ||
|
||
export const ProgressBar: React.FC<ProgressBarProps> = ({ value }) => { | ||
return <Progress value={value} max='100' />; | ||
}; | ||
|
||
const Progress = styled.progress` | ||
--progress-bg: ${p => p.theme.colors.bg1}; | ||
--progress-fg: ${p => p.theme.colors.main}; | ||
--progress-radius: 2rem; | ||
--progress-height: 0.5rem; | ||
flex: 1; | ||
appearance: none; | ||
// Needed for the border radius to work on chrome | ||
overflow: hidden; | ||
// Firefox | ||
border-radius: var(--progress-radius); | ||
height: var(--progress-height); | ||
background-color: var(--progress-bg); | ||
border: none; | ||
&[value]::-moz-progress-bar { | ||
background-color: var(--progress-fg); | ||
} | ||
// Chrome & Safari | ||
&[value]::-webkit-progress-bar { | ||
background-color: var(--progress-bg); | ||
border-radius: var(--progress-radius); | ||
height: var(--progress-height); | ||
} | ||
&[value]::-webkit-progress-value { | ||
background-color: var(--progress-fg); | ||
} | ||
`; |
This file contains 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 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 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,32 +1,48 @@ | ||
import { Resource, Version, useStore } from '@tomic/react'; | ||
import { useState, useEffect } from 'react'; | ||
import { Resource, Version, unknownSubject, useStore } from '@tomic/react'; | ||
import { useState, useEffect, useRef, useTransition } from 'react'; | ||
import { dedupeVersions } from './versionHelpers'; | ||
|
||
export interface UseVersionsResult { | ||
versions: Version[]; | ||
loading: boolean; | ||
progress: number; | ||
error: Error | undefined; | ||
} | ||
|
||
export function useVersions(resource: Resource): UseVersionsResult { | ||
const [versions, setVersions] = useState<Version[]>([]); | ||
const [progress, setProgress] = useState(0); | ||
const isRunning = useRef(false); | ||
const store = useStore(); | ||
const [_, startTransition] = useTransition(); | ||
const [loading, setLoading] = useState<boolean>(true); | ||
const [error, setError] = useState<Error | undefined>(undefined); | ||
|
||
useEffect(() => { | ||
resource | ||
.getHistory(store) | ||
.then(history => { | ||
setVersions(dedupeVersions(history)); | ||
}) | ||
.catch(e => { | ||
setError(e); | ||
}) | ||
.finally(() => { | ||
setLoading(false); | ||
}); | ||
if (resource.getSubject() === unknownSubject) { | ||
return; | ||
} | ||
|
||
if (isRunning.current) { | ||
return; | ||
} | ||
|
||
startTransition(() => { | ||
(async () => { | ||
try { | ||
isRunning.current = true; | ||
const history = await resource.getHistory(store, setProgress); | ||
const dedupedVersions = dedupeVersions(history); | ||
setVersions(dedupedVersions); | ||
} catch (e) { | ||
setError(e); | ||
} finally { | ||
setLoading(false); | ||
isRunning.current = false; | ||
} | ||
})(); | ||
}); | ||
}, [resource]); | ||
|
||
return { versions, loading, error }; | ||
return { versions, loading, error, progress }; | ||
} |
This file contains 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 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.