File size: 1,433 Bytes
2a26954
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { CONFIG } from "./config.js";
import { createClient } from "https://cdn.jsdelivr.net/npm/@supabase/supabase-js@2.45.4/+esm";

export const sb = createClient(CONFIG.SUPABASE_URL, CONFIG.SUPABASE_ANON_KEY);

export const h = s => String(s ?? "").replace(/[&<>"']/g, c => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));

export async function getSettings() {
  const { data, error } = await sb.from("settings").select("key,value");
  if (error) console.error(error);
  const s = {};
  (data || []).forEach(r => (s[r.key] = r.value));
  return s;
}

export function saveSetting(key, value) {
  return sb.from("settings").upsert({ key, value });
}

// Bila nenosiri: ukurasa wa studio unaonyeshwa moja kwa moja.
// Hakikisha /studio imelindwa kwenye hosting yako (password protection au Cloudflare Access).
export async function requireAdmin(gate, app) {
  gate.hidden = true;
  app.hidden = false;
}

// Kipindi kinachocheza sasa hivi: (saa - epoch) mod jumla, sawa na build_jaynestv.py
export function nowPlaying(sch, nowMs = Date.now()) {
  const items = sch?.items;
  if (!items?.length || !sch.total) return null;
  const epoch = Date.parse(sch.epoch) / 1000;
  let t = (((nowMs / 1000 - epoch) % sch.total) + sch.total) % sch.total;
  let i = 0;
  while (i < items.length - 1 && t >= items[i].dur) { t -= items[i].dur; i++; }
  return { cur: items[i], next: items[(i + 1) % items.length] };
}