Skip to content

Commit

Permalink
minor input improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
phughesmcr committed Sep 5, 2024
1 parent 26ecac6 commit 2441f48
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
8 changes: 5 additions & 3 deletions components/CustomPersonalityInput.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { MAX_PERSONALITY_LENGTH } from "lib/debate/inputValidation.ts";
import { personalities } from "lib/debate/personalities.ts";
import { cleanContent, sanitizeInput } from "lib/utils.ts";
import { useEffect, useState } from "preact/hooks";

interface CustomPersonalityInputProps {
Expand All @@ -14,12 +15,13 @@ export default function CustomPersonalityInput(props: CustomPersonalityInputProp

// Personality dropdown handler
useEffect(() => {
const matchingPersonality = personalities.find((p) => p.personality === value);
const sanitizedValue = cleanContent(sanitizeInput(value));
const matchingPersonality = personalities.find((p) => p.personality === sanitizedValue);
if (matchingPersonality) {
setSelectedOption(matchingPersonality.name);
} else {
setSelectedOption("custom");
setCustomInput(value);
setCustomInput(sanitizedValue);
}
}, [value]);

Expand All @@ -40,7 +42,7 @@ export default function CustomPersonalityInput(props: CustomPersonalityInputProp
};

const handleCustomInputChange = (e: Event) => {
const newValue = (e.target as HTMLTextAreaElement).value;
const newValue = cleanContent(sanitizeInput((e.target as HTMLTextAreaElement).value));
setCustomInput(newValue);
onChange(newValue);
};
Expand Down
9 changes: 5 additions & 4 deletions components/DebateFormInputs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
MIN_DEBATE_ROUNDS,
} from "lib/debate/inputValidation.ts";
import type { Personality } from "lib/debate/personalities.ts";
import { cleanContent, sanitizeInput } from "lib/utils.ts";
import type { VoiceType } from "routes/api/voicesynth.tsx";

interface DebateFormInputsProps {
Expand Down Expand Up @@ -87,7 +88,7 @@ export default function DebateFormInputs(props: DebateFormInputsProps) {
placeholder={EXAMPLE_POSITIONS[Math.floor(Math.random() * EXAMPLE_POSITIONS.length)]}
minLength={4}
maxLength={MAX_POSITION_LENGTH}
onInput={(e) => setPosition((e.target as HTMLInputElement).value)}
onChange={(e) => setPosition(cleanContent(sanitizeInput((e.target as HTMLInputElement).value)))}
class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100"
required
/>
Expand All @@ -102,7 +103,7 @@ export default function DebateFormInputs(props: DebateFormInputsProps) {
type="number"
id="numAgents"
value={numAgents}
onInput={(e) => setNumAgents(parseInt((e.target as HTMLInputElement).value, 10))}
onChange={(e) => setNumAgents(parseInt((e.target as HTMLInputElement).value, 10))}
min={MIN_AGENTS}
max={MAX_AGENTS}
class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100"
Expand All @@ -117,7 +118,7 @@ export default function DebateFormInputs(props: DebateFormInputsProps) {
type="number"
id="numDebateRounds"
value={numDebateRounds}
onInput={(e) => setNumDebateRounds(parseInt((e.target as HTMLInputElement).value, 10))}
onChange={(e) => setNumDebateRounds(parseInt((e.target as HTMLInputElement).value, 10))}
min={MIN_DEBATE_ROUNDS}
max={MAX_DEBATE_ROUNDS}
class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100"
Expand Down Expand Up @@ -157,7 +158,7 @@ export default function DebateFormInputs(props: DebateFormInputsProps) {
<textarea
id="context"
value={context}
onInput={(e) => setContext((e.target as HTMLTextAreaElement).value)}
onChange={(e) => setContext(cleanContent(sanitizeInput((e.target as HTMLTextAreaElement).value)))}
class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100"
rows={3}
placeholder="Provide any additional context for the debate. This will help the participants understand the topic better. For example, where is the debate taking place?"
Expand Down
11 changes: 6 additions & 5 deletions islands/AgentSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import CustomPersonalityInput from "components/CustomPersonalityInput.tsx";
import { MAX_NAME_LENGTH } from "lib/debate/inputValidation.ts";
import type { Personality } from "lib/debate/personalities.ts";
import { sanitizeInput } from "lib/utils.ts";
import { cleanContent, sanitizeInput } from "lib/utils.ts";

interface AgentSelectorProps {
agentDetails: Required<Personality>[];
Expand All @@ -15,7 +15,7 @@ export default function AgentSelector(props: AgentSelectorProps) {
const newAgents = [...agentDetails];
newAgents[index] = {
...newAgents[index],
personality,
personality: cleanContent(sanitizeInput(personality)),
};
setAgentDetails(newAgents);
};
Expand All @@ -25,8 +25,9 @@ export default function AgentSelector(props: AgentSelectorProps) {
field: keyof Personality,
value: string,
) => {
const sanitizedValue = sanitizeInput(value);
const newAgents = [...agentDetails];
newAgents[index] = { ...newAgents[index], [field]: value };
newAgents[index] = { ...newAgents[index], [field]: sanitizedValue };
setAgentDetails(newAgents);
};

Expand All @@ -51,8 +52,8 @@ export default function AgentSelector(props: AgentSelectorProps) {
<input
id={`agent-name-${index}`}
type="text"
value={agent.name.split(" ")[1]}
onInput={(e) =>
value={agent.name}
onChange={(e) =>
handleAgentDetailChange(
index,
"name",
Expand Down
2 changes: 1 addition & 1 deletion static/sw.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const CACHE_NAME = "debatething-cache-v2";
const CACHE_NAME = "debatething-cache-v3";
const urlsToCache = [
"/",
"/styles.css",
Expand Down

0 comments on commit 2441f48

Please sign in to comment.