File size: 1,611 Bytes
dc92053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a31b9a
 
 
dc92053
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Module to manipulate query params."""

from typing import Any

import streamlit as st

from core.state import CurrentProject
from core.state import RecordSet


class QueryParams:
    """Possible URL query params."""

    OPEN_PROJECT = "project"
    OPEN_RECORD_SET = "recordSet"


def _get_query_param(params: dict[str, Any], name: str) -> str | None:
    """Gets query param with the name `name`."""
    if name in params:
        param = params[name]
        if isinstance(param, list) and len(param) > 0:
            return param[0]
    return None


def _set_query_param(param: str, new_value: str) -> str | None:
    params = st.experimental_get_query_params()
    if params.get(param) == [new_value]:
        # The value already exists in the query params.
        return
    new_params = {k: v for k, v in params.items() if k != param}
    new_params[param] = new_value
    st.experimental_set_query_params(**new_params)


def is_record_set_expanded(record_set: RecordSet) -> bool:
    params = st.experimental_get_query_params()
    open_record_set_name = _get_query_param(params, QueryParams.OPEN_RECORD_SET)
    if open_record_set_name:
        return open_record_set_name == record_set.name
    return False


def expand_record_set(record_set: RecordSet) -> None:
    _set_query_param(QueryParams.OPEN_RECORD_SET, record_set.name)


def get_project_timestamp() -> str | None:
    params = st.experimental_get_query_params()
    return _get_query_param(params, QueryParams.OPEN_PROJECT)


def set_project(project: CurrentProject):
    _set_query_param(QueryParams.OPEN_PROJECT, project.path.name)