All files / src/web/utils filterUrlUtils.ts

100% Statements 197/197
98.83% Branches 85/86
100% Functions 5/5
100% Lines 197/197

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280                                              1x 49x 49x 49x   49x 49x 49x 49x 49x 49x 49x   49x 12x 12x 12x   49x 5x 5x 5x   49x 8x 8x 8x   49x 3x 3x 3x   49x 5x 5x 5x   49x 3x 3x 3x   49x 3x 3x 3x   49x 49x         1x 14x   14x 6x 6x   14x 2x 2x   14x 2x 2x   14x 2x 2x   14x 2x 2x   14x 2x 2x   14x 2x 2x   14x 14x         1x 6x 6x   6x 5x 6x 1x 1x 6x           1x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x     13x 13x 10x 10x 10x 10x   13x 3x 3x 3x   10x 10x           1x 49x   49x 27x 27x   23x 9x 9x 2x 2x 2x 9x 1x 1x 1x 9x 3x 3x 9x 3x 3x 9x 9x     23x 3x 3x   23x 2x 2x   23x 6x 6x   49x 3x 3x   49x 8x 8x   49x   11x 11x 11x   11x 11x   3x 3x 3x 3x 3x 11x   4x 4x 4x 4x 4x 4x 4x 4x 11x   1x 1x 1x 1x 1x 11x   1x 1x 1x 1x 1x 1x 1x 1x 11x   1x 1x 1x 1x 1x 11x   1x 1x 1x 1x 1x 1x 1x 11x   11x 11x 1x 1x 11x   22x 22x  
 
/**
 * Centralized URL parameter management for newsletter filters
 * Provides consistent handling of filter parameters across the application
 */
 
export interface FilterUrlParams {
  filter?: string;
  source?: string;
  groups?: string[];
  tags?: string[];
  time?: string;
  isRead?: boolean;
  isArchived?: boolean;
}
 
export interface ParsedUrlParams extends FilterUrlParams {
  hasParams: boolean;
}
 
/**
 * Parse URL search parameters into structured filter object
 */
export const parseFilterUrlParams = (searchParams: URLSearchParams): ParsedUrlParams => {
  const params: ParsedUrlParams = {
    hasParams: false,
  };
 
  const filter = searchParams.get('filter');
  const source = searchParams.get('source');
  const groups = searchParams.get('groups');
  const tags = searchParams.get('tags');
  const time = searchParams.get('time');
  const isRead = searchParams.get('isRead');
  const isArchived = searchParams.get('isArchived');
 
  if (filter) {
    params.filter = filter;
    params.hasParams = true;
  }
 
  if (source) {
    params.source = source;
    params.hasParams = true;
  }
 
  if (groups) {
    params.groups = groups.split(',').map(id => id.trim()).filter(Boolean);
    params.hasParams = true;
  }
 
  if (tags) {
    params.tags = tags.split(',').map(id => id.trim()).filter(Boolean);
    params.hasParams = true;
  }
 
  if (time && time !== 'all') {
    params.time = time;
    params.hasParams = true;
  }
 
  if (isRead !== null) {
    params.isRead = isRead === 'true';
    params.hasParams = true;
  }
 
  if (isArchived !== null) {
    params.isArchived = isArchived === 'true';
    params.hasParams = true;
  }
 
  return params;
};
 
/**
 * Build URL search parameters from filter object
 */
export const buildFilterUrlParams = (params: FilterUrlParams): URLSearchParams => {
  const searchParams = new URLSearchParams();
 
  if (params.filter && params.filter !== 'unread') {
    searchParams.set('filter', params.filter);
  }
 
  if (params.source) {
    searchParams.set('source', params.source);
  }
 
  if (params.groups && params.groups.length > 0) {
    searchParams.set('groups', params.groups.join(','));
  }
 
  if (params.tags && params.tags.length > 0) {
    searchParams.set('tags', params.tags.join(','));
  }
 
  if (params.time && params.time !== 'all') {
    searchParams.set('time', params.time);
  }
 
  if (params.isRead !== undefined) {
    searchParams.set('isRead', params.isRead.toString());
  }
 
  if (params.isArchived !== undefined) {
    searchParams.set('isArchived', params.isArchived.toString());
  }
 
  return searchParams;
};
 
/**
 * Update URL with new filter parameters
 */
export const updateFilterUrl = (params: FilterUrlParams, replace = true): void => {
  const searchParams = buildFilterUrlParams(params);
  const newUrl = `${window.location.pathname}${searchParams.toString() ? `?${searchParams.toString()}` : ''}`;
 
  if (replace) {
    window.history.replaceState({}, '', newUrl);
  } else {
    window.history.pushState({}, '', newUrl);
  }
};
 
/**
 * Sync URL parameters with current filter state
 * Returns true if URL was updated, false if no changes needed
 */
export const syncFilterUrl = (
  currentParams: FilterUrlParams,
  currentFilter: string,
  currentSource: string | null,
  currentGroups: string[],
  currentTags: string[],
  currentTimeRange: string
): boolean => {
  const newParams: FilterUrlParams = {
    filter: currentFilter !== 'unread' ? currentFilter : undefined,
    source: currentSource || undefined,
    groups: currentGroups.length > 0 ? currentGroups : undefined,
    tags: currentTags.length > 0 ? currentTags : undefined,
    time: currentTimeRange !== 'all' ? currentTimeRange : undefined,
  };
 
  // Check if anything actually changed
  const hasChanges =
    (currentParams.filter !== newParams.filter) ||
    (currentParams.source !== newParams.source) ||
    (JSON.stringify(currentParams.groups?.sort() || []) !== JSON.stringify(newParams.groups?.sort() || [])) ||
    (JSON.stringify(currentParams.tags?.sort() || []) !== JSON.stringify(newParams.tags?.sort() || [])) ||
    (currentParams.time !== newParams.time);
 
  if (hasChanges) {
    updateFilterUrl(newParams);
    return true;
  }
 
  return false;
};
 
/**
 * Convert URL parameters to newsletter filter object
 */
import type { NewsletterFilter } from '../../common/types/cache';
export const urlParamsToNewsletterFilter = (params: ParsedUrlParams): NewsletterFilter => {
  const filter: NewsletterFilter = {};
 
  if (!params.hasParams) {
    return filter;
  }
 
  if (params.filter) {
    switch (params.filter) {
      case 'unread':
        filter.isRead = false;
        filter.isArchived = false;
        break;
      case 'read':
        filter.isRead = true;
        filter.isArchived = false;
        break;
      case 'archived':
        filter.isArchived = true;
        break;
      case 'liked':
        filter.isLiked = true;
        break;
    }
  }
 
  // Handle separate isRead and isArchived parameters (override filter if present)
  if (params.isRead !== undefined) {
    filter.isRead = params.isRead;
  }
 
  if (params.isArchived !== undefined) {
    filter.isArchived = params.isArchived;
  }
 
  if (params.source) {
    filter.sourceIds = [params.source];
  }
 
  if (params.tags && params.tags.length > 0) {
    filter.tagIds = params.tags;
  }
 
  if (params.groups && params.groups.length > 0) {
    filter.groupIds = params.groups;
  }
 
  if (params.time && params.time !== 'all') {
    // Use local time for date calculations so 'day' reflects the user's local day
    const now = new Date();
    let dateFrom: Date;
    let dateTo: Date | undefined;
 
    switch (params.time) {
      case 'day': {
        // Start of the local day (local midnight)
        const startOfLocalDay = new Date(now);
        startOfLocalDay.setHours(0, 0, 0, 0);
        dateFrom = startOfLocalDay;
        break;
      }
      case 'week': {
        // Start of the current week (Monday 00:00 local time)
        const startOfWeek = new Date(now);
        startOfWeek.setHours(0, 0, 0, 0);
        const day = startOfWeek.getDay(); // 0=Sun,1=Mon,...6=Sat
        const diffSinceMonday = (day + 6) % 7; // days since Monday
        startOfWeek.setDate(startOfWeek.getDate() - diffSinceMonday);
        dateFrom = startOfWeek;
        break;
      }
      case 'month': {
        // Start of the current month at local midnight
        const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
        startOfMonth.setHours(0, 0, 0, 0);
        dateFrom = startOfMonth;
        break;
      }
      case 'last24h': {
        // Previous calendar day (local): [yesterday 00:00, today 00:00)
        const startOfToday = new Date(now);
        startOfToday.setHours(0, 0, 0, 0);
        const startOfYesterday = new Date(startOfToday);
        startOfYesterday.setDate(startOfYesterday.getDate() - 1);
        dateFrom = startOfYesterday;
        dateTo = startOfToday;
        break;
      }
      case '2days': {
        // Rolling 2 days (48 hours) based on local time
        const twoDaysAgo = new Date(now);
        twoDaysAgo.setDate(now.getDate() - 2);
        dateFrom = twoDaysAgo;
        break;
      }
      default: {
        // For unsupported values, fall back to start of current week
        const startOfWeek = new Date(now);
        startOfWeek.setHours(0, 0, 0, 0);
        const day = startOfWeek.getDay();
        const diffSinceMonday = (day + 6) % 7;
        startOfWeek.setDate(startOfWeek.getDate() - diffSinceMonday);
        dateFrom = startOfWeek;
      }
    }
 
    filter.dateFrom = dateFrom.toISOString();
    if (dateTo) {
      filter.dateTo = dateTo.toISOString();
    }
  }
 
  return filter;
};