Skip to content

Commit

Permalink
SWC-6854: store/read query in the hash fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-hodgson committed May 31, 2024
1 parent 1c04c82 commit a039ce7
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 39 deletions.
6 changes: 6 additions & 0 deletions src/main/java/org/sagebionetworks/web/client/Portal.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ public void onModuleLoad() {
if (initToken.length() > 0 && initToken.startsWith("!")) {
String fullUrl = Window.Location.getHref();
fullUrl = fullUrl.replace("#!", "");
if (
initToken.startsWith("!Synapse") && initToken.contains("/tables/query/")
) {
fullUrl = fullUrl.replace("/tables/query/", "/tables/#query/");
}

Window.Location.assign(fullUrl);
} else {
// This is a split point where the browser can download the first large code file.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,8 @@ void showJiraIssueCollector(
long getLastModified(JavaScriptObject blob);

void setIsInnerProgrammaticHistoryChange();

String setHash(String hash);

String getHash();
}
Original file line number Diff line number Diff line change
Expand Up @@ -921,4 +921,22 @@ public static native void _showJiraIssueCollector(
console.error(err);
}
}-*/;

@Override
public String setHash(String hash) {
return _setHash(hash);
}

private static final native String _setHash(String hash) /*-{
$wnd.history.pushState('', '', hash)
}-*/;

@Override
public String getHash() {
return _getHash();
}

private static final native String _getHash() /*-{
return $wnd.location.hash
}-*/;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.google.common.base.Function;
import com.google.common.util.concurrent.FluentFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.place.shared.Place;
import com.google.gwt.user.client.Window.Location;
import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -28,6 +30,7 @@
import org.sagebionetworks.web.client.FeatureFlagConfig;
import org.sagebionetworks.web.client.FeatureFlagKey;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.SynapseJavascriptClient;
import org.sagebionetworks.web.client.events.EntityUpdatedEvent;
import org.sagebionetworks.web.client.place.Synapse;
Expand Down Expand Up @@ -57,7 +60,7 @@
public abstract class AbstractTablesTab
implements TablesTabView.Presenter, QueryChangeHandler {

public static final String TABLE_QUERY_PREFIX = "query/";
public static final String TABLE_QUERY_PREFIX = "#query/";

private static final String VERSION_ALERT_DRAFT_DATASET_TITLE =
"This is a Draft Version of the Dataset";
Expand Down Expand Up @@ -99,6 +102,7 @@ public abstract class AbstractTablesTab
Long latestSnapshotVersionNumber;
SynapseJavascriptClient jsClient;
FeatureFlagConfig featureFlagConfig;
SynapseJSNIUtils jsniUtils;

protected abstract EntityArea getTabArea();

Expand All @@ -116,11 +120,13 @@ public abstract class AbstractTablesTab
public AbstractTablesTab(
Tab tab,
PortalGinInjector ginInjector,
FeatureFlagConfig featureFlagConfig
FeatureFlagConfig featureFlagConfig,
SynapseJSNIUtils jsniUtils
) {
this.tab = tab;
this.ginInjector = ginInjector;
this.featureFlagConfig = featureFlagConfig;
this.jsniUtils = jsniUtils;
}

public void configure(
Expand Down Expand Up @@ -255,19 +261,20 @@ public void onQueryChange(Query newQuery) {
Long versionNumber = QueryBundleUtils.getTableVersion(newQuery.getSql());
String synId = QueryBundleUtils.getTableIdFromSql(newQuery.getSql());
Query defaultQuery = tableEntityWidget.getDefaultQuery();
// SWC-6854: update query value in the hash
if (token != null && !newQuery.equals(defaultQuery)) {
areaToken = TABLE_QUERY_PREFIX + token;
} else {
areaToken = "";
jsniUtils.setHash(TABLE_QUERY_PREFIX + token);
}
updateVersionAndAreaToken(synId, versionNumber, areaToken);
tab.showTab(true);
}
}

public Query getQueryString() {
if (areaToken != null && areaToken.startsWith(TABLE_QUERY_PREFIX)) {
String token = areaToken.substring(TABLE_QUERY_PREFIX.length());
// SWC-6854:read query from hash, if present
String hash = jsniUtils.getHash();
if (hash != null && hash.startsWith(TABLE_QUERY_PREFIX)) {
String token = hash.substring(TABLE_QUERY_PREFIX.length());
return queryTokenProvider.tokenToQuery(token);
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.FeatureFlagConfig;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.place.Synapse.EntityArea;
import org.sagebionetworks.web.shared.WebConstants;

Expand All @@ -34,9 +35,10 @@ public class DatasetsTab extends AbstractTablesTab {
public DatasetsTab(
Tab tab,
PortalGinInjector ginInjector,
FeatureFlagConfig featureFlagConfig
FeatureFlagConfig featureFlagConfig,
SynapseJSNIUtils jsniUtils
) {
super(tab, ginInjector, featureFlagConfig);
super(tab, ginInjector, featureFlagConfig, jsniUtils);
this.tab = tab;
this.ginInjector = ginInjector;
this.featureFlagConfig = featureFlagConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.FeatureFlagConfig;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.place.Synapse.EntityArea;
import org.sagebionetworks.web.shared.WebConstants;

Expand All @@ -27,9 +28,10 @@ public class TablesTab extends AbstractTablesTab {
public TablesTab(
Tab tab,
PortalGinInjector ginInjector,
FeatureFlagConfig featureFlagConfig
FeatureFlagConfig featureFlagConfig,
SynapseJSNIUtils jsniUtils
) {
super(tab, ginInjector, featureFlagConfig);
super(tab, ginInjector, featureFlagConfig, jsniUtils);
this.tab = tab;
this.ginInjector = ginInjector;
this.featureFlagConfig = featureFlagConfig;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.PlaceChanger;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.SynapseJavascriptClient;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.place.Synapse;
Expand Down Expand Up @@ -167,12 +168,18 @@ public class DatasetsTabTest {
@Mock
FeatureFlagConfig mockFeatureFlagConfig;

@Mock
SynapseJSNIUtils mockJsniUtils;

@Captor
ArgumentCaptor<Map<String, String>> mapCaptor;

@Captor
ArgumentCaptor<Place> placeCaptor;

@Captor
ArgumentCaptor<String> stringCaptor;

DatasetsTab tab;
Query query;

Expand Down Expand Up @@ -204,7 +211,12 @@ public void setUp() {
.thenReturn(mockPlaceChanger);

tab =
new DatasetsTab(mockTab, mockPortalGinInjector, mockFeatureFlagConfig);
new DatasetsTab(
mockTab,
mockPortalGinInjector,
mockFeatureFlagConfig,
mockJsniUtils
);
tab.setEntitySelectedCallback(mockEntitySelectedCallback);

when(mockProjectEntityBundle.getEntity()).thenReturn(mockProjectEntity);
Expand Down Expand Up @@ -390,7 +402,8 @@ public void testSetTableQueryWithNoToken() {

Synapse place = getNewPlace(datasetName);
assertEquals(EntityArea.DATASETS, place.getArea());
assertTrue(place.getAreaToken().isEmpty());
assertNull(place.getAreaToken());
verify(mockJsniUtils, never()).setHash(anyString());
}

@Test
Expand Down Expand Up @@ -426,8 +439,9 @@ public void testSetTableQueryChangeVersion() {
verify(mockPlaceChanger).goTo(placeCaptor.capture());
Synapse place = (Synapse) placeCaptor.getValue();
assertEquals(EntityArea.DATASETS, place.getArea());
assertTrue(place.getAreaToken().isEmpty());
assertNull(place.getAreaToken());
assertEquals(newVersion, place.getVersionNumber());
verify(mockJsniUtils, never()).setHash(anyString());
}

@Test
Expand All @@ -446,7 +460,8 @@ public void testSetTableQueryChangeTableId() {
verify(mockPlaceChanger).goTo(placeCaptor.capture());
Synapse place = (Synapse) placeCaptor.getValue();
assertEquals(EntityArea.DATASETS, place.getArea());
assertTrue(place.getAreaToken().isEmpty());
assertNull(place.getAreaToken());
verify(mockJsniUtils, never()).setHash(anyString());
assertNull(place.getVersionNumber());
assertEquals("syn837874873843", place.getEntityId());
}
Expand Down Expand Up @@ -474,35 +489,37 @@ public void testSetTableQueryWithToken() {

Synapse place = getNewPlace(datasetName);
assertEquals(EntityArea.DATASETS, place.getArea());
assertTrue(place.getAreaToken().contains(encodedToken));
verify(mockJsniUtils).setHash(stringCaptor.capture());
assertTrue(stringCaptor.getValue().contains(encodedToken));
}

@Test
public void testGetTableQuery() {
Long version = null;
tab.setProject(projectEntityId, mockProjectEntityBundle, null);

String queryAreaToken;
String queryToken = null;
when(mockJsniUtils.getHash()).thenReturn(queryToken);
Query query1 = null;
queryAreaToken = null;
tab.configure(mockDatasetBundle, version, queryAreaToken);
tab.configure(mockDatasetBundle, version, null);
query1 = tab.getQueryString();
assertNull(query1);

queryAreaToken = "something else";
tab.configure(mockDatasetBundle, version, queryAreaToken);
tab.configure(mockDatasetBundle, version, null);
query1 = tab.getQueryString();
assertNull(query1);
String token = "encoded query token";
queryAreaToken = "query/" + token;
queryToken = "#query/" + token;
when(mockJsniUtils.getHash()).thenReturn(queryToken);
when(mockQueryTokenProvider.tokenToQuery(anyString())).thenReturn(query);
tab.configure(mockDatasetBundle, version, queryAreaToken);
tab.configure(mockDatasetBundle, version, null);
query1 = tab.getQueryString();
assertEquals(query, query1);
query.setSql("SELECT 'query/' FROM syn123 LIMIT 1");
token = "encoded query token 2";
queryAreaToken = "query/" + token;
tab.configure(mockDatasetBundle, version, queryAreaToken);
queryToken = "#query/" + token;
when(mockJsniUtils.getHash()).thenReturn(queryToken);
tab.configure(mockDatasetBundle, version, null);
query1 = tab.getQueryString();
assertEquals(query, query1);
}
Expand Down
Loading

0 comments on commit a039ce7

Please sign in to comment.