Skip to content

Commit 96a903e

Browse files
authored
Merge pull request #37 from TrueRyoB/feature/inputcache
前回の入力をキャッシュする程度の機能
2 parents f61be8f + 5798a49 commit 96a903e

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
package-lock.json

frontend/src/App.svelte

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55
import Message from "./components/Message.svelte";
66
77
import { Loader } from "@lucide/svelte";
8-
import { type Problem } from "./utils/types";
8+
import { type Problem, type ClosedRange, createValidRange } from "./utils/types";
9+
import { cacheInput, loadLastInput } from "./cacher";
910
10-
let under_diff = $state<string>("0");
11-
let over_diff = $state<string>("3854");
11+
const MIN_DIFF: number = 0;
12+
const MAX_DIFF: number = 3854;
13+
14+
let cachedInput : ClosedRange | null = loadLastInput();
15+
let currentInput : ClosedRange | null;
16+
17+
let under_diff = $state<number>(cachedInput ? cachedInput.min : MIN_DIFF);
18+
let over_diff = $state<number>(cachedInput ? cachedInput.max : MAX_DIFF);
1219
1320
let errors = $derived({
14-
rangeError: parseInt(under_diff) > parseInt(over_diff),
15-
isMinusUnderDiff: parseInt(under_diff) < 0,
16-
isMinusOverDiff: parseInt(over_diff) < 0,
21+
rangeError: !(currentInput = createValidRange(under_diff, over_diff)),
22+
isMinusUnderDiff: under_diff < 0,
23+
isMinusOverDiff: over_diff < 0,
1724
});
1825
1926
let result = $state<Problem | null>(null);
@@ -74,7 +81,7 @@
7481
<div class="flex items-center gap-2">
7582
<Input type="number" placeholder="最低Diffを入力してください。" isErrors={errors} bind:value={under_diff} />
7683
<Input type="number" placeholder="最高Diffを入力してください。" isErrors={errors} bind:value={over_diff} />
77-
<Button onclick={sendQuery} class="shrink-0 w-24 h-12 flex justify-center items-center" disabled={loading}>
84+
<Button onclick={() =>{sendQuery(), cacheInput(currentInput!)}} class="shrink-0 w-24 h-12 flex justify-center items-center" disabled={loading}>
7885
{#if loading}
7986
<div class="animate-spin [animation-duration: 1.05s]">
8087
<Loader size="1.5rem" />

frontend/src/cacher.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { ClosedRange } from './utils/types';
2+
3+
const rangeKey : string = 'lastDiff';
4+
5+
export const cacheInput = (range : ClosedRange): void => {
6+
localStorage.setItem(rangeKey, JSON.stringify(range));
7+
}
8+
export const loadLastInput = (): ClosedRange | null => {
9+
const data = localStorage.getItem(rangeKey);
10+
return data ? JSON.parse(data) as ClosedRange : null;
11+
}

frontend/src/utils/types.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,14 @@ export interface Problem {
33
contest_id: string;
44
name: string;
55
difficulty: number;
6-
};
6+
};
7+
8+
export type ClosedRange = {
9+
min: number;
10+
max: number;
11+
} & { readonly __brand: unique symbol };
12+
13+
// for validation
14+
export const createValidRange = (min: number, max: number): ClosedRange | null => {
15+
return min>max ? null : {min, max, __brand: Symbol('ClosedRange') as never};
16+
}

frontend/tsconfig.app.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"extends": "@tsconfig/svelte/tsconfig.json",
33
"compilerOptions": {
4+
"composite": true,
45
"target": "ES2022",
56
"useDefineForClassFields": true,
67
"module": "ESNext",

frontend/tsconfig.node.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"compilerOptions": {
33
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
4+
"composite": true,
45
"target": "ES2023",
56
"lib": ["ES2023"],
67
"module": "ESNext",
@@ -11,7 +12,7 @@
1112
"allowImportingTsExtensions": true,
1213
"verbatimModuleSyntax": true,
1314
"moduleDetection": "force",
14-
"noEmit": true,
15+
"noEmit": false,
1516

1617
/* Linting */
1718
"strict": true,

0 commit comments

Comments
 (0)