From c3b7278ea56605d7257a6451e81049fa38adb9e1 Mon Sep 17 00:00:00 2001 From: Johannes Nakayama Date: Wed, 11 Sep 2024 12:42:45 +0200 Subject: [PATCH 1/2] keep claim extraction state in local storage --- app/routes/index.tsx | 217 ++++++++++++++++++++++++++----------------- 1 file changed, 131 insertions(+), 86 deletions(-) diff --git a/app/routes/index.tsx b/app/routes/index.tsx index 310ab7cf..9fa2b96a 100644 --- a/app/routes/index.tsx +++ b/app/routes/index.tsx @@ -1,6 +1,6 @@ import { Link, useLoaderData } from '@remix-run/react' import moment from 'moment' -import { useState } from 'react' +import { type ChangeEvent, useState } from 'react' import { Markdown } from '#app/components/markdown.tsx' import PollResult from '#app/components/ui/poll-result.tsx' import { PostContent } from '#app/components/ui/post-content.tsx' @@ -10,6 +10,7 @@ import { db } from '#app/db.ts' import { type ClaimList } from '#app/repositories/fact-checking.ts' import { getChronologicalPolls } from '#app/repositories/ranking.ts' import { PollType, type FrontPagePost } from '#app/types/api-types.ts' +import { useDebounce } from '#app/utils/misc.tsx' import { useOptionalUser } from '#app/utils/user.ts' export async function loader() { @@ -22,14 +23,77 @@ export async function loader() { export default function ClaimExtraction() { const { feed } = useLoaderData() - const [statementValue, setStatementValue] = useState('') - const [originValue, setOriginValue] = useState('') - const [isExtractingClaims, setIsExtractingClaims] = useState(false) - const [claims, setClaims] = useState({ - claim_context: '', - extracted_claims: [], + const infoText = ` +## Jabble Polls + +You can create either **opinion polls** (*agree/disagree*) or **fact check polls** (*true/false*). + +Copy and paste something you want to fact-check or discuss here. +We use an LLM to extract claims. +You can then decide which ones you want to post. +You can also add an origin URL to give context to where you found the statement. +` + + return ( +
+
+
+ {infoText} +
+ +
+
+
+ {'## Recent Polls'} +
+ {feed.map((post, index) => { + return + })} +
+
+ ) +} + +function ClaimExtractionForm() { + const statementValueStorageKey = 'claim-extraction-statement' + const [statementValue, setStatementValue] = useState( + () => localStorage.getItem(statementValueStorageKey) ?? '', + ) + const statementValueChangeHandler = useDebounce( + (event: ChangeEvent) => { + localStorage.removeItem(claimsStorageKey) + localStorage.setItem(statementValueStorageKey, event.target.value) + }, + 500, + ) + + const originValueStorageKey = 'claim-extraction-origin' + const [originValue, setOriginValue] = useState( + () => localStorage.getItem(originValueStorageKey) ?? '', + ) + const originValueChangeHandler = useDebounce( + (event: ChangeEvent) => { + localStorage.setItem(originValueStorageKey, event.target.value) + }, + 500, + ) + + const claimsStorageKey = 'extracted-claims' + const [claims, setClaims] = useState(() => { + const claimsFromLocalStorage = localStorage.getItem(claimsStorageKey) + if (claimsFromLocalStorage == null) { + return { + claim_context: '', + extracted_claims: [], + } + } + return JSON.parse(claimsFromLocalStorage) as ClaimList }) - const [urlError, setUrlError] = useState(false) + + const [isExtractingClaims, setIsExtractingClaims] = useState(false) + const [urlError, setUrlError] = useState( + () => (!isValidUrl(originValue) && !(originValue == '')) || false, + ) async function handleExtractClaims() { setIsExtractingClaims(true) @@ -44,98 +108,79 @@ export default function ClaimExtraction() { }) const newExtractedClaims = (await response.json()) as ClaimList setClaims(newExtractedClaims) + localStorage.setItem(claimsStorageKey, JSON.stringify(newExtractedClaims)) } finally { setIsExtractingClaims(false) } } - const infoText = ` -## Jabble Polls - -You can create either **opinion polls** (*agree/disagree*) or **fact check polls** (*true/false*). - -Copy and paste something you want to fact-check or discuss here. -We use an LLM to extract claims. -You can then decide which ones you want to post. -You can also add an origin URL to give context to where you found the statement. -` - const disclaimer = ` Press **Ctrl + Enter** to extract claims. **Disclaimer**: Your text will be sent to the OpenAI API for analysis. ` return ( -
-
-
- {infoText} -
-