Skip to content

Commit a3e645a

Browse files
committed
store configs
1 parent c2f2b90 commit a3e645a

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

src/main/state/config-store.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Persistent user config storage
3+
* Reads/writes config.json in the platform-appropriate userData directory
4+
*/
5+
6+
import { app } from 'electron';
7+
import { readFileSync, writeFileSync, mkdirSync } from 'fs';
8+
import { join } from 'path';
9+
import { createLogger } from '../../utils/logger';
10+
11+
const logger = createLogger('ConfigStore');
12+
13+
export interface UserConfig {
14+
outputPath: string | null;
15+
}
16+
17+
const defaults: UserConfig = {
18+
outputPath: null,
19+
};
20+
21+
function configPath(): string {
22+
return join(app.getPath('userData'), 'config.json');
23+
}
24+
25+
export function loadConfig(): UserConfig {
26+
try {
27+
const raw = readFileSync(configPath(), 'utf-8');
28+
const parsed = JSON.parse(raw);
29+
return { ...defaults, ...parsed };
30+
} catch {
31+
return { ...defaults };
32+
}
33+
}
34+
35+
export function saveConfig(config: UserConfig): void {
36+
try {
37+
const dir = app.getPath('userData');
38+
mkdirSync(dir, { recursive: true });
39+
writeFileSync(configPath(), JSON.stringify(config, null, 2), 'utf-8');
40+
} catch (error) {
41+
logger.error('Failed to save config:', error);
42+
}
43+
}
44+
45+
export function getConfigValue<K extends keyof UserConfig>(key: K): UserConfig[K] {
46+
return loadConfig()[key];
47+
}
48+
49+
export function setConfigValue<K extends keyof UserConfig>(key: K, value: UserConfig[K]): void {
50+
const config = loadConfig();
51+
config[key] = value;
52+
saveConfig(config);
53+
}

src/main/state/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ export {
1414
createMouseTracker,
1515
cleanupRecording,
1616
} from './recording-state';
17+
18+
export {
19+
loadConfig,
20+
saveConfig,
21+
getConfigValue,
22+
setConfigValue,
23+
} from './config-store';
24+
export type { UserConfig } from './config-store';

src/main/state/recording-state.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { RecordingConfig, RecordingState } from '../../types';
1010
import type { Platform } from '../../platform';
1111
import { createLogger } from '../../utils/logger';
1212
import { showRecordingBarIdle } from '../recording-bar-window';
13+
import { getConfigValue, setConfigValue } from './config-store';
1314

1415
const logger = createLogger('RecordingState');
1516

@@ -18,7 +19,7 @@ let recordingState: RecordingState = {
1819
isRecording: false,
1920
};
2021
let currentRecordingConfig: RecordingConfig | null = null;
21-
let configuredOutputPath: string | null = null;
22+
let configuredOutputPath: string | null | undefined = undefined;
2223

2324
// Components
2425
let screenCapture: ScreenCapture | null = null;
@@ -34,6 +35,9 @@ export function getCurrentRecordingConfig(): RecordingConfig | null {
3435
}
3536

3637
export function getConfiguredOutputPath(): string | null {
38+
if (configuredOutputPath === undefined) {
39+
configuredOutputPath = getConfigValue('outputPath');
40+
}
3741
return configuredOutputPath;
3842
}
3943

@@ -56,6 +60,7 @@ export function setCurrentRecordingConfig(config: RecordingConfig | null): void
5660

5761
export function setConfiguredOutputPath(path: string | null): void {
5862
configuredOutputPath = path;
63+
setConfigValue('outputPath', path);
5964
}
6065

6166
// Create new instances

0 commit comments

Comments
 (0)