utils.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { API, showError } from '../helpers';
  2. export async function getOAuthState() {
  3. let path = '/api/oauth/state';
  4. let affCode = localStorage.getItem('aff');
  5. if (affCode && affCode.length > 0) {
  6. path += `?aff=${affCode}`;
  7. }
  8. const res = await API.get(path);
  9. const { success, message, data } = res.data;
  10. if (success) {
  11. return data;
  12. } else {
  13. showError(message);
  14. return '';
  15. }
  16. }
  17. export async function onGitHubOAuthClicked(github_client_id) {
  18. const state = await getOAuthState();
  19. if (!state) return;
  20. window.open(
  21. `https://github.com/login/oauth/authorize?client_id=${github_client_id}&state=${state}&scope=user:email`,
  22. );
  23. }
  24. export async function onLinuxDOOAuthClicked(linuxdo_client_id) {
  25. const state = await getOAuthState();
  26. if (!state) return;
  27. window.open(
  28. `https://connect.linux.do/oauth2/authorize?response_type=code&client_id=${linuxdo_client_id}&state=${state}`,
  29. );
  30. }
  31. let channelModels = undefined;
  32. export async function loadChannelModels() {
  33. const res = await API.get('/api/models');
  34. const { success, data } = res.data;
  35. if (!success) {
  36. return;
  37. }
  38. channelModels = data;
  39. localStorage.setItem('channel_models', JSON.stringify(data));
  40. }
  41. export function getChannelModels(type) {
  42. if (channelModels !== undefined && type in channelModels) {
  43. if (!channelModels[type]) {
  44. return [];
  45. }
  46. return channelModels[type];
  47. }
  48. let models = localStorage.getItem('channel_models');
  49. if (!models) {
  50. return [];
  51. }
  52. channelModels = JSON.parse(models);
  53. if (type in channelModels) {
  54. return channelModels[type];
  55. }
  56. return [];
  57. }