Skip to content

Commit 8d023a5

Browse files
authored
Merge pull request #46 from TomPlum/develop
ThemeContext & Test Coverage
2 parents 9b633ce + d047994 commit 8d023a5

14 files changed

Lines changed: 215 additions & 70 deletions

File tree

packages/library/src/_test/stubs.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { ThemeFunctions } from 'hooks/useTheme'
55
import { GraphData } from 'data'
66
import { GraphColumnState } from 'modules/Graph/components/GraphColumn'
77
import { GraphContextBag } from 'modules/Graph/context'
8+
import { ThemeContextBag } from 'context/ThemeContext'
89

910
export const commit = (commit?: Partial<Commit>): Commit => ({
1011
hash: 'aa2c148',
@@ -44,10 +45,8 @@ export const gitContextBag = (bag?: Partial<GitContextBag>): GitContextBag => ({
4445
setPreviewedCommit: vi.fn(),
4546
setSelectedCommit: vi.fn(),
4647
showBranchesTags: false,
47-
theme: 'dark',
4848
showTable: true,
4949
selectedCommit: commit({ hash: 'selected' }),
50-
colours: ['white'],
5150
headCommit: commit({ hash: 'HEAD' }),
5251
graphData: graphData(),
5352
showHeaders: true,
@@ -64,6 +63,12 @@ export const gitContextBag = (bag?: Partial<GitContextBag>): GitContextBag => ({
6463
...bag
6564
})
6665

66+
export const themeContextBag = (bag?: Partial<ThemeContextBag>): ThemeContextBag => ({
67+
theme: 'dark',
68+
colours: ['white'],
69+
...bag
70+
})
71+
6772
export const graphContextBag = (bag?: Partial<GraphContextBag>): GraphContextBag => ({
6873
nodeTheme: 'default',
6974
showCommitNodeTooltips: false,

packages/library/src/components/GitLogCore/GitLogCore.tsx

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import { GitLogCoreProps } from './types'
22
import { Children, isValidElement, PropsWithChildren, ReactElement, useCallback, useMemo, useState } from 'react'
33
import { GitContext, GitContextBag } from 'context/GitContext'
4-
import { neonAuroraDarkColours, neonAuroraLightColours, useTheme } from 'hooks/useTheme'
5-
import { generateRainbowGradient } from 'hooks/useTheme/createRainbowTheme'
64
import { computeNodePositions, computeRelationships, GraphData, temporalTopologicalSort } from 'data'
75
import { Tags } from 'modules/Tags'
86
import { Graph, GraphOrientation } from 'modules/Graph'
97
import { Table } from 'modules/Table'
108
import { Layout } from 'components/Layout'
119
import { Commit } from 'types/Commit'
1210
import { DEFAULT_NODE_SIZE, NODE_BORDER_WIDTH } from 'constants/constants'
11+
import { ThemeContextProvider } from 'context/ThemeContext'
1312

1413
export const GitLogCore = ({
1514
children,
@@ -89,33 +88,6 @@ export const GitLogCore = ({
8988
// TODO: Are we using graphWidth here or just ditching enableResize?
9089
const [, setGraphWidth] = useState(defaultGraphWidth ?? smallestAvailableGraphWidth)
9190

92-
const { shiftAlphaChannel } = useTheme()
93-
94-
const themeColours = useMemo<string[]>(() => {
95-
switch (colours) {
96-
case 'rainbow-light': {
97-
return generateRainbowGradient(graphData.graphWidth + 1)
98-
}
99-
case 'rainbow-dark': {
100-
return generateRainbowGradient(graphData.graphWidth + 1)
101-
.map(colour => shiftAlphaChannel(colour, 0.6))
102-
}
103-
case 'neon-aurora-dark': {
104-
return neonAuroraDarkColours
105-
}
106-
case 'neon-aurora-light': {
107-
return neonAuroraLightColours
108-
}
109-
default: {
110-
if (theme === 'light') {
111-
return colours
112-
}
113-
114-
return colours.map(colour => shiftAlphaChannel(colour, 0.6))
115-
}
116-
}
117-
}, [colours, graphData.graphWidth, shiftAlphaChannel, theme])
118-
11991
const handleSelectCommit = useCallback((commit?: Commit) => {
12092
setSelectedCommit(commit)
12193
onSelectCommit?.(commit)
@@ -183,11 +155,9 @@ export const GitLogCore = ({
183155
}, [defaultGraphWidth, smallestAvailableGraphWidth])
184156

185157
const value = useMemo<GitContextBag>(() => ({
186-
colours: themeColours,
187158
showTable: Boolean(table),
188159
showBranchesTags: Boolean(tags),
189160
classes,
190-
theme,
191161
selectedCommit,
192162
setSelectedCommit: handleSelectCommit,
193163
previewedCommit,
@@ -211,9 +181,7 @@ export const GitLogCore = ({
211181
setGraphOrientation,
212182
indexStatus
213183
}), [
214-
themeColours,
215184
classes,
216-
theme,
217185
selectedCommit,
218186
previewedCommit,
219187
handleSelectCommit,
@@ -239,7 +207,9 @@ export const GitLogCore = ({
239207

240208
return (
241209
<GitContext.Provider value={value}>
242-
<Layout tags={tags} graph={graph} table={table} />
210+
<ThemeContextProvider theme={theme} colours={colours} graphWidth={graphData.graphWidth}>
211+
<Layout tags={tags} graph={graph} table={table} />
212+
</ThemeContextProvider>
243213
</GitContext.Provider>
244214
)
245215
}

packages/library/src/context/GitContext/GitContext.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { createContext } from 'react'
22
import { GitContextBag } from './types'
3-
import { neonAuroraDarkColours } from 'hooks/useTheme'
43
import { Commit } from 'types/Commit'
54
import DataIntervalTree from 'node-interval-tree'
65
import { DEFAULT_NODE_SIZE } from 'constants/constants'
@@ -18,13 +17,11 @@ const defaultCommit: Commit = {
1817
}
1918

2019
export const GitContext = createContext<GitContextBag>({
21-
colours: neonAuroraDarkColours,
2220
headCommit: defaultCommit,
2321
indexCommit: defaultCommit,
2422
currentBranch: 'master',
2523
showTable: true,
2624
showBranchesTags: true,
27-
theme: 'light',
2825
selectedCommit: undefined,
2926
setSelectedCommit: (commit?: Commit) => {
3027
console.debug(`Tried to invoke setSelectedCommit(${JSON.stringify(commit)}) before the GitContext was initialised.`)

packages/library/src/context/GitContext/types.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import { Commit } from 'types/Commit'
2-
import { ThemeMode } from 'hooks/useTheme'
32
import { GraphData } from 'data'
43
import { GitLogIndexStatus, GitLogStylingProps } from '../../types'
54
import { GraphOrientation } from 'modules/Graph'
65

76
export interface GitContextBag {
8-
colours: string[]
9-
107
/**
118
* The name of the branch that is
129
* currently checked out.
@@ -177,15 +174,6 @@ export interface GitContextBag {
177174
*/
178175
classes?: GitLogStylingProps
179176

180-
/**
181-
* The variant of the default colour
182-
* them to apply to the log.
183-
*
184-
* Does not take effect if a custom
185-
* array of {@link colours} are passed.
186-
*/
187-
theme: ThemeMode
188-
189177
/**
190178
* The status of changed files in the
191179
* Git index.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createContext } from 'react'
2+
import { ThemeContextBag } from 'context/ThemeContext/types'
3+
import { neonAuroraDarkColours } from 'hooks/useTheme'
4+
5+
export const ThemeContext = createContext<ThemeContextBag>({
6+
colours: neonAuroraDarkColours,
7+
theme: 'light'
8+
})
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { ThemeContext } from 'context/ThemeContext/ThemeContext'
2+
import { PropsWithChildren, useMemo } from 'react'
3+
import { ThemeContextBag, ThemeContextProviderProps } from 'context/ThemeContext/types'
4+
import { neonAuroraDarkColours, neonAuroraLightColours, useTheme } from 'hooks/useTheme'
5+
import { generateRainbowGradient } from 'hooks/useTheme/createRainbowTheme'
6+
7+
export const ThemeContextProvider = ({ children, theme, colours, graphWidth }: PropsWithChildren<ThemeContextProviderProps>) => {
8+
9+
const { shiftAlphaChannel } = useTheme()
10+
11+
const themeColours = useMemo<string[]>(() => {
12+
switch (colours) {
13+
case 'rainbow-light': {
14+
return generateRainbowGradient(graphWidth + 1)
15+
}
16+
case 'rainbow-dark': {
17+
return generateRainbowGradient(graphWidth + 1)
18+
.map(colour => shiftAlphaChannel(colour, 0.6))
19+
}
20+
case 'neon-aurora-dark': {
21+
return neonAuroraDarkColours
22+
}
23+
case 'neon-aurora-light': {
24+
return neonAuroraLightColours
25+
}
26+
default: {
27+
if (theme === 'light') {
28+
return colours
29+
}
30+
31+
return colours.map(colour => shiftAlphaChannel(colour, 0.6))
32+
}
33+
}
34+
}, [colours, graphWidth, shiftAlphaChannel, theme])
35+
36+
const value = useMemo<ThemeContextBag>(() => ({
37+
colours: themeColours,
38+
theme
39+
}), [theme, themeColours])
40+
41+
return (
42+
<ThemeContext.Provider value={value}>
43+
{children}
44+
</ThemeContext.Provider>
45+
)
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from './types'
2+
export * from './useThemeContext'
3+
export * from './ThemeContextProvider'
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { ThemeColours, ThemeMode } from 'hooks/useTheme'
2+
3+
export interface ThemeContextBag {
4+
/**
5+
* The variant of the default colour
6+
* them to apply to the log.
7+
*
8+
* Does not take effect if a custom
9+
* array of {@link colours} are passed.
10+
*/
11+
theme: ThemeMode
12+
13+
/**
14+
* An array of colours to be used in
15+
* graph to denote the different branches
16+
* in the git log entries.
17+
*/
18+
colours: string[]
19+
}
20+
21+
export interface ThemeContextProviderProps {
22+
theme: ThemeMode
23+
graphWidth: number
24+
colours: ThemeColours | string[]
25+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { useContext } from 'react'
2+
import { ThemeContext } from 'context/ThemeContext/ThemeContext'
3+
4+
export const useThemeContext = () => useContext(ThemeContext)

0 commit comments

Comments
 (0)