Recent sheets per user

This commit is contained in:
Roman Krček
2025-09-08 15:45:36 +02:00
parent c6c3bbc024
commit e43101648b
4 changed files with 77 additions and 19 deletions

View File

@@ -1,5 +1,11 @@
<script lang="ts">
import { selectedSheet, currentStep, columnMapping } from '$lib/stores';
import {
selectedSheet,
currentStep,
columnMapping,
} from '$lib/stores';
import { userEmail } from '$lib/google';
import { hashString } from '$lib/utils';
import type { ColumnMappingType, SheetInfoType } from '$lib/stores';
import { getSheetNames, getSheetData, ensureToken } from '$lib/google';
import { onMount } from 'svelte';
@@ -33,7 +39,13 @@
{ key: 'alreadyPrinted', label: 'Already Printed', required: false }
];
const RECENT_SHEETS_KEY = 'recentSheets';
async function getRecentSheetsKey() {
const email = $userEmail;
if (email) {
return `recentSheets_${await hashString(email)}`;
}
return 'recentSheets_anonymous';
}
// Load available sheets when component mounts
onMount(async () => {
@@ -42,7 +54,8 @@
console.log('Selected sheet on mount:', $selectedSheet);
// Check if we already have saved mapping data
const recentSheetsData = localStorage.getItem(RECENT_SHEETS_KEY);
const key = await getRecentSheetsKey();
const recentSheetsData = localStorage.getItem(key);
if (recentSheetsData) {
try {
@@ -161,7 +174,7 @@
autoMapColumns();
// Check if we have saved column mapping for this sheet
loadSavedColumnMapping();
await loadSavedColumnMapping();
} else {
error = 'The selected sheet appears to be empty.';
console.warn('Sheet is empty');
@@ -235,14 +248,15 @@
updateMappingStatus();
}
function loadSavedColumnMapping() {
async function loadSavedColumnMapping() {
if (!$selectedSheet || !selectedSheetName) {
console.log('Cannot load saved column mapping: missing selectedSheet or selectedSheetName');
return;
}
try {
const existingData = localStorage.getItem(RECENT_SHEETS_KEY);
const key = await getRecentSheetsKey();
const existingData = localStorage.getItem(key);
if (existingData) {
const recentSheets = JSON.parse(existingData);
@@ -256,7 +270,7 @@
// Override auto-mapping with saved mapping
mappedIndices = {
name: savedSheet.columnMapping.name ?? -1,
nationality: savedSheet.columnMappin.nationality ?? -1,
nationality: savedSheet.columnMapping.nationality ?? -1,
birthday: savedSheet.columnMapping.birthday ?? -1,
pictureUrl: savedSheet.columnMapping.pictureUrl ?? -1,
alreadyPrinted: savedSheet.columnMapping.alreadyPrinted ?? -1,
@@ -318,12 +332,13 @@
});
}
function handleContinue() {
async function handleContinue() {
if (!mappingComplete || !$selectedSheet || !selectedSheetName) return;
// Save column mapping to localStorage for the selected sheet
try {
const existingData = localStorage.getItem(RECENT_SHEETS_KEY);
const key = await getRecentSheetsKey();
const existingData = localStorage.getItem(key);
let recentSheets = existingData ? JSON.parse(existingData) : [];
// Find the current sheet in recent sheets and update its column mapping
@@ -344,9 +359,6 @@
// Update existing entry
recentSheets[sheetIndex].columnMapping = columnMappingData;
recentSheets[sheetIndex].lastUsed = new Date().toISOString();
// Ensure we have consistent property names
recentSheets[sheetIndex].id = recentSheets[sheetIndex].id || recentSheets[sheetIndex].id;
} else {
// Add new entry
const newEntry = {
@@ -364,7 +376,7 @@
}
}
localStorage.setItem(RECENT_SHEETS_KEY, JSON.stringify(recentSheets));
localStorage.setItem(key, JSON.stringify(recentSheets));
} catch (err) {
console.error('Failed to save column mapping to localStorage:', err);
}