-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: keep a (short) history of your changes (#133)
- Loading branch information
Showing
8 changed files
with
242 additions
and
1 deletion.
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,59 @@ | ||
.fitHistory { | ||
background-color: #111111; | ||
color: #c5c5c5; | ||
font-size: 15px; | ||
margin: 0 auto; | ||
padding: 5px; | ||
position: relative; | ||
} | ||
|
||
.title { | ||
margin-left: 16px; | ||
} | ||
|
||
.history { | ||
display: flex; | ||
margin-top: 8px; | ||
width: 100%; | ||
} | ||
|
||
.history > img { | ||
cursor: pointer; | ||
height: 16px; | ||
margin-top: 3px; | ||
user-select: none; | ||
} | ||
|
||
.holder { | ||
border: 1px solid #202020; | ||
display: flex; | ||
flex: 1; | ||
width: 100%; | ||
} | ||
|
||
.entry { | ||
background-color: #626262; | ||
border-bottom: 3px solid #111111; | ||
border-top: 3px solid #111111; | ||
box-sizing: border-box; | ||
cursor: pointer; | ||
height: 19px; | ||
margin-left: 3px; | ||
user-select: none; | ||
width: 9px; | ||
} | ||
.entry.odd { | ||
background-color: #888888; | ||
} | ||
|
||
.entry.active { | ||
background-color: #bfbfbf; | ||
border: 0; | ||
height: 19px; | ||
width: 9px; | ||
} | ||
|
||
.inactive { | ||
cursor: default !important; | ||
filter: brightness(0.5); | ||
} |
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,46 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import React from "react"; | ||
|
||
import { fitArgType } from "../../../.storybook/fits"; | ||
import { useFitSelection, withDecoratorFull } from "../../../.storybook/helpers"; | ||
|
||
import { EsfFit, useCurrentFit } from "@/providers/CurrentFitProvider"; | ||
|
||
import { FitHistory } from "./"; | ||
|
||
type StoryProps = React.ComponentProps<typeof FitHistory> & { fit: EsfFit | null; width: number }; | ||
|
||
const meta: Meta<StoryProps> = { | ||
component: FitHistory, | ||
tags: ["autodocs"], | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<StoryProps>; | ||
|
||
const TestStory = () => { | ||
const currentFit = useCurrentFit(); | ||
|
||
return <div>Current Fit: {currentFit.fit?.name}</div>; | ||
}; | ||
|
||
export const Default: Story = { | ||
argTypes: { | ||
fit: fitArgType, | ||
}, | ||
args: { | ||
fit: null, | ||
historySize: 10, | ||
}, | ||
decorators: [withDecoratorFull], | ||
render: ({ fit, ...args }) => { | ||
useFitSelection(fit); | ||
|
||
return ( | ||
<div> | ||
<TestStory /> | ||
<FitHistory {...args} /> | ||
</div> | ||
); | ||
}, | ||
}; |
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,107 @@ | ||
import clsx from "clsx"; | ||
import React from "react"; | ||
|
||
import { Icon } from "@/components/Icon"; | ||
import { useCurrentFit } from "@/providers/CurrentFitProvider"; | ||
|
||
import styles from "./FitHistory.module.css"; | ||
|
||
export interface FitHistoryProps { | ||
/** Amount of entries to keep in history. */ | ||
historySize: number; | ||
} | ||
|
||
export const FitHistory = (props: FitHistoryProps) => { | ||
const currentFit = useCurrentFit(); | ||
const setFit = currentFit.setFit; | ||
|
||
const [history, setHistory] = React.useState<string[]>([]); | ||
const [currentIndex, setCurrentIndex] = React.useState<number>(-1); | ||
const [oddOffset, setOddOffset] = React.useState<number>(1); | ||
|
||
const historyRef = React.useRef(history); | ||
historyRef.current = history; | ||
const currentIndexRef = React.useRef(currentIndex); | ||
currentIndexRef.current = currentIndex; | ||
const historySizeRef = React.useRef(props.historySize); | ||
historySizeRef.current = props.historySize; | ||
|
||
React.useEffect(() => { | ||
const fit = currentFit.fit; | ||
if (fit === null) return; | ||
/* Store the fit as a JSON string, to ensure that any modifications | ||
* to the current doesn't impact the history. */ | ||
const fitString = JSON.stringify(fit); | ||
if (currentIndexRef.current !== -1 && historyRef.current[currentIndexRef.current] === fitString) return; | ||
|
||
setHistory((prev) => { | ||
if (prev.length >= historySizeRef.current) { | ||
setOddOffset((offset) => (offset + 1) % 2); | ||
return [...prev.slice(1), fitString]; | ||
} | ||
return [...prev, fitString]; | ||
}); | ||
setCurrentIndex(-1); | ||
}, [currentFit.fit]); | ||
|
||
React.useEffect(() => { | ||
if (currentIndex === -1) return; | ||
|
||
const fit = historyRef.current[currentIndex]; | ||
setFit(JSON.parse(fit)); | ||
}, [currentIndex, setFit]); | ||
|
||
const moveBack = React.useCallback(() => { | ||
setCurrentIndex((prev) => { | ||
if (prev === -1) return historyRef.current.length - 2; | ||
if (prev > 1) return prev - 1; | ||
return 0; | ||
}); | ||
}, []); | ||
|
||
const moveForward = React.useCallback(() => { | ||
setCurrentIndex((prev) => { | ||
if (prev === -1) return -1; | ||
if (prev >= historyRef.current.length - 2) return historyRef.current.length - 1; | ||
return prev + 1; | ||
}); | ||
}, []); | ||
|
||
const moveTo = React.useCallback((index: number) => { | ||
setCurrentIndex(index); | ||
}, []); | ||
|
||
return ( | ||
<div className={styles.fitHistory} style={{ width: props.historySize * 12 + 3 + 16 + 16 }}> | ||
<div className={styles.title}>Simulation History</div> | ||
<div className={styles.history}> | ||
<Icon | ||
name="arrow-left" | ||
size={16} | ||
className={clsx({ [styles.inactive]: history.length <= 1 || currentIndex === 0 })} | ||
onClick={moveBack} | ||
/> | ||
<div className={styles.holder}> | ||
{history.map((fit, i) => ( | ||
<div | ||
key={i} | ||
className={clsx(styles.entry, { | ||
[styles.odd]: (i + oddOffset) % 2 === 0, | ||
[styles.active]: (currentIndex === -1 && history.length - 1 === i) || i === currentIndex, | ||
})} | ||
onClick={() => moveTo(i)} | ||
> | ||
| ||
</div> | ||
))} | ||
</div> | ||
<Icon | ||
name="arrow-right" | ||
size={16} | ||
className={clsx({ [styles.inactive]: history.length - 1 === currentIndex || currentIndex === -1 })} | ||
onClick={moveForward} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; |
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 @@ | ||
export { FitHistory } from "./FitHistory"; |
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
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