import { renderToString } from "npm:react-dom@18/server";
import React from "npm:react@18";
const clientScript = `
import { parse } from 'https://esm.sh/chrono-node@2.3.8';
const inputEl = document.getElementById('dateInput');
const outputEl = document.getElementById('output');
const clearBtn = document.getElementById('clearBtn');
function detectDate() {
const inputText = inputEl.value;
const parsedResults = parse(inputText);
if (parsedResults.length > 0) {
const detectedDate = parsedResults[0].start.date();
const detectedDateString = parsedResults[0].text;
const formattedDate = formatDate(detectedDate);
outputEl.innerHTML = \`
<div class="flex space-x-2">
<span class="bg-gray-800 text-white px-2 py-1 rounded inline-flex items-center text-sm">
\${formattedDate}
</span>
<span class="border border-gray-800 text-gray-800 px-2 py-1 rounded inline-flex items-center text-sm">
\${detectedDateString}
</span>
</div>
\`;
inputEl.value = ''; // Clear the input after detection
} else {
outputEl.innerHTML = '';
}
}
function clearAll() {
inputEl.value = '';
outputEl.innerHTML = '';
inputEl.focus();
}
function formatDate(date) {
if (!date) return '';
return date.toLocaleString('en-US', {
month: '2-digit',
day: '2-digit',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
hour12: true,
});
}
inputEl.addEventListener('input', detectDate);
clearBtn.addEventListener('click', clearAll);
`;
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Date Input</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
</style>
</head>
<body class="bg-white">
<div class="max-w-md mx-auto p-4">
<h1 class="text-2xl font-bold mb-4">AI Date Input</h1>
<div class="relative mb-4">
<input
id="dateInput"
type="text"
class="w-full p-2 pr-8 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-gray-500"
placeholder="Enter a date or time..."
/>
<button
id="clearBtn"
class="absolute right-2 top-1/2 transform -translate-y-1/2 text-gray-400 hover:text-gray-600"
style="font-size: 1.2em;"
>
×
</button>
</div>
<div id="output" class="mt-2"></div>
<p class="mt-4 text-sm text-gray-600">
Try typing things like "tomorrow", "next Friday", "3 days from now", or "2024-09-15 14:30".
</p>
<div class="mt-8 text-xs text-gray-500">
<a href="${import.meta.url.replace("esm.town", "val.town")}" target="_blank" rel="noopener noreferrer" class="underline">
View Source
</a>
</div>
</div>
<script type="module">